From f43ae9689748a6a6f7136f6a53c9ac0022e0160a Mon Sep 17 00:00:00 2001 From: Bene Ramirez Date: Wed, 26 Feb 2020 10:46:05 -0800 Subject: [PATCH 1/6] changed avg issue in prep and T2 reg in vol --- PreFreeSurfer/PreFreeSurferPipeline_orig.sh | 446 ++++++++++++++++++ ...fMRIVolumeProcessingPipeline_orig_T1reg.sh | 399 ++++++++++++++++ 2 files changed, 845 insertions(+) create mode 100755 PreFreeSurfer/PreFreeSurferPipeline_orig.sh create mode 100755 fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh diff --git a/PreFreeSurfer/PreFreeSurferPipeline_orig.sh b/PreFreeSurfer/PreFreeSurferPipeline_orig.sh new file mode 100755 index 0000000..efe115d --- /dev/null +++ b/PreFreeSurfer/PreFreeSurferPipeline_orig.sh @@ -0,0 +1,446 @@ +#!/bin/bash -x + +export OMP_NUM_THREADS=1 +export PATH=`echo $PATH | sed 's|freesurfer/|freesurfer53/|g'` + +# Requirements for this script +# installed versions of: FSL5.0.1 or higher , FreeSurfer (version 5 or higher) , gradunwarp (python code from MGH) +# environment: FSLDIR , FREESURFER_HOME , HCPPIPEDIR , CARET7DIR , PATH (for gradient_unwarp.py) + +# make pipeline engine happy... +if [ $# -eq 1 ] ; then + echo "Version unknown..." + exit 0 +fi + +########################################## PIPELINE OVERVIEW ########################################## + +#TODO + +########################################## OUTPUT DIRECTORIES ########################################## + +## NB: NO assumption is made about the input paths with respect to the output directories - they can be totally different. All input are taken directly from the input variables without additions or modifications. +# NB: Output directories T1wFolder and T2wFolder MUST be different (as various output subdirectories containing standardly named files, e.g. full2std.mat, would overwrite each other) so if this script is modified, then keep these output directories distinct +# Output path specifiers: +# +# ${StudyFolder} is an input parameter +# ${Subject} is an input parameter +# +# Main output directories +# T1wFolder=${StudyFolder}/T1w +# T2wFolder=${StudyFolder}/T2w +# AtlasSpaceFolder=${StudyFolder}/MNINonLinear +# +# All outputs are within the directory: ${StudyFolder}/ +# The list of output directories are the following +# +# T1w/T1w${i}_GradientDistortionUnwarp +# T1w/AverageT1wImages +# T1w/ACPCAlignment +# T1w/BrainExtraction_FNIRTbased +# and the above for T2w as well (s/T1w/T2w/g) +# +# T2w/T2wToT1wDistortionCorrectAndReg +# T1w/BiasFieldCorrection_sqrtT1wXT1w +# MNINonLinear +# +# Also exist: +# T1w/xfms/ +# T2w/xfms/ +# MNINonLinear/xfms/ +# +########################################## SUPPORT FUNCTIONS ########################################## +# function for parsing options +getopt1() { + sopt="$1" + shift 1 + for fn in $@ ; do + if [ `echo $fn | grep -- "^${sopt}=" | wc -w` -gt 0 ] ; then + echo $fn | sed "s/^${sopt}=//" + return 0 + fi + done +} +defaultopt() { + echo $1 +} +################################################## OPTION PARSING ##################################################### + +# Input Variables +StudyFolder=`getopt1 "--path" $@` # "$1" #Path to subject's data folder +Subject=`getopt1 "--subject" $@` # "$2" #SubjectID +T1wInputImages=`getopt1 "--t1" $@` # "$3" #T1w1@T1w2@etc.. +T2wInputImages=`getopt1 "--t2" $@` # "$4" #T2w1@T2w2@etc.. +T1wTemplate=`getopt1 "--t1template" $@` # "$5" #MNI template +T1wTemplateBrain=`getopt1 "--t1templatebrain" $@` # "$6" #Brain extracted MNI T1wTemplate +T1wTemplate2mm=`getopt1 "--t1template2mm" $@` # "$7" #MNI2mm T1wTemplate +T2wTemplate=`getopt1 "--t2template" $@` # "${8}" #MNI T2wTemplate +T2wTemplateBrain=`getopt1 "--t2templatebrain" $@` # "$9" #Brain extracted MNI T2wTemplate +T2wTemplate2mm=`getopt1 "--t2template2mm" $@` # "${10}" #MNI2mm T2wTemplate +TemplateMask=`getopt1 "--templatemask" $@` # "${11}" #Brain mask MNI Template +Template2mmMask=`getopt1 "--template2mmmask" $@` # "${12}" #Brain mask MNI2mm Template +BrainSize=`getopt1 "--brainsize" $@` # "${13}" #StandardFOV mask for averaging structurals +FNIRTConfig=`getopt1 "--fnirtconfig" $@` # "${14}" #FNIRT 2mm T1w Config +MagnitudeInputName=`getopt1 "--fmapmag" $@` # "${16}" #Expects 4D magitude volume with two 3D timepoints +MagnitudeInputBrainName=`getopt1 "--fmapmagbrain" $@` # If you've already masked the magnitude. +PhaseInputName=`getopt1 "--fmapphase" $@` # "${17}" #Expects 3D phase difference volume +TE=`getopt1 "--echodiff" $@` # "${18}" #delta TE for field map +SpinEchoPhaseEncodeNegative=`getopt1 "--SEPhaseNeg" $@` +SpinEchoPhaseEncodePositive=`getopt1 "--SEPhasePos" $@` +DwellTime=`getopt1 "--echospacing" $@` +SEUnwarpDir=`getopt1 "--seunwarpdir" $@` +T1wSampleSpacing=`getopt1 "--t1samplespacing" $@` # "${19}" #DICOM field (0019,1018) +T2wSampleSpacing=`getopt1 "--t2samplespacing" $@` # "${20}" #DICOM field (0019,1018) +UnwarpDir=`getopt1 "--unwarpdir" $@` # "${21}" #z appears to be best +GradientDistortionCoeffs=`getopt1 "--gdcoeffs" $@` # "${25}" #Select correct coeffs for scanner or "NONE" to turn off +AvgrdcSTRING=`getopt1 "--avgrdcmethod" $@` # "${26}" #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" = average and distortion correct at the same time with topup/applytopup only works for 2 images currently +TopupConfig=`getopt1 "--topupconfig" $@` # "${27}" #Config for topup or "NONE" if not used +BiasFieldSmoothingSigma=`getopt1 "--bfsigma" $@` # "$9" +RUN=`getopt1 "--printcom" $@` # use ="echo" for just printing everything and not running the commands (default is to run) +useT2=`getopt1 "--useT2" $@` # useT2 flag added for excluding or including T2 processing, grabbed from batch file +T1wNormalized=`getopt1 "--t1normalized" $@` # brain normalized to matter intensities +useReverseEpi=`getopt1 "--revepi" $@` +MultiTemplateDir=`getopt1 "--multitemplatedir" $@` + + +# Defaults +T1wNormalized=${T1wNormalized:-"NONE"} + +echo "$StudyFolder $Subject" +pushd ${StudyFolder} + +# Paths for scripts etc (uses variables defined in SetUpHCPPipeline.sh) +PipelineScripts=${HCPPIPEDIR_PreFS} +GlobalScripts=${HCPPIPEDIR_Global} + +# Naming Conventions ... OMD these will likely have to be modified for OHSU +T1wImage="T1w" +T1wFolder="T1w" #Location of T1w images +if $useT2; then + T2wImage="T2w" + T2wFolder="T2w" +fi +#**T1wNFolder="T1wN" #Location of T1w Normalized Images +if [ ! $T1wNormalized = "NONE" ]; then + T1wNImage="T1wN" +fi +AtlasSpaceFolder="MNINonLinear" + +# Build Paths, OMD: Outputs, ie calculated data +T1wFolder=${StudyFolder}/${T1wFolder} +if $useT2; then T2wImage="T2w" + T2wFolder=${StudyFolder}/${T2wFolder} +fi +#**T1wNFolder=${StudyFolder}/${T1wNFolder} +AtlasSpaceFolder=${StudyFolder}/${AtlasSpaceFolder} + +echo "$T1wFolder $T2wFolder $T1wNFolder $AtlasSpaceFolder" + +# Unpack List of Images +T1wInputImages=`echo ${T1wInputImages} | sed 's/@/ /g'` #File and path to the T1 MPRG, space separated, OMD +if $useT2; then T2wInputImages=`echo ${T2wInputImages} | sed 's/@/ /g'`; fi #File and path to the T2 MPRG, space separated, OMD + +pushd ${StudyFolder}/ + +if [ ! -e ${T1wFolder}/xfms ] ; then + echo "mkdir -p ${T1wFolder}/xfms/" + mkdir -p ${T1wFolder}/xfms/ +fi +# Placing T1wN niftis in T1w folder -Dakota 10/26/17 +#if [ ! -e ${T1wNFolder}/xfms ]; then +# echo "mkdir -p ${T1wNFolder}/xfms/" +# mkdir -p ${T1wNFolder}/xfms/ +#fi +if $useT2; then + if [ ! -e ${T2wFolder}/xfms ] ; then + echo "mkdir -p ${T2wFolder}/xfms/" + mkdir -p ${T2wFolder}/xfms/ + fi +fi + +if [ ! -e ${AtlasSpaceFolder}/xfms ] ; then + echo "mkdir -p ${AtlasSpaceFolder}/xfms/" + mkdir -p ${AtlasSpaceFolder}/xfms/ +fi + +echo "POSIXLY_CORRECT="${POSIXLY_CORRECT} + + +########################################## DO WORK ########################################## + +######## LOOP over the same processing for T1w and T2w (just with different names) ######## +if $useT2; then Modalities="T1w T2w"; else Modalities="T1w"; fi #Removed T1wN -Dakota 10/26/17 +#bene added T1wN to modalities 6-20-17 but commented it out for now as I didn't know if that makes sense. +#if $useT2; then Modalities="T1w T2w T1wN"; else Modalities="T1w T1wN"; fi +#commented out T1wN steps -Dakota 10/26/17 + +for TXw in ${Modalities} ; do + # set up appropriate input variables + if [ $TXw = T1w ] ; then + TXwInputImages="${T1wInputImages}" + TXwFolder=${T1wFolder} + TXwImage=${T1wImage} #T1W, OMD + TXwTemplate=${T1wTemplate} # It points to the template at 0.7 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) + TXwTemplateBrain=${T1wTemplateBrain} + TXwTemplate2mm=${T1wTemplate2mm} # It points to the template at 2.0 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) + TXwExt=${Subject}_T1w_MPR_average + elif [ $TXw = T2w ]; then + TXwInputImages="${T2wInputImages}" + TXwFolder=${T2wFolder} + TXwImage=${T2wImage} + TXwTemplate=${T2wTemplate} + TXwTemplateBrain=${T2wTemplateBrain} + TXwTemplate2mm=${T2wTemplate2mm} + TXwExt=${Subject}_T2w_SPC_average +#** elif [ $TXw = T1wN ]; then +# TXwInputImages="${T1wNInputImages}" +# TXwFolder=${T1wNFolder} +# TXwImage=${T1wNImage} #T1W Normalized +# TXwTemplate=${T1wTemplate} # It points to the template at 0.7 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) +# TXwTemplateBrain=${T1wTemplateBrain} +# TXwTemplate2mm=${T1wTemplate2mm} # It points to the template at 2.0 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) +# TXwExt=${Subject}_T1w_MPR_average_AdultInt + fi + OutputTXwImageSTRING="" + +#### Gradient nonlinearity correction (for T1w and T2w) #### + + if [ ! $GradientDistortionCoeffs = "NONE" ] ; then + i=1 + for Image in $TXwInputImages ; do + wdir=${TXwFolder}/${TXwImage}${i}_GradientDistortionUnwarp + echo "mkdir -p $wdir" + mkdir -p $wdir + ${RUN} ${FSLDIR}/bin/fslreorient2std $Image ${wdir}/${TXwImage}${i} #Make sure input axes are oriented the same as the templates + ${RUN} ${GlobalScripts}/GradientDistortionUnwarp.sh \ + --workingdir=${wdir} \ + --coeffs=$GradientDistortionCoeffs \ + --in=${wdir}/${TXwImage}${i} \ + --out=${TXwFolder}/${TXwImage}${i}_gdc \ + --owarp=${TXwFolder}/xfms/${TXwImage}${i}_gdc_warp + OutputTXwImageSTRING="${OutputTXwImageSTRING}${TXwFolder}/${TXwImage}${i}_gdc " + i=$(($i+1)) + done + + else + echo "NOT PERFORMING GRADIENT DISTORTION CORRECTION" + i=1 + for Image in $TXwInputImages ; do + ${RUN} ${FSLDIR}/bin/fslreorient2std $Image ${TXwFolder}/${TXwImage}${i}_gdc + OutputTXwImageSTRING="${OutputTXwImageSTRING}${TXwFolder}/${TXwImage}${i}_gdc " + i=$(($i+1)) + done + fi + +#### Average Like Scans #### + + if [ `echo $TXwInputImages | wc -w` -gt 1 ] ; then + mkdir -p ${TXwFolder}/Average${TXw}Images + #if [ ${AvgrdcSTRING} = "TOPUP" ] ; then + # echo "PERFORMING TOPUP READOUT DISTORTION CORRECTION AND AVERAGING" + # ${RUN} ${PipelineScripts}/TopupDistortionCorrectAndAverage.sh ${TXwFolder}/Average${TXw}Images "${OutputTXwImageSTRING}" ${TXwFolder}/${TXwImage} ${TopupConfig} + #else + echo "PERFORMING SIMPLE AVERAGING" + ${RUN} ${PipelineScripts}/AnatomicalAverage.sh -o ${TXwFolder}/${TXwImage} -s ${TXwTemplate} -m ${TemplateMask} \ + -n -w ${TXwFolder}/Average${TXw}Images --noclean -v -b $BrainSize $OutputTXwImageSTRING + #fi + else + echo "ONLY ONE AVERAGE FOUND: COPYING" + ${RUN} ${FSLDIR}/bin/imcp ${TXwFolder}/${TXwImage}1_gdc ${TXwFolder}/${TXwImage} + fi + +#### ACPC align T1w and T2w image to 0.7mm MNI T1wTemplate to create native volume space #### +#** if [ ! $TXw = "T1wN" ]; then + mkdir -p ${TXwFolder}/ACPCAlignment + # Assume Brain has been placed in T1w from Prep stage. + ${RUN} ${PipelineScripts}/ACPCAlignment.sh \ + --workingdir=${TXwFolder}/ACPCAlignment \ + --in=${TXwFolder}/${TXwImage}_brain \ + --ref=${TXwTemplateBrain} \ + --out=${TXwFolder}/${TXwImage}_acpc_brain \ + --omat=${TXwFolder}/xfms/acpc.mat \ + --brainsize=${BrainSize} + # Apply linear transform to head. +# flirt -in ${TXwFolder}/${TXwImage}_brain -ref ${TXwTemplate} -applyxfm -init ${TXwFolder}/xfms/acpc.mat -out ${TXwFolder}/${TXwImage}_acpc_brain +# ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask +#** else + # ensure that the same T1w transform is applied to normed brain. +# echo "applying acpc warp to Normalized image" +# cp ${T1wFolder}/xfms/acpc.mat ${TXwFolder}/xfms/acpc.mat +# ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${TXwFolder}/${TXwImage}" -r "${TXwTemplate}" \ +# --premat="${TXwFolder}/xfms/acpc.mat" -o "${TXwFolder}/${TXwImage}_acpc" +# fi + +#### Brain Extraction (FNIRT-based Masking) #### + +############ FNL - this is performed using ANTs in Prep +# mkdir -p ${TXwFolder}/BrainExtraction_FNIRTbased +# ${RUN} ${PipelineScripts}/BrainExtraction_FNIRTbased.sh \ +# --workingdir=${TXwFolder}/BrainExtraction_ANTsbased \ +# --in=${TXwFolder}/${TXwImage}_acpc \ +# --ref=${TXwTemplate} \ +# --refmask=${TemplateMask} \ +# --ref2mm=${TXwTemplate2mm} \ +# --ref2mmmask=${Template2mmMask} \ +# --outbrain=${TXwFolder}/${TXwImage}_acpc_brain \ +# --outbrainmask=${TXwFolder}/${TXwImage}_acpc_brain_mask \ +# --fnirtconfig=${FNIRTConfig}; +# ${FSLDIR}/bin/flirt -in ${TXwFolder}/${TXwImage}_brain -ref ${TXwTemplate} -applyxfm -init ${TXwFolder}/xfms/acpc.mat -out ${TXwFolder}/${TXwImage}_acpc_brain +# ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask +#** if [ $TXw = T1wN ]; then ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc -mas ${T1wFolder}/${T1wImage}_acpc_brain_mask ${TXwFolder}/${TXwImage}_acpc_brain; fi + + #apply acpc.mat to head + ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${TXwFolder}/${TXwImage}" -r "${TXwTemplate}" \ + --premat="${TXwFolder}/xfms/acpc.mat" -o "${TXwFolder}/${TXwImage}_acpc" + #make brain mask + ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask +done + +######## END LOOP over T1w and T2w ######### + +#Orient T1wN image to standard and create ACPC aligned T1wN image +if [ ! $T1wNormalized = "NONE" ]; then + pushd ${T1wFolder} > /dev/null + cp ${T1wNormalized} ./${T1wNImage}.nii.gz + ${RUN} ${FSLDIR}/bin/fslreorient2std ./${T1wNImage} ${T1wFolder}/${T1wNImage}1_gdc + ${RUN} ${FSLDIR}/bin/imcp ${T1wFolder}/${T1wNImage}1_gdc ${T1wFolder}/${T1wNImage} + + ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${T1wFolder}/${T1wNImage}" -r "${T1wTemplate}" \ + --premat="${T1wFolder}/xfms/acpc.mat" -o "${T1wFolder}/${T1wNImage}_acpc" + ${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wNImage}_acpc -mas ${T1wFolder}/${T1wImage}_acpc_brain_mask ${T1wFolder}/${T1wNImage}_acpc_brain + popd > /dev/null +fi + +if ${useReverseEpi:-false}; then + # Time is too long for Resting State, so we average it first. + mcflirt "$SpinEchoPhaseEncodeNegative" -out "$T1wFolder"/tmp_REST_mc.nii.gz + fslmaths "$T1wFolder"/tmp_REST_mc.nii.gz -Tmean "$T1wFolder"/PEForwardScout.nii.gz + SpinEchoPhaseEncodeNegative="$T1wFolder"/PEForwardScout.nii.gz + rm "$T1wFolder"/tmp_REST_mc.nii.gz +fi + +#### T2w to T1w Registration and Optional Readout Distortion Correction #### +if $useT2; then + if [[ ${AvgrdcSTRING} = "FIELDMAP" || ${AvgrdcSTRING} = "TOPUP" ]] ; then + echo "PERFORMING ${AvgrdcSTRING} READOUT DISTORTION CORRECTION" + wdir=${T2wFolder}/T2wToT1wDistortionCorrectAndReg + if [ -d ${wdir} ] ; then + # DO NOT change the following line to "rm -r ${wdir}" because the chances of something going wrong with that are much higher, and rm -r always needs to be treated with the utmost caution + rm -r ${T2wFolder}/T2wToT1wDistortionCorrectAndReg + fi + mkdir -p ${wdir} + + ${RUN} ${PipelineScripts}/T2wToT1wDistortionCorrectAndReg.sh \ + --workingdir=${wdir} \ + --t1=${T1wFolder}/${T1wImage}_acpc \ + --t1brain=${T1wFolder}/${T1wImage}_acpc_brain \ + --t2=${T2wFolder}/${T2wImage}_acpc \ + --t2brain=${T2wFolder}/${T2wImage}_acpc_brain \ + --fmapmag=${MagnitudeInputName} \ + --fmapmagbrain=${MagnitudeInputBrainName} \ + --fmapphase=${PhaseInputName} \ + --echodiff=${TE} \ + --SEPhaseNeg=${SpinEchoPhaseEncodeNegative} \ + --SEPhasePos=${SpinEchoPhaseEncodePositive} \ + --echospacing=${DwellTime} \ + --seunwarpdir=${SEUnwarpDir} \ + --t1sampspacing=${T1wSampleSpacing} \ + --t2sampspacing=${T2wSampleSpacing} \ + --unwarpdir=${UnwarpDir} \ + --ot1=${T1wFolder}/${T1wImage}_acpc_dc \ + --ot1brain=${T1wFolder}/${T1wImage}_acpc_dc_brain \ + --ot1warp=${T1wFolder}/xfms/${T1wImage}_dc \ + --ot2=${T1wFolder}/${T2wImage}_acpc_dc \ + --ot2warp=${T1wFolder}/xfms/${T2wImage}_reg_dc \ + --method=${AvgrdcSTRING} \ + --topupconfig=${TopupConfig} \ + --gdcoeffs=${GradientDistortionCoeffs} + else + wdir=${T2wFolder}/T2wToT1wReg + if [ -e ${wdir} ] ; then + # DO NOT change the following line to "rm -r ${wdir}" because the chances of something going wrong with that are much higher, and rm -r always needs to be treated with the utmost caution + rm -r ${T2wFolder}/T2wToT1wReg + fi + mkdir -p ${wdir} + ${RUN} ${PipelineScripts}/T2wToT1wReg.sh \ + ${wdir} \ + ${T1wFolder}/${T1wImage}_acpc \ + ${T1wFolder}/${T1wImage}_acpc_brain \ + ${T2wFolder}/${T2wImage}_acpc \ + ${T2wFolder}/${T2wImage}_acpc_brain \ + ${T1wFolder}/${T1wImage}_acpc_dc \ + ${T1wFolder}/${T1wImage}_acpc_dc_brain \ + ${T1wFolder}/xfms/${T1wImage}_dc \ + ${T1wFolder}/${T2wImage}_acpc_dc \ + ${T1wFolder}/xfms/${T2wImage}_reg_dc + fi +else + imcp ${T1wFolder}/${T1wImage}_acpc ${T1wFolder}/${T1wImage}_acpc_dc + imcp ${T1wFolder}/${T1wImage}_acpc_brain ${T1wFolder}/${T1wImage}_acpc_dc_brain +fi + +#### Bias Field Correction: Calculate bias field using square root of the product of T1w and T2w iamges. #### +if $useT2; then + if [ ! -z ${BiasFieldSmoothingSigma} ] ; then + BiasFieldSmoothingSigma="--bfsigma=${BiasFieldSmoothingSigma}" + fi + mkdir -p ${T1wFolder}/BiasFieldCorrection_sqrtT1wXT1w + ${RUN} ${PipelineScripts}/BiasFieldCorrection_sqrtT1wXT1w.sh \ + --workingdir=${T1wFolder}/BiasFieldCorrection_sqrtT1wXT1w \ + --T1im=${T1wFolder}/${T1wImage}_acpc_dc \ + --T1brain=${T1wFolder}/${T1wImage}_acpc_dc_brain \ + --T2im=${T1wFolder}/${T2wImage}_acpc_dc \ + --obias=${T1wFolder}/BiasField_acpc_dc \ + --oT1im=${T1wFolder}/${T1wImage}_acpc_dc_restore \ + --oT1brain=${T1wFolder}/${T1wImage}_acpc_dc_restore_brain \ + --oT2im=${T1wFolder}/${T2wImage}_acpc_dc_restore \ + --oT2brain=${T1wFolder}/${T2wImage}_acpc_dc_restore_brain \ + ${BiasFieldSmoothingSigma} +else + imcp ${T1wFolder}/${T1wImage}_acpc_dc ${T1wFolder}/${T1wImage}_acpc_dc_restore + imcp ${T1wFolder}/${T1wImage}_acpc_dc_brain ${T1wFolder}/${T1wImage}_acpc_dc_restore_brain +fi + +#### Atlas Registration to MNI152: FLIRT + FNIRT #Also applies registration to T1w and T2w images #### +#Consider combining all transforms and recreating files with single resampling steps +${RUN} ${PipelineScripts}/AtlasRegistrationToMNI152_FLIRTandFNIRT.sh \ + --workingdir=${AtlasSpaceFolder} \ + --t1=${T1wFolder}/${T1wImage}_acpc_dc \ + --t1rest=${T1wFolder}/${T1wImage}_acpc_dc_restore \ + --t1restbrain=${T1wFolder}/${T1wImage}_acpc_dc_restore_brain \ + --t2=${T1wFolder}/${T2wImage}_acpc_dc \ + --t2rest=${T1wFolder}/${T2wImage}_acpc_dc_restore \ + --t2restbrain=${T1wFolder}/${T2wImage}_acpc_dc_restore_brain \ + --ref=${T1wTemplate} \ + --refbrain=${T1wTemplateBrain} \ + --refmask=${TemplateMask} \ + --ref2mm=${T1wTemplate2mm} \ + --ref2mmmask=${Template2mmMask} \ + --owarp=${AtlasSpaceFolder}/xfms/acpc_dc2standard.nii.gz \ + --oinvwarp=${AtlasSpaceFolder}/xfms/standard2acpc_dc.nii.gz \ + --ot1=${AtlasSpaceFolder}/${T1wImage} \ + --ot1rest=${AtlasSpaceFolder}/${T1wImage}_restore \ + --ot1restbrain=${AtlasSpaceFolder}/${T1wImage}_restore_brain \ + --ot2=${AtlasSpaceFolder}/${T2wImage} \ + --ot2rest=${AtlasSpaceFolder}/${T2wImage}_restore \ + --ot2restbrain=${AtlasSpaceFolder}/${T2wImage}_restore_brain \ + --fnirtconfig=${FNIRTConfig} \ + --useT2=${useT2} + +MultiTemplateT1wBrain=T1w_brain.nii.gz +MultiTemplateSeg=Segmentation.nii.gz +Council=($(ls "$MultiTemplateDir")) # we have to make sure only subdirectories are inside... +cmd="${HCPPIPEDIR_PreFS}/run_JLF.sh --working-dir=${T1wFolder}/TemplateLabelFusion2 \ + --target=$T1wFolder/${T1wImage}_acpc_dc_restore_brain.nii.gz \ + --refdir=${MultiTemplateDir} --output=${T1wFolder}/aseg_acpc.nii.gz --ncores=${OMP_NUM_THREADS:-1}" +for ((i=0; i<${#Council[@]}; i++)); do + cmd=${cmd}" -g ${Council[$i]}/$MultiTemplateT1wBrain -l ${Council[$i]}/$MultiTemplateSeg" +done +echo $cmd +$cmd + + +#### Next stage: FreeSurfer/FreeSurferPipeline.sh +echo "-------------------------------" +echo "END OF PRE-FREESURFER.sh SCRIPT" +echo "Please Verify Clean Error File" diff --git a/fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh b/fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh new file mode 100755 index 0000000..32e506d --- /dev/null +++ b/fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh @@ -0,0 +1,399 @@ +#!/bin/bash +#set -e + +export PATH=`echo $PATH | sed 's|freesurfer/|freesurfer53/|g'` +export OMP_NUM_THREADS=1 + +# Requirements for this script +# installed versions of: FSL5.0.2 or higher , FreeSurfer (version 5 or higher) , gradunwarp (python code from MGH) +# environment: use SetUpHCPPipeline.sh (or individually set FSLDIR, FREESURFER_HOME, HCPPIPEDIR, PATH - for gradient_unwarp.py) + +# make pipeline engine happy... +if [ $# -eq 1 ] +then + echo "Version unknown..." + exit 0 +fi + +########################################## PIPELINE OVERVIEW ########################################## + +# TODO + +########################################## OUTPUT DIRECTORIES ########################################## + +# TODO + +################################################ SUPPORT FUNCTIONS ################################################## + +# function for parsing options +getopt1() { + sopt="$1" + shift 1 + for fn in $@ ; do + if [ `echo $fn | grep -- "^${sopt}=" | wc -w` -gt 0 ] ; then + echo $fn | sed "s/^${sopt}=//" + return 0 + fi + done +} + +defaultopt() { + echo $1 +} + +################################################## OPTION PARSING ##################################################### +#set echo + +# Just give usage if no arguments specified +if [ $# -eq 0 ] ; then Usage; exit 0; fi + +# parse arguments +Path=`getopt1 "--path" $@` # "$1" +Subject=`getopt1 "--subject" $@` # "$2" +NameOffMRI=`getopt1 "--fmriname" $@` # "$6" +fMRITimeSeries=`getopt1 "--fmritcs" $@` # "$3" +fMRIScout=`getopt1 "--fmriscout" $@` # "$4" +SpinEchoPhaseEncodeNegative=`getopt1 "--SEPhaseNeg" $@` # "$7" +SpinEchoPhaseEncodePositive=`getopt1 "--SEPhasePos" $@` # "$5" +MagnitudeInputName=`getopt1 "--fmapmag" $@` # "$8" #Expects 4D volume with two 3D timepoints +MagnitudeInputBrainName=`getopt1 "--fmapmagbrain" $@` # If you've already masked the magnitude. +PhaseInputName=`getopt1 "--fmapphase" $@` # "$9" +DwellTime=`getopt1 "--echospacing" $@` # "${11}" +deltaTE=`getopt1 "--echodiff" $@` # "${12}" +UnwarpDir=`getopt1 "--unwarpdir" $@` # "${13}" +FinalfMRIResolution=`getopt1 "--fmrires" $@` # "${14}" +DistortionCorrection=`getopt1 "--dcmethod" $@` # "${17}" #FIELDMAP or TOPUP +GradientDistortionCoeffs=`getopt1 "--gdcoeffs" $@` # "${18}" +TopupConfig=`getopt1 "--topupconfig" $@` # "${20}" #NONE if Topup is not being used +ContrastEnhanced=`getopt1 "--ce" $@` +RUN=`getopt1 "--printcom" $@` # use ="echo" for just printing everything and not running the commands (default is to run) +useT2=`getopt1 "--useT2" $@` +useRevEpi=`getopt1 "--userevepi" $@` # true/false uses the scout brain and reverse se/reverse epi instead of a spin echo pair. +PreviousTask=`getopt1 "--previousregistration" $@` +if [ ! -z $PreviousTask ]; then + if [[ $PreviousTask =~ task-.* ]]; then + PreviousRegistration=true + else + echo "previousregistration $PreviousTask must start with a 'task-'" + fi +else + PreviousRegistration=false +fi + +set -ex +# Setup PATHS +PipelineScripts=${HCPPIPEDIR_fMRIVol} +GlobalScripts=${HCPPIPEDIR_Global} +GlobalBinaries=${HCPPIPEDIR_Bin} + +#Naming Conventions +T1wImage="T1w_acpc_dc" +T1wRestoreImage="T1w_acpc_dc_restore" +T1wRestoreImageBrain="T1w_acpc_dc_restore_brain" +T1wFolder="T1w" #Location of T1w images +AtlasSpaceFolder="MNINonLinear" +ResultsFolder="Results" +BiasField="BiasField_acpc_dc" +BiasFieldMNI="BiasField" +T1wAtlasName="T1w_restore" +MovementRegressor="Movement_Regressors" #No extension, .txt appended +MotionMatrixFolder="MotionMatrices" +MotionMatrixPrefix="MAT_" +FieldMapOutputName="FieldMap" +MagnitudeOutputName="Magnitude" +MagnitudeBrainOutputName="Magnitude_brain" +ScoutName="Scout" +OrigScoutName="${ScoutName}_orig" +OrigTCSName="${NameOffMRI}_orig" +FreeSurferBrainMask="brainmask_fs" +fMRI2strOutputTransform="${NameOffMRI}2str" +RegOutput="Scout2T1w" +AtlasTransform="acpc_dc2standard" +OutputfMRI2StandardTransform="${NameOffMRI}2standard" +Standard2OutputfMRITransform="standard2${NameOffMRI}" +QAImage="T1wMulEPI" +JacobianOut="Jacobian" +SubjectFolder="$Path" +########################################## DO WORK ########################################## +T1wFolder="$Path"/"$T1wFolder" +AtlasSpaceFolder="$Path"/"$AtlasSpaceFolder" +ResultsFolder="$AtlasSpaceFolder"/"$ResultsFolder"/"$NameOffMRI" + +fMRIFolder="$Path"/"$NameOffMRI" +echo +if [ ! -e "$fMRIFolder" ] ; then + mkdir "$fMRIFolder" +fi +cp "$fMRITimeSeries" "$fMRIFolder"/"$OrigTCSName".nii.gz + +#Create fake "Scout" if it doesn't exist +if [ $fMRIScout = "NONE" ] ; then + ${RUN} ${FSLDIR}/bin/fslroi "$fMRIFolder"/"$OrigTCSName" "$fMRIFolder"/"$OrigScoutName" 0 1 + FakeScout="True" +else + cp "$fMRIScout" "$fMRIFolder"/"$OrigScoutName".nii.gz +fi + +#Gradient Distortion Correction of fMRI +if [ ! $GradientDistortionCoeffs = "NONE" ] ; then + mkdir -p "$fMRIFolder"/GradientDistortionUnwarp + ${RUN} "$GlobalScripts"/GradientDistortionUnwarp.sh \ + --workingdir="$fMRIFolder"/GradientDistortionUnwarp \ + --coeffs="$GradientDistortionCoeffs" \ + --in="$fMRIFolder"/"$OrigTCSName" \ + --out="$fMRIFolder"/"$NameOffMRI"_gdc \ + --owarp="$fMRIFolder"/"$NameOffMRI"_gdc_warp + + mkdir -p "$fMRIFolder"/"$ScoutName"_GradientDistortionUnwarp + ${RUN} "$GlobalScripts"/GradientDistortionUnwarp.sh \ + --workingdir="$fMRIFolder"/"$ScoutName"_GradientDistortionUnwarp \ + --coeffs="$GradientDistortionCoeffs" \ + --in="$fMRIFolder"/"$OrigScoutName" \ + --out="$fMRIFolder"/"$ScoutName"_gdc \ + --owarp="$fMRIFolder"/"$ScoutName"_gdc_warp +else + echo "NOT PERFORMING GRADIENT DISTORTION CORRECTION" + ${RUN} ${FSLDIR}/bin/imcp "$fMRIFolder"/"$OrigTCSName" "$fMRIFolder"/"$NameOffMRI"_gdc + ${RUN} ${FSLDIR}/bin/fslroi "$fMRIFolder"/"$NameOffMRI"_gdc "$fMRIFolder"/"$NameOffMRI"_gdc_warp 0 3 + ${RUN} ${FSLDIR}/bin/fslmaths "$fMRIFolder"/"$NameOffMRI"_gdc_warp -mul 0 "$fMRIFolder"/"$NameOffMRI"_gdc_warp + ${RUN} ${FSLDIR}/bin/imcp "$fMRIFolder"/"$OrigScoutName" "$fMRIFolder"/"$ScoutName"_gdc +fi + + +echo "RUNNING MOTIONCORRECTION_FLIRTBASED" +mkdir -p "$fMRIFolder"/MotionCorrection_MCFLIRTbased +### ERIC'S DEBUGGING ECHO ### +${RUN} "$PipelineScripts"/MotionCorrection.sh \ + "$fMRIFolder"/MotionCorrection_MCFLIRTbased \ + "$fMRIFolder"/"$NameOffMRI"_gdc \ + "$fMRIFolder"/"$ScoutName"_gdc \ + "$fMRIFolder"/"$NameOffMRI"_mc \ + "$fMRIFolder"/"$MovementRegressor" \ + "$fMRIFolder"/"$MotionMatrixFolder" \ + "$MotionMatrixPrefix" \ + "MCFLIRT" + +if [ ${FakeScout} = "True" ] ; then + fslmaths "$fMRIFolder"/"$NameOffMRI"_mc -Tmean "$fMRIFolder"/"$ScoutName"_gdc + invwarp -r "$fMRIFolder"/"$NameOffMRI"_gdc_warp -w "$fMRIFolder"/"$NameOffMRI"_gdc_warp -o "$fMRIFolder"/"$NameOffMRI"_gdc_invwarp + applywarp --interp=spline -i "$fMRIFolder"/"$ScoutName"_gdc -r "$fMRIFolder"/"$ScoutName"_gdc -w "$fMRIFolder"/"$NameOffMRI"_gdc_invwarp -o "$fMRIFolder"/"$OrigScoutName" +fi + + +#EPI Distortion Correction and EPI to T1w Registration +if [ -e ${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased ] ; then + rm -r ${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased +fi +mkdir -p ${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased + +if [ $DistortionCorrection = FIELDMAP ]; then +### ERIC'S DEBUGGING ECHO ### +# INSERTING MANUAL MASK IF IT IS FOUND IN PATH... +if [ -e ${fMRIFolder}/../masks/${Subject}_${NameOffMRI}_mask.nii.gz ]; then + echo "using manual mask for this subject..." + InputMaskImage=${fMRIFolder}/../masks/${Subject}_${NameOffMRI}_mask.nii.gz +fi + + +${RUN} ${PipelineScripts}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased.sh \ + --workingdir=${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased \ + --scoutin=${fMRIFolder}/${ScoutName}_gdc \ + --t1=${T1wFolder}/${T1wImage} \ + --t1restore=${T1wFolder}/${T1wRestoreImage} \ + --t1brain=${T1wFolder}/${T1wRestoreImageBrain} \ + --fmapmag=${MagnitudeInputName} \ + --fmapphase=${PhaseInputName} \ + --echodiff=${deltaTE} \ + --SEPhaseNeg=${SpinEchoPhaseEncodeNegative} \ + --SEPhasePos=${SpinEchoPhaseEncodePositive} \ + --echospacing=${DwellTime} \ + --unwarpdir=${UnwarpDir} \ + --owarp=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ + --biasfield=${T1wFolder}/${BiasField} \ + --oregim=${fMRIFolder}/${RegOutput} \ + --freesurferfolder=${T1wFolder} \ + --freesurfersubjectid=${Subject} \ + --gdcoeffs=${GradientDistortionCoeffs} \ + --qaimage=${fMRIFolder}/${QAImage} \ + --method=${DistortionCorrection} \ + --topupconfig=${TopupConfig} \ + --ojacobian=${fMRIFolder}/${JacobianOut} \ + --ce=${ContrastEnhanced} \ + --inputmask=$InputMaskImage + + +elif [ $DistortionCorrection = TOPUP ]; then + mkdir ${fMRIFolder}/FieldMap 2> /dev/null + if ! ${PreviousRegistration:-false}; then + if ${useRevEpi:-false}; then + SpinEchoPhaseEncodePositive=${fMRIFolder}/${ScoutName}_gdc + fi + echo ${HCPPIPEDIR_Global}/TopupPreprocessingAll.sh \ + --workingdir=${fMRIFolder}/FieldMap \ + --phaseone=${SpinEchoPhaseEncodePositive} \ + --phasetwo=${SpinEchoPhaseEncodeNegative} \ + --scoutin=${fMRIFolder}/${ScoutName}_gdc \ + --echospacing=${DwellTime} \ + --unwarpdir=${SEUnwarpDir} \ + --ofmapmag=${fMRIFolder}/Magnitude \ + --ofmapmagbrain=${fMRIFolder}/Magnitude_brain \ + --ofmap=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ + --ojacobian=${fMRIFolder}/${JacobianOut} \ + --gdcoeffs=${GradientDistortionCoeffs} \ + --topupconfig=${TopupConfig} + ${HCPPIPEDIR_Global}/TopupPreprocessingAll.sh \ + --workingdir=${fMRIFolder}/FieldMap \ + --phaseone=${SpinEchoPhaseEncodeNegative} \ + --phasetwo=${SpinEchoPhaseEncodePositive} \ + --scoutin=${fMRIFolder}/${ScoutName}_gdc \ + --echospacing=${DwellTime} \ + --unwarpdir=${UnwarpDir} \ + --ofmapmag=${fMRIFolder}/Magnitude \ + --ofmapmagbrain=${fMRIFolder}/Magnitude_brain \ + --owarp=${fMRIFolder}/WarpField \ + --ojacobian=${fMRIFolder}/${JacobianOut} \ + --gdcoeffs=${GradientDistortionCoeffs} \ + --topupconfig=${TopupConfig} + + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${fMRIFolder}/${ScoutName}_gdc -w ${fMRIFolder}/WarpField.nii.gz -o ${fMRIFolder}/${ScoutName}_gdc_undistorted + # apply Jacobian correction to scout image (optional) + ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mul ${fMRIFolder}/FieldMap/Jacobian ${fMRIFolder}/${ScoutName}_gdc_undistorted + # register undistorted scout image to T1w + ${FSLDIR}/bin/flirt -interp spline -dof 6 -in ${fMRIFolder}/${ScoutName}_gdc_undistorted -ref ${T1wFolder}/${T1wRestoreImageBrain} -omat "$fMRIFolder"/Scout2T1w.mat -out ${fMRIFolder}/Scout2T1w.nii.gz -searchrx -30 30 -searchry -30 30 -searchrz -30 30 -cost mutualinfo + ${FSLDIR}/bin/convert_xfm -omat "$fMRIFolder"/T1w2Scout.mat -inverse "$fMRIFolder"/Scout2T1w.mat + ${FSLDIR}/bin/applywarp --interp=nn -i ${T1wFolder}/${T1wRestoreImageBrain} -r ${fMRIFolder}/${ScoutName}_gdc_undistorted --premat="$fMRIFolder"/T1w2Scout.mat -o ${fMRIFolder}/Scout_brain_mask.nii.gz + ${FSLDIR}/bin/fslmaths ${fMRIFolder}/Scout_brain_mask.nii.gz -bin ${fMRIFolder}/Scout_brain_mask.nii.gz + ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mas ${fMRIFolder}/Scout_brain_mask.nii.gz ${fMRIFolder}/Scout_brain_dc.nii.gz + ${FSLDIR}/bin/epi_reg --epi=${fMRIFolder}/Scout_brain_dc.nii.gz --pedir=${UnwarpDir} --t1=${T1wFolder}/${T1wImage} --t1brain=${T1wFolder}/${T1wRestoreImageBrain} --out=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init + # generate combined warpfields and spline interpolated images + apply bias field correction + ${FSLDIR}/bin/convertwarp --relout --rel -r ${T1wFolder}/${T1wImage} --warp1=${fMRIFolder}/WarpField --postmat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp + + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/Jacobian.nii.gz -r ${T1wFolder}/${T1wImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${T1wFolder}/${T1wImage} -w ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init + # apply Jacobian correction to scout image (optional) + ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init -div ${T1wFolder}/${BiasField} -mul ${fMRIFolder}/Jacobian2T1w.nii.gz ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz + SUBJECTS_DIR=${T1wFolder} + + + + #echo ${FREESURFER_HOME}/bin/bbregister --s ${Subject} --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --surf white --init-reg ${T1wFolder}/mri/transforms/eye.dat --bold --reg ${fMRIFolder}/EPItoT1w.dat --o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w.nii.gz + #${FREESURFER_HOME}/bin/bbregister --s ${Subject} --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --surf white --init-reg ${T1wFolder}/mri/transforms/eye.dat --bold --reg ${fMRIFolder}/EPItoT1w.dat --o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w.nii.gz + # Create FSL-style matrix and then combine with existing warp fields + + + + + #echo ${FREESURFER_HOME}/bin/tkregister2 --noedit --reg ${fMRIFolder}/EPItoT1w.dat --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --targ ${T1wFolder}/${T1wRestoreImageBrain}.nii.gz --fslregout ${fMRIFolder}/fMRI2str.mat + #${FREESURFER_HOME}/bin/tkregister2 --noedit --reg ${fMRIFolder}/EPItoT1w.dat --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --targ ${T1wFolder}/${T1wRestoreImageBrain}.nii.gz --fslregout ${fMRIFolder}/fMRI2str.mat + + #${FSLDIR}/bin/convertwarp --relout --rel --warp1=${fMRIFolder}/${ScoutName}_undistorted2T1w_init_warp.nii.gz --ref=${T1wFolder}/${T1wImage} --postmat=${fMRIFolder}/fMRI2str.mat --out=${T1wFolder}/xfms/${fMRI2strOutputTransform} + + #@TODO re-evaluate: this xfm may be lackluster compared to a proper bbr. We could try the resolved bbr from WashU's pipe. + cp ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp.nii.gz ${T1wFolder}/xfms/${fMRI2strOutputTransform}.nii.gz + imcp ${fMRIFolder}/Jacobian2T1w.nii.gz ${fMRIFolder}/$JacobianOut # this is the proper "JacobianOut" for input into OneStepResampling. + + elif ${PreviousRegistration}; then + # take bold results, as they tend to be more accurate transforms until we can improve them. Combine rigid ferumox -> bold + echo "using ${PreviousTask} to calculate ${NameOffMRI} registration to anatomical" + PrevTaskFolder="$Path"/${PreviousTask} + Lin2PrevTask="${fMRIFolder}"/${PreviousTask}_2_${NameOffMRI}.mat + flirt -in "${fMRIFolder}"/Scout_orig.nii.gz -cost mutualinfo -dof 6 -ref "${PrevTaskFolder}"/Scout_orig.nii.gz -omat ${Lin2PrevTask} + PrevTaskTransform=${PreviousTask}2str + imcp "$PrevTaskFolder"/"$JacobianOut" "$fMRIFolder"/"$JacobianOut" + convertwarp --rel --relout --out=${T1wFolder}/xfms/${fMRI2strOutputTransform}.nii.gz --warp1=${T1wFolder}/xfms/${PrevTaskTransform}.nii.gz --premat=${Lin2PrevTask} --ref=${T1wFolder}/${T1wImage} + fi + +else + # fake jacobian out + # DFM is still being applied from CYA, will put DFM procedure here later. + fslmaths ${T1wFolder}/${T1wImage} -abs -add 1 -bin ${fMRIFolder}/${JacobianOut} +fi + + +echo "RUNNING ONE STEP RESAMPLING" +#One Step Resampling +mkdir -p ${fMRIFolder}/OneStepResampling +echo ${RUN} ${PipelineScripts}/OneStepResampling.sh \ + --workingdir=${fMRIFolder}/OneStepResampling \ + --infmri=${fMRIFolder}/${OrigTCSName}.nii.gz \ + --t1=${AtlasSpaceFolder}/${T1wAtlasName} \ + --fmriresout=${FinalfMRIResolution} \ + --fmrifolder=${fMRIFolder} \ + --fmri2structin=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ + --struct2std=${AtlasSpaceFolder}/xfms/${AtlasTransform} \ + --owarp=${AtlasSpaceFolder}/xfms/${OutputfMRI2StandardTransform} \ + --oiwarp=${AtlasSpaceFolder}/xfms/${Standard2OutputfMRITransform} \ + --motionmatdir=${fMRIFolder}/${MotionMatrixFolder} \ + --motionmatprefix=${MotionMatrixPrefix} \ + --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin \ + --freesurferbrainmask=${AtlasSpaceFolder}/${FreeSurferBrainMask} \ + --biasfield=${AtlasSpaceFolder}/${BiasFieldMNI} \ + --gdfield=${fMRIFolder}/${NameOffMRI}_gdc_warp \ + --scoutin=${fMRIFolder}/${OrigScoutName} \ + --scoutgdcin=${fMRIFolder}/${ScoutName}_gdc \ + --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ + --jacobianin=${fMRIFolder}/${JacobianOut} \ + --ojacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} + +${RUN} ${PipelineScripts}/OneStepResampling.sh \ + --workingdir=${fMRIFolder}/OneStepResampling \ + --infmri=${fMRIFolder}/${OrigTCSName}.nii.gz \ + --t1=${AtlasSpaceFolder}/${T1wAtlasName} \ + --fmriresout=${FinalfMRIResolution} \ + --fmrifolder=${fMRIFolder} \ + --fmri2structin=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ + --struct2std=${AtlasSpaceFolder}/xfms/${AtlasTransform} \ + --owarp=${AtlasSpaceFolder}/xfms/${OutputfMRI2StandardTransform} \ + --oiwarp=${AtlasSpaceFolder}/xfms/${Standard2OutputfMRITransform} \ + --motionmatdir=${fMRIFolder}/${MotionMatrixFolder} \ + --motionmatprefix=${MotionMatrixPrefix} \ + --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin \ + --freesurferbrainmask=${AtlasSpaceFolder}/${FreeSurferBrainMask} \ + --biasfield=${AtlasSpaceFolder}/${BiasFieldMNI} \ + --gdfield=${fMRIFolder}/${NameOffMRI}_gdc_warp \ + --scoutin=${fMRIFolder}/${OrigScoutName} \ + --scoutgdcin=${fMRIFolder}/${ScoutName}_gdc \ + --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ + --jacobianin=${fMRIFolder}/${JacobianOut} \ + --ojacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} + + +echo "RUNNING INTENSITY NORMALIZATION & BIAS REMOVAL" +#Intensity Normalization and Bias Removal +### ERIC'S DEBUGGING ECHO ### +echo ${RUN} ${PipelineScripts}/IntensityNormalization.sh \ + --infmri=${fMRIFolder}/${NameOffMRI}_nonlin \ + --biasfield=${fMRIFolder}/${BiasFieldMNI}.${FinalfMRIResolution} \ + --jacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} \ + --brainmask=${fMRIFolder}/${FreeSurferBrainMask}.${FinalfMRIResolution} \ + --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin_norm \ + --inscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ + --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm \ + --usejacobian=false +${RUN} ${PipelineScripts}/IntensityNormalization.sh \ + --infmri=${fMRIFolder}/${NameOffMRI}_nonlin \ + --biasfield=${fMRIFolder}/${BiasFieldMNI}.${FinalfMRIResolution} \ + --jacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} \ + --brainmask=${fMRIFolder}/${FreeSurferBrainMask}.${FinalfMRIResolution} \ + --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin_norm \ + --inscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ + --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm \ + --usejacobian=false + +mkdir -p ${ResultsFolder} +# MJ QUERY: WHY THE -r OPTIONS BELOW? +${RUN} cp -r ${fMRIFolder}/${NameOffMRI}_nonlin_norm.nii.gz ${ResultsFolder}/${NameOffMRI}.nii.gz +${RUN} cp -r ${fMRIFolder}/${MovementRegressor}.txt ${ResultsFolder}/${MovementRegressor}.txt +${RUN} cp -r ${fMRIFolder}/${MovementRegressor}_dt.txt ${ResultsFolder}/${MovementRegressor}_dt.txt +${RUN} cp -r ${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm.nii.gz ${ResultsFolder}/${NameOffMRI}_SBRef.nii.gz +${RUN} cp -r ${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution}.nii.gz ${ResultsFolder}/${NameOffMRI}_${JacobianOut}.nii.gz +###Add stuff for RMS### +${RUN} cp -r ${fMRIFolder}/Movement_RelativeRMS.txt ${ResultsFolder}/Movement_RelativeRMS.txt +${RUN} cp -r ${fMRIFolder}/Movement_AbsoluteRMS.txt ${ResultsFolder}/Movement_AbsoluteRMS.txt +${RUN} cp -r ${fMRIFolder}/Movement_RelativeRMS_mean.txt ${ResultsFolder}/Movement_RelativeRMS_mean.txt +${RUN} cp -r ${fMRIFolder}/Movement_AbsoluteRMS_mean.txt ${ResultsFolder}/Movement_AbsoluteRMS_mean.txt +###Add stuff for RMS### + +echo "-------------------------------" +echo "END OF fMRI-VOLUME-PROCESSING.sh SCRIPT" +echo "Please Verify Clean Error File" From b74b1688fc4c9d9ca00b13722d8a4a55643d43e6 Mon Sep 17 00:00:00 2001 From: Bene Ramirez Date: Wed, 26 Feb 2020 14:35:49 -0800 Subject: [PATCH 2/6] Changed Prep Average issue and Vol reg to T2 --- PreFreeSurfer/PreFreeSurferPipeline.sh | 19 +++++-- .../GenericfMRIVolumeProcessingPipeline.sh | 54 +++++++++++++++---- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/PreFreeSurfer/PreFreeSurferPipeline.sh b/PreFreeSurfer/PreFreeSurferPipeline.sh index efe115d..d444a6d 100755 --- a/PreFreeSurfer/PreFreeSurferPipeline.sh +++ b/PreFreeSurfer/PreFreeSurferPipeline.sh @@ -244,6 +244,12 @@ for TXw in ${Modalities} ; do ${RUN} ${PipelineScripts}/AnatomicalAverage.sh -o ${TXwFolder}/${TXwImage} -s ${TXwTemplate} -m ${TemplateMask} \ -n -w ${TXwFolder}/Average${TXw}Images --noclean -v -b $BrainSize $OutputTXwImageSTRING #fi + ###Added by Bene to use created warp above and apply it to the brain + ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_brain -bin ${TXwFolder}/${TXwImage}_mask + ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_brain -bin ${TXwFolder}/${TXwImage}_mask_rot2first #take this out once done testing just something for QC for now + $FSLDIR/bin/applywarp --rel -i ${TXwFolder}/${TXwImage}_mask --premat=${TXwFolder}/Average${TXw}Images/ToHalfTrans0001.mat -r ${TXwFolder}/${TXwImage} -o ${TXwFolder}/${TXwImage}_mask --interp=nn + ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage} -mas ${TXwFolder}/${TXwImage}_mask ${TXwFolder}/${TXwImage}_brain + ### Done with edits by Bene else echo "ONLY ONE AVERAGE FOUND: COPYING" ${RUN} ${FSLDIR}/bin/imcp ${TXwFolder}/${TXwImage}1_gdc ${TXwFolder}/${TXwImage} @@ -287,13 +293,16 @@ for TXw in ${Modalities} ; do # --fnirtconfig=${FNIRTConfig}; # ${FSLDIR}/bin/flirt -in ${TXwFolder}/${TXwImage}_brain -ref ${TXwTemplate} -applyxfm -init ${TXwFolder}/xfms/acpc.mat -out ${TXwFolder}/${TXwImage}_acpc_brain # ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask -#** if [ $TXw = T1wN ]; then ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc -mas ${T1wFolder}/${T1wImage}_acpc_brain_mask ${TXwFolder}/${TXwImage}_acpc_brain; fi - - #apply acpc.mat to head +#** if [ $TXw = T1wN ]; then ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc -mas ${T1wFolder}/${T1wImage}_acpc_brain_mask ${TXwFolder}/${TXwImage}_acpc_brain; fi +#apply acpc.mat to head ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${TXwFolder}/${TXwImage}" -r "${TXwTemplate}" \ --premat="${TXwFolder}/xfms/acpc.mat" -o "${TXwFolder}/${TXwImage}_acpc" - #make brain mask - ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask + + #Bene alternative fix added: apply warp to mask, then make brain mask and use it to mask T1w acpc + ${FSLDIR}/bin/applywarp --rel --interp=nn -i ${TXwFolder}/${TXwImage}_mask -r ${TXwTemplateBrain} --premat=${TXwFolder}/xfms/acpc.mat -o ${TXwFolder}/${TXwImage}_acpc_brain_mask + ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc -mas ${TXwFolder}/${TXwImage}_acpc_brain_mask ${TXwFolder}/${TXwImage}_acpc_brain + #make brain mask taken out by Bene and replaced with above + #${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask done ######## END LOOP over T1w and T2w ######### diff --git a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh index 32e506d..b80b007 100755 --- a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh +++ b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh @@ -255,26 +255,62 @@ elif [ $DistortionCorrection = TOPUP ]; then --gdcoeffs=${GradientDistortionCoeffs} \ --topupconfig=${TopupConfig} +########################################################################################################################################################################### +##Bene Changes: Use T2 instead of T1 +#if useT2=none fake T2 and make T2=T2wRestoreImage and apply T1 mask to make T2wRestoreImageBrain +#TODO +#Here is how I have been faking the T2 and it seems to be working better than using the T1 brain because of the high intensity eyes. +#${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wRestoreImage} -mul -1 ${T1wFolder}/FakeT2Head.nii.gz +#${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -add 500 ${T1wFolder}/FakeT2Head.nii.gz +#${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -uthrp 95 ${T1wFolder}/FakeT2Head.nii.gz +#${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -mas ${T1wFolder}/T1w_acpc_brain_mask.nii.gz ${T1wFolder}/FakeT2brain.nii.gz +#TODO define T2 images (need to make this better but hard code for now) +echo "this has only been testin with T2, if we don't have one, fake it here" +#Create fake "T2" if it doesn't exist +if [ $useT2 = "True" ] ; then + T2wRestoreImage=T2w_acpc_dc_restore + T2wRestoreImageBrain=T2w_acpc_dc_restore_brain + T1wImage=T1w_acpc_dc_restore ##not sure where this comes from, so this could be a different image. +else + ${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wRestoreImage} -mul -1 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz + ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -add 500 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz + ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -uthrp 95 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz + ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -mas ${T1wFolder}/T1w_acpc_brain_mask.nii.gz ${T1wFolder}/T2w_acpc_dc_restore_brain.nii.gz + T2wRestoreImage=T2w_acpc_dc_restore + T2wRestoreImageBrain=T2w_acpc_dc_restore_brain + T1wImage=T1w_acpc_dc_restore ##not sure where this comes from, so this could be a different image. +fi + + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${fMRIFolder}/${ScoutName}_gdc -w ${fMRIFolder}/WarpField.nii.gz -o ${fMRIFolder}/${ScoutName}_gdc_undistorted # apply Jacobian correction to scout image (optional) ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mul ${fMRIFolder}/FieldMap/Jacobian ${fMRIFolder}/${ScoutName}_gdc_undistorted - # register undistorted scout image to T1w - ${FSLDIR}/bin/flirt -interp spline -dof 6 -in ${fMRIFolder}/${ScoutName}_gdc_undistorted -ref ${T1wFolder}/${T1wRestoreImageBrain} -omat "$fMRIFolder"/Scout2T1w.mat -out ${fMRIFolder}/Scout2T1w.nii.gz -searchrx -30 30 -searchry -30 30 -searchrz -30 30 -cost mutualinfo - ${FSLDIR}/bin/convert_xfm -omat "$fMRIFolder"/T1w2Scout.mat -inverse "$fMRIFolder"/Scout2T1w.mat - ${FSLDIR}/bin/applywarp --interp=nn -i ${T1wFolder}/${T1wRestoreImageBrain} -r ${fMRIFolder}/${ScoutName}_gdc_undistorted --premat="$fMRIFolder"/T1w2Scout.mat -o ${fMRIFolder}/Scout_brain_mask.nii.gz + # register undistorted scout image to T2w head + ${FSLDIR}/bin/flirt -interp spline -dof 6 -in ${fMRIFolder}/${ScoutName}_gdc_undistorted -ref ${T1wFolder}/${T2wRestoreImage} -omat "$fMRIFolder"/Scout2T2w.mat -out ${fMRIFolder}/Scout2T2w.nii.gz -searchrx -30 30 -searchry -30 30 -searchrz -30 30 -cost mutualinfo + ${FSLDIR}/bin/convert_xfm -omat "$fMRIFolder"/T2w2Scout.mat -inverse "$fMRIFolder"/Scout2T2w.mat + ${FSLDIR}/bin/applywarp --interp=nn -i ${T1wFolder}/${T2wRestoreImageBrain} -r ${fMRIFolder}/${ScoutName}_gdc_undistorted --premat="$fMRIFolder"/T2w2Scout.mat -o ${fMRIFolder}/Scout_brain_mask.nii.gz ${FSLDIR}/bin/fslmaths ${fMRIFolder}/Scout_brain_mask.nii.gz -bin ${fMRIFolder}/Scout_brain_mask.nii.gz ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mas ${fMRIFolder}/Scout_brain_mask.nii.gz ${fMRIFolder}/Scout_brain_dc.nii.gz - ${FSLDIR}/bin/epi_reg --epi=${fMRIFolder}/Scout_brain_dc.nii.gz --pedir=${UnwarpDir} --t1=${T1wFolder}/${T1wImage} --t1brain=${T1wFolder}/${T1wRestoreImageBrain} --out=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init +## Added step here to make it work better, re-registering the maked brain to the T2 brain: This is ca;;ed undistorted2T1w even though it's registered to T2. but wanted to keep naming the same for future checks. +echo " ${ScoutName}_gdc_undistorted2T1w_init.mat is technically ${ScoutName}_gdc_undistorted2T2w_init.mat " + ${FSLDIR}/bin/flirt -interp spline -dof 6 -in ${fMRIFolder}/Scout_brain_dc.nii.gz -ref ${T1wFolder}/${T2wRestoreImageBrain} -omat "$fMRIFolder"/${ScoutName}_gdc_undistorted2T1w_init.mat -out ${fMRIFolder}/${ScoutName}_gdc_undistorted2T2w_init -searchrx -30 30 -searchry -30 30 -searchrz -30 30 -cost mutualinfo +#Taking out epi_reg because it is really bad at registering especially with contrast images. +# ${FSLDIR}/bin/epi_reg -v --epi=${fMRIFolder}/Scout_brain2T1w.nii.gz --pedir=${UnwarpDir} --t1=${T1wFolder}/${T1wRestoreImage} --t1brain=${T1wFolder}/${T1wRestoreImageBrain} --out=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init + # generate combined warpfields and spline interpolated images + apply bias field correction - ${FSLDIR}/bin/convertwarp --relout --rel -r ${T1wFolder}/${T1wImage} --warp1=${fMRIFolder}/WarpField --postmat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp + ${FSLDIR}/bin/convertwarp --relout --rel -r ${T1wFolder}/${T2wRestoreImage} --warp1=${fMRIFolder}/WarpField --postmat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp + +#this gives me a blank image, because the jacobian is empty. Trying other Jacobian in FielMap folder which works. This is because Jacobian gets overwritten with this file later so if this step doesn't work its not going to work.But should work next time when the above is fixed. + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/Jacobian.nii.gz -r ${T1wFolder}/${T2wRestoreImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz + #${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/FieldMap/Jacobian.nii.gz -r ${T1wFolder}/${T2wRestoreImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz - ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/Jacobian.nii.gz -r ${T1wFolder}/${T1wImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz - ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${T1wFolder}/${T1wImage} -w ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init + +${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${T1wFolder}/${T2wRestoreImage} -w ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init # apply Jacobian correction to scout image (optional) ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init -div ${T1wFolder}/${BiasField} -mul ${fMRIFolder}/Jacobian2T1w.nii.gz ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz SUBJECTS_DIR=${T1wFolder} - +##Done with Bene's Changes ######################################################################################################################################################################### #echo ${FREESURFER_HOME}/bin/bbregister --s ${Subject} --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --surf white --init-reg ${T1wFolder}/mri/transforms/eye.dat --bold --reg ${fMRIFolder}/EPItoT1w.dat --o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w.nii.gz #${FREESURFER_HOME}/bin/bbregister --s ${Subject} --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --surf white --init-reg ${T1wFolder}/mri/transforms/eye.dat --bold --reg ${fMRIFolder}/EPItoT1w.dat --o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w.nii.gz From b81707ef02ea52a9c4350287faf08b3c38fee329 Mon Sep 17 00:00:00 2001 From: Bene Ramirez Date: Tue, 10 Mar 2020 13:10:41 -0700 Subject: [PATCH 3/6] updated Kathy and Erics comments --- .../GenericfMRIVolumeProcessingPipeline.sh | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh index b80b007..a58576e 100755 --- a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh +++ b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh @@ -257,32 +257,31 @@ elif [ $DistortionCorrection = TOPUP ]; then ########################################################################################################################################################################### ##Bene Changes: Use T2 instead of T1 -#if useT2=none fake T2 and make T2=T2wRestoreImage and apply T1 mask to make T2wRestoreImageBrain -#TODO -#Here is how I have been faking the T2 and it seems to be working better than using the T1 brain because of the high intensity eyes. -#${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wRestoreImage} -mul -1 ${T1wFolder}/FakeT2Head.nii.gz -#${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -add 500 ${T1wFolder}/FakeT2Head.nii.gz -#${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -uthrp 95 ${T1wFolder}/FakeT2Head.nii.gz -#${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -mas ${T1wFolder}/T1w_acpc_brain_mask.nii.gz ${T1wFolder}/FakeT2brain.nii.gz -#TODO define T2 images (need to make this better but hard code for now) -echo "this has only been testin with T2, if we don't have one, fake it here" -#Create fake "T2" if it doesn't exist -if [ $useT2 = "True" ] ; then - T2wRestoreImage=T2w_acpc_dc_restore - T2wRestoreImageBrain=T2w_acpc_dc_restore_brain - T1wImage=T1w_acpc_dc_restore ##not sure where this comes from, so this could be a different image. -else - ${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wRestoreImage} -mul -1 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz - ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -add 500 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz - ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -uthrp 95 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz - ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -mas ${T1wFolder}/T1w_acpc_brain_mask.nii.gz ${T1wFolder}/T2w_acpc_dc_restore_brain.nii.gz - T2wRestoreImage=T2w_acpc_dc_restore - T2wRestoreImageBrain=T2w_acpc_dc_restore_brain - T1wImage=T1w_acpc_dc_restore ##not sure where this comes from, so this could be a different image. -fi + #if useT2=none fake T2 and make T2=T2wRestoreImage and apply T1 mask to make T2wRestoreImageBrain + #TODO + #Here is how I have been faking the T2 and it seems to be working better than using the T1 brain because of the high intensity eyes. + #${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wRestoreImage} -mul -1 ${T1wFolder}/FakeT2Head.nii.gz + #${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -add 500 ${T1wFolder}/FakeT2Head.nii.gz + #${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -uthrp 95 ${T1wFolder}/FakeT2Head.nii.gz + #${FSLDIR}/bin/fslmaths ${T1wFolder}/FakeT2Head.nii.gz -mas ${T1wFolder}/T1w_acpc_brain_mask.nii.gz ${T1wFolder}/FakeT2brain.nii.gz + #TODO define T2 images (need to make this better but hard code for now) + echo "this has only been tested with T2, if we don't have one, fake it here" + #Create fake "T2" if it doesn't exist + #if [ $useT2 = "True" ] ; then + if [[ "TRUE"==${useT2^^}]] ; then + T2wRestoreImage=T2w_acpc_dc_restore + T2wRestoreImageBrain=T2w_acpc_dc_restore_brain + else + ${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wRestoreImage} -mul -1 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz + ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -add 500 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz + ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -uthrp 95 ${T1wFolder}/T2w_acpc_dc_restore.nii.gz + ${FSLDIR}/bin/fslmaths ${T1wFolder}/T2w_acpc_dc_restore.nii.gz -mas ${T1wFolder}/T1w_acpc_brain_mask.nii.gz ${T1wFolder}/T2w_acpc_dc_restore_brain.nii.gz + T2wRestoreImage=T2w_acpc_dc_restore + T2wRestoreImageBrain=T2w_acpc_dc_restore_brain + fi - ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${fMRIFolder}/${ScoutName}_gdc -w ${fMRIFolder}/WarpField.nii.gz -o ${fMRIFolder}/${ScoutName}_gdc_undistorted + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${fMRIFolder}/${ScoutName}_gdc -w ${fMRIFolder}/WarpField.nii.gz -o ${fMRIFolder}/${ScoutName}_gdc_undistorted # apply Jacobian correction to scout image (optional) ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mul ${fMRIFolder}/FieldMap/Jacobian ${fMRIFolder}/${ScoutName}_gdc_undistorted # register undistorted scout image to T2w head @@ -291,21 +290,21 @@ fi ${FSLDIR}/bin/applywarp --interp=nn -i ${T1wFolder}/${T2wRestoreImageBrain} -r ${fMRIFolder}/${ScoutName}_gdc_undistorted --premat="$fMRIFolder"/T2w2Scout.mat -o ${fMRIFolder}/Scout_brain_mask.nii.gz ${FSLDIR}/bin/fslmaths ${fMRIFolder}/Scout_brain_mask.nii.gz -bin ${fMRIFolder}/Scout_brain_mask.nii.gz ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mas ${fMRIFolder}/Scout_brain_mask.nii.gz ${fMRIFolder}/Scout_brain_dc.nii.gz -## Added step here to make it work better, re-registering the maked brain to the T2 brain: This is ca;;ed undistorted2T1w even though it's registered to T2. but wanted to keep naming the same for future checks. -echo " ${ScoutName}_gdc_undistorted2T1w_init.mat is technically ${ScoutName}_gdc_undistorted2T2w_init.mat " + ## Added step here to make it work better, re-registering the maked brain to the T2 brain: This is ca;;ed undistorted2T1w even though it's registered to T2. but wanted to keep naming the same for future checks. + echo " ${ScoutName}_gdc_undistorted2T1w_init.mat is technically ${ScoutName}_gdc_undistorted2T2w_init.mat " ${FSLDIR}/bin/flirt -interp spline -dof 6 -in ${fMRIFolder}/Scout_brain_dc.nii.gz -ref ${T1wFolder}/${T2wRestoreImageBrain} -omat "$fMRIFolder"/${ScoutName}_gdc_undistorted2T1w_init.mat -out ${fMRIFolder}/${ScoutName}_gdc_undistorted2T2w_init -searchrx -30 30 -searchry -30 30 -searchrz -30 30 -cost mutualinfo -#Taking out epi_reg because it is really bad at registering especially with contrast images. -# ${FSLDIR}/bin/epi_reg -v --epi=${fMRIFolder}/Scout_brain2T1w.nii.gz --pedir=${UnwarpDir} --t1=${T1wFolder}/${T1wRestoreImage} --t1brain=${T1wFolder}/${T1wRestoreImageBrain} --out=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init + #Taking out epi_reg because it is really bad at registering especially with contrast images. + # ${FSLDIR}/bin/epi_reg -v --epi=${fMRIFolder}/Scout_brain2T1w.nii.gz --pedir=${UnwarpDir} --t1=${T1wFolder}/${T1wRestoreImage} --t1brain=${T1wFolder}/${T1wRestoreImageBrain} --out=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init # generate combined warpfields and spline interpolated images + apply bias field correction ${FSLDIR}/bin/convertwarp --relout --rel -r ${T1wFolder}/${T2wRestoreImage} --warp1=${fMRIFolder}/WarpField --postmat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -#this gives me a blank image, because the jacobian is empty. Trying other Jacobian in FielMap folder which works. This is because Jacobian gets overwritten with this file later so if this step doesn't work its not going to work.But should work next time when the above is fixed. + #this gives me a blank image, because the jacobian is empty. Trying other Jacobian in FieldMap folder which works. This is because Jacobian gets overwritten with this file later so if this step doesn't work its not going to work.But should work next time when the above is fixed. ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/Jacobian.nii.gz -r ${T1wFolder}/${T2wRestoreImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz #${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/FieldMap/Jacobian.nii.gz -r ${T1wFolder}/${T2wRestoreImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz -${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${T1wFolder}/${T2wRestoreImage} -w ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init + ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${T1wFolder}/${T2wRestoreImage} -w ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init # apply Jacobian correction to scout image (optional) ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init -div ${T1wFolder}/${BiasField} -mul ${fMRIFolder}/Jacobian2T1w.nii.gz ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz SUBJECTS_DIR=${T1wFolder} From 90e7e3f96d3d0773f6d048d0e376aa071b35f033 Mon Sep 17 00:00:00 2001 From: Bene Ramirez Date: Wed, 18 Mar 2020 10:41:04 -0700 Subject: [PATCH 4/6] fixed typo in if statement on line 271 --- fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh index a58576e..1fdbe5b 100755 --- a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh +++ b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh @@ -268,7 +268,7 @@ elif [ $DistortionCorrection = TOPUP ]; then echo "this has only been tested with T2, if we don't have one, fake it here" #Create fake "T2" if it doesn't exist #if [ $useT2 = "True" ] ; then - if [[ "TRUE"==${useT2^^}]] ; then + if [[ "TRUE"==${useT2^^} ]]; then T2wRestoreImage=T2w_acpc_dc_restore T2wRestoreImageBrain=T2w_acpc_dc_restore_brain else From ec8aead0485eb58b97eb823e8717e6e01a227fa6 Mon Sep 17 00:00:00 2001 From: Kathy Snider Date: Thu, 19 Mar 2020 11:54:04 -0700 Subject: [PATCH 5/6] Lose extra file. --- ...fMRIVolumeProcessingPipeline_orig_T1reg.sh | 399 ------------------ 1 file changed, 399 deletions(-) delete mode 100755 fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh diff --git a/fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh b/fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh deleted file mode 100755 index 32e506d..0000000 --- a/fMRIVolume/GenericfMRIVolumeProcessingPipeline_orig_T1reg.sh +++ /dev/null @@ -1,399 +0,0 @@ -#!/bin/bash -#set -e - -export PATH=`echo $PATH | sed 's|freesurfer/|freesurfer53/|g'` -export OMP_NUM_THREADS=1 - -# Requirements for this script -# installed versions of: FSL5.0.2 or higher , FreeSurfer (version 5 or higher) , gradunwarp (python code from MGH) -# environment: use SetUpHCPPipeline.sh (or individually set FSLDIR, FREESURFER_HOME, HCPPIPEDIR, PATH - for gradient_unwarp.py) - -# make pipeline engine happy... -if [ $# -eq 1 ] -then - echo "Version unknown..." - exit 0 -fi - -########################################## PIPELINE OVERVIEW ########################################## - -# TODO - -########################################## OUTPUT DIRECTORIES ########################################## - -# TODO - -################################################ SUPPORT FUNCTIONS ################################################## - -# function for parsing options -getopt1() { - sopt="$1" - shift 1 - for fn in $@ ; do - if [ `echo $fn | grep -- "^${sopt}=" | wc -w` -gt 0 ] ; then - echo $fn | sed "s/^${sopt}=//" - return 0 - fi - done -} - -defaultopt() { - echo $1 -} - -################################################## OPTION PARSING ##################################################### -#set echo - -# Just give usage if no arguments specified -if [ $# -eq 0 ] ; then Usage; exit 0; fi - -# parse arguments -Path=`getopt1 "--path" $@` # "$1" -Subject=`getopt1 "--subject" $@` # "$2" -NameOffMRI=`getopt1 "--fmriname" $@` # "$6" -fMRITimeSeries=`getopt1 "--fmritcs" $@` # "$3" -fMRIScout=`getopt1 "--fmriscout" $@` # "$4" -SpinEchoPhaseEncodeNegative=`getopt1 "--SEPhaseNeg" $@` # "$7" -SpinEchoPhaseEncodePositive=`getopt1 "--SEPhasePos" $@` # "$5" -MagnitudeInputName=`getopt1 "--fmapmag" $@` # "$8" #Expects 4D volume with two 3D timepoints -MagnitudeInputBrainName=`getopt1 "--fmapmagbrain" $@` # If you've already masked the magnitude. -PhaseInputName=`getopt1 "--fmapphase" $@` # "$9" -DwellTime=`getopt1 "--echospacing" $@` # "${11}" -deltaTE=`getopt1 "--echodiff" $@` # "${12}" -UnwarpDir=`getopt1 "--unwarpdir" $@` # "${13}" -FinalfMRIResolution=`getopt1 "--fmrires" $@` # "${14}" -DistortionCorrection=`getopt1 "--dcmethod" $@` # "${17}" #FIELDMAP or TOPUP -GradientDistortionCoeffs=`getopt1 "--gdcoeffs" $@` # "${18}" -TopupConfig=`getopt1 "--topupconfig" $@` # "${20}" #NONE if Topup is not being used -ContrastEnhanced=`getopt1 "--ce" $@` -RUN=`getopt1 "--printcom" $@` # use ="echo" for just printing everything and not running the commands (default is to run) -useT2=`getopt1 "--useT2" $@` -useRevEpi=`getopt1 "--userevepi" $@` # true/false uses the scout brain and reverse se/reverse epi instead of a spin echo pair. -PreviousTask=`getopt1 "--previousregistration" $@` -if [ ! -z $PreviousTask ]; then - if [[ $PreviousTask =~ task-.* ]]; then - PreviousRegistration=true - else - echo "previousregistration $PreviousTask must start with a 'task-'" - fi -else - PreviousRegistration=false -fi - -set -ex -# Setup PATHS -PipelineScripts=${HCPPIPEDIR_fMRIVol} -GlobalScripts=${HCPPIPEDIR_Global} -GlobalBinaries=${HCPPIPEDIR_Bin} - -#Naming Conventions -T1wImage="T1w_acpc_dc" -T1wRestoreImage="T1w_acpc_dc_restore" -T1wRestoreImageBrain="T1w_acpc_dc_restore_brain" -T1wFolder="T1w" #Location of T1w images -AtlasSpaceFolder="MNINonLinear" -ResultsFolder="Results" -BiasField="BiasField_acpc_dc" -BiasFieldMNI="BiasField" -T1wAtlasName="T1w_restore" -MovementRegressor="Movement_Regressors" #No extension, .txt appended -MotionMatrixFolder="MotionMatrices" -MotionMatrixPrefix="MAT_" -FieldMapOutputName="FieldMap" -MagnitudeOutputName="Magnitude" -MagnitudeBrainOutputName="Magnitude_brain" -ScoutName="Scout" -OrigScoutName="${ScoutName}_orig" -OrigTCSName="${NameOffMRI}_orig" -FreeSurferBrainMask="brainmask_fs" -fMRI2strOutputTransform="${NameOffMRI}2str" -RegOutput="Scout2T1w" -AtlasTransform="acpc_dc2standard" -OutputfMRI2StandardTransform="${NameOffMRI}2standard" -Standard2OutputfMRITransform="standard2${NameOffMRI}" -QAImage="T1wMulEPI" -JacobianOut="Jacobian" -SubjectFolder="$Path" -########################################## DO WORK ########################################## -T1wFolder="$Path"/"$T1wFolder" -AtlasSpaceFolder="$Path"/"$AtlasSpaceFolder" -ResultsFolder="$AtlasSpaceFolder"/"$ResultsFolder"/"$NameOffMRI" - -fMRIFolder="$Path"/"$NameOffMRI" -echo -if [ ! -e "$fMRIFolder" ] ; then - mkdir "$fMRIFolder" -fi -cp "$fMRITimeSeries" "$fMRIFolder"/"$OrigTCSName".nii.gz - -#Create fake "Scout" if it doesn't exist -if [ $fMRIScout = "NONE" ] ; then - ${RUN} ${FSLDIR}/bin/fslroi "$fMRIFolder"/"$OrigTCSName" "$fMRIFolder"/"$OrigScoutName" 0 1 - FakeScout="True" -else - cp "$fMRIScout" "$fMRIFolder"/"$OrigScoutName".nii.gz -fi - -#Gradient Distortion Correction of fMRI -if [ ! $GradientDistortionCoeffs = "NONE" ] ; then - mkdir -p "$fMRIFolder"/GradientDistortionUnwarp - ${RUN} "$GlobalScripts"/GradientDistortionUnwarp.sh \ - --workingdir="$fMRIFolder"/GradientDistortionUnwarp \ - --coeffs="$GradientDistortionCoeffs" \ - --in="$fMRIFolder"/"$OrigTCSName" \ - --out="$fMRIFolder"/"$NameOffMRI"_gdc \ - --owarp="$fMRIFolder"/"$NameOffMRI"_gdc_warp - - mkdir -p "$fMRIFolder"/"$ScoutName"_GradientDistortionUnwarp - ${RUN} "$GlobalScripts"/GradientDistortionUnwarp.sh \ - --workingdir="$fMRIFolder"/"$ScoutName"_GradientDistortionUnwarp \ - --coeffs="$GradientDistortionCoeffs" \ - --in="$fMRIFolder"/"$OrigScoutName" \ - --out="$fMRIFolder"/"$ScoutName"_gdc \ - --owarp="$fMRIFolder"/"$ScoutName"_gdc_warp -else - echo "NOT PERFORMING GRADIENT DISTORTION CORRECTION" - ${RUN} ${FSLDIR}/bin/imcp "$fMRIFolder"/"$OrigTCSName" "$fMRIFolder"/"$NameOffMRI"_gdc - ${RUN} ${FSLDIR}/bin/fslroi "$fMRIFolder"/"$NameOffMRI"_gdc "$fMRIFolder"/"$NameOffMRI"_gdc_warp 0 3 - ${RUN} ${FSLDIR}/bin/fslmaths "$fMRIFolder"/"$NameOffMRI"_gdc_warp -mul 0 "$fMRIFolder"/"$NameOffMRI"_gdc_warp - ${RUN} ${FSLDIR}/bin/imcp "$fMRIFolder"/"$OrigScoutName" "$fMRIFolder"/"$ScoutName"_gdc -fi - - -echo "RUNNING MOTIONCORRECTION_FLIRTBASED" -mkdir -p "$fMRIFolder"/MotionCorrection_MCFLIRTbased -### ERIC'S DEBUGGING ECHO ### -${RUN} "$PipelineScripts"/MotionCorrection.sh \ - "$fMRIFolder"/MotionCorrection_MCFLIRTbased \ - "$fMRIFolder"/"$NameOffMRI"_gdc \ - "$fMRIFolder"/"$ScoutName"_gdc \ - "$fMRIFolder"/"$NameOffMRI"_mc \ - "$fMRIFolder"/"$MovementRegressor" \ - "$fMRIFolder"/"$MotionMatrixFolder" \ - "$MotionMatrixPrefix" \ - "MCFLIRT" - -if [ ${FakeScout} = "True" ] ; then - fslmaths "$fMRIFolder"/"$NameOffMRI"_mc -Tmean "$fMRIFolder"/"$ScoutName"_gdc - invwarp -r "$fMRIFolder"/"$NameOffMRI"_gdc_warp -w "$fMRIFolder"/"$NameOffMRI"_gdc_warp -o "$fMRIFolder"/"$NameOffMRI"_gdc_invwarp - applywarp --interp=spline -i "$fMRIFolder"/"$ScoutName"_gdc -r "$fMRIFolder"/"$ScoutName"_gdc -w "$fMRIFolder"/"$NameOffMRI"_gdc_invwarp -o "$fMRIFolder"/"$OrigScoutName" -fi - - -#EPI Distortion Correction and EPI to T1w Registration -if [ -e ${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased ] ; then - rm -r ${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased -fi -mkdir -p ${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased - -if [ $DistortionCorrection = FIELDMAP ]; then -### ERIC'S DEBUGGING ECHO ### -# INSERTING MANUAL MASK IF IT IS FOUND IN PATH... -if [ -e ${fMRIFolder}/../masks/${Subject}_${NameOffMRI}_mask.nii.gz ]; then - echo "using manual mask for this subject..." - InputMaskImage=${fMRIFolder}/../masks/${Subject}_${NameOffMRI}_mask.nii.gz -fi - - -${RUN} ${PipelineScripts}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased.sh \ - --workingdir=${fMRIFolder}/DistortionCorrectionAndEPIToT1wReg_FLIRTBBRAndFreeSurferBBRbased \ - --scoutin=${fMRIFolder}/${ScoutName}_gdc \ - --t1=${T1wFolder}/${T1wImage} \ - --t1restore=${T1wFolder}/${T1wRestoreImage} \ - --t1brain=${T1wFolder}/${T1wRestoreImageBrain} \ - --fmapmag=${MagnitudeInputName} \ - --fmapphase=${PhaseInputName} \ - --echodiff=${deltaTE} \ - --SEPhaseNeg=${SpinEchoPhaseEncodeNegative} \ - --SEPhasePos=${SpinEchoPhaseEncodePositive} \ - --echospacing=${DwellTime} \ - --unwarpdir=${UnwarpDir} \ - --owarp=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ - --biasfield=${T1wFolder}/${BiasField} \ - --oregim=${fMRIFolder}/${RegOutput} \ - --freesurferfolder=${T1wFolder} \ - --freesurfersubjectid=${Subject} \ - --gdcoeffs=${GradientDistortionCoeffs} \ - --qaimage=${fMRIFolder}/${QAImage} \ - --method=${DistortionCorrection} \ - --topupconfig=${TopupConfig} \ - --ojacobian=${fMRIFolder}/${JacobianOut} \ - --ce=${ContrastEnhanced} \ - --inputmask=$InputMaskImage - - -elif [ $DistortionCorrection = TOPUP ]; then - mkdir ${fMRIFolder}/FieldMap 2> /dev/null - if ! ${PreviousRegistration:-false}; then - if ${useRevEpi:-false}; then - SpinEchoPhaseEncodePositive=${fMRIFolder}/${ScoutName}_gdc - fi - echo ${HCPPIPEDIR_Global}/TopupPreprocessingAll.sh \ - --workingdir=${fMRIFolder}/FieldMap \ - --phaseone=${SpinEchoPhaseEncodePositive} \ - --phasetwo=${SpinEchoPhaseEncodeNegative} \ - --scoutin=${fMRIFolder}/${ScoutName}_gdc \ - --echospacing=${DwellTime} \ - --unwarpdir=${SEUnwarpDir} \ - --ofmapmag=${fMRIFolder}/Magnitude \ - --ofmapmagbrain=${fMRIFolder}/Magnitude_brain \ - --ofmap=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ - --ojacobian=${fMRIFolder}/${JacobianOut} \ - --gdcoeffs=${GradientDistortionCoeffs} \ - --topupconfig=${TopupConfig} - ${HCPPIPEDIR_Global}/TopupPreprocessingAll.sh \ - --workingdir=${fMRIFolder}/FieldMap \ - --phaseone=${SpinEchoPhaseEncodeNegative} \ - --phasetwo=${SpinEchoPhaseEncodePositive} \ - --scoutin=${fMRIFolder}/${ScoutName}_gdc \ - --echospacing=${DwellTime} \ - --unwarpdir=${UnwarpDir} \ - --ofmapmag=${fMRIFolder}/Magnitude \ - --ofmapmagbrain=${fMRIFolder}/Magnitude_brain \ - --owarp=${fMRIFolder}/WarpField \ - --ojacobian=${fMRIFolder}/${JacobianOut} \ - --gdcoeffs=${GradientDistortionCoeffs} \ - --topupconfig=${TopupConfig} - - ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${fMRIFolder}/${ScoutName}_gdc -w ${fMRIFolder}/WarpField.nii.gz -o ${fMRIFolder}/${ScoutName}_gdc_undistorted - # apply Jacobian correction to scout image (optional) - ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mul ${fMRIFolder}/FieldMap/Jacobian ${fMRIFolder}/${ScoutName}_gdc_undistorted - # register undistorted scout image to T1w - ${FSLDIR}/bin/flirt -interp spline -dof 6 -in ${fMRIFolder}/${ScoutName}_gdc_undistorted -ref ${T1wFolder}/${T1wRestoreImageBrain} -omat "$fMRIFolder"/Scout2T1w.mat -out ${fMRIFolder}/Scout2T1w.nii.gz -searchrx -30 30 -searchry -30 30 -searchrz -30 30 -cost mutualinfo - ${FSLDIR}/bin/convert_xfm -omat "$fMRIFolder"/T1w2Scout.mat -inverse "$fMRIFolder"/Scout2T1w.mat - ${FSLDIR}/bin/applywarp --interp=nn -i ${T1wFolder}/${T1wRestoreImageBrain} -r ${fMRIFolder}/${ScoutName}_gdc_undistorted --premat="$fMRIFolder"/T1w2Scout.mat -o ${fMRIFolder}/Scout_brain_mask.nii.gz - ${FSLDIR}/bin/fslmaths ${fMRIFolder}/Scout_brain_mask.nii.gz -bin ${fMRIFolder}/Scout_brain_mask.nii.gz - ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted -mas ${fMRIFolder}/Scout_brain_mask.nii.gz ${fMRIFolder}/Scout_brain_dc.nii.gz - ${FSLDIR}/bin/epi_reg --epi=${fMRIFolder}/Scout_brain_dc.nii.gz --pedir=${UnwarpDir} --t1=${T1wFolder}/${T1wImage} --t1brain=${T1wFolder}/${T1wRestoreImageBrain} --out=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init - # generate combined warpfields and spline interpolated images + apply bias field correction - ${FSLDIR}/bin/convertwarp --relout --rel -r ${T1wFolder}/${T1wImage} --warp1=${fMRIFolder}/WarpField --postmat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp - - ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/Jacobian.nii.gz -r ${T1wFolder}/${T1wImage} --premat=${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.mat -o ${fMRIFolder}/Jacobian2T1w.nii.gz - ${FSLDIR}/bin/applywarp --rel --interp=spline -i ${fMRIFolder}/${ScoutName}_gdc -r ${T1wFolder}/${T1wImage} -w ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp -o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init - # apply Jacobian correction to scout image (optional) - ${FSLDIR}/bin/fslmaths ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init -div ${T1wFolder}/${BiasField} -mul ${fMRIFolder}/Jacobian2T1w.nii.gz ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz - SUBJECTS_DIR=${T1wFolder} - - - - #echo ${FREESURFER_HOME}/bin/bbregister --s ${Subject} --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --surf white --init-reg ${T1wFolder}/mri/transforms/eye.dat --bold --reg ${fMRIFolder}/EPItoT1w.dat --o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w.nii.gz - #${FREESURFER_HOME}/bin/bbregister --s ${Subject} --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --surf white --init-reg ${T1wFolder}/mri/transforms/eye.dat --bold --reg ${fMRIFolder}/EPItoT1w.dat --o ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w.nii.gz - # Create FSL-style matrix and then combine with existing warp fields - - - - - #echo ${FREESURFER_HOME}/bin/tkregister2 --noedit --reg ${fMRIFolder}/EPItoT1w.dat --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --targ ${T1wFolder}/${T1wRestoreImageBrain}.nii.gz --fslregout ${fMRIFolder}/fMRI2str.mat - #${FREESURFER_HOME}/bin/tkregister2 --noedit --reg ${fMRIFolder}/EPItoT1w.dat --mov ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init.nii.gz --targ ${T1wFolder}/${T1wRestoreImageBrain}.nii.gz --fslregout ${fMRIFolder}/fMRI2str.mat - - #${FSLDIR}/bin/convertwarp --relout --rel --warp1=${fMRIFolder}/${ScoutName}_undistorted2T1w_init_warp.nii.gz --ref=${T1wFolder}/${T1wImage} --postmat=${fMRIFolder}/fMRI2str.mat --out=${T1wFolder}/xfms/${fMRI2strOutputTransform} - - #@TODO re-evaluate: this xfm may be lackluster compared to a proper bbr. We could try the resolved bbr from WashU's pipe. - cp ${fMRIFolder}/${ScoutName}_gdc_undistorted2T1w_init_warp.nii.gz ${T1wFolder}/xfms/${fMRI2strOutputTransform}.nii.gz - imcp ${fMRIFolder}/Jacobian2T1w.nii.gz ${fMRIFolder}/$JacobianOut # this is the proper "JacobianOut" for input into OneStepResampling. - - elif ${PreviousRegistration}; then - # take bold results, as they tend to be more accurate transforms until we can improve them. Combine rigid ferumox -> bold - echo "using ${PreviousTask} to calculate ${NameOffMRI} registration to anatomical" - PrevTaskFolder="$Path"/${PreviousTask} - Lin2PrevTask="${fMRIFolder}"/${PreviousTask}_2_${NameOffMRI}.mat - flirt -in "${fMRIFolder}"/Scout_orig.nii.gz -cost mutualinfo -dof 6 -ref "${PrevTaskFolder}"/Scout_orig.nii.gz -omat ${Lin2PrevTask} - PrevTaskTransform=${PreviousTask}2str - imcp "$PrevTaskFolder"/"$JacobianOut" "$fMRIFolder"/"$JacobianOut" - convertwarp --rel --relout --out=${T1wFolder}/xfms/${fMRI2strOutputTransform}.nii.gz --warp1=${T1wFolder}/xfms/${PrevTaskTransform}.nii.gz --premat=${Lin2PrevTask} --ref=${T1wFolder}/${T1wImage} - fi - -else - # fake jacobian out - # DFM is still being applied from CYA, will put DFM procedure here later. - fslmaths ${T1wFolder}/${T1wImage} -abs -add 1 -bin ${fMRIFolder}/${JacobianOut} -fi - - -echo "RUNNING ONE STEP RESAMPLING" -#One Step Resampling -mkdir -p ${fMRIFolder}/OneStepResampling -echo ${RUN} ${PipelineScripts}/OneStepResampling.sh \ - --workingdir=${fMRIFolder}/OneStepResampling \ - --infmri=${fMRIFolder}/${OrigTCSName}.nii.gz \ - --t1=${AtlasSpaceFolder}/${T1wAtlasName} \ - --fmriresout=${FinalfMRIResolution} \ - --fmrifolder=${fMRIFolder} \ - --fmri2structin=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ - --struct2std=${AtlasSpaceFolder}/xfms/${AtlasTransform} \ - --owarp=${AtlasSpaceFolder}/xfms/${OutputfMRI2StandardTransform} \ - --oiwarp=${AtlasSpaceFolder}/xfms/${Standard2OutputfMRITransform} \ - --motionmatdir=${fMRIFolder}/${MotionMatrixFolder} \ - --motionmatprefix=${MotionMatrixPrefix} \ - --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin \ - --freesurferbrainmask=${AtlasSpaceFolder}/${FreeSurferBrainMask} \ - --biasfield=${AtlasSpaceFolder}/${BiasFieldMNI} \ - --gdfield=${fMRIFolder}/${NameOffMRI}_gdc_warp \ - --scoutin=${fMRIFolder}/${OrigScoutName} \ - --scoutgdcin=${fMRIFolder}/${ScoutName}_gdc \ - --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ - --jacobianin=${fMRIFolder}/${JacobianOut} \ - --ojacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} - -${RUN} ${PipelineScripts}/OneStepResampling.sh \ - --workingdir=${fMRIFolder}/OneStepResampling \ - --infmri=${fMRIFolder}/${OrigTCSName}.nii.gz \ - --t1=${AtlasSpaceFolder}/${T1wAtlasName} \ - --fmriresout=${FinalfMRIResolution} \ - --fmrifolder=${fMRIFolder} \ - --fmri2structin=${T1wFolder}/xfms/${fMRI2strOutputTransform} \ - --struct2std=${AtlasSpaceFolder}/xfms/${AtlasTransform} \ - --owarp=${AtlasSpaceFolder}/xfms/${OutputfMRI2StandardTransform} \ - --oiwarp=${AtlasSpaceFolder}/xfms/${Standard2OutputfMRITransform} \ - --motionmatdir=${fMRIFolder}/${MotionMatrixFolder} \ - --motionmatprefix=${MotionMatrixPrefix} \ - --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin \ - --freesurferbrainmask=${AtlasSpaceFolder}/${FreeSurferBrainMask} \ - --biasfield=${AtlasSpaceFolder}/${BiasFieldMNI} \ - --gdfield=${fMRIFolder}/${NameOffMRI}_gdc_warp \ - --scoutin=${fMRIFolder}/${OrigScoutName} \ - --scoutgdcin=${fMRIFolder}/${ScoutName}_gdc \ - --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ - --jacobianin=${fMRIFolder}/${JacobianOut} \ - --ojacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} - - -echo "RUNNING INTENSITY NORMALIZATION & BIAS REMOVAL" -#Intensity Normalization and Bias Removal -### ERIC'S DEBUGGING ECHO ### -echo ${RUN} ${PipelineScripts}/IntensityNormalization.sh \ - --infmri=${fMRIFolder}/${NameOffMRI}_nonlin \ - --biasfield=${fMRIFolder}/${BiasFieldMNI}.${FinalfMRIResolution} \ - --jacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} \ - --brainmask=${fMRIFolder}/${FreeSurferBrainMask}.${FinalfMRIResolution} \ - --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin_norm \ - --inscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ - --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm \ - --usejacobian=false -${RUN} ${PipelineScripts}/IntensityNormalization.sh \ - --infmri=${fMRIFolder}/${NameOffMRI}_nonlin \ - --biasfield=${fMRIFolder}/${BiasFieldMNI}.${FinalfMRIResolution} \ - --jacobian=${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution} \ - --brainmask=${fMRIFolder}/${FreeSurferBrainMask}.${FinalfMRIResolution} \ - --ofmri=${fMRIFolder}/${NameOffMRI}_nonlin_norm \ - --inscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \ - --oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm \ - --usejacobian=false - -mkdir -p ${ResultsFolder} -# MJ QUERY: WHY THE -r OPTIONS BELOW? -${RUN} cp -r ${fMRIFolder}/${NameOffMRI}_nonlin_norm.nii.gz ${ResultsFolder}/${NameOffMRI}.nii.gz -${RUN} cp -r ${fMRIFolder}/${MovementRegressor}.txt ${ResultsFolder}/${MovementRegressor}.txt -${RUN} cp -r ${fMRIFolder}/${MovementRegressor}_dt.txt ${ResultsFolder}/${MovementRegressor}_dt.txt -${RUN} cp -r ${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm.nii.gz ${ResultsFolder}/${NameOffMRI}_SBRef.nii.gz -${RUN} cp -r ${fMRIFolder}/${JacobianOut}_MNI.${FinalfMRIResolution}.nii.gz ${ResultsFolder}/${NameOffMRI}_${JacobianOut}.nii.gz -###Add stuff for RMS### -${RUN} cp -r ${fMRIFolder}/Movement_RelativeRMS.txt ${ResultsFolder}/Movement_RelativeRMS.txt -${RUN} cp -r ${fMRIFolder}/Movement_AbsoluteRMS.txt ${ResultsFolder}/Movement_AbsoluteRMS.txt -${RUN} cp -r ${fMRIFolder}/Movement_RelativeRMS_mean.txt ${ResultsFolder}/Movement_RelativeRMS_mean.txt -${RUN} cp -r ${fMRIFolder}/Movement_AbsoluteRMS_mean.txt ${ResultsFolder}/Movement_AbsoluteRMS_mean.txt -###Add stuff for RMS### - -echo "-------------------------------" -echo "END OF fMRI-VOLUME-PROCESSING.sh SCRIPT" -echo "Please Verify Clean Error File" From fc1a757d5b34d9334f4584132e47672ca9a77767 Mon Sep 17 00:00:00 2001 From: Kathy Snider Date: Thu, 19 Mar 2020 12:00:46 -0700 Subject: [PATCH 6/6] Lose extra files. --- PreFreeSurfer/PreFreeSurferPipeline_orig.sh | 446 -------------------- 1 file changed, 446 deletions(-) delete mode 100755 PreFreeSurfer/PreFreeSurferPipeline_orig.sh diff --git a/PreFreeSurfer/PreFreeSurferPipeline_orig.sh b/PreFreeSurfer/PreFreeSurferPipeline_orig.sh deleted file mode 100755 index efe115d..0000000 --- a/PreFreeSurfer/PreFreeSurferPipeline_orig.sh +++ /dev/null @@ -1,446 +0,0 @@ -#!/bin/bash -x - -export OMP_NUM_THREADS=1 -export PATH=`echo $PATH | sed 's|freesurfer/|freesurfer53/|g'` - -# Requirements for this script -# installed versions of: FSL5.0.1 or higher , FreeSurfer (version 5 or higher) , gradunwarp (python code from MGH) -# environment: FSLDIR , FREESURFER_HOME , HCPPIPEDIR , CARET7DIR , PATH (for gradient_unwarp.py) - -# make pipeline engine happy... -if [ $# -eq 1 ] ; then - echo "Version unknown..." - exit 0 -fi - -########################################## PIPELINE OVERVIEW ########################################## - -#TODO - -########################################## OUTPUT DIRECTORIES ########################################## - -## NB: NO assumption is made about the input paths with respect to the output directories - they can be totally different. All input are taken directly from the input variables without additions or modifications. -# NB: Output directories T1wFolder and T2wFolder MUST be different (as various output subdirectories containing standardly named files, e.g. full2std.mat, would overwrite each other) so if this script is modified, then keep these output directories distinct -# Output path specifiers: -# -# ${StudyFolder} is an input parameter -# ${Subject} is an input parameter -# -# Main output directories -# T1wFolder=${StudyFolder}/T1w -# T2wFolder=${StudyFolder}/T2w -# AtlasSpaceFolder=${StudyFolder}/MNINonLinear -# -# All outputs are within the directory: ${StudyFolder}/ -# The list of output directories are the following -# -# T1w/T1w${i}_GradientDistortionUnwarp -# T1w/AverageT1wImages -# T1w/ACPCAlignment -# T1w/BrainExtraction_FNIRTbased -# and the above for T2w as well (s/T1w/T2w/g) -# -# T2w/T2wToT1wDistortionCorrectAndReg -# T1w/BiasFieldCorrection_sqrtT1wXT1w -# MNINonLinear -# -# Also exist: -# T1w/xfms/ -# T2w/xfms/ -# MNINonLinear/xfms/ -# -########################################## SUPPORT FUNCTIONS ########################################## -# function for parsing options -getopt1() { - sopt="$1" - shift 1 - for fn in $@ ; do - if [ `echo $fn | grep -- "^${sopt}=" | wc -w` -gt 0 ] ; then - echo $fn | sed "s/^${sopt}=//" - return 0 - fi - done -} -defaultopt() { - echo $1 -} -################################################## OPTION PARSING ##################################################### - -# Input Variables -StudyFolder=`getopt1 "--path" $@` # "$1" #Path to subject's data folder -Subject=`getopt1 "--subject" $@` # "$2" #SubjectID -T1wInputImages=`getopt1 "--t1" $@` # "$3" #T1w1@T1w2@etc.. -T2wInputImages=`getopt1 "--t2" $@` # "$4" #T2w1@T2w2@etc.. -T1wTemplate=`getopt1 "--t1template" $@` # "$5" #MNI template -T1wTemplateBrain=`getopt1 "--t1templatebrain" $@` # "$6" #Brain extracted MNI T1wTemplate -T1wTemplate2mm=`getopt1 "--t1template2mm" $@` # "$7" #MNI2mm T1wTemplate -T2wTemplate=`getopt1 "--t2template" $@` # "${8}" #MNI T2wTemplate -T2wTemplateBrain=`getopt1 "--t2templatebrain" $@` # "$9" #Brain extracted MNI T2wTemplate -T2wTemplate2mm=`getopt1 "--t2template2mm" $@` # "${10}" #MNI2mm T2wTemplate -TemplateMask=`getopt1 "--templatemask" $@` # "${11}" #Brain mask MNI Template -Template2mmMask=`getopt1 "--template2mmmask" $@` # "${12}" #Brain mask MNI2mm Template -BrainSize=`getopt1 "--brainsize" $@` # "${13}" #StandardFOV mask for averaging structurals -FNIRTConfig=`getopt1 "--fnirtconfig" $@` # "${14}" #FNIRT 2mm T1w Config -MagnitudeInputName=`getopt1 "--fmapmag" $@` # "${16}" #Expects 4D magitude volume with two 3D timepoints -MagnitudeInputBrainName=`getopt1 "--fmapmagbrain" $@` # If you've already masked the magnitude. -PhaseInputName=`getopt1 "--fmapphase" $@` # "${17}" #Expects 3D phase difference volume -TE=`getopt1 "--echodiff" $@` # "${18}" #delta TE for field map -SpinEchoPhaseEncodeNegative=`getopt1 "--SEPhaseNeg" $@` -SpinEchoPhaseEncodePositive=`getopt1 "--SEPhasePos" $@` -DwellTime=`getopt1 "--echospacing" $@` -SEUnwarpDir=`getopt1 "--seunwarpdir" $@` -T1wSampleSpacing=`getopt1 "--t1samplespacing" $@` # "${19}" #DICOM field (0019,1018) -T2wSampleSpacing=`getopt1 "--t2samplespacing" $@` # "${20}" #DICOM field (0019,1018) -UnwarpDir=`getopt1 "--unwarpdir" $@` # "${21}" #z appears to be best -GradientDistortionCoeffs=`getopt1 "--gdcoeffs" $@` # "${25}" #Select correct coeffs for scanner or "NONE" to turn off -AvgrdcSTRING=`getopt1 "--avgrdcmethod" $@` # "${26}" #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" = average and distortion correct at the same time with topup/applytopup only works for 2 images currently -TopupConfig=`getopt1 "--topupconfig" $@` # "${27}" #Config for topup or "NONE" if not used -BiasFieldSmoothingSigma=`getopt1 "--bfsigma" $@` # "$9" -RUN=`getopt1 "--printcom" $@` # use ="echo" for just printing everything and not running the commands (default is to run) -useT2=`getopt1 "--useT2" $@` # useT2 flag added for excluding or including T2 processing, grabbed from batch file -T1wNormalized=`getopt1 "--t1normalized" $@` # brain normalized to matter intensities -useReverseEpi=`getopt1 "--revepi" $@` -MultiTemplateDir=`getopt1 "--multitemplatedir" $@` - - -# Defaults -T1wNormalized=${T1wNormalized:-"NONE"} - -echo "$StudyFolder $Subject" -pushd ${StudyFolder} - -# Paths for scripts etc (uses variables defined in SetUpHCPPipeline.sh) -PipelineScripts=${HCPPIPEDIR_PreFS} -GlobalScripts=${HCPPIPEDIR_Global} - -# Naming Conventions ... OMD these will likely have to be modified for OHSU -T1wImage="T1w" -T1wFolder="T1w" #Location of T1w images -if $useT2; then - T2wImage="T2w" - T2wFolder="T2w" -fi -#**T1wNFolder="T1wN" #Location of T1w Normalized Images -if [ ! $T1wNormalized = "NONE" ]; then - T1wNImage="T1wN" -fi -AtlasSpaceFolder="MNINonLinear" - -# Build Paths, OMD: Outputs, ie calculated data -T1wFolder=${StudyFolder}/${T1wFolder} -if $useT2; then T2wImage="T2w" - T2wFolder=${StudyFolder}/${T2wFolder} -fi -#**T1wNFolder=${StudyFolder}/${T1wNFolder} -AtlasSpaceFolder=${StudyFolder}/${AtlasSpaceFolder} - -echo "$T1wFolder $T2wFolder $T1wNFolder $AtlasSpaceFolder" - -# Unpack List of Images -T1wInputImages=`echo ${T1wInputImages} | sed 's/@/ /g'` #File and path to the T1 MPRG, space separated, OMD -if $useT2; then T2wInputImages=`echo ${T2wInputImages} | sed 's/@/ /g'`; fi #File and path to the T2 MPRG, space separated, OMD - -pushd ${StudyFolder}/ - -if [ ! -e ${T1wFolder}/xfms ] ; then - echo "mkdir -p ${T1wFolder}/xfms/" - mkdir -p ${T1wFolder}/xfms/ -fi -# Placing T1wN niftis in T1w folder -Dakota 10/26/17 -#if [ ! -e ${T1wNFolder}/xfms ]; then -# echo "mkdir -p ${T1wNFolder}/xfms/" -# mkdir -p ${T1wNFolder}/xfms/ -#fi -if $useT2; then - if [ ! -e ${T2wFolder}/xfms ] ; then - echo "mkdir -p ${T2wFolder}/xfms/" - mkdir -p ${T2wFolder}/xfms/ - fi -fi - -if [ ! -e ${AtlasSpaceFolder}/xfms ] ; then - echo "mkdir -p ${AtlasSpaceFolder}/xfms/" - mkdir -p ${AtlasSpaceFolder}/xfms/ -fi - -echo "POSIXLY_CORRECT="${POSIXLY_CORRECT} - - -########################################## DO WORK ########################################## - -######## LOOP over the same processing for T1w and T2w (just with different names) ######## -if $useT2; then Modalities="T1w T2w"; else Modalities="T1w"; fi #Removed T1wN -Dakota 10/26/17 -#bene added T1wN to modalities 6-20-17 but commented it out for now as I didn't know if that makes sense. -#if $useT2; then Modalities="T1w T2w T1wN"; else Modalities="T1w T1wN"; fi -#commented out T1wN steps -Dakota 10/26/17 - -for TXw in ${Modalities} ; do - # set up appropriate input variables - if [ $TXw = T1w ] ; then - TXwInputImages="${T1wInputImages}" - TXwFolder=${T1wFolder} - TXwImage=${T1wImage} #T1W, OMD - TXwTemplate=${T1wTemplate} # It points to the template at 0.7 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) - TXwTemplateBrain=${T1wTemplateBrain} - TXwTemplate2mm=${T1wTemplate2mm} # It points to the template at 2.0 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) - TXwExt=${Subject}_T1w_MPR_average - elif [ $TXw = T2w ]; then - TXwInputImages="${T2wInputImages}" - TXwFolder=${T2wFolder} - TXwImage=${T2wImage} - TXwTemplate=${T2wTemplate} - TXwTemplateBrain=${T2wTemplateBrain} - TXwTemplate2mm=${T2wTemplate2mm} - TXwExt=${Subject}_T2w_SPC_average -#** elif [ $TXw = T1wN ]; then -# TXwInputImages="${T1wNInputImages}" -# TXwFolder=${T1wNFolder} -# TXwImage=${T1wNImage} #T1W Normalized -# TXwTemplate=${T1wTemplate} # It points to the template at 0.7 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) -# TXwTemplateBrain=${T1wTemplateBrain} -# TXwTemplate2mm=${T1wTemplate2mm} # It points to the template at 2.0 mm skull+brain, OMD (Remember, scan was performed at 0.7mm isotropic) -# TXwExt=${Subject}_T1w_MPR_average_AdultInt - fi - OutputTXwImageSTRING="" - -#### Gradient nonlinearity correction (for T1w and T2w) #### - - if [ ! $GradientDistortionCoeffs = "NONE" ] ; then - i=1 - for Image in $TXwInputImages ; do - wdir=${TXwFolder}/${TXwImage}${i}_GradientDistortionUnwarp - echo "mkdir -p $wdir" - mkdir -p $wdir - ${RUN} ${FSLDIR}/bin/fslreorient2std $Image ${wdir}/${TXwImage}${i} #Make sure input axes are oriented the same as the templates - ${RUN} ${GlobalScripts}/GradientDistortionUnwarp.sh \ - --workingdir=${wdir} \ - --coeffs=$GradientDistortionCoeffs \ - --in=${wdir}/${TXwImage}${i} \ - --out=${TXwFolder}/${TXwImage}${i}_gdc \ - --owarp=${TXwFolder}/xfms/${TXwImage}${i}_gdc_warp - OutputTXwImageSTRING="${OutputTXwImageSTRING}${TXwFolder}/${TXwImage}${i}_gdc " - i=$(($i+1)) - done - - else - echo "NOT PERFORMING GRADIENT DISTORTION CORRECTION" - i=1 - for Image in $TXwInputImages ; do - ${RUN} ${FSLDIR}/bin/fslreorient2std $Image ${TXwFolder}/${TXwImage}${i}_gdc - OutputTXwImageSTRING="${OutputTXwImageSTRING}${TXwFolder}/${TXwImage}${i}_gdc " - i=$(($i+1)) - done - fi - -#### Average Like Scans #### - - if [ `echo $TXwInputImages | wc -w` -gt 1 ] ; then - mkdir -p ${TXwFolder}/Average${TXw}Images - #if [ ${AvgrdcSTRING} = "TOPUP" ] ; then - # echo "PERFORMING TOPUP READOUT DISTORTION CORRECTION AND AVERAGING" - # ${RUN} ${PipelineScripts}/TopupDistortionCorrectAndAverage.sh ${TXwFolder}/Average${TXw}Images "${OutputTXwImageSTRING}" ${TXwFolder}/${TXwImage} ${TopupConfig} - #else - echo "PERFORMING SIMPLE AVERAGING" - ${RUN} ${PipelineScripts}/AnatomicalAverage.sh -o ${TXwFolder}/${TXwImage} -s ${TXwTemplate} -m ${TemplateMask} \ - -n -w ${TXwFolder}/Average${TXw}Images --noclean -v -b $BrainSize $OutputTXwImageSTRING - #fi - else - echo "ONLY ONE AVERAGE FOUND: COPYING" - ${RUN} ${FSLDIR}/bin/imcp ${TXwFolder}/${TXwImage}1_gdc ${TXwFolder}/${TXwImage} - fi - -#### ACPC align T1w and T2w image to 0.7mm MNI T1wTemplate to create native volume space #### -#** if [ ! $TXw = "T1wN" ]; then - mkdir -p ${TXwFolder}/ACPCAlignment - # Assume Brain has been placed in T1w from Prep stage. - ${RUN} ${PipelineScripts}/ACPCAlignment.sh \ - --workingdir=${TXwFolder}/ACPCAlignment \ - --in=${TXwFolder}/${TXwImage}_brain \ - --ref=${TXwTemplateBrain} \ - --out=${TXwFolder}/${TXwImage}_acpc_brain \ - --omat=${TXwFolder}/xfms/acpc.mat \ - --brainsize=${BrainSize} - # Apply linear transform to head. -# flirt -in ${TXwFolder}/${TXwImage}_brain -ref ${TXwTemplate} -applyxfm -init ${TXwFolder}/xfms/acpc.mat -out ${TXwFolder}/${TXwImage}_acpc_brain -# ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask -#** else - # ensure that the same T1w transform is applied to normed brain. -# echo "applying acpc warp to Normalized image" -# cp ${T1wFolder}/xfms/acpc.mat ${TXwFolder}/xfms/acpc.mat -# ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${TXwFolder}/${TXwImage}" -r "${TXwTemplate}" \ -# --premat="${TXwFolder}/xfms/acpc.mat" -o "${TXwFolder}/${TXwImage}_acpc" -# fi - -#### Brain Extraction (FNIRT-based Masking) #### - -############ FNL - this is performed using ANTs in Prep -# mkdir -p ${TXwFolder}/BrainExtraction_FNIRTbased -# ${RUN} ${PipelineScripts}/BrainExtraction_FNIRTbased.sh \ -# --workingdir=${TXwFolder}/BrainExtraction_ANTsbased \ -# --in=${TXwFolder}/${TXwImage}_acpc \ -# --ref=${TXwTemplate} \ -# --refmask=${TemplateMask} \ -# --ref2mm=${TXwTemplate2mm} \ -# --ref2mmmask=${Template2mmMask} \ -# --outbrain=${TXwFolder}/${TXwImage}_acpc_brain \ -# --outbrainmask=${TXwFolder}/${TXwImage}_acpc_brain_mask \ -# --fnirtconfig=${FNIRTConfig}; -# ${FSLDIR}/bin/flirt -in ${TXwFolder}/${TXwImage}_brain -ref ${TXwTemplate} -applyxfm -init ${TXwFolder}/xfms/acpc.mat -out ${TXwFolder}/${TXwImage}_acpc_brain -# ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask -#** if [ $TXw = T1wN ]; then ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc -mas ${T1wFolder}/${T1wImage}_acpc_brain_mask ${TXwFolder}/${TXwImage}_acpc_brain; fi - - #apply acpc.mat to head - ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${TXwFolder}/${TXwImage}" -r "${TXwTemplate}" \ - --premat="${TXwFolder}/xfms/acpc.mat" -o "${TXwFolder}/${TXwImage}_acpc" - #make brain mask - ${FSLDIR}/bin/fslmaths ${TXwFolder}/${TXwImage}_acpc_brain -bin ${TXwFolder}/${TXwImage}_acpc_brain_mask -done - -######## END LOOP over T1w and T2w ######### - -#Orient T1wN image to standard and create ACPC aligned T1wN image -if [ ! $T1wNormalized = "NONE" ]; then - pushd ${T1wFolder} > /dev/null - cp ${T1wNormalized} ./${T1wNImage}.nii.gz - ${RUN} ${FSLDIR}/bin/fslreorient2std ./${T1wNImage} ${T1wFolder}/${T1wNImage}1_gdc - ${RUN} ${FSLDIR}/bin/imcp ${T1wFolder}/${T1wNImage}1_gdc ${T1wFolder}/${T1wNImage} - - ${FSLDIR}/bin/applywarp --rel --interp=spline -i "${T1wFolder}/${T1wNImage}" -r "${T1wTemplate}" \ - --premat="${T1wFolder}/xfms/acpc.mat" -o "${T1wFolder}/${T1wNImage}_acpc" - ${FSLDIR}/bin/fslmaths ${T1wFolder}/${T1wNImage}_acpc -mas ${T1wFolder}/${T1wImage}_acpc_brain_mask ${T1wFolder}/${T1wNImage}_acpc_brain - popd > /dev/null -fi - -if ${useReverseEpi:-false}; then - # Time is too long for Resting State, so we average it first. - mcflirt "$SpinEchoPhaseEncodeNegative" -out "$T1wFolder"/tmp_REST_mc.nii.gz - fslmaths "$T1wFolder"/tmp_REST_mc.nii.gz -Tmean "$T1wFolder"/PEForwardScout.nii.gz - SpinEchoPhaseEncodeNegative="$T1wFolder"/PEForwardScout.nii.gz - rm "$T1wFolder"/tmp_REST_mc.nii.gz -fi - -#### T2w to T1w Registration and Optional Readout Distortion Correction #### -if $useT2; then - if [[ ${AvgrdcSTRING} = "FIELDMAP" || ${AvgrdcSTRING} = "TOPUP" ]] ; then - echo "PERFORMING ${AvgrdcSTRING} READOUT DISTORTION CORRECTION" - wdir=${T2wFolder}/T2wToT1wDistortionCorrectAndReg - if [ -d ${wdir} ] ; then - # DO NOT change the following line to "rm -r ${wdir}" because the chances of something going wrong with that are much higher, and rm -r always needs to be treated with the utmost caution - rm -r ${T2wFolder}/T2wToT1wDistortionCorrectAndReg - fi - mkdir -p ${wdir} - - ${RUN} ${PipelineScripts}/T2wToT1wDistortionCorrectAndReg.sh \ - --workingdir=${wdir} \ - --t1=${T1wFolder}/${T1wImage}_acpc \ - --t1brain=${T1wFolder}/${T1wImage}_acpc_brain \ - --t2=${T2wFolder}/${T2wImage}_acpc \ - --t2brain=${T2wFolder}/${T2wImage}_acpc_brain \ - --fmapmag=${MagnitudeInputName} \ - --fmapmagbrain=${MagnitudeInputBrainName} \ - --fmapphase=${PhaseInputName} \ - --echodiff=${TE} \ - --SEPhaseNeg=${SpinEchoPhaseEncodeNegative} \ - --SEPhasePos=${SpinEchoPhaseEncodePositive} \ - --echospacing=${DwellTime} \ - --seunwarpdir=${SEUnwarpDir} \ - --t1sampspacing=${T1wSampleSpacing} \ - --t2sampspacing=${T2wSampleSpacing} \ - --unwarpdir=${UnwarpDir} \ - --ot1=${T1wFolder}/${T1wImage}_acpc_dc \ - --ot1brain=${T1wFolder}/${T1wImage}_acpc_dc_brain \ - --ot1warp=${T1wFolder}/xfms/${T1wImage}_dc \ - --ot2=${T1wFolder}/${T2wImage}_acpc_dc \ - --ot2warp=${T1wFolder}/xfms/${T2wImage}_reg_dc \ - --method=${AvgrdcSTRING} \ - --topupconfig=${TopupConfig} \ - --gdcoeffs=${GradientDistortionCoeffs} - else - wdir=${T2wFolder}/T2wToT1wReg - if [ -e ${wdir} ] ; then - # DO NOT change the following line to "rm -r ${wdir}" because the chances of something going wrong with that are much higher, and rm -r always needs to be treated with the utmost caution - rm -r ${T2wFolder}/T2wToT1wReg - fi - mkdir -p ${wdir} - ${RUN} ${PipelineScripts}/T2wToT1wReg.sh \ - ${wdir} \ - ${T1wFolder}/${T1wImage}_acpc \ - ${T1wFolder}/${T1wImage}_acpc_brain \ - ${T2wFolder}/${T2wImage}_acpc \ - ${T2wFolder}/${T2wImage}_acpc_brain \ - ${T1wFolder}/${T1wImage}_acpc_dc \ - ${T1wFolder}/${T1wImage}_acpc_dc_brain \ - ${T1wFolder}/xfms/${T1wImage}_dc \ - ${T1wFolder}/${T2wImage}_acpc_dc \ - ${T1wFolder}/xfms/${T2wImage}_reg_dc - fi -else - imcp ${T1wFolder}/${T1wImage}_acpc ${T1wFolder}/${T1wImage}_acpc_dc - imcp ${T1wFolder}/${T1wImage}_acpc_brain ${T1wFolder}/${T1wImage}_acpc_dc_brain -fi - -#### Bias Field Correction: Calculate bias field using square root of the product of T1w and T2w iamges. #### -if $useT2; then - if [ ! -z ${BiasFieldSmoothingSigma} ] ; then - BiasFieldSmoothingSigma="--bfsigma=${BiasFieldSmoothingSigma}" - fi - mkdir -p ${T1wFolder}/BiasFieldCorrection_sqrtT1wXT1w - ${RUN} ${PipelineScripts}/BiasFieldCorrection_sqrtT1wXT1w.sh \ - --workingdir=${T1wFolder}/BiasFieldCorrection_sqrtT1wXT1w \ - --T1im=${T1wFolder}/${T1wImage}_acpc_dc \ - --T1brain=${T1wFolder}/${T1wImage}_acpc_dc_brain \ - --T2im=${T1wFolder}/${T2wImage}_acpc_dc \ - --obias=${T1wFolder}/BiasField_acpc_dc \ - --oT1im=${T1wFolder}/${T1wImage}_acpc_dc_restore \ - --oT1brain=${T1wFolder}/${T1wImage}_acpc_dc_restore_brain \ - --oT2im=${T1wFolder}/${T2wImage}_acpc_dc_restore \ - --oT2brain=${T1wFolder}/${T2wImage}_acpc_dc_restore_brain \ - ${BiasFieldSmoothingSigma} -else - imcp ${T1wFolder}/${T1wImage}_acpc_dc ${T1wFolder}/${T1wImage}_acpc_dc_restore - imcp ${T1wFolder}/${T1wImage}_acpc_dc_brain ${T1wFolder}/${T1wImage}_acpc_dc_restore_brain -fi - -#### Atlas Registration to MNI152: FLIRT + FNIRT #Also applies registration to T1w and T2w images #### -#Consider combining all transforms and recreating files with single resampling steps -${RUN} ${PipelineScripts}/AtlasRegistrationToMNI152_FLIRTandFNIRT.sh \ - --workingdir=${AtlasSpaceFolder} \ - --t1=${T1wFolder}/${T1wImage}_acpc_dc \ - --t1rest=${T1wFolder}/${T1wImage}_acpc_dc_restore \ - --t1restbrain=${T1wFolder}/${T1wImage}_acpc_dc_restore_brain \ - --t2=${T1wFolder}/${T2wImage}_acpc_dc \ - --t2rest=${T1wFolder}/${T2wImage}_acpc_dc_restore \ - --t2restbrain=${T1wFolder}/${T2wImage}_acpc_dc_restore_brain \ - --ref=${T1wTemplate} \ - --refbrain=${T1wTemplateBrain} \ - --refmask=${TemplateMask} \ - --ref2mm=${T1wTemplate2mm} \ - --ref2mmmask=${Template2mmMask} \ - --owarp=${AtlasSpaceFolder}/xfms/acpc_dc2standard.nii.gz \ - --oinvwarp=${AtlasSpaceFolder}/xfms/standard2acpc_dc.nii.gz \ - --ot1=${AtlasSpaceFolder}/${T1wImage} \ - --ot1rest=${AtlasSpaceFolder}/${T1wImage}_restore \ - --ot1restbrain=${AtlasSpaceFolder}/${T1wImage}_restore_brain \ - --ot2=${AtlasSpaceFolder}/${T2wImage} \ - --ot2rest=${AtlasSpaceFolder}/${T2wImage}_restore \ - --ot2restbrain=${AtlasSpaceFolder}/${T2wImage}_restore_brain \ - --fnirtconfig=${FNIRTConfig} \ - --useT2=${useT2} - -MultiTemplateT1wBrain=T1w_brain.nii.gz -MultiTemplateSeg=Segmentation.nii.gz -Council=($(ls "$MultiTemplateDir")) # we have to make sure only subdirectories are inside... -cmd="${HCPPIPEDIR_PreFS}/run_JLF.sh --working-dir=${T1wFolder}/TemplateLabelFusion2 \ - --target=$T1wFolder/${T1wImage}_acpc_dc_restore_brain.nii.gz \ - --refdir=${MultiTemplateDir} --output=${T1wFolder}/aseg_acpc.nii.gz --ncores=${OMP_NUM_THREADS:-1}" -for ((i=0; i<${#Council[@]}; i++)); do - cmd=${cmd}" -g ${Council[$i]}/$MultiTemplateT1wBrain -l ${Council[$i]}/$MultiTemplateSeg" -done -echo $cmd -$cmd - - -#### Next stage: FreeSurfer/FreeSurferPipeline.sh -echo "-------------------------------" -echo "END OF PRE-FREESURFER.sh SCRIPT" -echo "Please Verify Clean Error File"