Skip to content

Commit

Permalink
Merge pull request #97 from coalsont/dedrift_mr_multi
Browse files Browse the repository at this point in the history
add support for multiple MR reapply groups in a single call, example script for MR FIX
  • Loading branch information
glasserm authored Mar 14, 2019
2 parents f5704c9 + 06c5778 commit 5488769
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 59 deletions.
62 changes: 36 additions & 26 deletions DeDriftAndResample/DeDriftAndResamplePipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ PARAMETERs are [ ] = optional; < > = user supplied value
(e.g. sulc@curvature@corrThickness@thickness)
[--myelin-maps=<myelin@maps>] @-delimited map name strings corresponding to myelin maps
(e.g. MyelinMap@SmoothedMyelinMap). No _BC, this will be reapplied.
[--multirun-fix-names=<MRFIXed@fMRI@Names>] @-delimited fMRIName strings corresponding to maps that
will have multi-run ICA+FIX reapplied to them (could be either rfMRI or tfMRI). Requires
specifying --multirun-fix-concat-name also.
[--multirun-fix-concat-name=<ConcatName>] the name of the concatenated multi-run fix timeseries, only
required when using --multirun-fix-names.
[--multirun-fix-names=<day1run1@day1run2%day2run1@day2run2>] @ and % delimited list of lists of
fMRIName strings that will have multi-run ICA+FIX reapplied to them (could be either rfMRI or
tfMRI). Requires specifying --multirun-fix-concat-names also, with same number of concat names as
lists of runs in this option.
[--multirun-fix-concat-names=<day1_concat@day2_concat>] @-delimited list of names of the concatenated
timeseries, only required when using --multirun-fix-names.
[--fix-names=<ICA+FIXed@fMRI@Names>] @-delimited fMRIName strings corresponding to maps that will
have ICA+FIX reapplied to them (could be either rfMRI or tfMRI). Previously known as --rfmri-names.
[--dont-fix-names=<not@ICA+FIXed@fMRI@Names>] @-delimited fMRIName strings corresponding to maps that
Expand Down Expand Up @@ -113,8 +114,8 @@ get_options()
unset p_ConcatRegName
unset p_Maps # @ delimited
p_MyelinMaps=NONE # @ delimited
p_mrFIXConcatName=NONE
p_mrFIXNames=NONE # @ delimited
p_mrFIXConcatNames=NONE # @ delimited
p_mrFIXNames=NONE # @ and % delimited, % is top-level and must match number of @s in ConcatNames
p_rfMRINames=NONE # @ delimited
p_tfMRINames=NONE # @ delimited
unset p_SmoothingFWHM
Expand Down Expand Up @@ -179,8 +180,8 @@ get_options()
p_MyelinMaps=${argument#*=}
index=$(( index + 1 ))
;;
--multirun-fix-concat-name=*)
p_mrFIXConcatName=${argument#*=}
--multirun-fix-concat-names=*)
p_mrFIXConcatNames=${argument#*=}
index=$(( index + 1 ))
;;
--multirun-fix-names=*)
Expand Down Expand Up @@ -409,7 +410,7 @@ main()
local rfMRINames="${10}"
local tfMRINames="${11}"
local mrFIXNames="${12}"
local mrFIXConcatName="${13}"
local mrFIXConcatNames="${13}"
local SmoothingFWHM="${14}"
local HighPass="${15}"
local MotionRegression="${16}"
Expand Down Expand Up @@ -442,7 +443,7 @@ main()
log_Msg "Maps: ${Maps}"
log_Msg "MyelinMaps: ${MyelinMaps}"
log_Msg "mrFIXNames: ${mrFIXNames}"
log_Msg "mrFIXConcatName: ${mrFIXConcatName}"
log_Msg "mrFIXConcatNames: ${mrFIXConcatNames}"
log_Msg "rfMRINames: ${rfMRINames}"
log_Msg "tfMRINames: ${tfMRINames}"
log_Msg "SmoothingFWHM: ${SmoothingFWHM}"
Expand Down Expand Up @@ -491,16 +492,26 @@ main()
if [[ "${mrFIXNames}" == "NONE" ]] ; then
mrFIXNames=()
else
#mrFIXNames=`echo "$mrFIXNames" | sed s/"@"/" "/g`
IFS=@ read -a mrFIXNames <<< "${mrFIXNames}"
#need a flat list of all the names in order to resample - do this before we destroy the original value of the variable
IFS=@% read -a mrFIXNamesAll <<< "${mrFIXNames}"
#two-level list, % and @, parse only one stage here
IFS=% read -a mrFIXNames <<< "${mrFIXNames}"
fi
log_Msg "After delimiter substitution, mrFIXNames: ${mrFIXNames[@]}"

