xtract 16.1 KB
Newer Older
Saad Jbabdi's avatar
Saad Jbabdi committed
1
2
3
4
5
6
#!/bin/bash

#   Copyright (C) 2019 University of Oxford
#
#   SHCOPYRIGHT

7
# Written by Saad Jbabdi, Stam Sotiropoulos & Shaun Warrington (based on Marius de Groot autoPtx code)
Saad Jbabdi's avatar
Saad Jbabdi committed
8
9
# Protocols created by Rogier Mars et al.

10
# Location of probtrackx2_gpu binary
11
ptxbin_gpu="$FSLDIR/bin/probtrackx2_gpu"
Saad Jbabdi's avatar
Saad Jbabdi committed
12

Saad Jbabdi's avatar
Saad Jbabdi committed
13
# Location of xtract data
14
datadir="$FSLDIR/data/xtract_data"
Saad Jbabdi's avatar
Saad Jbabdi committed
15
16
17
18

Usage() {
    cat << EOF

19
Usage:
20
    xtract -bpx <bedpostX_dir> -out <outputDir> -species <SPECIES> [options]
21
    xtract -bpx <bedpostX_dir> -out <outputDir> -species CUSTOM -str <file> -p <folder> -stdref <reference> [options]
22
    xtract -list
Saad Jbabdi's avatar
Saad Jbabdi committed
23
24
25

    Compulsory arguments:

26
27
       -bpx <folder>                          Path to bedpostx folder
       -out <folder>                          Path to output folder
28
29
30
31
32
33
       -species <SPECIES>                     One of HUMAN or MACAQUE or CUSTOM

    If -species CUSTOM:
      -str <file>                            Structures file (format: format: <tractName> [samples=1], 1 means 1000, '#' to skip lines)
      -p <folder>                            Protocols folder (all masks in same standard space)
      -stdref <reference>                    Standard space reference image
Saad Jbabdi's avatar
Saad Jbabdi committed
34
35

    Optional arguments:
36
37
       -list                                  List the tract names used in XTRACT
       -str <file>                            Structures file (format: <tractName> per line OR format: <tractName> [samples=1], 1 means 1000, '#' to skip lines)
38
       -p <folder>                            Protocols folder (all masks in same standard space) (Default=$FSLDIR/data/xtract_data/<SPECIES>)
39
       -stdwarp <std2diff> <diff2std>         Standard2diff and Diff2standard transforms (Default=bedpostx_dir/xfms/{standard2diff,diff2standard})
40
       -stdref <reference>                    Standard space reference image (Default = $FSLDIR/data/standard/MNI152_T1_1mm [HUMAN], $datadir/standard/F99/mri/struct_brain [MACAQUE])
41
42
43
44
45
46
47
48
49
       -gpu                                   Use GPU version
       -res <mm>                              Output resolution (Default=same as in protocol folders unless '-native' used)
       -ptx_options <options.txt>	            Pass extra probtrackx2 options as a text file to override defaults, e.g. --steplength=0.2 --distthresh=10)

       And EITHER:
       -native                                Run tractography in native (diffusion) space

       OR:
       -ref <refimage> <diff2ref> <ref2diff>  Reference image for running tractography in reference space, Diff2Reference and Reference2Diff transforms
Saad Jbabdi's avatar
Saad Jbabdi committed
50
51
52
53
54

EOF
    exit 1
}

Saad Jbabdi's avatar
Saad Jbabdi committed
55
56
57
58
Splash (){

cat <<EOF

59
 __  _______ ____      _    ____ _____
Saad Jbabdi's avatar
Saad Jbabdi committed
60
 \ \/ /_   _|  _ \    / \  / ___|_   _|
61
62
63
64
  \  /  | | | |_) |  / _ \| |     | |
  /  \  | | |  _ <  / ___ \ |___  | |
 /_/\_\ |_| |_| \_\/_/   \_\____| |_|

Saad Jbabdi's avatar
Saad Jbabdi committed
65
66
67
68
69
70
EOF

}

