Skip to content

Commit

Permalink
Make formatting in shell scripts more consistent
Browse files Browse the repository at this point in the history
- Unify the formatting of messages (start with INFO/WARNING/ERROR or indent)
- Add/fix punctuation of messages
- Remove extra semi-colon after exit statements
- Move flags --parallel and --threads
- Use += to append to arrays
  • Loading branch information
dkuegler committed Oct 2, 2024
1 parent 5d922d6 commit 4e838f2
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 195 deletions.
100 changes: 54 additions & 46 deletions recon_surf/recon-surf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ case $key in
shift # past value
;;
--vol_segstats)
echo "WARNING: the --vol_segstats flag is obsolete and will be removed, --vol_segstats ignored."
echo "WARNING: The --vol_segstats flag is obsolete and will be removed, --vol_segstats ignored."
;;
--segstats_legacy) segstats_legacy="true" ;;
--fstess) fstess=1 ;;
Expand All @@ -190,8 +190,9 @@ case $key in
if [ -f "$1" ]; then
export FS_LICENSE="$1"
else
echo "Provided FreeSurfer license file $1 could not be found. Make sure to provide the full path and name. Exiting..."
exit 1;
echo "ERROR: Provided FreeSurfer license file $1 could not be found. Make sure to provide"
echo " the full path and name. Exiting..."
exit 1
fi
shift # past value
;;
Expand All @@ -218,26 +219,28 @@ echo ""
# Warning if run as root user
if [ -z "$allow_root" ] && [ "$(id -u)" == "0" ]
then
echo "You are trying to run '$0' as root. We advice to avoid running FastSurfer as root, "
echo "because it will lead to files and folders created as root."
echo "If you are running FastSurfer in a docker container, you can specify the user with "
echo "'-u \$(id -u):\$(id -g)' (see https://docs.docker.com/engine/reference/run/#user)."
echo "If you want to force running as root, you may pass --allow_root to recon-surf.sh."
echo "ERROR: You are trying to run '$0' as root. We advice to avoid running FastSurfer "
echo " as root, because it will lead to files and folders created as root."
echo " If you are running FastSurfer in a docker container, you can specify the user "
echo " with '-u \$(id -u):\$(id -g)' (see https://docs.docker.com/engine/reference/run/#user)."
echo " If you want to force running as root, you may pass --allow_root to recon-surf.sh."
exit 1;
fi

if [ -z "$SUBJECTS_DIR" ]
then
echo "\$SUBJECTS_DIR not set. Either set it via the shell prior to running recon-surf.sh or supply it via the --sd flag."
echo "ERROR: \$SUBJECTS_DIR not set. Either set it via the shell prior to running recon-surf.sh"
echo " or supply it via the --sd flag."
exit 1
fi

if [ -z "$FREESURFER_HOME" ]
then
echo "Did not find \$FREESURFER_HOME. A working version of FreeSurfer $FS_VERSION_SUPPORT is needed to run recon-surf locally."
echo "Make sure to export and source FreeSurfer before running recon-surf.sh: "
echo "export FREESURFER_HOME=/path/to/your/local/fs$FS_VERSION_SUPPORT"
echo "source \$FREESURFER_HOME/SetUpFreeSurfer.sh"
echo "ERROR: Did not find \$FREESURFER_HOME. A working version of FreeSurfer $FS_VERSION_SUPPORT"
echo " is needed to run recon-surf locally."
echo " Make sure to export and source FreeSurfer before running recon-surf.sh: "
echo " export FREESURFER_HOME=/path/to/your/local/fs$FS_VERSION_SUPPORT"
echo " source \$FREESURFER_HOME/SetUpFreeSurfer.sh"
exit 1
fi
# needed in FS72 due to a bug in recon-all --fill using FREESURFER instead of FREESURFER_HOME
Expand All @@ -248,10 +251,11 @@ then
if grep -q -v "${FS_VERSION_SUPPORT}" "$FREESURFER_HOME/build-stamp.txt"
then
echo "ERROR: You are trying to run recon-surf with FreeSurfer version $(cat "$FREESURFER_HOME/build-stamp.txt")."
echo "We are currently supporting only FreeSurfer $FS_VERSION_SUPPORT"
echo "Therefore, make sure to export and source the correct FreeSurfer version before running recon-surf.sh: "
echo "export FREESURFER_HOME=/path/to/your/local/fs$FS_VERSION_SUPPORT"
echo "source \$FREESURFER_HOME/SetUpFreeSurfer.sh"
echo " We are currently supporting only FreeSurfer $FS_VERSION_SUPPORT"
echo " Therefore, make sure to export and source the correct FreeSurfer version"
echo " before running recon-surf.sh: "
echo " export FREESURFER_HOME=/path/to/your/local/fs$FS_VERSION_SUPPORT"
echo " source \$FREESURFER_HOME/SetUpFreeSurfer.sh"
exit 1
fi
fi
Expand All @@ -266,21 +270,21 @@ if [ "$long" == "1" ]
then
basedir="$SUBJECTS_DIR/$baseid"
if [ ! -f "$basedir/base-tps.fastsurfer" ] ; then
echo "$baseid is either not found in SUBJECTS_DIR or it is not a longitudinal template"
echo "directory, which needs to contain base-tps.fastsurfer file. Please ensure that"
echo "the base (template) has been created when running with --long flag."
echo "ERROR: $baseid is either not found in SUBJECTS_DIR or it is not a longitudinal"
echo " template directory, which needs to contain base-tps.fastsurfer file. Please"
echo " ensure that the base (template) has been created when running with --long flag."
exit 1
fi
if [ ! grep -Fxq $basedir/base-tps.fastsurfer $subject ] ; then
echo "$subject id not found in base-tps.fastsurfer. Please ensure that this time point"
echo "was included during creation of the base (template)."
elif ! grep -Fxq "$basedir/base-tps.fastsurfer" "$subject" ; then
echo "ERROR: $subject id not found in base-tps.fastsurfer. Please ensure that this time"
echo " point was included during creation of the base (template)."
exit 1
fi
fi

