Skip to content

Commit

Permalink
Merge pull request #7 from madisoth/master
Browse files Browse the repository at this point in the history
10.5T branch commits + new pipeline options
  • Loading branch information
madisoth authored Aug 4, 2021
2 parents e31ab3a + c9a7967 commit df17842
Show file tree
Hide file tree
Showing 11 changed files with 948 additions and 277 deletions.
9 changes: 9 additions & 0 deletions Examples/Scripts/PreFreeSurferPipeLine.bat
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ for Subject in $Subjlist ; do
T2wTemplate2mm="${HCPPIPEDIR_Templates}/MacaqueYerkes19_T2w_1.0mm_grad.nii.gz" #MacaqueYerkes1.0mm T2wTemplate
TemplateMask="${HCPPIPEDIR_Templates}/MacaqueYerkes19_T1w_0.5mm_brain_mask_dil.nii.gz" #Brain mask MacaqueYerkes0.5mm template
Template2mmMask="${HCPPIPEDIR_Templates}/MacaqueYerkes19_T1w_1.0mm_brain_mask_dil.nii.gz" #MacaqueYerkes1.0mm template
StudyTemplate="${HCPPIPEDIR_Templates}/MacaqueYerkes19_T1w_0.5mm_grad.nii.gz"
StudyTemplateBrain="${HCPPIPEDIR_Templates}/MacaqueYerkes19_T1w_0.5mm_brain.nii.gz"

#Scan Settings
TE="2.46" #delta TE in ms for field map or "NONE" if not used
Expand All @@ -100,6 +102,7 @@ for Subject in $Subjlist ; do
AvgrdcSTRING="FIELDMAP" #Averaging and readout distortion correction methods: "NONE" = average any repeats with no readout correction "FIELDMAP" = average any repeats and use field map for readout correction "TOPUP" = Use Spin Echo FieldMap
TopupConfig="NONE" #Config for topup or "NONE" if not used
BiasFieldSmoothingSigma="1.5"
useAntsReg="false"

${FSLDIR}/bin/fsl_sub ${QUEUE} \
${HCPPIPEDIR}/PreFreeSurfer/PreFreeSurferPipeline.sh \
Expand Down Expand Up @@ -131,6 +134,9 @@ for Subject in $Subjlist ; do
--avgrdcmethod="$AvgrdcSTRING" \
--topupconfig="$TopupConfig" \
--bfsigma="$BiasFieldSmoothingSigma" \
--useAntsReg="${useAntsReg}" \
--StudyTemplate="${StudyTemplate}" \
--StudyTemplateBrain="${StudyTemplateBrain}" \
--printcom=$PRINTCOM

# The following lines are used for interactive debugging to set the positional parameters: $1 $2 $3 ...
Expand Down Expand Up @@ -163,6 +169,9 @@ for Subject in $Subjlist ; do
--avgrdcmethod=${AvgrdcSTRING} \
--topupconfig=${TopupConfig} \
--bfsigma=${BiasFieldSmoothingSigma} \
--useAntsReg=${useAntsReg} \
--StudyTemplate=${StudyTemplate} \
--StudyTemplateBrain=${StudyTemplateBrain} \
--printcom=${PRINTCOM}"

echo ". ${EnvironmentScript}"
Expand Down
6 changes: 3 additions & 3 deletions Examples/Scripts/PreFreeSurferPipelineBatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ main()
--brainsize="$BrainSize" \
--fnirtconfig="$FNIRTConfig" \
--fmapmag="$MagnitudeInputName" \
--fmapmagbrain="$StudyFolder"/masks/$(basename $(remove_ext "$MagnitudeInputName"))_brain.nii.gz \
--fmapmagbrain="$StudyFolder"/masks/$(basename $(remove_ext "$MagnitudeInputName"))_brain.nii.gz \
--fmapphase="$PhaseInputName" \
--fmapgeneralelectric="$GEB0InputName" \
--echodiff="$TE" \
Expand All @@ -482,8 +482,8 @@ main()
--unwarpdir="$UnwarpDir" \
--gdcoeffs="$GradientDistortionCoeffs" \
--avgrdcmethod="$AvgrdcSTRING" \
--topupconfig="$TopupConfig" \
--useT2="$useT2" \
--topupconfig="$TopupConfig" \
--useT2="$useT2" \
--t1normalized="$T1wNormalized" \
--printcom=$PRINTCOM \
--revepi=${ReverseEpi:-false}
Expand Down
51 changes: 47 additions & 4 deletions FreeSurfer/FreeGreyPipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ useT2=`opts_GetOpt1 "--useT2" $@` #AP 20162111
MaxThickness=`opts_GetOpt1 "--maxThickness" $@` # Max threshold for thickness measurements (default = 5mm)
NormMethod=`opts_GetOpt1 "--normalizationMethod" $@` # Normalization method to be used (or none)
hypernormalize=`opts_GetOpt1 "--hypernormalize" $@` #deprecated - lose after BIDSApp doesn't use it anymore.
NormGMStdDevScale=`opts_GetOpt1 "--normgmstddevscale" $@` # normalized GM std dev scale factor
NormWMStdDevScale=`opts_GetOpt1 "--normwmstddevscale" $@` # normalized WM std dev scale factor
NormCSFStdDevScale=`opts_GetOpt1 "--normcsfstddevscale" $@` # normalized CSF std dev scale factor