Splash

71
[ -z "$1" ] && Usage
Saad Jbabdi's avatar
Saad Jbabdi committed
72
73
74
75
76
77
78
79


# Set default options
bpx=""
out=""
str=""
p=""
std2diff=""
80
ptx_opts=""
81
stdref=""
Saad Jbabdi's avatar
Saad Jbabdi committed
82
83
gpu=0
nat=0
84
85
86
ref=""
diff2ref=""
ref2diff=""
Saad Jbabdi's avatar
Saad Jbabdi committed
87
88
spec=""
res=-1
89
90
list=0
seedget=0 # if user specifies -str and format is <tractName>, try to get nseeds from default structureList (i.e. if using a subset of XTRACT tracts)
Saad Jbabdi's avatar
Saad Jbabdi committed
91
92

# Parse command-line arguments
93
while [ -n "$1" ];do
Saad Jbabdi's avatar
Saad Jbabdi committed
94
95
96
97
98
    case "$1" in
	-bpx) bpx=$2;shift;;
	-out) out=$2;shift;;
	-str) str=$2;shift;;
	-p)   p=$2;shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
99
	-species) spec=$2;shift;;  # converts to uppercase
Saad Jbabdi's avatar
Saad Jbabdi committed
100
	-stdwarp) std2diff=$2;diff2std=$3;shift;shift;;
101
  -stdref) stdref=$2;shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
102
103
	-gpu) gpu=1;;
	-native) nat=1;;
104
  -ref) ref=$2;diff2ref=$3;ref2diff=$4;shift;shift;shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
105
	-res) res=$2;shift;;
106
  -list) list=1;shift;;
107
	-ptx_options) ptx_opts=`cat "$2"`;shift;;
108

Saad Jbabdi's avatar
Saad Jbabdi committed
109
110
111
112
	*) echo "Unknown option '$1'";exit 1;;
    esac
    shift
done
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

# list argument
if [ "$list" -eq 1 ];then
    tractNames=""
    echo ""
    echo "Tract names:"
    while read structstring; do
      struct=`echo $structstring | awk '{print $1}'`
      if [ "${struct:0:1}" == "#" ];then
    	foo=0
      elif [ "$struct" == "" ];then
    	foo=0
      else
      tractNames="${tractNames} ${struct},"
      fi
128
    done < "$datadir/Human/structureList"
129
130
131
132
    echo ${tractNames%,}
    exit 1
fi