if [ -z "$t1" ] || [ ! -f "$t1" ]
then
echo "ERROR: T1 image ($t1) could not be found. Must supply an existing T1 input (conformed, full head) via --t1 (absolute path and name)."
echo "ERROR: T1 image ($t1) could not be found. Must supply an existing T1 input"
echo " (conformed, full head) via --t1 (absolute path and name)."
# needed to create orig.mgz and to get file name. This will eventually be changed.
exit 1
fi
Expand All @@ -301,7 +305,9 @@ if [ ! -f "$asegdkt_segfile" ]
then
# No segmentation found, exit with error
echo "ERROR: Segmentation ($asegdkt_segfile) could not be found! "
echo "Segmentation must either exist in default location (\$SUBJECTS_DIR/\$SID/mri/aparc.DKTatlas+aseg.deep.mgz) or you must supply the absolute path and name via --asegdkt_segfile."
echo " Segmentation must either exist in default location (\$SUBJECTS_DIR/\$SID/mri/"
echo " aparc.DKTatlas+aseg.deep.mgz) or you must supply the absolute path and name via"
echo " --asegdkt_segfile <filename>."
exit 1
fi

Expand All @@ -318,16 +324,17 @@ fi

if [ "$(echo -n "${SUBJECTS_DIR}/${subject}" | wc -m)" -gt 185 ]
then
echo "ERROR: subject directory path is very long."
echo "This is known to cause errors due to some commands run by freesurfer versions built for Ubuntu."
echo "--sd + --sid should be less than 185 characters long."
echo "ERROR: Subject directory path is very long."
echo " This is known to cause errors due to some commands run by freesurfer versions built for Ubuntu."
echo " --sd + --sid should be less than 185 characters long."
exit 1
fi

# Check if running on an existing subject directory
if [ -f "$SUBJECTS_DIR/$subject/mri/wm.mgz" ] || [ -f "$SUBJECTS_DIR/$subject/mri/aparc.DKTatlas+aseg.orig.mgz" ] ; then
echo "ERROR: running on top of an existing subject directory!"
echo "The output directory must not contain data from a previous invocation of recon-surf."
if [ -f "$SUBJECTS_DIR/$subject/mri/wm.mgz" ] || [ -f "$SUBJECTS_DIR/$subject/mri/aparc.DKTatlas+aseg.orig.mgz" ]
then
echo "ERROR: Running on top of an existing subject directory!"
echo " The output directory must not contain data from a previous invocation of recon-surf."
exit 1
fi

Expand Down Expand Up @@ -404,9 +411,6 @@ cmd="$python $FASTSURFER_HOME/FastSurferCNN/quick_qc.py --asegdkt_segfile $asegd
RunIt "$cmd" "$LF"
echo "" | tee -a "$LF"




########################################## START ########################################################