# option to make white surface from adult-normalized T1w (if it exists)
MakeWhiteFromNormT1=`opts_GetOpt1 "--makewhitefromnormt1" $@`
MakeWhiteFromNormT1="$(echo ${MakeWhiteFromNormT1} | tr '[:upper:]' '[:lower:]')" # to lower case

# option to keep the pial surface generated from initial pass of mris_make_surfaces
# (using the hypernormalized brain.AN.mgz T1w image, unless hypernormalization was omitted)
# instead of using it as a prior for a 2nd pass of mris_make_surfaces
# (2nd pass uses the non-hypernormalized brain.finalsurfs.mgz T1w by default)
SinglePassPial=`opts_GetOpt1 "--singlepasspial" $@`
SinglePassPial="$(echo ${SinglePassPial} | tr '[:upper:]' '[:lower:]')" # to lower case

if [ -z "${NormMethod}" ] ; then
# Default is to use the adult grey matter intensity profile.
Expand Down Expand Up @@ -59,7 +73,7 @@ Subnum=$SubjectID
if [[ "${NormMethod^^}" == "NONE" ]] ; then
echo Skipping hyper-normalization step per request.
else
${HCPPIPEDIR_FS}/hypernormalize.sh ${SubjectDIR} ${NormMethod^^}
${HCPPIPEDIR_FS}/hypernormalize.sh ${SubjectDIR} ${NormMethod^^} ${NormGMStdDevScale} ${NormWMStdDevScale} ${NormCSFStdDevScale}
T1wNImage="T1wN_acpc.nii.gz"
T1wNImageBrain="T1wN_acpc_brain.nii.gz"
echo T1wNImage=$T1wNImage
Expand Down Expand Up @@ -274,6 +288,7 @@ for TXw in $Modalities; do
popd
done
SubjectID=$Subnum

#grab white matter
fslmaths ${AsegFile}_1mm.nii.gz -thr 41 -uthr 41 -bin blah41.nii.gz
fslmaths ${AsegFile}_1mm.nii.gz -thr 2 -uthr 2 -bin blah2.nii.gz
Expand All @@ -287,7 +302,23 @@ mri_pretess wm.asegedit.mgz wm norm.mgz wm.mgz
mri_fill -a ../scripts/ponscc.cut.log -xform transforms/talairach.lta -segmentation aseg.mgz wm.mgz filled.mgz
popd
echo "BEGIN: recon-all"
recon-all -subjid ${SubjectID} -tessellate -smooth1 -inflate1 -qsphere -fix -white -smooth2 -inflate2 -sphere

recon-all -subjid ${SubjectID} -tessellate -smooth1 -inflate1 -qsphere -fix

if [ $MakeWhiteFromNormT1 = true ] && [ ! -z $T1wNImage ]; then
# make white surfaces from adult-normalized T1w
echo "Making white surfaces from normalized T1w"
# copy adult-normalized T1w volume to main subject mri dir
cp -T -n ${SubjectID}N/mri/brain.finalsurfs.mgz ${SubjectID}/mri/brain.AN.mgz
for hemi in l r; do
mris_make_surfaces -whiteonly -noaparc -mgz -T1 brain.AN ${SubjectID} ${hemi}h
else
# make white surfaces from non-normalized T1w (same as default recon-all -white)
for hemi in l r; do
mris_make_surfaces -whiteonly -noaparc -mgz -T1 brain.finalsurfs ${SubjectID} ${hemi}h
fi