Saad Jbabdi's avatar
Saad Jbabdi committed
133
# Default warps
134
135
136
137
if [ -z "$std2diff" ];then
    std2diff="$bpx/xfms/standard2diff"
    diff2std="$bpx/xfms/diff2standard"
    if [ `$FSLDIR/bin/imtest "$std2diff"` -eq 0 ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
138
139
140
	echo "Image $std2diff not found."
	exit 1
    fi
141
    if [ `$FSLDIR/bin/imtest "$diff2std"` -eq 0 ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
142
143
144
	echo "Image $diff2std not found."
	exit 1
    fi
145
146
147
148
149
elif [ ! "$ref" == "" ];then
    if [ "$diff2ref" == "" ] || [ "$ref2diff" == "" ];then
  echo "If running in ref space, you must specify '-ref <refimage> <diff2ref> <ref2diff>'"
  exit 1
    fi
150
    if [ `$FSLDIR/bin/imtest "$diff2ref"` -eq 0 ];then
151
152
153
  echo "Image $diff2ref not found."
  exit 1
    fi
154
    if [ `$FSLDIR/bin/imtest "$ref2diff"` -eq 0 ];then
155
156
157
  echo "Image $ref2diff not found."
  exit 1
    fi
Saad Jbabdi's avatar
Saad Jbabdi committed
158
159
fi

Saad Jbabdi's avatar
Saad Jbabdi committed
160
echo SPECIES $spec
Saad Jbabdi's avatar
Saad Jbabdi committed
161
162
163

# GPU stuff
if [ $gpu -eq 0 ];then
164
    ptxbin="$FSLDIR/bin/probtrackx2"
Saad Jbabdi's avatar
Saad Jbabdi committed
165
166
else
    # Temp location of CUDA code
167
    ptxbin="${ptxbin_gpu}"
Saad Jbabdi's avatar
Saad Jbabdi committed
168
169
170
171
fi

# Check compulsory arguments
errflag=0
172
if [ -z "$bpx" ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
173
174
    echo "Must set compulsory argument '-bpx'"
    errflag=1
175
elif [ ! -d "$bpx" ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
176
177
178
    echo "Bedpostx folder $bpx not found"
    errflag=1
fi
179
if [ -z "$out" ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
180
181
182
183
    echo "Must set compulsory argument '-out'"
    errflag=1
fi

184
# Check which species and protocols to run
185
if [ -z "$spec" ];then
186
187
188
  echo "Must set compulsory argument '-species'"
  errflag=1
elif [ "$spec" == "HUMAN" ];then
189
190
    if [ -z "$stdref" ];then
        stdref="$FSLDIR/data/standard/MNI152_T1_1mm"
191
    fi
192
193
194
195
196
    strdef="$datadir/Human/structureList"
    if [ -z "$p" ];then
      p="$datadir/Human"
      if [ -z "$str" ];then
        str="$p/structureList"
197
      fi
198
199
    elif [ -n "$p" ];then
      if [ -z "$str" ];then
200
201
202
        echo "If selecting a protocol folder, must set argument '-str'"
        errflag=1
      fi
Saad Jbabdi's avatar
Saad Jbabdi committed
203
    fi
204
elif [ "$spec" == "MACAQUE" ];then
205
206
    if [ -z "$stdref" ];then
        stdref="$datadir/standard/F99/mri/struct_brain"
207
    fi
208
209
210
211
212
    strdef="$datadir/Macaque/structureList"
    if [ -z "$p" ];then
      p="$datadir/Macaque"
      if [ -z "$str" ];then
        str="$p/structureList"
213
      fi
214
215
    elif [ -n "$p" ];then
      if [ -z "$str" ];then
216
217
218
        echo "If selecting a protocol folder, must set argument '-str'"
        errflag=1
      fi
Saad Jbabdi's avatar
Saad Jbabdi committed
219
    fi
220
elif [ "$spec" == "CUSTOM" ];then
221
    if [ -z "$stdref" ];then
222
223
224
        echo "If -species CUSTOM, must set argument '-stdref'"
        errflag=1
    fi
225
    if [ -z "$p" ];then
226
227
228
        echo "If -species CUSTOM, must set argument '-p'"
        errflag=1
    fi
229
    if [ -z "$str" ];then
230
231
232
        echo "If -species CUSTOM, must set argument '-str'"
        errflag=1
    fi
Saad Jbabdi's avatar
Saad Jbabdi committed
233
else
234
    echo "Species must be one of HUMAN or MACAQUE or CUSTOM"
235
236
237
    errflag=1
fi

238
239
240
241
242
# Check that -stdref, -str and -p exist
if [ ! `$FSLDIR/bin/imtest $stdref` -eq 1 ];then
  echo "Standard space reference image '-stdref' $stdref not found"
  errflag=1
fi
243
if [ ! -d "$p" ];then
244
245
246
  echo "Protocol folder $p not found"
  errflag=1
fi
247
if [ ! -f "$str" ];then
248
249
250
251
  echo "Structures files $str not found"
  errflag=1
fi

252
253
254
255
256
257
if [ "$errflag" -eq 1 ];then
    echo ""
    echo "Exit without doing anything.."
    exit 1
fi

258
# Check -str file format
259
if [ "$spec" == "HUMAN" ] || [ "$spec" == "MACAQUE" ];then
260
261
262
263
264
265
tchk=()
if [ ! "$str" == "$strdef" ];then
  while read structstring; do
    struct=`echo $structstring | awk '{print $1}'`
    if [ "${struct:0:1}" == "#" ];then
  	foo=0
266
    elif [ -z "$struct" ];then
267
  	foo=0
Saad Jbabdi's avatar
Saad Jbabdi committed
268
    else
269
270
271
272
273
      if [ "`echo $structstring | awk '{print $2}'`" == "" ];then
      tchk+=("1") # if empty, then 1 - get nseeds from default -str file
      else
      tchk+=("0")
      fi
Saad Jbabdi's avatar
Saad Jbabdi committed
274
    fi
275
  done < "$str"
276
277
278
279
280
281
282
283
284
285
  IFS=$'\n'; tchk=($(sort <<<"${tchk[*]}")); unset IFS
  if [ ! "${tchk[0]}" -eq "${tchk[${#tchk[@]}-1]}" ];then
    echo ""
    echo "-str file format is inconsistent. Format should be either:"
    echo "<tractName> per line"
    echo "OR"
    echo "<tractName> [samples=1] per line"
    echo "samples=1, 1 means 1000. Use '#' to skip lines"
    errflag=1
  elif [ ${tchk[0]} -eq 1 ];then
286
    echo " -- getting 'nsamples' from default structure file"
287
288
    seedget=1
  fi
Saad Jbabdi's avatar
Saad Jbabdi committed
289
fi
290
291
292
293
294
295
elif [ "$spec" == "CUSTOM" ];then
# add in check for str file format, user must provide number of seeds
  while read structstring; do
    struct=`echo $structstring | awk '{print $1}'`
    if [ "${struct:0:1}" == "#" ];then
    foo=0
296
    elif [ -z "$struct" ];then
297
298
299
300
301
302
303
304
305
    foo=0
    else
      if [ "`echo $structstring | awk '{print $2}'`" == "" ];then
      echo "If using -species CUSTOM -str file format must follow:"
      echo "<tractName> [samples=1] per line"
      echo "samples=1, 1 means 1000. Use '#' to skip lines"
      errflag=1
      fi
    fi
306
  done < "$str"
307
fi
Saad Jbabdi's avatar
Saad Jbabdi committed
308

309
# Check space option
310
if [ $nat -eq 1 ] && [ -n "$ref" ];then
311
312
313
314
315
    echo "You have selected the native space and ref space options"
    echo "Must select EITHER '-native', '-ref <refimage> <diff2ref> <ref2diff>', OR use the default standard space"
    errflag=1
fi

Saad Jbabdi's avatar
Saad Jbabdi committed
316
317
318
319
320
321
322
if [ "$errflag" -eq 1 ];then
    echo ""
    echo "Exit without doing anything.."
    exit 1
fi

# Create output folders
323
324
325
mkdir -p "$out"
mkdir -p "$out/logs"
mkdir -p "$out/tracts"
Saad Jbabdi's avatar
Saad Jbabdi committed
326
327
328
329
330

# Set common ptx options
opts=" -s $bpx/merged -m $bpx/nodif_brain_mask -V 1"
opts=" $opts --loopcheck --forcedir --opd --ompl --seedref=$stdref --sampvox=1 --randfib=1 "

331
332
333
# Add any user-defined ptx options
opts=" $opts $ptx_opts"

334
if [ "$nat" -eq 0 ] && [ -z "$ref" ];then
335
    opts="$opts --seedref=$stdref --xfm=$std2diff --invxfm=$diff2std "
336
elif [ -n "$ref" ]; then
337
338
339
340
    opts="$opts --seedref=$ref --xfm=$ref2diff --invxfm=$diff2ref "
fi

# If running in reference space, combine std2diff and diff2ref for std2ref
341
if [ -n "$ref" ];then
342
    echo " -- combining standard-to-diffusion and diffusion-to-reference transforms"
343
344
    std2ref="$out/standard2ref"
    $FSLDIR/bin/convertwarp -o "$std2ref" -r "$ref" "--warp1=$std2diff" "--warp2=$diff2ref"
Saad Jbabdi's avatar
Saad Jbabdi committed
345
346
347
fi

# Loop over structures
348
349
commands="$out/commands.txt"
rm -rf "$commands"
Saad Jbabdi's avatar
Saad Jbabdi committed
350
351
echo "Preparing submission script..."
while read structstring; do
352
353
354
  struct=`echo $structstring | awk '{print $1}'`
  # skip empty lines and lines that start with '#'
  if [ "${struct:0:1}" == "#" ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
355
356
357
	# do nothing
	foo=0
	#echo "----- Skip line $structstring -----"
358
  elif [ -z "$struct" ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
359
360
361
	# do nothing
	foo=0
	#echo "----- Skip empty line -----"
362
  else
Saad Jbabdi's avatar
Saad Jbabdi committed
363
	#echo "autoTrack $struct"
364
	mkdir -p "$out/tracts/$struct"
Saad Jbabdi's avatar
Saad Jbabdi committed
365

366
367
368
  # if running a subset of tracts and -str format is <tractName>, check for nseed in str file
  if [ "$seedget" -eq 1 ]; then
      nseed=`grep -w ${struct} $strdef | awk '{print $2}'`
369
      if [ -z "$nseed" ];then
370
371
372
373
374
        echo "Couldn't find number of samples for '$struct'. Exiting now."
        exit 1
      fi
  else
	    nseed=`echo $structstring | awk '{print $2}'`
375
	    if [ -z "$nseed" ];then nseed=1; fi
376
  fi
Saad Jbabdi's avatar
Saad Jbabdi committed
377
	nseed=$(echo "scale=0; 1000 * ${nseed} / 1"|bc)
378

379
	maskdir="$p/$struct"
Saad Jbabdi's avatar
Saad Jbabdi committed
380

381
382
	#  DEALING WITH RESAMPLING --
	# Pick space to run tractography in (diffusion or standard)
Saad Jbabdi's avatar
Saad Jbabdi committed
383
	if [ "$nat" -eq 1 ];then
384
	    echo "${struct} -- transforming masks into native space"
385
	    mkdir -p "$out/masks/$struct"
Saad Jbabdi's avatar
Saad Jbabdi committed
386
	    for m in seed stop exclude;do
387
388
389
		if [ `$FSLDIR/bin/imtest "$maskdir/$m"` -eq 1 ];then
		    $FSLDIR/bin/applywarp -i "$maskdir/$m" -o "$out/masks/$struct/$m" -w "$std2diff" -r "$bpx/nodif_brain_mask" -d float
		    $FSLDIR/bin/fslmaths "$out/masks/$struct/$m" -thr 0.1 -bin "$out/masks/$struct/$m" -odt char
Saad Jbabdi's avatar
Saad Jbabdi committed
390
391
		fi
		eval "${m}=$out/masks/$struct/$m"
392
	    done
393
394
  elif [ ! "$ref" == "" ];then
      echo "${struct} -- transforming masks into ref space"
395
      mkdir -p "$out/masks/$struct"
396
      for m in seed stop exclude;do
397
398
399
    if [ `$FSLDIR/bin/imtest "$maskdir/$m"` -eq 1 ];then
        $FSLDIR/bin/applywarp -i "$maskdir/$m" -o "$out/masks/$struct/$m" -w "$std2ref" -r "$ref" -d float
        $FSLDIR/bin/fslmaths "$out/masks/$struct/$m" -thr 0.1 -bin "$out/masks/$struct/$m" -odt char
400
401
402
    fi
    eval "${m}=$out/masks/$struct/$m"
      done
Saad Jbabdi's avatar
Saad Jbabdi committed
403
	else
404
	    for m in seed stop exclude;do
Saad Jbabdi's avatar
Saad Jbabdi committed
405
406
		if [ $res -gt 0 ];then
		    # Resample at a different resolution
407
408
409
410
		    mkdir -p "$out/masks/$struct"
		    if [ `$FSLDIR/bin/imtest "$maskdir/$m"` -eq 1 ];then
			$FSLDIR/bin/flirt -in "$maskdir/$m" -out "$out/masks/$struct/$m" -applyisoxfm "$res" -ref "$maskdir/$m"
			$FSLDIR/bin/fslmaths "$out/masks/$struct/$m" -thr 0.1 -bin "$out/masks/$struct/$m" -odt char
411
412
		    fi
		    eval "${m}=$out/masks/$struct/$m"
Saad Jbabdi's avatar
Saad Jbabdi committed
413
414
415
416
		else
		    eval "${m}=$maskdir/$m"
		fi
	    done
417
	fi
Saad Jbabdi's avatar
Saad Jbabdi committed
418
419

	# Deal with targets (in cases where there may be more than one)
420
421
	targets=`$FSLDIR/bin/imglob "$maskdir"/target*`
	targetfile="$out/tracts/$struct/targets.txt"
Saad Jbabdi's avatar
Saad Jbabdi committed
422
423
	if [ "$nat" -eq 1 ];then
	    for tfile in $targets;do
424
425
426
		t=`basename "$tfile"`
		$FSLDIR/bin/applywarp -i "$tfile" -o "$out/masks/$struct/$t" -w "$std2diff" -r "$bpx/nodif_brain_mask" -d float
		$FSLDIR/bin/fslmaths "$out/masks/$struct/$t" -thr 0.1 -bin "$out/masks/$struct/$t" -odt char
Saad Jbabdi's avatar
Saad Jbabdi committed
427
	    done
428
	    echo $out/masks/$struct/target* > "$targetfile"
429
430
  elif [ ! "$ref" == "" ];then
      for tfile in $targets;do
431
432
433
    t=`basename "$tfile"`
    $FSLDIR/bin/applywarp -i "$tfile" -o "$out/masks/$struct/$t" -w "$std2ref" -r "$ref" -d float
    $FSLDIR/bin/fslmaths "$out/masks/$struct/$t" -thr 0.1 -bin "$out/masks/$struct/$t" -odt char
434
      done
435
      echo $out/masks/$struct/target* > "$targetfile"
Saad Jbabdi's avatar
Saad Jbabdi committed
436
437
438
439
	else
	    if [ $res -gt 0 ];then
		# Resample at a different resolution
		for tfile in $targets;do
440
441
442
		    t=`basename "$tfile"`
		    $FSLDIR/bin/flirt -in "$tfile" -out "$out/masks/$struct/$t" -applyisoxfm "$res" -ref "$tfile"
		    $FSLDIR/bin/fslmaths "$out/masks/$struct/$t" -thr 0.1 -bin "$out/masks/$struct/$t" -odt char
Saad Jbabdi's avatar
Saad Jbabdi committed
443
		done
444
		echo $out/masks/$struct/target* > "$targetfile"
Saad Jbabdi's avatar
Saad Jbabdi committed
445
	    else
446
		echo $targets > "$targetfile"
Saad Jbabdi's avatar
Saad Jbabdi committed
447
448
449
450
451
	    fi
	fi

	# Get generic options
	o=$opts
452

Saad Jbabdi's avatar
Saad Jbabdi committed
453
        # Add inclusion/exclusion masks
454
	if [ `$FSLDIR/bin/imtest "$stop"` -eq 1 ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
455
456
	    o="$o --stop=$stop"
	fi
457
	if [ `$FSLDIR/bin/imtest "$exclude"` -eq 1 ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
458
459
460
461
462
	    o="$o --avoid=$exclude"
	fi

	# Add seed/target
	o1="$o --nsamples=$nseed -x $seed "
463

464
	if [ -n "${targets}" ];then #Add waypoints if there are any
Saad Jbabdi's avatar
Saad Jbabdi committed
465
466
467
468
469
470
       	    o1=" $o1 --waypoints=$targetfile "
	fi

        # Outputs
	o1=" $o1 -o density --dir=$out/tracts/$struct"

471
472

	# Does the protocol define a second run with inverted seed / target masks?
473
474
475
476
477
	if [ -e "$maskdir/invert" ]; then  #Invert-mode
	    if 	[ `$FSLDIR/bin/imtest "$maskdir/target.nii.gz"` -eq 1 ];then 	# Check if a target.nii.gz image exists when invert option has been selected.
		mkdir -p "$out/tracts/$struct/tractsInv"
		if [ `$FSLDIR/bin/imtest "$out/masks/$struct/target.nii.gz"` -eq 1 ]; then
		    target="$out/masks/$struct/target"
Saad Jbabdi's avatar
Saad Jbabdi committed
478
		else
479
		    target="$maskdir/target"
Saad Jbabdi's avatar
Saad Jbabdi committed
480
		fi
481
		o2="$o --nsamples=\"$nseed\" -x \"${target}\" --waypoints=\"$seed\" -o density --dir=\"$out/tracts/$struct/tractsInv\""
Saad Jbabdi's avatar
Saad Jbabdi committed
482
483

		# merge runs for forward and inverted tractography runs and then normalise (create commands but don't execute)
484
		mergecmd="$FSLDIR/bin/fslmaths \"$out/tracts/$struct/density\" -add \"$out/tracts/$struct/tractsInv/density\" \"$out/tracts/$struct/sum_density\""
Saad Jbabdi's avatar
Saad Jbabdi committed
485
486

		#Add waypoints (create command but don't execute)
487
		addcmd="echo \"scale=5; \`cat \"$out/tracts/$struct/waytotal\"\` + \`cat \"$out/tracts/$struct/tractsInv/waytotal\"\` \"|bc > \"$out/tracts/$struct/sum_waytotal\""
488

Saad Jbabdi's avatar
Saad Jbabdi committed
489
		# Waypoint normalisation (create command but don't execute)
490
		normcmd="$FSLDIR/bin/fslmaths \"$out/tracts/$struct/sum_density\" -div \`cat \"$out/tracts/$struct/sum_waytotal\"\` \"$out/tracts/$struct/densityNorm\""
Saad Jbabdi's avatar
Saad Jbabdi committed
491
492

		# Append to command list
493
		echo "$ptxbin $o1; $ptxbin $o2; $mergecmd; $addcmd; $normcmd" >> "$commands"
Saad Jbabdi's avatar
Saad Jbabdi committed
494
495
496
497
498
499
	    else
		echo "Invert Option selected, but more than one target defined! A 'target.nii.gz' is expected. Exiting now"
		exit 1
	    fi
	else  	#No invert-mode
	    # Waypoint normalisation (create command but don't execute)
500
	    normcmd="$FSLDIR/bin/fslmaths \"$out/tracts/$struct/density\" -div \`cat \"$out/tracts/$struct/waytotal\"\` \"$out/tracts/$struct/densityNorm\""
Saad Jbabdi's avatar
Saad Jbabdi committed
501
502

	    # Append to command list
503
	    echo "$ptxbin $o1; $normcmd" >> "$commands"
Saad Jbabdi's avatar
Saad Jbabdi committed
504
505
506
507
508
	fi
    fi

done < $str

509
chmod +x "$commands"
Saad Jbabdi's avatar
Saad Jbabdi committed
510

511
512
# Submit all commands to run in parallel on the cluster
# One job per tract for a CPU cluster, one job for all tracts for a GPU cluster.
Duncan Mortimer's avatar
Duncan Mortimer committed
513
514
515
if [ $gpu -eq 0 ]; then
  fsl_sub -T 2160 -n -l "$out/logs" -N xtract -t "$commands"
else
516
  fsl_sub -T 300 --coprocessor=cuda -l "$out/logs" -N xtract "$commands"
Saad Jbabdi's avatar
Saad Jbabdi committed
517
518
519
fi

#EOF