File size: 2,360 Bytes
4bef185
 
767d580
4bef185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767d580
4bef185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f9da36
 
 
 
 
 
 
 
 
 
 
4bef185
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh

# Displays usage information
usage() {
  echo "Usage: $0 --license LICENSE --python PYTHON INPUT OUTPUT"
  echo "Arguments:"
  echo "  -l, --license LICENSE    Path to FreeSurfer license (required)"
  echo "  -p, --python PYTHON      Path to Python-executable to use (required)"
  echo "  INPUT                    Input file (required)"
  echo "  OUTPUT                   Output file (required)"
}

LICENSE=""
PYTHON=""
INPUT=""
OUTPUT=""

while [ $# -gt 0 ]; do
  case $1 in
    -l|--license)
      LICENSE="$2"
      shift 2
      ;;
    -p|--python)
      PYTHON="$2"
      shift 2
      ;;
    -*|--*)
      echo "Unknown option $1"
      exit 1
      ;;
    *)
      # Handle positional arguments
      if [ -z "$INPUT" ]; then
        INPUT="$1"
      elif [ -z "$OUTPUT" ]; then
        OUTPUT="$1"
      else
        echo "Error: Too many positional arguments"
        usage
        exit 1
      fi
      shift
      ;;
  esac
done

# Validate that exactly two positional arguments are provided
if [ -z "$INPUT" ] || [ -z "$OUTPUT" ]; then
  echo "Error: This script requires exactly two arguments: INPUT and OUTPUT"
  usage
  exit 1
fi

if [ -z "$LICENSE" ]; then
  echo "Error: License is required"
  usage
  exit 1
fi
if [ -z "$PYTHON" ]; then
  echo "Error: Python is required"
  usage
  exit 1
fi

if [ ! -d "$INPUT" ]; then
  echo "Error: Input directory '$INPUT' does not exist"
  exit 1
fi

NIFTI_FILES=$(find "$INPUT" -name "*.nii.gz" -type f)
echo "Found $(echo "$NIFTI_FILES" | wc -l) .nii.gz files"

# Extract unique filenames and check for duplicates
UNIQUE_FILENAMES=$(echo "$NIFTI_FILES" | xargs -n1 basename | sort | uniq)
FILENAME_COUNT=$(echo "$UNIQUE_FILENAMES" | wc -l)
TOTAL_COUNT=$(echo "$NIFTI_FILES" | wc -l)

if [ "$FILENAME_COUNT" -ne "$TOTAL_COUNT" ]; then
  echo "Error: Duplicate filenames detected!"
  echo "Duplicate filenames:"
  echo "$NIFTI_FILES" | xargs -n1 basename | sort | uniq -d
  exit 1
fi

# Loop through each NIFTI file path
echo "$NIFTI_FILES" | while IFS= read -r filepath; do
  IMAGE=$(basename "$filepath" .nii.gz)
  if [ ! -f "$OUTPUT/$IMAGE/mri/mask.mgz" ]; then
    $FASTSURFER_HOME/run_fastsurfer.sh \
      --sd $OUTPUT \
      --sid $IMAGE \
      --t1 $filepath \
      --fs_license $LICENSE \
      --py $PYTHON \
      --seg_only
  else
    echo "$IMAGE already processed"
  fi
done