-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstroke_preproc.def
295 lines (261 loc) · 9.38 KB
/
stroke_preproc.def
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
Bootstrap: docker
From: ubuntu:18.04
%setup
# CIVET 2.1.1 binaries for Ubuntu 18.04
# E.g. from https://github.com/aces/CIVET_Full_Project/releases/download/CIVET_2_1_1/civet-2.1.1-binaries-ubuntu-18.tar.gz
# Assumed to be in local directory; change this path to appropriate location if it was extracted elsewhere.
IMPORT_DIR="civet-2.1.1-binaries-ubuntu-18/Linux-x86_64/"
rsync -r -L ${IMPORT_DIR}/* ${SINGULARITY_ROOTFS}/
cp ./nii_slice.py ${SINGULARITY_ROOTFS}/nii_slice.py
cp ./mask_diff.py ${SINGULARITY_ROOTFS}/mask_diff.py
cp ./requirements.txt ${SINGULARITY_ROOTFS}/requirements.txt
%post
# Perl dependencies for CIVET
apt-get update
apt-get install -y python3.8 python3-pip
python3.8 -m pip install --upgrade pip
python3.8 -m pip install -r /requirements.txt
apt-get install -y perl perl-base perl-depends perl-modules-5.26
chmod -R o+rx CIVET-2.1.1
chmod o+rx /nii_slice.py
chmod -R o+rx /bin/
chmod -R o+rx /lib
chmod -R o+rx /perl
chmod -R o+rx /share
# CIVET's init.sh
MNIBASEPATH=/
CIVET=CIVET-2.1.1
echo "export LC_CTYPE=en_US.UTF-8" >> ${SINGULARITY_ENVIRONMENT}
echo "export LC_ALL=C" >> ${SINGULARITY_ENVIRONMENT}
echo "export PATH=\"$MNIBASEPATH/$CIVET/progs:$MNIBASEPATH/bin:\"$PATH" >> ${SINGULARITY_ENVIRONMENT}
echo "export LD_LIBRARY_PATH=\"$MNIBASEPATH/lib:\"$LD_LIBRARY_PATH" >> ${SINGULARITY_ENVIRONMENT}
echo "export MNI_DATAPATH=\"$MNIBASEPATH/share:\"$MNI_DATAPATH" >> ${SINGULARITY_ENVIRONMENT}
echo "export PERL5LIB=\"$MNIBASEPATH/perl:\"$PERL5LIB" >> ${SINGULARITY_ENVIRONMENT}
echo "export R_LIBS=\"$MNIBASEPATH/R_LIBS:\"$R_LIBS" >> ${SINGULARITY_ENVIRONMENT}
echo "export VOLUME_CACHE_THRESHOLD=-1" >> ${SINGULARITY_ENVIRONMENT}
echo "export BRAINVIEW=\"$MNIBASEPATH/share/brain-view\"" >> ${SINGULARITY_ENVIRONMENT}
echo "export MINC_FORCE_V2=1" >> ${SINGULARITY_ENVIRONMENT}
echo "export MINC_COMPRESS=4" >> ${SINGULARITY_ENVIRONMENT}
echo "export CIVET_JOB_SCHEDULER=DEFAULT" >> ${SINGULARITY_ENVIRONMENT}
# Add CIVET scrips to PATH
echo 'export PATH=${PATH}:/CIVET-2.1.1/' >> ${SINGULARITY_ENVIRONMENT}
%runscript
#!/bin/bash
# This script takes in a BIDS-compatible directory and applies some of the CIVET scripts to the specified data.
#set -e -u
helpstr="$(basename $0) [-h] --subject DIR --output DIR [--session SES] [--model PATH] [--ignore-t1] [--ignore-t2] [--ignore-pd] [-q, --quiet] [--intermediate DIR]
where:
-h,--help Show this message.
--subject DIR BIDS-compliant subject directory. If defined, supercedes --cohort. Either --subject or --cohort must be specified.
--output DIR Directory to place pipeline output.
--lesion-t1 PATH Optional. Path to T1 lesion mask to co-register.
--lesion-flair PATH Optional. Path to FLAIR lesion mask to co-register.
--lesion-dwi PATH Optional. Path to DWI lesion mask to co-register.
--qc DIR Optional. Directory where to place QC image.
--mask-overlap-db PATH Optional. Path to the SQL database containing the mask overlap results. The file will be created if it doesn't exist. If not set, mask overlap will not be computed.
--mask-overlap-csv PATH Optional. If set, will create a CSV copy of the DB. Requires --mask-overlap-db to be set.
--session SES Specify the session to analyze.
--ignore-t1 Optional. Ignore T1 image. Default: False.
--ignore-t2 Optional. Ignore T2 image. Default: False.
--ignore-pd Optional. Ignore PD image. Default: False.
-q, --quiet Optional. Suppress text output. Default: False.
"
# Variable instantiation
session_list=()
ignore_t1=0
ignore_t2=0
ignore_pd=0
quiet=0
keep=0
lesion_list=()
model="/share/mni-models/mni_icbm152_t1_tal_nlin_sym_09a.mnc"
model_mask="/share/mni-models/mni_icbm152_t1_tal_nlin_sym_09a_mask.mnc"
model_label="MNI152NLin2009aSym"
while (( "$#" )); do
case "$1" in
-h|--help)
echo "${helpstr}"
exit 0
;;
--subject)
subject_dir=`readlink -f "${2}"`
buf="${subject_dir##*/}"
subject_id="${buf##sub-}"
shift 2
;;
--output)
output=`readlink -f "${2}"`
mkdir -p ${output}
shift 2
;;
--input_list)
input_list_file="${2}"
shift 2
;;
--ignore-t1)
ignore_t1=1
shift 1
;;
--ignore-t2)
ignore_t2=1
shift 1
;;
--ignore-pd)
ignore_pd=1
shift 1
;;
-q|--quiet)
quiet=1
shift 1
;;
--intermediate)
keep=1
inter_dir="${2}"
shift 2
;;
--qc)
qc_dir="${2}"
shift 2
;;
--civet-opts)
shift 1
civet_opts="$@"
break
;;
--session)
session="${2}"
# Sanity check if user inputs "ses-1/" instead of "1"
if [ "${session[@]: -1}" == "/" ]; then
session=${session[@]::$((${#session[@]}-1))}
fi
session="${session#ses-}"
shift 2
;;
--lesion-t1)
lesion_t1="${2}"
shift 2
;;
--lesion-flair)
lesion_flair="${2}"
shift 2
;;
--lesion-dwi)
lesion_dwi="${2}"
shift 2
;;
--mask-overlap-db)
mask_db="${2}"
shift 2
;;
--mask-overlap-csv)
mask_csv="${2}"
shift 2
;;
*)
>&2 echo "Unexpected argument: ${1}"
exit 1
;;
esac
done
# Define quiet echo
qecho () {
if [ "${quiet}" -eq 0 ]; then
echo "$@"
fi
}
# Define temo working dir
tmpdir=`mktemp -d`
qecho "Starting execution."
# Check session
if [ -z "${session}" ]; then
>&2 echo "Parameter --session is undefined"
exit 1
elif [ ! -d "${subject_dir}/ses-${session}" ]; then
>&2 echo "Session ${session} is not defined in subject ${subject_id}"
exit 1
fi
# Get T1 image
anat_pathlist="${subject_dir}/ses-${session}/anat"
buf=(`find ${anat_pathlist} -name "*T1w.nii*"`)
t1_img=${buf[0]}
bids_dir_out="${output}/sub-${subject_id}/ses-${session}/anat"
mkdir -p "${bids_dir_out}"
# Check if we need to gunzip T1 image; nii2mnc assumes .nii and not .nii.gz
t1_mnc="${tmpdir}/t1.mnc"
if [ ".gz" == "${t1_img[@]: -3}" ]; then
gunzip -c -k "${t1_img}" > "${tmpdir}/t1.nii"
nii2mnc -quiet "${tmpdir}/t1.nii" "${t1_mnc}"
else
nii2mnc -quiet "${t1_img}" "${t1_mnc}"
fi
### Registration
qecho "Registering ${t1_mnc} to ${model}..."
transformation="${bids_dir_out}/sub-${subject_id}_ses-${session}_desc-T1${model_label}.xfm"
tmptransf="${tmpdir}/init.xfm"
t1_blur="${t1_mnc%.mnc}"
model_blur="${tmpdir}/model"
qecho "Saving to ${transformation}"
perl /CIVET-2.1.1/progs/bestlinreg.pl -clobber -lsq12 -nmi "${t1_mnc}" "${model}" "${transformation}"
### NUC
# First try do brain extraction using transformation
# Get model mask to native space
qecho "Performing non-uniformity correction"
mask_native="${tmpdir}/mask_to_native.mnc"
mincresample -clobber -quiet -transformation "${transformation}" -invert_transformation -like "${t1_mnc}" -nearest_neighbour "${model_mask}" "${mask_native}"
nu_correct -quiet -clobber -tmpdir "${tmpdir}/nu_correct/" -mask "${mask_native}" -distance 200 -mapping_dir "${tmpdir}" "${t1_mnc}" "${tmpdir}/nu_correct/t1_nuc_tmp0.mnc"
qecho "Looping..."
for i in `seq 0 2`; do
qecho "Loop ${i}"
nu_correct -quiet -clobber -tmpdir "${tmpdir}/nu_correct/" -mask "${mask_native}" -distance 200 -mapping_dir "${tmpdir}" "${tmpdir}/nu_correct/t1_nuc_tmp${i}.mnc" "${tmpdir}/nu_correct/t1_nuc_tmp$((i+1)).mnc"
done
### Transform nuc_image to model
qecho "Resampling."
mincresample -clobber -quiet -transformation "${transformation}" -like "${model}" -trilinear "${tmpdir}/nu_correct/t1_nuc_tmp${i}.mnc" "${tmpdir}/resampled.mnc"
### Normalize
qecho "Normalizing."
inormalize -quiet -clobber -range 1.0 -model "${model}" "${tmpdir}/resampled.mnc" "${tmpdir}/norm_reg.mnc"
# Convert to nii & gzip
qecho "Finalizing T1w image"
bids_final_name="sub-${subject_id}_ses-${session}_space-${model_label}_desc-T1FinalResampledNorm.nii.gz"
qecho "Converting ${tmpdir}/norm_reg.mnc to ${tmpdir}/norm_reg.nii"
mnc2nii "${tmpdir}/norm_reg.mnc" "${tmpdir}/norm_reg.nii"
gzip "${tmpdir}/norm_reg.nii"
mv "${tmpdir}/norm_reg.nii.gz" "${bids_dir_out}/${bids_final_name}"
### Apply transform to lesion mask
qecho "Transforming lesion mask."
lesion_filename=`basename ${lesion_t1}`
lesion_pre="${lesion_filename%space-*}"
lesion_post="${lesion_filename#*space-*_}"
lesion_outname="${lesion_pre}space-${model_label}_${lesion_post}"
if [ -n "${lesion_t1}" ]; then
ext="${lesion_t1[*]: -3}"
if [ "${ext}" == ".gz" ]; then
qecho "Gunzipping ${lesion_t1}"
lesion_file="${tmpdir}/t1_lesion.nii"
gunzip -k -c "${lesion_t1}" > "${lesion_file}"
else
lesion_file="${lesion_t1}"
fi
qecho "Converting ${lesion_file} to mnc"
nii2mnc "${lesion_file}" "${tmpdir}/t1_lesion.mnc"
mincresample -clobber -quiet -transformation "${transformation}" -like "${model}" -nearest_neighbour "${tmpdir}/t1_lesion.mnc" "${tmpdir}/lesion_reg.mnc"
mnc2nii "${tmpdir}/lesion_reg.mnc" "${tmpdir}/lesion_reg.nii"
gzip "${tmpdir}/lesion_reg.nii"
mv "${tmpdir}/lesion_reg.nii.gz" "${bids_dir_out}/${lesion_outname}"
fi
### QC
qc_name="${bids_dir_out}/sub-${subject_id}_ses-${session}_desc-QC.png"
model_mask_nii="${tmpdir}/model_mask.nii"
mnc2nii "${model_mask}" "${model_mask_nii}"
if [ -n "${model_mask}" ]; then
python3.8 /nii_slice.py "${bids_dir_out}/${bids_final_name}" "${qc_name}" --mask_path ${bids_dir_out}/${lesion_outname} --nslices 9 --mask_template "${model_mask_nii}"
else
python3.8 /nii_slice.py "${bids_dir_out}/${bids_final_name}" "${qc_name}" --mask_path ${bids_dir_out}/${lesion_outname} --nslices 9
fi
if [ -n "${qc_dir}" ]; then
mkdir -p ${qc_dir}
cp ${qc_name} "${qc_dir}/`basename ${qc_name}`"
fi
if [ -n "${mask_db}" ]; then
python3.8 /mask_diff.py --mask_subject ${bids_dir_out}/${lesion_outname} --mask_ref ${model_mask} --database ${mask_db} --csv ${mask_csv}
fi