From 0cfa7052ab15d9a206f8c3de7da8e29bbd163e98 Mon Sep 17 00:00:00 2001 From: Koen Helwegen Date: Tue, 21 Mar 2023 13:36:45 +0100 Subject: [PATCH 1/3] add group mode to structural pipeline --- make.template | 23 ++++---- src/structural_pipeline/structural_pipeline.m | 58 +++++++++++++++++-- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/make.template b/make.template index 3babaa3..64e8600 100644 --- a/make.template +++ b/make.template @@ -22,15 +22,16 @@ usage() echo -n "structural_pipeline for structural connectivity reconstruction. Synopsis: - structural_pipeline -s SUBJECTDIR -m MCRROOT [OPTION]... - structural_pipeline [STEPS]... -s SUBJECTDIR -m MCRROOT [OPTION]... + structural_pipeline -s SUBJECTSELECTION -m MCRROOT [OPTION]... + structural_pipeline [STEPS]... -s SUBJECTSELECTION -m MCRROOT [OPTION]... Description: CATO structural connectivity reconstruction pipeline. Execution of specific pipeline steps can be requested on the command line by the STEPS. Required arguments: - -s, --subjectDir Directory with a subject's Freesurfer and DWI data + -s, --subjectSelection Directory with a subject's Freesurfer and DWI data + or file with list of subject directories. -m, --mcrRoot Installation directory of MATLAB runtime Optional arguments: @@ -53,12 +54,12 @@ networkSteps= while [ -n "$1" ]; do shopt -s nocasematch case "$1" in - -s | --subjectDir) - subjectDir=$2 + -s | --subjectSelection) + subjectSelection=$2 shift 2 ;; - --subjectDir=*) - subjectDir=${1#*=} + --subjectSelection=*) + subjectSelection=${1#*=} shift ;; -m | --mcrRoot) @@ -118,9 +119,9 @@ main() { parse_input "$@" - if [[ ! -d "$subjectDir" ]] + if [[ ! -e "$subjectSelection" ]] then - echo "Subject directory argument missing / not a directory." + echo "Subject selection argument missing / does not exist." exit 1 fi @@ -158,9 +159,9 @@ main() { # Add requested reconstruction steps if specified. if [ ! -z "$networkSteps" ]; then - $target "$subjectDir" "${NameValueParams[@]}" reconstructionSteps ${networkSteps// /,} + $target "$subjectSelection" "${NameValueParams[@]}" reconstructionSteps ${networkSteps// /,} else - $target "$subjectDir" "${NameValueParams[@]}" + $target "$subjectSelection" "${NameValueParams[@]}" fi # clean up and exit diff --git a/src/structural_pipeline/structural_pipeline.m b/src/structural_pipeline/structural_pipeline.m index 01062f3..8bd3d08 100644 --- a/src/structural_pipeline/structural_pipeline.m +++ b/src/structural_pipeline/structural_pipeline.m @@ -1,10 +1,13 @@ -function configParams = structural_pipeline(subjectDir, varargin) +function configParams = structural_pipeline(subjectSelection, varargin) % STRUCTURAL_PIPELINE Reconstruct structural connectivity. % % structural_pipeline(SUBJECTDIR) reconstructs structural connectivity of % a subjects T1 and DWI data in the SUBJECTDIR directory using default % parameters. % +% structural_pipeline(SUBJECTLISTFILE) takes a file in which each line is +% a subject directory, and calls structural_pipeline for each directory. +% % structural_pipeline(SUBJECTDIR, 'Param1', VAL1, 'Param2', VAL2, ...) % specifies additional parameter name-value pairs chosen from: % @@ -61,19 +64,62 @@ clear toolboxInstalled % PARSE INPUT -[subjectDir, varargin{:}] = convertStringsToChars(subjectDir, varargin{:}); +[subjectSelection, varargin{:}] = convertStringsToChars(subjectSelection, varargin{:}); -assert(ischar(subjectDir), ... +assert(ischar(subjectSelection), ... 'CATO:structural_pipeline:subjectDirNotText', ... 'subjectDir must be a row vector of characters or string scalar.'); -assert(isdir(subjectDir), ... +assert(isdir(subjectSelection) | isfile(subjectSelection), ... 'CATO:structural_pipeline:subjectDirNotDir', ... - 'subjectDir (%s) is not a directory', subjectDir); + 'subjectSelection (%s) is not a directory or file', subjectSelection); -[configFile, runType, configParamsCl] = parseVarargin(varargin{:}); +%% Group mode + +if isfile(subjectSelection) + subjectListFile = subjectSelection; + fprintf('\n--------strarting group run----------\n') + + % read file + fi = fopen(subjectListFile); + + fprintf('Processing subjects in %s\n\n', subjectListFile); + + % read line by line, keep track of processed/failed/unrecognized + subjectDir = fgetl(fi); + processedSubjects = 0; + failedSubjects = 0; + unrecognizedSubjects = 0; + + while ischar(subjectDir) + if isdir(subjectDir) + try + configParams = structural_pipeline(subjectDir, varargin{:}); + processedSubjects = processedSubjects + 1; + catch + failedSubjects = failedSubjects + 1; + end + else + unrecognizedSubjects = unrecognizedSubjects + 1; + warning('CATO:structural_pipeline:subjectDirNotDir', ... + 'subjectDir (%s) is not a directory', subjectDir); + end + subjectDir = fgetl(fi); + end + + fprintf('\n---------group run finished----------\n') + fprintf('Processed %d subjects, %d failed, %d unrecognized\n\n', ... + processedSubjects, failedSubjects, unrecognizedSubjects); + + fclose(fi); + return +end + +subjectDir = subjectSelection; %% Setup +[configFile, runType, configParamsCl] = parseVarargin(varargin{:}); + % Setup path (note this is done before log files or other stuff). oldPath = pwd; From aefd532977f03abb01896c784f406da8352a4176 Mon Sep 17 00:00:00 2001 From: Koen Helwegen Date: Tue, 21 Mar 2023 13:48:17 +0100 Subject: [PATCH 2/3] make template fc/sc agnostic --- make.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make.template b/make.template index 64e8600..6805bb0 100644 --- a/make.template +++ b/make.template @@ -30,7 +30,7 @@ usage() specific pipeline steps can be requested on the command line by the STEPS. Required arguments: - -s, --subjectSelection Directory with a subject's Freesurfer and DWI data + -s, --subjectSelection Directory with a subject's Freesurfer and MRI data or file with list of subject directories. -m, --mcrRoot Installation directory of MATLAB runtime From afc9b7d7dfb2a2938e1115a93afc6427d8e36cf4 Mon Sep 17 00:00:00 2001 From: Koen Helwegen Date: Tue, 21 Mar 2023 13:48:38 +0100 Subject: [PATCH 3/3] add group mode to functional pipeline --- src/functional_pipeline/functional_pipeline.m | 60 ++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/functional_pipeline/functional_pipeline.m b/src/functional_pipeline/functional_pipeline.m index 7729113..70d43f0 100644 --- a/src/functional_pipeline/functional_pipeline.m +++ b/src/functional_pipeline/functional_pipeline.m @@ -1,10 +1,13 @@ -function configParams = functional_pipeline(subjectDir, varargin) +function configParams = functional_pipeline(subjectSelection, varargin) % FUNCTIONAL_PIPELINE Reconstruct structural connectivity. % % functional_pipeline(SUBJECTDIR) reconstructs functional connectivity of % a subjects T1 and rs-fMRI data in the SUBJECTDIR directory using default % parameters. % +% functional_pipeline(SUBJECTLISTFILE) takes a file in which each line is +% a subject directory, and calls functional_pipeline for each directory. +% % functional_pipeline(SUBJECTDIR, 'Param1', VAL1, 'Param2', VAL2, ...) % specifies additional parameter name-value pairs chosen from: % @@ -62,20 +65,63 @@ % PARSE INPUT -[subjectDir, varargin{:}] = convertStringsToChars(subjectDir, varargin{:}); +[subjectSelection, varargin{:}] = convertStringsToChars(subjectSelection, varargin{:}); -assert(ischar(subjectDir), ... +assert(ischar(subjectSelection), ... 'CATO:functional_pipeline:subjectDirNotText', ... 'subjectDir must be a row vector of characters or string scalar.'); -assert(isdir(subjectDir), ... +assert(isdir(subjectSelection) | isfile(subjectSelection), ... 'CATO:functional_pipeline:subjectDirNotDir', ... - 'subjectDir (%s) is not a directory.', subjectDir); + 'subjectDir (%s) is not a directory.', subjectSelection); -[configFile, runType, configParamsCl] = parseVarargin(varargin{:}); -% parseVarargin does also error handling. +%% Group mode + +if isfile(subjectSelection) + subjectListFile = subjectSelection; + fprintf('\n--------strarting group run----------\n') + + % read file + fi = fopen(subjectListFile); + + fprintf('Processing subjects in %s\n\n', subjectListFile); + + % read line by line, keep track of processed/failed/unrecognized + subjectDir = fgetl(fi); + processedSubjects = 0; + failedSubjects = 0; + unrecognizedSubjects = 0; + + while ischar(subjectDir) + if isdir(subjectDir) + try + configParams = functional_pipeline(subjectDir, varargin{:}); + processedSubjects = processedSubjects + 1; + catch + failedSubjects = failedSubjects + 1; + end + else + unrecognizedSubjects = unrecognizedSubjects + 1; + warning('CATO:functional_pipeline:subjectDirNotDir', ... + 'subjectDir (%s) is not a directory', subjectDir); + end + subjectDir = fgetl(fi); + end + + fprintf('\n---------group run finished----------\n') + fprintf('Processed %d subjects, %d failed, %d unrecognized\n\n', ... + processedSubjects, failedSubjects, unrecognizedSubjects); + + fclose(fi); + return +end + +subjectDir = subjectSelection; %% Setup +[configFile, runType, configParamsCl] = parseVarargin(varargin{:}); +% parseVarargin does also error handling. + % Setup path (note this is done before log files or other stuff). oldPath = pwd;