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..1fdbe5b 100755 --- a/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh +++ b/fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh @@ -255,26 +255,61 @@ elif [ $DistortionCorrection = TOPUP ]; then --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 +########################################################################################################################################################################### +##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 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 # 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 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}/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