-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfeenics.nf
391 lines (274 loc) · 9.91 KB
/
feenics.nf
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
usage = file("${baseDir}/usage/feenics_usage")
bindings = [ "rewrite":"$params.rewrite",
"preartifact":"$params.preartifact",
"simg":"$params.simg"]
engine = new groovy.text.SimpleTemplateEngine()
toprint = engine.createTemplate(usage.text).make(bindings)
printhelp = params.help
if (!params.study || !params.out){
log.info("Insufficient specification")
log.info("Both --study and --out are required!")
printhelp = true
}
if (params.subjects) {
log.info("Subject file provided: $params.subjects")
}
//Overwrite
if (!params.rewrite) {
log.info("--rewrite flag not used, will skip existing outputs")
}else{
log.info("--rewrite flag is on! Will re-run on existing outputs!")
log.info("If you want to completely re-run, please delete subject output")
}
//If using pre-artifacted spirals
if (params.preartifact) {
log.info("--preartifact flag is on! Will rename files but not performing cleaning!")
}
if (printhelp){
print(toprint)
System.exit(0)
}
log.info("Output directory: $params.out")
// Main processes
//nifti directory
nifti_dir="/archive/data/$params.study/$params.nii"
//Inputs
input_sessions_dir = new File(nifti_dir)
input_sessions = input_sessions_dir.list()
//Outputs
output_sessions_dir = new File("$params.out/$params.application")
output_sessions = output_sessions_dir.list()
//Get subjects
if (params.subjects){
to_run = new File(params.subjects).readLines()
}else{
to_run = input_sessions
}
//Filter subjects based on outputs if rewrite isn't specified
if (!params.rewrite && output_sessions) {
to_run = to_run.findAll { !(output_sessions.contains(it)) }
}
//Filter for unavailable subjects if specified
if (params.subjects){
//Pull non-empty inputs
input_sub_channel = Channel.from(to_run)
.filter{ (it?.trim()) }
process split_invalid{
publishDir "$params.out/pipeline_logs/$params.application/", \
mode: 'copy', \
saveAs: { 'invalid_subjects.log' }, \
pattern: 'invalid'
input:
val subs from input_sub_channel.collect()
val available_subs from Channel.from(input_sessions).collect()
output:
file 'valid' into valid_subs
file 'invalid' optional true into invalid_subs
"""
#!/usr/bin/env python
import os
print(os.getcwd())
def nflist_2_pylist(x):
x = x.strip('[').strip(']')
x = [x.strip(' ').strip("\\n") for x in x.split(',')]
return x
#Process full BIDS subjects
bids_subs = nflist_2_pylist("$available_subs")
input_subs = nflist_2_pylist("$subs")
print(input_subs)
valid_subs = [x for x in input_subs if x in bids_subs]
invalid_subs = [x for x in input_subs if x not in valid_subs]
with open('valid','w') as f:
f.writelines("\\n".join(valid_subs))
if invalid_subs:
with open('invalid','w') as f:
f.writelines("\\n".join(invalid_subs))
f.write("\\n")
"""
}
input_subs = valid_subs
.splitText() { it.strip() }
}else{
input_subs = Channel.from(to_run)
}
if (params.preartifact) {
//Split into artifact and pre-artifact
preartifact = new File(params.preartifact).readLines()
feenics_channel = Channel.create()
preartifact_channel = Channel.create()
input_subs.choice(preartifact_channel,feenics_channel){preartifact.contains(it) ? 0 : 1}
input_subs = feenics_channel
//Now point preartifact to SPRL-COMB
preartifact_sprls = preartifact_channel
.map{ n -> [
n,
new File("$nifti_dir/$n/")
.list()
.findAll { it.contains("SPRL-COMB") }
]
}
.filter { !(it[1].isEmpty()) }
.map{ n,f -> [
n,
new File("$nifti_dir/$n/${f[0]}").toPath().toRealPath()
]
}
//Process non-artifacted SPRLS need to corrct PRS
process transfer_preartifact {
stageInMode "copy"
module 'freesurfer'
publishDir "$params.out/${params.application}", \
mode: 'move',
saveAs: { "$sub" }
input:
set val(sub), file("sprl.nii") from preartifact_sprls
output:
set val(sub), file("$sub") into preartifact_pseudo_out
shell:
'''
#!/bin/bash
#GZIP the nii file
gzip sprl.nii
#Fix orientation issue if exists
orientation=$(mri_info --orientation sprl.nii.gz)
if [ "$orientation" = "PRS" ]; then
fslorient -deleteorient sprl.nii.gz
fslswapdim sprl.nii.gz -x -y z sprl.nii.gz
fslorient -setqformcode 1 sprl.nii.gz
#Save list of reoriented scans
mkdir -p !{params.out}/feenics/
reorient_list=!{params.out}/feenics/reoriented.log
echo !{sub} >> $reorient_list
fi
#Set up output directory
mkdir !{sub}
mv sprl.nii.gz !{sub}/!{sub}.sprlCOMBINED.denoised.nii.gz
'''
}
}
sub_channel = input_subs
.map { n -> [
n,
new File("$nifti_dir/$n").list()
.findAll { it.contains("SPRL-IN") ||
it.contains("SPRL-OUT") }
.sort()
]
}
.filter { !it[1].isEmpty() }
.map { n -> [
n[0],
new File("$nifti_dir/${n[0]}/${n[1][0]}").toPath().toRealPath(),
new File("$nifti_dir/${n[0]}/${n[1][1]}").toPath().toRealPath()
]
}
/// ARTIFACT STREAM
//GZIP files
process gzip_nii {
stageInMode 'copy'
input:
set val(sub), file(sprlIN), file(sprlOUT) from sub_channel
output:
set val(sub), file("${sprlIN}.gz"), file("${sprlOUT}.gz") into gzipped_channel
shell:
'''
gzip !{sprlIN}
gzip !{sprlOUT}
'''
}
//Pre-process components with PRS orientation indicating they need to be reoriented
process reorient_bad {
module 'freesurfer'
module 'FSL/5.0.11'
stageInMode 'copy'
input:
set val(sub), file(sprlIN), file(sprlOUT) from gzipped_channel
output:
set val(sub), file(sprlIN), file(sprlOUT) into oriented_subs
shell:
'''
#!/bin/bash
orientation=$(mri_info --orientation !{sprlIN})
if [ "$orientation" = "PRS" ]; then
fslorient -deleteorient !{sprlIN}
fslorient -deleteorient !{sprlOUT}
fslswapdim !{sprlIN} -x -y z !{sprlIN}
fslswapdim !{sprlOUT} -x -y z !{sprlOUT}
fslorient -setqformcode 1 !{sprlIN}
fslorient -setqformcode 1 !{sprlOUT}
#Save list of reoriented scans
mkdir -p !{params.out}/feenics/
reorient_list=!{params.out}/feenics/reoriented.log
echo !{sub} >> $reorient_list
fi
'''
}
//These ones we can just throw in there!
//Run subject level FeenICS
process run_feenics{
container "$params.simg"
stageInMode 'copy'
scratch "/tmp/"
container params.simg
containerOptions "-B ${params.out}:${params.out}"
//Move things to output directory
publishDir "$params.out/${params.application}", \
mode: 'move',
saveAs: { "$sub" }
input:
set val(sub), file("sprlIN.nii.gz"), file("sprlOUT.nii.gz") from oriented_subs
output:
file("exp/$sub") into melodic_out
val "$sub" into pseudo_out
shell:
'''
#Set up logging
logging_dir=!{params.out}/pipeline_logs/!{params.application}
mkdir -p ${logging_dir}
log_out=${logging_dir}/!{sub}.out
log_err=${logging_dir}/!{sub}.err
#Record task attempt
echo "TASK ATTEMPT !{task.attempt}" >> ${log_out}
echo "WORKDIR: $(pwd)" >> ${log_out}
echo "============================" >> ${log_out}
echo "TASK ATTEMPT !{task.attempt}" >> ${log_err}
echo "WORKDIR: $(pwd)" >> ${log_err}
echo "============================" >> ${log_err}
#Set up folder structure
mkdir -p ./exp/!{sub}/{sprlIN,sprlOUT}
mv "sprlIN.nii.gz" ./exp/!{sub}/sprlIN/
mv "sprlOUT.nii.gz" ./exp/!{sub}/sprlOUT/
#Run FeenICS pipeline
(
s1_folder_setup.py $(pwd)/exp
s2_identify_components.py $(pwd)/exp
s3_remove_flagged_components.py $(pwd)/exp
) 2>> ${log_err} 1>> ${log_out}
#combine spiral files
combinesprl exp/
#Move relevant files over
mv exp/*sprlIN*nii.gz exp/!{sub}/
mv exp/*sprlOUT*nii.gz exp/!{sub}/
mv exp/*sprlCOMBINED*nii.gz exp/!{sub}/
'''
}
process run_icarus{
stageInMode 'copy'
container params.simg
container "$params.simg"
containerOptions "-H $params.out:$params.out"
publishDir "$params.out/${params.application}", \
mode: 'copy'
input:
val "*" from pseudo_out.collect()
output:
val 'pseudo' into pseudo_out2
echo true
shell:
'''
#!/bin/bash
cd !{params.out}/!{params.application}
sprls=$(ls -1d */sprl*/)
icarus-report ${sprls}
'''
}