recon-all -subjid ${SubjectID} -smooth2 -inflate2 -sphere
# Using monkey .tif for calculating surface registration.
echo "Registering surface using average.curvature.filled from MacaqueYerkes19"
pushd ${SUBJECTS_DIR}/${SubjectID}/surf
Expand All @@ -308,13 +339,25 @@ if [ ! -z $T1wNImage ]; then
cp -T -n ${SubjectID}/surf/"${hemi}"h.pial ${SubjectID}/surf/"${hemi}"h.pial.noAN
#mris_make_surfaces ${MAXTHICKNESS} -orig_white white.noAN -white NOWRITE -mgz -T1 brain.AN $SubjectID ${hemi}h
mris_make_surfaces ${MAXTHICKNESS} -white NOWRITE -mgz -T1 brain.AN $SubjectID ${hemi}h
mris_make_surfaces ${MAXTHICKNESS} -orig_pial pial -white NOWRITE -mgz -T1 brain.finalsurfs $SubjectID ${hemi}h
# if true, skip 2nd pass of mris_make_surfaces
# and keep the pial derived from adult-normalized brain as the final surface
if [ $SinglePassPial = false ]; then
mris_make_surfaces ${MAXTHICKNESS} -orig_pial pial -white NOWRITE -mgz -T1 brain.finalsurfs $SubjectID ${hemi}h
else
echo "Using single pass pial"
fi
done
else
for hemi in l r; do
#mris_make_surfaces ${MAXTHICKNESS} -orig_white white.noAN -white NOWRITE -mgz -T1 brain.AN $SubjectID ${hemi}h
mris_make_surfaces ${MAXTHICKNESS} -white NOWRITE -mgz -T1 brain.finalsurfs $SubjectID ${hemi}h
mris_make_surfaces ${MAXTHICKNESS} -orig_pial pial -white NOWRITE -mgz -T1 brain.finalsurfs $SubjectID ${hemi}h
# if true, skip 2nd pass of mris_make_surfaces
# and keep the initial pial as the final surface
if [ $SinglePassPial = false ]; then
mris_make_surfaces ${MAXTHICKNESS} -orig_pial pial -white NOWRITE -mgz -T1 brain.finalsurfs $SubjectID ${hemi}h
else
echo "Using single pass pial"
fi
done
fi

Expand Down
13 changes: 11 additions & 2 deletions FreeSurfer/scripts/Change2AdultIntensity.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ base_image=$1
adult_image=$2 #adult freesurfer atlas
# EXAMPLE means_RB_all

NormGMStdDevScale=$3 # scaling factor for standard deviation of the normalized gray matter (relative to the standard deviation of the reference adult GM image).
# If T1w_acpc_dc_restore has regions of low/uneven GM intensity causing issues with surface generation,
# reducing SD (e.g. scaling by 0.5) may yield improvement.
# Alternatively, consider manually setting these threshold values used by FreeSurfer's mris_make_surfaces:
# MIN_GRAY_AT_WHITE_BORDER, MAX_GRAY, MAX_GRAY_AT_CSF_BORDER, MIN_GRAY_AT_CSF_BORDER


NormWMStdDevScale=$4 # scaling factor for standard deviation of the normalized white matter (relative to the standard deviation of the reference adult WM image.)

#Changing intensities of monkey WM,GM and CSF to match adult freesurfer template
fslmaths ${base_image} -sub `fslstats ${base_image}_GM -M` -div `fslstats ${base_image}_GM -S` -mul `fslstats ${adult_image}_GM -S` -add `fslstats ${adult_image}_GM -M` ${base_image}_GMshifted
fslmaths ${base_image} -sub `fslstats ${base_image}_WM -M` -div `fslstats ${base_image}_WM -S` -mul `fslstats ${adult_image}_WM -S` -add `fslstats ${adult_image}_WM -M` -mas ${base_image}_WM ${base_image}_WM_AdultInt
fslmaths ${base_image} -sub `fslstats ${base_image}_GM -M` -div `fslstats ${base_image}_GM -S` -mul `fslstats ${adult_image}_GM -S` -mul ${NormGMStdDevScale} -add `fslstats ${adult_image}_GM -M` ${base_image}_GMshifted
fslmaths ${base_image} -sub `fslstats ${base_image}_WM -M` -div `fslstats ${base_image}_WM -S` -mul `fslstats ${adult_image}_WM -S` -mul ${NormWMStdDevScale} -add `fslstats ${adult_image}_WM -M` -mas ${base_image}_WM ${base_image}_WM_AdultInt

#Replace WM in GM_shifted with shifted WM

Expand Down
16 changes: 13 additions & 3 deletions FreeSurfer/scripts/ChangeROIs2AdultIntensity.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
base_image=$1 # EXAMPLE T1w_acpc_dc_restore - do not include file extensions
adult_image=$2 # THIS IS ADULT FREESURFER ATLAS-GET INTENSITIES FROM HERE; EXAMPLE means_RB_all

NormGMStdDevScale=$3 # scaling factor for standard deviation of the normalized gray matter (relative to the standard deviation of the reference adult GM image).
# If T1w_acpc_dc_restore has regions of low/uneven GM intensity causing issues with surface generation,
# reducing SD (e.g. scaling by 0.5) may help.
# Alternatively, consider manually setting these threshold values used by FreeSurfer's mris_make_surfaces:
# MIN_GRAY_AT_WHITE_BORDER, MAX_GRAY, MAX_GRAY_AT_CSF_BORDER, MIN_GRAY_AT_CSF_BORDER