{
Expand Down Expand Up @@ -481,6 +485,10 @@ if [ ! -f "$mask" ] || [ ! -f "$mdir/aseg.auto_noCCseg.mgz" ] ; then
echo " "
echo "============= Creating aseg.auto_noCCseg (map aparc labels back) ==============="
echo " "
msg="mri/mask.mgz or mri/aseg.auto_noCCseg.mgz are missing, but these files are
required in recon-surf.sh and always created in the segmentation pipeline run.
It is recommended to transfer these files from there!"
echo "WARNING: $msg"
} | tee -a "$LF"

# reduce labels to aseg, then create mask (dilate 5, erode 4, largest component), also mask aseg to remove outliers
Expand Down Expand Up @@ -579,8 +587,8 @@ fi
} | tee -a $LF

# create norm by masking nu
cmd="mri_mask $mdir/nu.mgz $mdir/mask.mgz $mdir/norm.mgz"
RunIt "$cmd" "$LF"
cmd=(mri_mask "$mdir/nu.mgz" "$mask" "$mdir/norm.mgz")
RunIt "${cmd[*]}" "$LF"
if [ "$get_t1" == "1" ]
then
# create T1.mgz from nu (!! here we could also try passing aseg?)
Expand All @@ -597,11 +605,11 @@ then
# base_flags="-w $mdir/ctrl_vol.mgz $mdir/bias_vol.mgz"
#fi
# cmd="mri_normalize -g 1 -seed 1234 -mprage $base_flags $mdir/nu.mgz $mdir/T1.mgz $noconform_if_hires"
cmd="mri_normalize -g 1 -seed 1234 -mprage $mdir/nu.mgz $mdir/T1.mgz $noconform_if_hires"
RunIt "$cmd" "$LF"
cmd=(mri_normalize -g 1 -seed 1234 -mprage "$mdir/nu.mgz" "$mdir/T1.mgz" $noconform_if_hires)
RunIt "${cmd[*]}" "$LF"
# create brainmask by masking T1
cmd="mri_mask $mdir/T1.mgz $mdir/mask.mgz $mdir/brainmask.mgz"
RunIt "$cmd" "$LF"
cmd=(mri_mask "$mdir/T1.mgz" "$mask" "$mdir/brainmask.mgz")
RunIt "${cmd[*]}" "$LF"
else
# create brainmask by linkage to norm.mgz (masked nu.mgz)
pushd "$mdir" > /dev/null || ( echo "Could not cd to $mdir" ; exit 1 )
Expand Down Expand Up @@ -892,7 +900,7 @@ for hemi in lh rh ; do
# -aseg $SUBJECTS_DIR/$subject/mri/aseg.presurf.mgz \
# -seed 1234 $subject $hemi $SUBJECTS_DIR/$subject/surf/${hemi}.sphere.reg \
# $SUBJECTS_DIR/$subject/label/${hemi}.aparc.DKTatlas-guided.annot"

else # longitudinal

# SPHERE (mapping with minimal distortion) we copy it from base:
Expand Down Expand Up @@ -975,7 +983,7 @@ for hemi in lh rh ; do
echo "pushd $mdir > /dev/null" >> "$CMDF"

# CREATE WHITE SURFACE:
# 4 min compute white :
# 4 min compute white :
inputsurf="../surf/$hemi.white.preaparc"
longmaxdist=""
if [ "$long" == "1" ] ; then
Expand All @@ -985,7 +993,7 @@ for hemi in lh rh ; do
cmd="mris_place_surface --adgws-in ../surf/autodet.gw.stats.${hemi}.dat --seg aseg.presurf.mgz --threads $threads --wm wm.mgz --invol brain.finalsurfs.mgz --$hemi --i $inputsurf --o ../surf/${hemi}.white --white --nsmooth 0 --rip-label ../label/${hemi}.cortex.label --rip-bg --rip-surf ../surf/${hemi}.white.preaparc --aparc $aparc $longmaxdist"
RunIt "$cmd" "$LF" "$CMDF"

# CREAT PIAL SURFACE
# CREAT PIAL SURFACE
# 4 min compute pial :
inputsurf="../surf/$hemi.white"
longmaxdist=""
Expand All @@ -995,7 +1003,7 @@ for hemi in lh rh ; do
fi
cmd="mris_place_surface --adgws-in ../surf/autodet.gw.stats.${hemi}.dat --seg aseg.presurf.mgz --threads $threads --wm wm.mgz --invol brain.finalsurfs.mgz --$hemi --i $inputsurf --o ../surf/${hemi}.pial.T1 --pial --nsmooth 0 --rip-label ../label/${hemi}.cortex+hipamyg.label --pin-medial-wall ../label/${hemi}.cortex.label --aparc $aparc --repulse-surf ../surf/${hemi}.white --white-surf ../surf/${hemi}.white $longmaxdist"
RunIt "$cmd" "$LF" "$CMDF"

echo "popd > /dev/null" >> "$CMDF"

# Here insert DoT2Pial later --> if T2pial is not run, need to softlink pial.T1 to pial!
Expand All @@ -1017,7 +1025,7 @@ for hemi in lh rh ; do
cmd="mris_place_surface --thickness ../surf/$hemi.white ../surf/$hemi.pial 20 5 ../surf/$hemi.thickness"
RunIt "$cmd" "$LF" "$CMDF"
echo "popd > /dev/null" >> "$CMDF"



# ============================= CURVSTATS ===============================================
Expand Down
32 changes: 20 additions & 12 deletions recon_surf/talairach-reg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,32 @@
# The call signature is:
usage="talairach-reg.sh <mri-directory> <3T atlas: true/false> <Logfile>"

if [[ "$#" == "0" ]]
then
echo "usage: $usage"
exit 0
fi

if [[ "$#" != "3" ]]
then
echo "Invalid number of arguments to talairach-reg.sh, must be '$usage'"
echo "ERROR: Invalid number of arguments to talairach-reg.sh, must be '$usage'"
exit 1
fi
if ! [[ -d "$1" ]]
then
echo "First argument must be the mri-directory: $usage"
echo "ERROR: First argument must be the mri-directory: $usage"
exit 1
fi
mdir="$1"
if [[ "$2" != "true" ]] && [[ "$2" != "false" ]]
then
echo "Second argument must be true or false: $usage"
echo "ERROR: Second argument must be true or false: $usage"
exit 1
fi
atlas3T="$2"
if ! [[ -f "$3" ]]
then
echo "Third argument must be the logfile (must already exist): $usage"
echo "ERROR: Third argument must be the logfile (must already exist): $usage"
exit 1
fi
LF="$3"
Expand Down Expand Up @@ -78,8 +84,10 @@ RunIt "$cmd" $LF
cmd="cp $mdir/transforms/talairach.auto.xfm $mdir/transforms/talairach.xfm"
RunIt "$cmd" $LF
# talairach.lta: convert to lta
cmd="lta_convert --src $mdir/orig.mgz --trg $FREESURFER_HOME/average/mni305.cor.mgz --inxfm $mdir/transforms/talairach.xfm --outlta $mdir/transforms/talairach.xfm.lta --subject fsaverage --ltavox2vox"
RunIt "$cmd" $LF
cmd=(lta_convert --src "$mdir/orig.mgz" --trg "$FREESURFER_HOME/average/mni305.cor.mgz"
--inxfm "$mdir/transforms/talairach.xfm" --outlta "$mdir/transforms/talairach.xfm.lta"
--subject fsaverage --ltavox2vox)
RunIt "${cmd[*]}" "$LF"

# FS would here create better nu.mgz using talairach transform (finds wm and maps it to approx 110)
#NuIterations="1 --proto-iters 1000 --distance 50" # default 3T
Expand All @@ -89,18 +97,18 @@ RunIt "$cmd" $LF

# Since we do not run mri_em_register we sym-link other talairach transform files here
pushd "$mdir/transforms" > /dev/null || ( echo "ERROR: Could not change to the transforms directory $mdir/transforms!" | tee -a "$LF" && exit 1 )
cmd="ln -sf talairach.xfm.lta talairach_with_skull.lta"
RunIt "$cmd" $LF
cmd="ln -sf talairach.xfm.lta talairach.lta"
RunIt "$cmd" $LF
cmd=(ln -sf "talairach.xfm.lta" "talairach_with_skull.lta")
RunIt "${cmd[*]}" "$LF"
cmd=(ln -sf "talairach.xfm.lta" "talairach.lta")
RunIt "${cmd[*]}" "$LF"
popd > /dev/null || exit 1

# Add xfm to nu
# (use orig_nu, if nu.mgz does not exist already); by default, it should exist
if [[ -e "$mdir/nu.mgz" ]]; then src_nu_file="$mdir/nu.mgz"
else src_nu_file="$mdir/orig_nu.mgz"
fi
cmd="mri_add_xform_to_header -c $mdir/transforms/talairach.xfm $src_nu_file $mdir/nu.mgz"
RunIt "$cmd" $LF
cmd=(mri_add_xform_to_header -c "$mdir/transforms/talairach.xfm" "$src_nu_file" "$mdir/nu.mgz")
RunIt "${cmd[*]}" "$LF"

popd > /dev/null || return
Loading

0 comments on commit 4e838f2

Please sign in to comment.