if [[ "$mrFIXConcatName" == "NONE" ]]
if [[ "$mrFIXConcatNames" == "NONE" ]]
then
mrFIXConcatName=""
mrFIXConcatNames=()
else
IFS=@ read -a mrFIXConcatNames <<< "$mrFIXConcatNames"
fi

log_Msg "After delimiter substitution, mrFIXConcatNames: ${mrFIXConcatNames[@]}"

if (( ${#mrFIXNames[@]} != ${#mrFIXConcatNames[@]} ))
then
log_Err_Abort "number of MR FIX concat names and run groups are different"
fi

CorrectionSigma=$(echo "sqrt ( 200 )" | bc -l)
log_Msg "CorrectionSigma: ${CorrectionSigma}"

Expand Down Expand Up @@ -827,7 +838,7 @@ main()

# Resample (and resmooth) TS from Native
log_Msg "Resample (and resmooth) TS from Native"
for fMRIName in "${rfMRINames[@]}" "${tfMRINames[@]}" "${mrFIXNames[@]}" ; do
for fMRIName in "${rfMRINames[@]}" "${tfMRINames[@]}" "${mrFIXNamesAll[@]}" ; do
log_Msg "fMRIName: ${fMRIName}"
cp ${ResultsFolder}/${fMRIName}/${fMRIName}_Atlas${InRegName}.dtseries.nii ${ResultsFolder}/${fMRIName}/${fMRIName}_Atlas_${ConcatRegName}.dtseries.nii
for Hemisphere in L R ; do
Expand Down Expand Up @@ -861,17 +872,16 @@ main()

# reapply multirun fix

if [[ "${mrFIXConcatName}" != "" ]]
then
for (( i = 0; i < ${#mrFIXConcatNames[@]}; ++i ))
do
log_Msg "ReApply MultiRun FIX Cleanup"
log_Msg "mrFIXNames: ${mrFIXNames[@]}"
log_Msg "mrFIXConcatName: ${mrFIXConcatName}"
#reconstruct the @-delimited list
OIFS="$IFS"; IFS=@ filesat="${mrFIXNames[*]}" IFS="$OIFS"
reapply_mr_fix_cmd=("${HCPPIPEDIR}/ICAFIX/ReApplyFixMultiRunPipeline.sh" --path="${StudyFolder}" --subject="${Subject}" --fmri-names="${filesat}" --concat-fmri-name="${mrFIXConcatName}" --high-pass="${HighPass}" --reg-name="${ConcatRegName}" --matlab-run-mode="${MatlabRunMode}" --motion-regression="${MotionRegression}")
log_Msg "mrFIXNames: ${mrFIXNames[$i]}"
log_Msg "mrFIXConcatNames: ${mrFIXConcatNames[$i]}"
#stage 2 parsing is done by reapply script
reapply_mr_fix_cmd=("${HCPPIPEDIR}/ICAFIX/ReApplyFixMultiRunPipeline.sh" --path="${StudyFolder}" --subject="${Subject}" --fmri-names="${mrFIXNames[$i]}" --concat-fmri-name="${mrFIXConcatNames[$i]}" --high-pass="${HighPass}" --reg-name="${ConcatRegName}" --matlab-run-mode="${MatlabRunMode}" --motion-regression="${MotionRegression}")
log_Msg "reapply_mr_fix_cmd: ${reapply_mr_fix_cmd[*]}"
"${reapply_mr_fix_cmd[@]}"
fi
done

log_Msg "Completing main functionality"
}
Expand Down Expand Up @@ -913,7 +923,7 @@ if [[ ${1} == --* ]]; then

# Invoke main functionality use positional parameters
# ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} ${15} ${16} ${17} ${18} ${19}
main "${p_StudyFolder}" "${p_Subject}" "${p_HighResMesh}" "${p_LowResMeshes}" "${p_RegName}" "${p_DeDriftRegFiles}" "${p_ConcatRegName}" "${p_Maps}" "${p_MyelinMaps}" "${p_rfMRINames}" "${p_tfMRINames}" "${p_mrFIXNames}" "${p_mrFIXConcatName}" "${p_SmoothingFWHM}" "${p_HighPass}" "${p_MotionRegression}" "${p_MyelinTargetFile}" "${p_InRegName}" "${p_MatlabRunMode}"
main "${p_StudyFolder}" "${p_Subject}" "${p_HighResMesh}" "${p_LowResMeshes}" "${p_RegName}" "${p_DeDriftRegFiles}" "${p_ConcatRegName}" "${p_Maps}" "${p_MyelinMaps}" "${p_rfMRINames}" "${p_tfMRINames}" "${p_mrFIXNames}" "${p_mrFIXConcatNames}" "${p_SmoothingFWHM}" "${p_HighPass}" "${p_MotionRegression}" "${p_MyelinTargetFile}" "${p_InRegName}" "${p_MatlabRunMode}"

else
# Positional parameters are used
Expand Down
88 changes: 55 additions & 33 deletions Examples/Scripts/IcaFixProcessingBatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,23 @@ main() {
export FSL_FIXDIR=${FixDir}
fi

# set list of fMRI on which to run ICA+FIX
fMRINames="rfMRI_REST1_LR rfMRI_REST1_RL rfMRI_REST2_LR rfMRI_REST2_RL"
# set list of fMRI on which to run ICA+FIX, separate MR FIX groups with %, use spaces (or @ like dedrift...) to otherwise separate runs
# the MR FIX groups determine what gets concatenated before doing ICA
# the groups can be whatever you want, you can make a day 1 group and a day 2 group, or just concatenate everything, etc
fMRINames="tfMRI_WM_RL tfMRI_WM_LR tfMRI_GAMBLING_RL tfMRI_GAMBLING_LR tfMRI_MOTOR_RL tfMRI_MOTOR_LR%tfMRI_LANGUAGE_RL tfMRI_LANGUAGE_LR tfMRI_SOCIAL_RL tfMRI_SOCIAL_LR tfMRI_RELATIONAL_RL tfMRI_RELATIONAL_LR tfMRI_EMOTION_RL tfMRI_EMOTION_LR"

# If you wish to run "multi-run" (concatenated) FIX, specify the name to give the concatenated output files
# If you wish to run "multi-run" (concatenated) FIX, specify the names to give the concatenated output files
# In this case, all the runs included in ${fMRINames} become the input to multi-run FIX
# Otherwise, leave ConcatName empty (in which case "single-run" FIX is executed serially on each run in ${fMRINames})
ConcatName=""
# ConcatName="rfMRI_REST1_2_LR_RL" ## Do NOT include spaces!
# Otherwise, leave ConcatNames empty (in which case "single-run" FIX is executed serially on each run in ${fMRINames})
ConcatNames=""
ConcatNames="tfMRI_WM_GAMBLING_MOTOR_RL_LR tfMRI_LANGUAGE_SOCIAL_RELATIONAL_EMOTION_RL_LR" ## Use space (or @) to separate concatenation groups

# set temporal highpass full-width (2*sigma) to use, in seconds
# set temporal highpass full-width (2*sigma) to use, in seconds, cannot be 0 for single-run FIX
bandpass=2000
# MR FIX also supports 0 for a linear detrend, or "pdX" for a polynomial detrend of order X
# e.g., bandpass=pd1 is linear detrend (functionally equivalent to bandpass=0)
# bandpass=pd2 is a quadratic detrend
bandpass=0

# set whether or not to regress motion parameters (24 regressors)
# out of the data as part of FIX (TRUE or FALSE)
Expand All @@ -157,53 +163,69 @@ main() {
# establish queue for job submission
QUEUE="-q hcp_priority.q"
if [ "${RunLocal}" == "TRUE" ]; then
queuing_command=""
queuing_command=()
else
queuing_command="${FSLDIR}/bin/fsl_sub ${QUEUE}"
queuing_command=("${FSLDIR}/bin/fsl_sub" "${QUEUE}")
fi

DIR=$(pwd)

for Subject in ${Subjlist}; do
echo ${Subject}

ResultsFolder="${StudyFolder}/${Subject}/MNINonLinear/Results"
cd ${ResultsFolder}

if [ -z ${ConcatName} ]; then
if [ -z ${ConcatNames} ]; then
# single-run FIX
FixScript=${HCPPIPEDIR}/ICAFIX/hcp_fix

for fMRIName in ${fMRINames}; do
fMRINamesFlat=$(echo ${fMRINames} | sed 's/[@%]/ /g')

for fMRIName in ${fMRINamesFlat}; do
echo " ${fMRIName}"

InputFile="${fMRIName}/${fMRIName}"
InputFile="${ResultsFolder}/${fMRIName}/${fMRIName}"

cmd="${queuing_command} ${FixScript} ${InputFile} ${bandpass} ${domot} ${TrainingData} ${FixThreshold}"
echo "About to run: ${cmd}"
${cmd}
cmd=("${queuing_command[@]}" "${FixScript}" "${InputFile}" ${bandpass} ${domot} "${TrainingData}" ${FixThreshold})
echo "About to run: ${cmd[*]}"
"${cmd[@]}"
done

else
# multi-run FIX
FixScript=${HCPPIPEDIR}/ICAFIX/hcp_fix_multi_run
ConcatNameFile="${ConcatName}/${ConcatName}"

InputFile=""
for fMRIName in ${fMRINames}; do
InputFile+="${fMRIName}/${fMRIName}@"
#need arrays to sanity check number of concat groups
IFS=' @' read -a concatarray <<< "${ConcatNames}"
IFS=% read -a fmriarray <<< "${fMRINames}"

if ((${#concatarray[@]} != ${#fmriarray[@]})); then
echo "ERROR: number of names in ConcatNames does not match number of fMRINames groups"
exit 1
fi

for ((i = 0; i < ${#concatarray[@]}; ++i))
do
ConcatName="${concatarray[$i]}"
fMRINamesGroup="${fmriarray[$i]}"
# multi-run FIX
FixScript=${HCPPIPEDIR}/ICAFIX/hcp_fix_multi_run
ConcatFileName="${ResultsFolder}/${ConcatName}/${ConcatName}"

IFS=' @' read -a namesgrouparray <<< "${fMRINamesGroup}"
InputFile=""
for fMRIName in "${namesgrouparray[@]}"; do
if [[ "$InputFile" == "" ]]; then
InputFile="${ResultsFolder}/${fMRIName}/${fMRIName}"
else
InputFile+="@${ResultsFolder}/${fMRIName}/${fMRIName}"
fi
done

echo " InputFile: ${InputFile}"

cmd=("${queuing_command[@]}" "${FixScript}" "${InputFile}" ${bandpass} "${ConcatFileName}" ${domot} "${TrainingData}" ${FixThreshold})
echo "About to run: ${cmd[*]}"
"${cmd[@]}"
done

echo " InputFile: ${InputFile}"

cmd="${queuing_command} ${FixScript} ${InputFile} ${bandpass} ${ConcatFileName} ${domot} ${TrainingData} ${FixThreshold}"
echo "About to run: ${cmd}"
${cmd}

fi

cd ${DIR}

done
} # main()

Expand Down

0 comments on commit 5488769

Please sign in to comment.