NormWMStdDevScale=$4 # scaling factor for standard deviation of the normalized white matter (relative to the standard deviation of the reference adult WM image).

NormCSFStdDevScale=$5 # scaling factor for standard deviation of the normalized CSF (relative to the standard deviation of the reference adult CSF image).

# Change intensities of monkey WM,GM and CSF to match adult freesurfer template.
fslmaths ${base_image}_WM -sub `fslstats ${base_image}_WM -M` -div `fslstats ${base_image}_WM -S` -mul `fslstats ${adult_image}_WM -S` -add `fslstats ${adult_image}_WM -M` -mas ${base_image}_WM ${base_image}_WM_AdultInt
fslmaths ${base_image}_GM -sub `fslstats ${base_image}_GM -M` -div `fslstats ${base_image}_GM -S` -mul `fslstats ${adult_image}_GM -S` -add `fslstats ${adult_image}_GM -M` -mas ${base_image}_GM ${base_image}_GM_AdultInt
fslmaths ${base_image}_CSF -sub `fslstats ${base_image}_CSF -M` -div `fslstats ${base_image}_CSF -S` -mul `fslstats ${adult_image}_CSF -S` -add `fslstats ${adult_image}_CSF -M` -mas ${base_image}_CSF ${base_image}_CSF_AdultInt
fslmaths ${base_image}_WM -sub `fslstats ${base_image}_WM -M` -div `fslstats ${base_image}_WM -S` -mul `fslstats ${adult_image}_WM -S` -mul ${NormGMStdDevScale} -add `fslstats ${adult_image}_WM -M` -mas ${base_image}_WM ${base_image}_WM_AdultInt
fslmaths ${base_image}_GM -sub `fslstats ${base_image}_GM -M` -div `fslstats ${base_image}_GM -S` -mul `fslstats ${adult_image}_GM -S` -mul ${NormWMStdDevScale} -add `fslstats ${adult_image}_GM -M` -mas ${base_image}_GM ${base_image}_GM_AdultInt
fslmaths ${base_image}_CSF -sub `fslstats ${base_image}_CSF -M` -div `fslstats ${base_image}_CSF -S` -mul `fslstats ${adult_image}_CSF -S` -mul ${NormCSFStdDevScale} -add `fslstats ${adult_image}_CSF -M` -mas ${base_image}_CSF ${base_image}_CSF_AdultInt

# Put monkey image back together.
fslmaths ${base_image}_WM_AdultInt -add ${base_image}_GM_AdultInt -add ${base_image}_CSF_AdultInt ${base_image}_AdultInt
Expand Down
7 changes: 5 additions & 2 deletions FreeSurfer/scripts/hypernormalize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ echo SOURCE_DIR=${SOURCE_DIR}

subjectdir=$1
NormMethod=$2
NormGMStdDevScale=$3
NormWMStdDevScale=$4
NormCSFStdDevScale=$5
if [ -z "${NormMethod}" ] ; then
# Default is to use the adult grey matter intensity profile.
NormMethod="ADULT_GM_IP"
Expand All @@ -21,10 +24,10 @@ ${SOURCE_DIR}/make_WMGMCSF_masks.sh T1w_acpc_dc_restore.nii.gz aseg_acpc.nii.gz

if [[ "${NormMethod^^}" == "ROI_IPS" ]] ; then
# Call the old script that changes each ROI's intensity profile and puts them back together.
${SOURCE_DIR}/ChangeROIs2AdultIntensity.bash T1w_acpc_dc_restore ${HCPPIPEDIR_Templates}/means_RB_all
${SOURCE_DIR}/ChangeROIs2AdultIntensity.bash T1w_acpc_dc_restore ${HCPPIPEDIR_Templates}/means_RB_all ${NormGMStdDevScale} ${NormWMStdDevScale} ${NormCSFStdDevScale}
else
# Default: call script to change whole brain based on grey matter in adult ref.
${SOURCE_DIR}/Change2AdultIntensity.bash T1w_acpc_dc_restore ${HCPPIPEDIR_Templates}/means_RB_all
${SOURCE_DIR}/Change2AdultIntensity.bash T1w_acpc_dc_restore ${HCPPIPEDIR_Templates}/means_RB_all ${NormGMStdDevScale} ${NormWMStdDevScale}
fi
mv T1w_acpc_dc_restore_AdultInt_thr.nii.gz T1wN_acpc.nii.gz
${FSLDIR}/bin/fslmaths T1wN_acpc.nii.gz -mas T1w_acpc_dc_restore_brain.nii.gz T1wN_acpc_brain.nii.gz
Expand Down
Loading

0 comments on commit df17842

Please sign in to comment.