Commit 4cb9b283 authored by Duncan Mortimer's avatar Duncan Mortimer
Browse files

Quote strings with variables within

This prevents issues like variables being passed as 'something; rm -rf *'
It also gives some robustness to spaces in file paths
Also good practice to use -n for non-empty strings and -z for empty strings
parent 227748f3
......@@ -8,10 +8,10 @@
# Protocols created by Rogier Mars et al.
# Location of probtrackx2_gpu binary
ptxbin_gpu=$FSLDIR/bin/probtrackx2_gpu
ptxbin_gpu="$FSLDIR/bin/probtrackx2_gpu"
# Location of xtract data
datadir=$FSLDIR/data/xtract_data
datadir="$FSLDIR/data/xtract_data"
Usage() {
cat << EOF
......@@ -68,7 +68,7 @@ EOF
Splash
[ "$1" = "" ] && Usage
[ -z "$1" ] && Usage
# Set default options
......@@ -90,7 +90,7 @@ 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)
# Parse command-line arguments
while [ ! -z "$1" ];do
while [ -n "$1" ];do
case "$1" in
-bpx) bpx=$2;shift;;
-out) out=$2;shift;;
......@@ -104,7 +104,7 @@ while [ ! -z "$1" ];do
-ref) ref=$2;diff2ref=$3;ref2diff=$4;shift;shift;shift;;
-res) res=$2;shift;;
-list) list=1;shift;;
-ptx_options) ptx_opts=`cat $2`;shift;;
-ptx_options) ptx_opts=`cat "$2"`;shift;;
*) echo "Unknown option '$1'";exit 1;;
esac
......@@ -125,20 +125,20 @@ if [ "$list" -eq 1 ];then
else
tractNames="${tractNames} ${struct},"
fi
done < $datadir/Human/structureList
done < "$datadir/Human/structureList"
echo ${tractNames%,}
exit 1
fi
# Default warps
if [ "$std2diff" == "" ];then
std2diff=$bpx/xfms/standard2diff
diff2std=$bpx/xfms/diff2standard
if [ `$FSLDIR/bin/imtest $std2diff` -eq 0 ];then
if [ -z "$std2diff" ];then
std2diff="$bpx/xfms/standard2diff"
diff2std="$bpx/xfms/diff2standard"
if [ `$FSLDIR/bin/imtest "$std2diff"` -eq 0 ];then
echo "Image $std2diff not found."
exit 1
fi
if [ `$FSLDIR/bin/imtest $diff2std` -eq 0 ];then
if [ `$FSLDIR/bin/imtest "$diff2std"` -eq 0 ];then
echo "Image $diff2std not found."
exit 1
fi
......@@ -147,11 +147,11 @@ elif [ ! "$ref" == "" ];then
echo "If running in ref space, you must specify '-ref <refimage> <diff2ref> <ref2diff>'"
exit 1
fi
if [ `$FSLDIR/bin/imtest $diff2ref` -eq 0 ];then
if [ `$FSLDIR/bin/imtest "$diff2ref"` -eq 0 ];then
echo "Image $diff2ref not found."
exit 1
fi
if [ `$FSLDIR/bin/imtest $ref2diff` -eq 0 ];then
if [ `$FSLDIR/bin/imtest "$ref2diff"` -eq 0 ];then
echo "Image $ref2diff not found."
exit 1
fi
......@@ -161,72 +161,72 @@ echo SPECIES $spec
# GPU stuff
if [ $gpu -eq 0 ];then
ptxbin=$FSLDIR/bin/probtrackx2
ptxbin="$FSLDIR/bin/probtrackx2"
else
# Temp location of CUDA code
ptxbin=${ptxbin_gpu}
ptxbin="${ptxbin_gpu}"
fi
# Check compulsory arguments
errflag=0
if [ "$bpx" == "" ];then
if [ -z "$bpx" ];then
echo "Must set compulsory argument '-bpx'"
errflag=1
elif [ ! -d $bpx ];then
elif [ ! -d "$bpx" ];then
echo "Bedpostx folder $bpx not found"
errflag=1
fi
if [ "$out" == "" ];then
if [ -z "$out" ];then
echo "Must set compulsory argument '-out'"
errflag=1
fi
# Check which species and protocols to run
if [ "$spec" == "" ];then
if [ -z "$spec" ];then
echo "Must set compulsory argument '-species'"
errflag=1
elif [ "$spec" == "HUMAN" ];then
if [ "$stdref" == "" ];then
stdref=$FSLDIR/data/standard/MNI152_T1_1mm
if [ -z "$stdref" ];then
stdref="$FSLDIR/data/standard/MNI152_T1_1mm"
fi
strdef=$datadir/Human/structureList
if [ "$p" == "" ];then
p=$datadir/Human
if [ "$str" == "" ];then
str=$p/structureList
strdef="$datadir/Human/structureList"
if [ -z "$p" ];then
p="$datadir/Human"
if [ -z "$str" ];then
str="$p/structureList"
fi
elif [ ! "$p" == "" ];then
if [ "$str" == "" ];then
elif [ -n "$p" ];then
if [ -z "$str" ];then
echo "If selecting a protocol folder, must set argument '-str'"
errflag=1
fi
fi
elif [ "$spec" == "MACAQUE" ];then
if [ "$stdref" == "" ];then
stdref=$datadir/standard/F99/mri/struct_brain
if [ -z "$stdref" ];then
stdref="$datadir/standard/F99/mri/struct_brain"
fi
strdef=$datadir/Macaque/structureList
if [ "$p" == "" ];then
p=$datadir/Macaque
if [ "$str" == "" ];then
str=$p/structureList
strdef="$datadir/Macaque/structureList"
if [ -z "$p" ];then
p="$datadir/Macaque"
if [ -z "$str" ];then
str="$p/structureList"
fi
elif [ ! "$p" == "" ];then
if [ "$str" == "" ];then
elif [ -n "$p" ];then
if [ -z "$str" ];then
echo "If selecting a protocol folder, must set argument '-str'"
errflag=1
fi
fi
elif [ "$spec" == "CUSTOM" ];then
if [ "$stdref" == "" ];then
if [ -z "$stdref" ];then
echo "If -species CUSTOM, must set argument '-stdref'"
errflag=1
fi
if [ "$p" == "" ];then
if [ -z "$p" ];then
echo "If -species CUSTOM, must set argument '-p'"
errflag=1
fi
if [ "$str" == "" ];then
if [ -z "$str" ];then
echo "If -species CUSTOM, must set argument '-str'"
errflag=1
fi
......@@ -240,11 +240,11 @@ if [ ! `$FSLDIR/bin/imtest $stdref` -eq 1 ];then
echo "Standard space reference image '-stdref' $stdref not found"
errflag=1
fi
if [ ! -d $p ];then
if [ ! -d "$p" ];then
echo "Protocol folder $p not found"
errflag=1
fi
if [ ! -f $str ];then
if [ ! -f "$str" ];then
echo "Structures files $str not found"
errflag=1
fi
......@@ -263,7 +263,7 @@ if [ ! "$str" == "$strdef" ];then
struct=`echo $structstring | awk '{print $1}'`
if [ "${struct:0:1}" == "#" ];then
foo=0
elif [ "$struct" == "" ];then
elif [ -z "$struct" ];then
foo=0
else
if [ "`echo $structstring | awk '{print $2}'`" == "" ];then
......@@ -272,7 +272,7 @@ if [ ! "$str" == "$strdef" ];then
tchk+=("0")
fi
fi
done < $str
done < "$str"
IFS=$'\n'; tchk=($(sort <<<"${tchk[*]}")); unset IFS
if [ ! "${tchk[0]}" -eq "${tchk[${#tchk[@]}-1]}" ];then
echo ""
......@@ -293,7 +293,7 @@ elif [ "$spec" == "CUSTOM" ];then
struct=`echo $structstring | awk '{print $1}'`
if [ "${struct:0:1}" == "#" ];then
foo=0
elif [ "$struct" == "" ];then
elif [ -z "$struct" ];then
foo=0
else
if [ "`echo $structstring | awk '{print $2}'`" == "" ];then
......@@ -303,11 +303,11 @@ elif [ "$spec" == "CUSTOM" ];then
errflag=1
fi
fi
done < $str
done < "$str"
fi
# Check space option
if [ $nat -eq 1 ] && [ ! "$ref" == "" ];then
if [ $nat -eq 1 ] && [ -n "$ref" ];then
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
......@@ -320,9 +320,9 @@ if [ "$errflag" -eq 1 ];then
fi
# Create output folders
mkdir -p $out
mkdir -p $out/logs
mkdir -p $out/tracts
mkdir -p "$out"
mkdir -p "$out/logs"
mkdir -p "$out/tracts"
# Set common ptx options
opts=" -s $bpx/merged -m $bpx/nodif_brain_mask -V 1"
......@@ -331,22 +331,22 @@ opts=" $opts --loopcheck --forcedir --opd --ompl --seedref=$stdref --sampvox=1 -
# Add any user-defined ptx options
opts=" $opts $ptx_opts"
if [ "$nat" -eq 0 ] && [ "$ref" == "" ];then
if [ "$nat" -eq 0 ] && [ -z "$ref" ];then
opts="$opts --seedref=$stdref --xfm=$std2diff --invxfm=$diff2std "
elif [ ! "$ref" == "" ]; then
elif [ -n "$ref" ]; then
opts="$opts --seedref=$ref --xfm=$ref2diff --invxfm=$diff2ref "
fi
# If running in reference space, combine std2diff and diff2ref for std2ref
if [ ! "$ref" == "" ];then
if [ -n "$ref" ];then
echo " -- combining standard-to-diffusion and diffusion-to-reference transforms"
std2ref=$out/standard2ref
$FSLDIR/bin/convertwarp -o $std2ref -r $ref --warp1=$std2diff --warp2=$diff2ref
std2ref="$out/standard2ref"
$FSLDIR/bin/convertwarp -o "$std2ref" -r "$ref" "--warp1=$std2diff" "--warp2=$diff2ref"
fi
# Loop over structures
commands=$out/commands.txt
rm -rf $commands
commands="$out/commands.txt"
rm -rf "$commands"
echo "Preparing submission script..."
while read structstring; do
struct=`echo $structstring | awk '{print $1}'`
......@@ -355,48 +355,48 @@ while read structstring; do
# do nothing
foo=0
#echo "----- Skip line $structstring -----"
elif [ "$struct" == "" ];then
elif [ -z "$struct" ];then
# do nothing
foo=0
#echo "----- Skip empty line -----"
else
#echo "autoTrack $struct"
mkdir -p $out/tracts/$struct
mkdir -p "$out/tracts/$struct"
# 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}'`
if [ "$nseed" == "" ];then
if [ -z "$nseed" ];then
echo "Couldn't find number of samples for '$struct'. Exiting now."
exit 1
fi
else
nseed=`echo $structstring | awk '{print $2}'`
if [ "$nseed" == "" ];then nseed=1; fi
if [ -z "$nseed" ];then nseed=1; fi
fi
nseed=$(echo "scale=0; 1000 * ${nseed} / 1"|bc)
maskdir=$p/$struct
maskdir="$p/$struct"
# DEALING WITH RESAMPLING --
# Pick space to run tractography in (diffusion or standard)
if [ "$nat" -eq 1 ];then
echo "${struct} -- transforming masks into native space"
mkdir -p $out/masks/$struct
mkdir -p "$out/masks/$struct"
for m in seed stop exclude;do
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
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
fi
eval "${m}=$out/masks/$struct/$m"
done
elif [ ! "$ref" == "" ];then
echo "${struct} -- transforming masks into ref space"
mkdir -p $out/masks/$struct
mkdir -p "$out/masks/$struct"
for m in seed stop exclude;do
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
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
fi
eval "${m}=$out/masks/$struct/$m"
done
......@@ -404,10 +404,10 @@ while read structstring; do
for m in seed stop exclude;do
if [ $res -gt 0 ];then
# Resample at a different resolution
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
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
fi
eval "${m}=$out/masks/$struct/$m"
else
......@@ -417,33 +417,33 @@ while read structstring; do
fi
# Deal with targets (in cases where there may be more than one)
targets=`imglob $maskdir/target*`
targetfile=$out/tracts/$struct/targets.txt
targets=`$FSLDIR/bin/imglob "$maskdir"/target*`
targetfile="$out/tracts/$struct/targets.txt"
if [ "$nat" -eq 1 ];then
for tfile in $targets;do
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
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
done
echo $out/masks/$struct/target* > $targetfile
echo $out/masks/$struct/target* > "$targetfile"
elif [ ! "$ref" == "" ];then
for tfile in $targets;do
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
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
done
echo $out/masks/$struct/target* > $targetfile
echo $out/masks/$struct/target* > "$targetfile"
else
if [ $res -gt 0 ];then
# Resample at a different resolution
for tfile in $targets;do
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
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
done
echo $out/masks/$struct/target* > $targetfile
echo $out/masks/$struct/target* > "$targetfile"
else
echo $targets > $targetfile
echo $targets > "$targetfile"
fi
fi
......@@ -451,17 +451,17 @@ while read structstring; do
o=$opts
# Add inclusion/exclusion masks
if [ `$FSLDIR/bin/imtest $stop` -eq 1 ];then
if [ `$FSLDIR/bin/imtest "$stop"` -eq 1 ];then
o="$o --stop=$stop"
fi
if [ `$FSLDIR/bin/imtest $exclude` -eq 1 ];then
if [ `$FSLDIR/bin/imtest "$exclude"` -eq 1 ];then
o="$o --avoid=$exclude"
fi
# Add seed/target
o1="$o --nsamples=$nseed -x $seed "
if [ "x${targets}" != "x" ];then #Add waypoints if there are any
if [ -n "${targets}" ];then #Add waypoints if there are any
o1=" $o1 --waypoints=$targetfile "
fi
......@@ -470,53 +470,50 @@ while read structstring; do
# Does the protocol define a second run with inverted seed / target masks?
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
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"
else
target=$maskdir/target
target="$maskdir/target"
fi
o2="$o --nsamples=$nseed -x ${target} --waypoints=$seed -o density --dir=$out/tracts/$struct/tractsInv"
o2="$o --nsamples=\"$nseed\" -x \"${target}\" --waypoints=\"$seed\" -o density --dir=\"$out/tracts/$struct/tractsInv\""
# merge runs for forward and inverted tractography runs and then normalise (create commands but don't execute)
mergecmd="$FSLDIR/bin/fslmaths $out/tracts/$struct/density -add $out/tracts/$struct/tractsInv/density $out/tracts/$struct/sum_density"
mergecmd="$FSLDIR/bin/fslmaths \"$out/tracts/$struct/density\" -add \"$out/tracts/$struct/tractsInv/density\" \"$out/tracts/$struct/sum_density\""
#Add waypoints (create command but don't execute)
addcmd="echo \"scale=5; \`cat $out/tracts/$struct/waytotal\` + \`cat $out/tracts/$struct/tractsInv/waytotal\` \"|bc > $out/tracts/$struct/sum_waytotal"
addcmd="echo \"scale=5; \`cat \"$out/tracts/$struct/waytotal\"\` + \`cat \"$out/tracts/$struct/tractsInv/waytotal\"\` \"|bc > \"$out/tracts/$struct/sum_waytotal\""
# Waypoint normalisation (create command but don't execute)
normcmd="$FSLDIR/bin/fslmaths $out/tracts/$struct/sum_density -div \`cat $out/tracts/$struct/sum_waytotal\` $out/tracts/$struct/densityNorm"
normcmd="$FSLDIR/bin/fslmaths \"$out/tracts/$struct/sum_density\" -div \`cat \"$out/tracts/$struct/sum_waytotal\"\` \"$out/tracts/$struct/densityNorm\""
# Append to command list
echo "$ptxbin $o1; $ptxbin $o2; $mergecmd; $addcmd; $normcmd" >> $commands
echo "$ptxbin $o1; $ptxbin $o2; $mergecmd; $addcmd; $normcmd" >> "$commands"
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)
normcmd="$FSLDIR/bin/fslmaths $out/tracts/$struct/density -div \`cat $out/tracts/$struct/waytotal\` $out/tracts/$struct/densityNorm"
normcmd="$FSLDIR/bin/fslmaths \"$out/tracts/$struct/density\" -div \`cat \"$out/tracts/$struct/waytotal\"\` \"$out/tracts/$struct/densityNorm\""
# Append to command list
echo "$ptxbin $o1; $normcmd" >> $commands
echo "$ptxbin $o1; $normcmd" >> "$commands"
fi
fi
done < $str
chmod +x $commands
chmod +x "$commands"
if [ "x$SGE_ROOT" != "x" ]; then # 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.
# 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.
if [ $gpu -eq 0 ]; then
fsl_sub -T 2160 -n -l "$out/logs" -N xtract -t "$commands"
else
fsl_sub -T 300 -q $FSLGECUDAQ -l $out/logs -N xtract $commands
fi
else # If no SGE, run locally
sh $commands
fsl_sub -T 300 --coprocessor=cuda -l "$out/logs" -N xtract "$commands"
fi
#EOF
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment