Skip to content

Commit

Permalink
run_fastsurfer.sh
Browse files Browse the repository at this point in the history
- Adds the --edits flag
- Add additional log messages for edits
- Support for manedit files for asegdkt_segfile.
- Also propagate asegdkt_segfile into  aseg_segfile and mask_name

recon_surf/recon-surf.sh
- Adds the --edits flag
- Add additional log messages for edits
- Change check for existing run into a message instead a fail criteria in edits mode
- Do not delete logfile in edits mode
- Make mask and aseg_noCC required for edits mode (otherwise it might get unclear what should or was already done between run_fastsurfer and recon-surf)
- Add manedit "search" for mask and aseg_nocc (the latter is

recon_surf/talairach-reg.sh
- Add an extra argument (new argument #3 to indicate whether talairach.reg should support edits)
- Support for edits: some messages and if in edit mode, only run talairach registration if mri/transforms/talairach.xfm and mri/transforms/talairach.auto.xfm are the same (so there are no edits) -- thus edits in talairach.xfm superceed automatic results
- talairch-reg.sh will now fail if mri/transforms/talairach.xfm exists, but the script is not running in edits mode (independent if talairach.auto.xfm is the same or not).

recon_surf/functions.sh
- add the add_file_suffix function, which adds a file suffix (for example manedits) to a file name
  • Loading branch information
dkuegler committed Oct 11, 2024
1 parent 0d6bc97 commit 82cd0aa
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 22 deletions.
23 changes: 22 additions & 1 deletion recon_surf/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,25 @@ function echo_quoted()
sep=" "
done
echo ""
}
}

function add_file_suffix()
{
# params:
# 1: filename
# 2: suffix to add

# example: add_file_suffix /path/to/file.nii.gz suffix -> /path/to/file.suffix.nii.gz

# file extensions supported:
file_extensions=(nii.gz nii mgz stats annot ctab label log txt lta xfm yaml)
for extension in "${file_extensions[@]}"
do
pattern="\.${extension//./\\.}$"
if [[ "$1" =~ $pattern ]]
then
length=$((${#1} - ${#extension}))
echo "${1:0:$length}$2.$extension"
fi
done
}
75 changes: 60 additions & 15 deletions recon_surf/recon-surf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ FLAGS:
that segmentations produced otherwise are conformed.
Requires an ABSOLUTE Path! Default location:
\$SUBJECTS_DIR/\$sid/mri/aparc.DKTatlas+aseg.deep.mgz
--mask_name <mask_file> Path to the brainmask file to use. Default location:
\$SUBJECTS_DIR/\$sid/mri/mask.mgz
--edits Disable the check for existing recon-surf.sh run,
replaces mri/wm.mgz by mri/wm.manedit.mgz as well as
mri/aseg.auto_noCC.mgz by mri/aseg.auto_noCC.manedit.mgz,
includes brain.finalsurfs.manedit.mgz edits, and enables
FreeSurfer-style WM control points.
--fstess Revert to FreeSurfer mri_tesselate for surface creation
(default: mri_mc)
--fsqsphere Revert to FreeSurfer iterative inflation for qsphere
Expand Down Expand Up @@ -177,9 +184,11 @@ case $key in
asegdkt_segfile="$1"
shift # past value
;;
--mask_name) mask="$1" ; shift ;;
--vol_segstats)
echo "WARNING: The --vol_segstats flag is obsolete and will be removed, --vol_segstats ignored."
;;
--edits) edits="true" ;;
--segstats_legacy) segstats_legacy="true" ;;
--fstess) fstess=1 ;;
--fsqsphere) fsqsphere=1 ;;
Expand Down Expand Up @@ -269,7 +278,7 @@ fi

if [[ "$base" == "1" ]]
then
if [ ! -f "$sd/$subject/base-tps.fastsurfer" ] ; then
if [ ! -f "$SUBJECTS_DIR/$subject/base-tps.fastsurfer" ] ; then
echo "ERROR: $subject is either not found in SUBJECTS_DIR"
echo "or it is not a longitudinal template directory (base),"
echo "which needs to contain base-tps.fastsurfer file. Please ensure that"
Expand Down Expand Up @@ -347,9 +356,17 @@ 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."
exit 1
on_existing_run="true"
if [[ "$edits" == "true" ]]
then
echo "INFO: Running on top of an existing subject directory, but edits is $edits."
else
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
else
on_existing_run="false"
fi

# collect info
Expand All @@ -375,14 +392,16 @@ sdir="$SUBJECTS_DIR/$subject/surf"
statsdir="$SUBJECTS_DIR/$subject/stats"
ldir="$SUBJECTS_DIR/$subject/label"

mask="$mdir/mask.mgz"

# set default mask and make mask absolute
if [[ -z "$mask" ]] ; then mask="$mdir/mask.mgz"
elif [[ "${mask:0:1}" != "/" ]] ; then mask="$SUBJECTS_DIR/$subject/$mask"
fi

# Set up log file
DoneFile="$SUBJECTS_DIR/$subject/scripts/recon-surf.done"
if [ "$DoneFile" != /dev/null ] ; then rm -f "$DoneFile" ; fi
LF="$SUBJECTS_DIR/$subject/scripts/recon-surf.log"
if [ "$LF" != /dev/null ] ; then rm -f "$LF" ; fi
if [ "$LF" != /dev/null ] && [[ "$edits" != "true" ]]; then rm -f "$LF" ; fi
echo "Log file for recon-surf.sh" >> "$LF"
{ # all output tee -a "$LF"
date 2>&1
Expand All @@ -394,13 +413,16 @@ echo "Log file for recon-surf.sh" >> "$LF"
cat "$FREESURFER_HOME/build-stamp.txt" 2>&1
echo "$VERSION"
uname -a 2>&1
if [[ "$on_existing_run" == "true" ]]
then
echo "Running on top of an existing subject directory with edits=$edits!"
fi
echo " "
if [ "$base" == "1" ] ; then
echo " "
echo "================== BASE - Longitudinal Template Creation ========================="
echo " "
fi
if [ "$long" == "1" ] ; then
elif [ "$long" == "1" ] ; then
echo " "
echo "================== LONG - Longitudinal Timpe Point Creation ======================"
echo "long: using template directory (base) $baseid"
Expand Down Expand Up @@ -468,6 +490,12 @@ fi
cmd="mri_convert $t1 $mdir/orig.mgz"
RunIt "$cmd" "$LF"

asegdkt_segfile_manedit=$(add_file_suffix "$asegdkt_segfile" "manedit")
if [[ ! "$asegdkt_segfile_manedit" =~ (\.manedit){2,}\. ]] && # skipping; exclude double manedit
[[ -f "$asegdkt_segfile_manedit" ]]
then
asegdkt_segfile="$asegdkt_segfile_manedit" # use the manedit file
fi
cmd="mri_convert $asegdkt_segfile $mdir/aparc.DKTatlas+aseg.orig.mgz"
RunIt "$cmd" "$LF"

Expand All @@ -492,13 +520,15 @@ if [ "$long" == "1" ] ; then
run_it "$LF" "${cmda[@]}"
fi

if [ ! -f "$mask" ] || [ ! -f "$mdir/aseg.auto_noCCseg.mgz" ] ; then
aseg_nocc="aseg.auto_noCCseg.mgz"
if [ ! -f "$mask" ] || [ ! -f "$mdir/$aseg_nocc" ] ; then
# independently of the existence of manedit files, generate the baseline files.
# Mask or aseg.auto_noCCseg not found; create them from aparc.DKTatlas+aseg
{
# Mask or aseg.auto_noCCseg not found; create them from aparc.DKTatlas+aseg
echo " "
echo "============= Creating aseg.auto_noCCseg (map aparc labels back) ==============="
echo " "
echo "WARNING: mri/mask.mgz or mri/aseg.auto_noCCseg.mgz are missing, but these files are"
echo "WARNING: $mask or mri/$aseg_nocc are missing, but these files are"
echo " required in recon-surf.sh and always created in the segmentation pipeline run."
echo " It is recommended to transfer these files from there!"
} | tee -a "$LF"
Expand All @@ -518,8 +548,18 @@ if [ ! -f "$mask" ] || [ ! -f "$mdir/aseg.auto_noCCseg.mgz" ] ; then
fi

run_it "$LF" "${cmda[@]}"
mask_generated_in_recon_surf="true"
else
mask_generated_in_recon_surf="false"
fi

# replace mask by manedit-ed mask
mask_manedit="$(add_file_suffix "$mask" "manedit")"
if [[ "$edits" == "true" ]] && [[ -e "$mask_manedit" ]]
then
echo "INFO: mri/$mask_manedit detected, supersedes mri/$mask in recon_surf.sh."
mask="$mask_manedit"
fi

# ============================= NU BIAS CORRECTION =======================================

Expand Down Expand Up @@ -557,6 +597,7 @@ if [[ ! -f "$mdir/transforms/talairach.lta" ]] || [[ ! -f "$mdir/transforms/tala
cmda=("$binpath/talairach-reg.sh" "$LF"
--dir "$mdir" --conformed_name "$mdir/orig.mgz" --norm_name "$mdir/orig_nu.mgz")
if [[ "$long" == "1" ]] ; then cmda+=(--long "$basedir") ; fi
if [[ "$edits" == "1" ]] ; then cmda+=(--edits) ; fi
if [[ "$atlas3T" == "true" ]] ; then cmda+=(--3T) ; fi

{
Expand All @@ -577,7 +618,7 @@ fi
} | tee -a $LF

# the difference between nu and orig_nu is the fact that nu has the talairach-registration header
# create norm by masking nu
# create norm by masking nu (supports manedit-ed mask)
cmda=(mri_mask "$mdir/nu.mgz" "$mask" "$mdir/norm.mgz")
run_it "$LF" "${cmda[@]}"
if [ "$get_t1" == "1" ]
Expand All @@ -598,7 +639,7 @@ then
# cmd="mri_normalize -g 1 -seed 1234 -mprage $base_flags $mdir/nu.mgz $mdir/T1.mgz $noconform_if_hires"
cmda=(mri_normalize -g 1 -seed 1234 -mprage "$mdir/nu.mgz" "$mdir/T1.mgz" $noconform_if_hires)
run_it "$LF" "${cmda[@]}"
# create brainmask by masking T1
# create brainmask by masking T1 (supports manedit-ed mask)
cmda=(mri_mask "$mdir/T1.mgz" "$mask" "$mdir/brainmask.mgz")
run_it "$LF" "${cmda[@]}"
else
Expand All @@ -619,7 +660,7 @@ fi
# create aseg.auto including corpus callosum segmentation and 46 sec, requires norm.mgz
# Note: if original input segmentation already contains CC, this will exit with ERROR
# in the future maybe check and skip this step (and next)
cmd="mri_cc -aseg aseg.auto_noCCseg.mgz -o aseg.auto.mgz -lta $mdir/transforms/cc_up.lta $subject"
cmd="mri_cc -aseg $aseg_nocc -o aseg.auto.mgz -lta $mdir/transforms/cc_up.lta $subject"
RunIt "$cmd" "$LF"
# add CC into aparc.DKTatlas+aseg.deep (not sure if this is really needed)
cmd="$python ${binpath}paint_cc_into_pred.py -in_cc $mdir/aseg.auto.mgz -in_pred $asegdkt_segfile -out $mdir/aparc.DKTatlas+aseg.deep.withCC.mgz"
Expand Down Expand Up @@ -775,6 +816,10 @@ for hemi in lh rh ; do

# ============================= FIX - WHITEPREAPARC ==================================================

wm_file="wm.mgz"
wm_manedit="$(add_file_suffix "$wm_file" "manedit")"
if [[ -e "$wm_manedit" ]] ; then wm_file="$wm_manedit" ; fi

# In Long stream we skip topo fix
if [ "$long" == "0" ] ; then

Expand Down
Loading

0 comments on commit 82cd0aa

Please sign in to comment.