forked from jerdra/TIGR_PURR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_pepolar.nf
187 lines (154 loc) · 5.46 KB
/
generate_pepolar.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
nextflow.preview.dsl = 2
usage = file("${workflow.scriptFile.getParent()}/usage/generate_pepolar")
engine = new groovy.text.SimpleTemplateEngine()
bindings = [
"bold_tags": params.bold_tags,
"sbref_tag": params.sbref_tag,
"subjects": params.subjects,
"rewrite": params.rewrite,
"usebold": params.usebold
]
toprint = engine.createTemplate(usage.text).make(bindings)
printhelp = params.help
// Basic argument check
req_param = ["--study": params.study,
"--bold_tags": params.bold_tags,
"--out": params.out]
missing_arg = req_param.grep{ (it.value == null || it.value == "") }
if (missing_arg){
log.error("Missing required arguments(s)!")
missing_arg.each{ log.error("Missing ${it.key}") }
printhelp = true
}
if (printhelp){
print(toprint)
System.exit(0)
}
process publish_pepolar{
/*
Map a subject SBRef scan to the derived scan for PEPOLAR
*/
publishDir path: "${params.out}/${sub}",\
mode: 'copy',\
saveAs: { "${name}.nii.gz" },
pattern: "*.nii.gz",
overwrite: params.rewrite
publishDir path: "${params.out}/${sub}",\
mode: 'copy',\
saveAs: { "${name}.json" },
pattern: "*.json",
overwrite: params.rewrite
input:
tuple val(sub), path(img), path(json), val(name)
output:
tuple path(img), path(json)
shell:
'''
echo "Transferring !{img} to !{name}"
'''
}
process gen_pepolar{
/*
Extract first volume from BOLD to derived scan for PEPOLAR
*/
label 'FSL'
input:
tuple val(sub), val(ser), path(bold)
output:
tuple val(sub), val(ser), path("${sub}_${ser}_boldref.nii.gz"),\
emit: boldref
shell:
'''
#!/bin/bash
fslroi !{bold} !{sub}_!{ser}_boldref.nii.gz 0 1
'''
}
// Get full list of available subjects to process
archive_path = "/archive/data/${params.study}/data/nii/"
input_channel = Channel.fromPath("${archive_path}/*", type: 'dir')
.map{ i -> i.getBaseName() }
// Only process data in subjects list if provided
if (params.subjects){
subjects_channel = Channel.fromPath(params.subjects)
.splitText() {it.strip()}
input_channel = input_channel.join(subjects_channel)
}
// Remove subjects that already have an output directory available
if (!params.rewrite){
out_channel = Channel.fromPath("${params.out}/*", type: 'dir')
.map{ o -> [o.getBaseName(), 'o']}
.ifEmpty(['', 'o'])
input_channel = input_channel.join(out_channel, remainder:true)
.filter{it.last() == null}
.map{i, n -> i}
}
// Pull all BOLD scans
bold_scans = input_channel
.map{i->[
i,
params.bold_tags.collect{t ->
file("${archive_path}/${i}/*${t}*.nii.gz")
}.flatten()
]
}
.transpose()
// Break scan file into keys
scan_re = /(?<study>[^_]+)_(?<site>[^_]+)_(?<subject>[^_]+)(?<!PHA)_(?<timepoint>[^_]+)_(?<session>[^_]+)/
series_re = /(?<tag>[^_]+)_(?<series>\d+)_(?<description>.*?)(?<ext>.nii.gz)/
filename_re = /${scan_re}_${series_re}/
// Extract series + description + phase encode
pe_re = /(?<=-)(PA|AP)/
bold_sbref = bold_scans
.map{i,s->[
i,s,
{
def match = s.getName() =~ filename_re
match.matches()
match
}()
]}
.map{i,s,m->[
i,s,
m.group("series").toInteger(),
m.group("description")
]}
.map{i,s,ser,d->[
i,s,ser,d,
{
def match = d =~ pe_re
match.find()
match[0][0]
}(),
file("${archive_path}/${i}/${i}*${params.sbref_tag}*"
+ "${(ser-1).toString().padLeft(2,'0')}*${d}*.nii.gz")
]}
workflow{
main:
// Generate PEPOLAR from dummy BOLD
i_gen_pepolar = bold_sbref.filter{it[-1].isEmpty()}
.map{i,s,ser,d,pe,f ->[i,ser,s]}
gen_pepolar(i_gen_pepolar)
bold2map = bold_sbref.filter{it[-1].isEmpty()}
.map{i,s,ser,d,pe,f ->[
i,ser,
"${i}_${ser.toString().padLeft(2,'0')}_BOLD2FMAP-${pe}"
]}
.join(gen_pepolar.out.boldref, by: [0,1])
.map{i,ser,n,ref->[
i,ser,ref,n
]}
.join(i_gen_pepolar, by:[0, 1])
.map{i,ser,ref,n,bold->[
i,ref,
bold.toString().replace(".nii.gz",".json"),
n
]}
sbref2map = bold_sbref.filter{!(it[-1].isEmpty())}
.map{i,s,ser,d,pe,f ->[
i,f,
f[0].toString().replace(".nii.gz",".json"),
"${i}_${(ser-1).toString().padLeft(2,'0')}"
+ "_SBREF2FMAP-${pe}"
]}
publish_pepolar(sbref2map.mix(bold2map))
}