-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·239 lines (191 loc) · 8.81 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /bin/bash
#
# Flywheel 2017
#
#
################################################################################
# DEFINE VARIABLES
FLYWHEEL_BASE=/flywheel/v0
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
INPUT_DIR=$FLYWHEEL_BASE/input
GEAR_OUTPUT_DIR=$FLYWHEEL_BASE/output
WORKING_DIR=$GEAR_OUTPUT_DIR/work/
CONTAINER='[flywheel/xcpengine]'
XCP_OUTPUT_DIR=$GEAR_OUTPUT_DIR/
FMRIPREPDIR=$INPUT_DIR/fmriprepdir
IMG=$INPUT_DIR/img
M0=$INPUT_DIR/m0
STRUCT=$INPUT_DIR/antsct
DESIGNFILE=$INPUT_DIR/designfile
CUSTOM_REGRESSORS=$INPUT_DIR/custom_regressors
TASKFILE=$INPUT_DIR/taskfile
BIDS_DIR=$GEAR_OUTPUT_DIR/bids_dataset
################################################################################
# DISPLAY XCPENGINE DOCKERFILE FOR BUILD INFO
echo -e "$CONTAINER BEGIN XCPENGINE DOCKERFILE\n"
cat ${FLYWHEEL_BASE}/*Dockerfile
echo -e "$CONTAINER END XCPENGINE DOCKERFILE \n"
################################################################################
# CONFIGURE ENV
source ${FLYWHEEL_BASE}/docker-env.sh
##############################################################################
# Parse configuration
# get the inputs files
jq1=$XCPEDIR/thirdparty/jq/jq-linux64
if [[ -f $CONFIG_FILE ]]; then
eval $($jq1 -r '.inputs | to_entries[] | "XCP_INPUT_\(.key)=\(.value.location.path)"' $CONFIG_FILE)
echo "$(set | grep XCP_INPUT)"
fi
function parse_config {
CONFIG_FILE=$FLYWHEEL_BASE/config.json
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
if [[ -f $CONFIG_FILE ]]; then
echo "$(cat $CONFIG_FILE | $jq1 -r '.config.'$1)"
else
CONFIG_FILE=$MANIFEST_FILE
echo "$(cat $CONFIG_FILE | $jq1 -r '.config.'$1'.default')"
fi
}
analysis_type="$(parse_config 'analysis_type')"
task_name="$(parse_config 'task_name')"
ses="$(parse_config 'session')"
space="$(parse_config 'space')"
apikey=$( cat ${FLYWHEEL_BASE}/config.json | $jq1 -r '.inputs.api_key.key')
destinationid=$( cat ${FLYWHEEL_BASE}/config.json | $jq1 -r '.destination.id')
subjlabel=$( python -c "import flywheel; fw=flywheel.Client('${apikey}'); b=fw.get('${destinationid}'); print(fw.get(b.parents.subject).label)")
echo $analysis_type
echo $task_name
echo $ses
echo $subjlabel
# task name to be work on
################################################################################
# check the design file
if [[ -z "${DESIGNFILE}/*dsn" ]]; then
echo -e "The design file is not supply or not available \n!"
echo -e " SEE https://xcpengine.readthedocs.io/config/design.html#designfile \n "
exit 1
fi
cohortfile=${XCP_OUTPUT_DIR}/cohortfile.csv
# check fmriprep directory
if [[ $(ls -f $FMRIPREPDIR/*zip 2>/dev/null ) ]]; then
echo "unzip FMRIPREP"
fmriprepout=$(ls -f $FMRIPREPDIR/*zip )
mkdir -p $XCP_OUTPUT_DIR/fmriprep
unzip -q ${fmriprepout} -d $XCP_OUTPUT_DIR/fmriprep 2>/dev/null
rm -rf ${cohortfile}
echo id0,id1,id2,id3,img > ${cohortfile} # 3 ids for now
if [[ -z $task_name ]]; then
im=$(ls -f $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*/ses-${ses}/func/*${space}*desc-preproc_bold.nii.gz 2>/dev/null)
base1=$(basename $(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*))
#base2=$(basename $(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*/ses-*))
for i in $im
do
id0=$(echo $i |cut -d'_' -f3)
id1=$(echo $i |cut -d'_' -f4)
id3=$(basename $(dirname $(dirname $i)))
echo $base1,$id3,$id0,$id1,$i >> $cohortfile
done
else
im=$(ls -f $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*/ses-${ses}/func/*${task_name}*${space}*_desc-preproc_bold.nii.gz 2>/dev/null)
taskfile=$(ls -f $TASKFILE/*zip 2>/dev/null)
mkdir -p $XCP_OUTPUT_DIR/taskfile
unzip -q ${taskfile} -d $XCP_OUTPUT_DIR/taskfile/ 2>/dev/null
mv $XCP_OUTPUT_DIR/taskfile/*/* $XCP_OUTPUT_DIR/taskfile/ 2>/dev/null
if [[ -f $(ls -f $XCP_OUTPUT_DIR/taskfile/*json 2>/dev/null) ]]; then
cp $FLYWHEEL_BASE/template.fsf $XCP_OUTPUT_DIR/template.fsf
python ${FLYWHEEL_BASE}/taskfslmodel.py -t ${XCP_OUTPUT_DIR}/template.fsf -f $XCP_OUTPUT_DIR/taskfile
id0=$(echo $im |cut -d'_' -f3)
id1=$(echo $im |cut -d'_' -f4)
base1=$(basename $(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*))
base2=$(basename $(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*/ses-${ses}))
/opt/fsl-5.0.10/bin/feat_model ${XCP_OUTPUT_DIR}/template
python ${FLYWHEEL_BASE}/generate_taskfsfmodel.py -i ${im} -t ${XCP_OUTPUT_DIR}/template.mat -o ${XCP_OUTPUT_DIR}
rm -rf $cohortfile
echo id0,id1,id2,id3,img,task_design,confound2_custom > $cohortfile
echo $base1,$base2,$id0,$id1,$im,$XCP_OUTPUT_DIR/template.fsf,${XCP_OUTPUT_DIR}/taskhrfconvolved.txt >> $cohortfile
if [[ -f $(ls -f ${CUSTOM_REGRESSORS}/*txt 2>/dev/null) ]]; then #this will combine custom and task
custom_reg=$(ls -f ${CUSTOM_REGRESSORS}/*txt)
python ${FLYWHEEL_BASE}/generate_taskfsfmodel.py -i ${imp} -c ${custom_reg} -t ${XCP_OUTPUT_DIR}/template.mat -o ${XCP_OUTPUT_DIR}
rm -rf $cohortfile
echo id0,id1,id2,id3,img,task_design,confound2_custom, > $cohortfile
echo $base1,$base2,$id0,$id1,$im,$XCP_OUTPUT_DIR/template.fsf,${XCP_OUTPUT_DIR}/taskhrfconvolved.txt >> $cohortfile
fi
elif [[ -f $(ls -f ${CUSTOM_REGRESSORS}/*txt 2>/dev/null) ]] && [[ ! -f $(ls -f $XCP_OUTPUT_DIR/taskfile/*json 2>/dev/null) ]]; then
custom_reg=$(ls -f ${CUSTOM_REGRESSORS}/*txt)
id0=$(echo $im |cut -d'_' -f3)
id1=$(echo $im |cut -d'_' -f4)
base1=$(basename $(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*))
base2=$(basename $(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*/ses-${ses}))
custom_reg=$(ls -f ${CUSTOM_REGRESSORS}/*txt)
python ${FLYWHEEL_BASE}/generate_taskfsfmodel.py -i ${im} -c ${custom_reg} -o ${XCP_OUTPUT_DIR}
rm -rf $cohortfile
echo id0,id1,id2,id3,img,confound2_custom, > $cohortfile
echo $base1,$base2,$id0,$id1,$im,${XCP_OUTPUT_DIR}/taskhrfconvolved.txt >> $cohortfile
fi
fi
fi
if [[ -f $(ls -f ${IMG}/*nii.gz 2>/dev/null ) ]]; then
img=$(ls -f ${IMG}/*nii.gz)
echo " Ensure appropriate design file is supplied \n"
anatdir=$(ls -d $XCP_OUTPUT_DIR/fmriprep/*/fmriprep/sub-*/anat/ 2>/dev/null )
# create bids directory to get fielmap directory if is there
python ${FLYWHEEL_BASE}/create_archive_fw_heudiconv.py
if [[ -d $(ls -d ${BIDS_DIR}/*/*/fmap ) ]]; then
fmapdir=${BIDS_DIR}/*/*/fmap
fi
if [[ -d ${anatdir} ]]; then
rm -rf ${cohortfile}
echo "\n"
echo id0,img,anatdir,dico_fmapdir > $cohortfile
echo sub-${subjlabel},${img},${anatdir},$fmapdir >> $cohortfile
if [[ -f $(ls -f ${M0}/*nii.gz 2>/dev/null ) ]]; then
m0=$(ls -f ${M0}/*nii.gz )
rm -rf ${cohortfile}
echo id0,img,anatdir,m0,dico_fmapdir > $cohortfile
echo sub-${subjlabel},${mg},${anatdir},${m0},$fmapdir >> $cohortfile
fi
elif [[ -f $(ls -f ${STRUCT}/*zip 2>/dev/null ) ]]; then
rm -rf ${cohortfile}
anat=$(ls -f ${STRUCT}/*zip)
unzip -q ${anat} -d $XCP_OUTPUT_DIR/antsct
antsctdir=$(ls -d $XCP_OUTPUT_DIR/antsct/xcpengine/*/struc)
echo id0,img,antsct,dico_fmapdir > $cohortfile
echo sub-${subjlabel},${img},${antsctdir},$fmapdir >> $cohortfile
if [[ -f $(ls -f ${M0}/*nii.gz) ]]; then
m0=$(ls -f ${M0}/*nii.gz )
rm -rf ${cohortfile}
echo id0,img,antsct,dico_fmapdir > $cohortfile
echo sub-${subjlabel},${img},${antsctdir},$fmapdir >> $cohortfile
fi
else
rm -rf ${cohortfile}
echo id0,img,dico_fmapdir > $cohortfile
echo sub-${subjlabel},${img},$fmapdir >> $cohortfile
fi
fi
################################################################################
# RUN XCPEENGINE now
designfile=$(ls -f $DESIGNFILE/*dsn)
time /xcpEngine/xcpEngine \
-d $designfile \
-c $cohortfile \
-o ${XCP_OUTPUT_DIR}/xcpengine \
-i ${WORKING_DIR}
rm -rf $XCP_OUTPUT_DIR/fmriprep
cd $XCP_OUTPUT_DIR/
zip -r -q ${XCP_OUTPUT_DIR}/xcpEngineouput_${analysis_type}.zip xcpengine
rm -rf $XCP_OUTPUT_DIR/xcpengine 2>/dev/null
rm -rf ${TASKFILE} 2>/dev/null
rm -rf $XCP_OUTPUT_DIR/*txt $XCP_OUTPUT_DIR/*json 2>/dev/null
rm -rf $XCP_OUTPUT_DIR/taskfile 2>/dev/null
XCPENGINE_EXITSTATUS=0
################################################################################
# CLEANUP OUTPUTS
echo "$CONTAINER completed the execution. Check the log files for anyg errors.".
echo ' '
echo ' cleaning the working directory'
cd "$GEAR_OUTPUT_DIR"
chmod -R 777 $GEAR_OUTPUT_DIR
# Clean up
rm -rf "$WORKING_DIR"
exit $XCPENGINE_EXITSTATUS