-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
323 lines (261 loc) · 7.19 KB
/
main.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
#!/usr/bin/env nextflow
/*
* Inputs preparation
*/
if(!(params.containsKey("groups")) || (params.groups == null)) {
params.with_groups = false
} else {
params.with_groups = true
}
references = file(params.references)
summary_config = file(params.summary_list)
// targets: [name, [...files]]
Channel.from(params.targets.entrySet())
.map { [it.key, it.value.collect { file(it).baseName }] }
.set { targets_grouped_ch }
// all targets combinations [file basename, name]
Channel.from(params.targets.entrySet())
.flatMap { it.value.collect { x -> [file(x).baseName, it.key] } }
.set { files_with_target_ch }
// all files from all targets in a flat list
Channel.from(params.targets.entrySet())
.flatMap { it.value.collect { file(it) } }
.into { targets_allfiles_ch; targets_allfiles_ch2 }
if (params.with_groups) {
// groups: [name, [...targets]]
Channel.from(params.groups.entrySet())
.map { [it.key, it.value] }
.set { groups_ch }
// all groups combinations [target, name]
Channel.from(params.groups.entrySet())
.flatMap { it.value.collect { x -> [x, it.key] } }
.into { targets_with_group_ch; targets_with_group_ch2 }
}
/*
* Get observed tandems from fasta files
*/
process getObservedTandems {
tag "$target"
input:
path fasta_file from targets_allfiles_ch
path references
output:
tuple val(target), path("${target}.tsv") into observed_tandems_ch
script:
target = "${fasta_file.baseName}"
"""
get_tandems.py $fasta_file $references ${target}.tsv
"""
}
/*
* Join observed tandems under the same target
*/
targets_grouped_ch
.flatMap { it[1].collect{ x -> [x, it[0]] } }
.join(observed_tandems_ch)
.collectFile(
keepHeader: true,
skip: 1,
storeDir: 'output/tandem_info/observed/single/'
) {
["${it[1]}.tsv", it[2]]
}
.map { [it.baseName, it] }
.set { observed_tandems_grouped_ch }
/*
* Get mutational frequecies
*/
process getMutationalFrequencies {
tag "$target"
input:
path sequences from targets_allfiles_ch2
path references
output:
tuple val(target), path("${target}.tsv"), path("mbs_${target}.txt") into ungrouped_mutational_info_ch
script:
target = sequences.baseName
"""
get_probabilities.py $sequences $references -m mbs_${target}.txt
"""
}
/*
* Add target to mutational information and split channel
* in the ones that need merging and the ones that not
*/
ungrouped_mutational_info_ch
.join(files_with_target_ch)
.groupTuple(by: 3)
.map { [it[3], it[1], it[2]] }
.branch {
single: it[1].size() == 1
multi: it[1].size() > 1
}
.set { multational_info_pre_ch }
/*
* Merge mutational info when there are multiple files for a single target
*/
process mergeTargetMutationalInfo {
tag "$name"
publishDir 'output/mutation_info/single/', mode: 'copy', pattern: '*.tsv'
input:
tuple val(name), path(mutation_info_files), path(mutations_by_seq_files) from multational_info_pre_ch.multi
output:
tuple val(name), path("${name}.tsv"), path("mbs_${name}.txt") into merged_mutational_info_ch
path "mbs_${name}.txt" into mutations_by_seq_ch
script:
"""
merge_mutation_info.py $mutation_info_files -o ${name}.tsv
merge_mutations_by_seq.py $mutations_by_seq_files -n $name -o mbs_${name}.txt
"""
}
/*
* Merge single and merged mutational info files in a single channel
*/
multational_info_pre_ch.single
.map { it.flatten() }
.concat(merged_mutational_info_ch)
.multiMap {
full1: it
full2: it
mutations_by_seq: it[2]
}
.set { mutational_info_ch }
/*
* Merge all "mutations by sequence" files
*/
mutational_info_ch.mutations_by_seq
.collectFile(
name: 'mutations_by_seq.txt',
storeDir: 'output/mutation_info/single/',
newLine: true,
sort: { it.text }
)
/*
* Make simulation
*/
process simulateTandems {
tag "$name"
cpus 4
input:
tuple val(name), path(mutation_info), path(mutations_by_seq) from mutational_info_ch.full1
path references
output:
tuple val(name), path('tandems.tsv') into simulated_tandems_ch, simulated_tandems_ch2
script:
reference_names = params.targets[name].collect { file(it).baseName }.join(' ')
productive_only = params.with_stopcodons ? "" : "-p"
"""
tandem_generation.py $mutation_info $references $mutations_by_seq \
${params.n_simulations} tandems.tsv -r $reference_names -t ${task.cpus} $productive_only
"""
}
/*
* Sort and compress tandem files
*/
process sortAndCompress {
tag "$name"
publishDir "output/tandem_info/simulated/single/", mode: 'copy'
input:
tuple val(name), path(tandems_file) from simulated_tandems_ch
output:
path "${name}.tsv.gz"
script:
"""
sort -nk1 -nk2 $tandems_file | gzip > ${name}.tsv.gz
"""
}
/*
* Summarize tandem info
*/
if (params.with_summaries) {
process summarizeTandems {
tag "$name"
publishDir "output/tandem_info_summarized/single/", mode: 'copy'
input:
tuple val(name), path(tandems_file) from simulated_tandems_ch2
path summary_config
output:
tuple val(name), path("${name}", type: 'dir') into summaries_ch
script:
"""
summarize_tandem_info.py $tandems_file $summary_config -o ${name}
"""
}
}
/*
* Grouping tasks: only executing them if there are groups defined
*/
if (params.with_groups) {
/*
* Merge tandem observed info by groups
*/
groups_ch
.flatMap { it[1].collect{ x -> [x, it[0]] } }
.combine(observed_tandems_grouped_ch, by: 0)
.collectFile(
keepHeader: true,
skip: 1,
storeDir: 'output/tandem_info/observed/grouped/'
) {
["${it[1]}.tsv", it[2]]
}
/*
* Arrange mutational info in groups to merge them later
*/
mutational_info_ch.full2
.combine(targets_with_group_ch, by: 0)
.groupTuple(by: 3)
.map { [it[3], it[1], it[2]] }
.set { mutational_info_by_group_ch }
/*
* Merge mutational info by groups
*/
process mergeGroupMutationalInfo {
tag "$group_name"
publishDir 'output/mutation_info/grouped/', mode: 'copy', pattern: '*.tsv'
input:
tuple val(group_name), path(mutation_info_files), path(mutations_by_seq_files) from mutational_info_by_group_ch
output:
path "${group_name}.tsv"
path 'mutations_by_seq.txt' into group_mutations_by_seq_ch
script:
"""
merge_mutation_info.py $mutation_info_files -o ${group_name}.tsv
merge_mutations_by_seq.py $mutations_by_seq_files -n $group_name
"""
}
/*
* Merge grouped "mutations by sequence" files
*/
group_mutations_by_seq_ch
.collectFile(
name: 'mutations_by_seq.txt',
storeDir: 'output/mutation_info/grouped/',
newLine: true,
sort: { it.text }
)
/*
* Arrange summary directories in groups to merge them later
*/
summaries_ch
.combine(targets_with_group_ch2, by: 0)
.groupTuple(by: 2)
.map { [it[2], it[1]] }
.set { summaries_by_group_ch }
/*
* Merge summaries by group
*/
process mergeSummariesByGroup {
tag "$group_name"
publishDir "output/tandem_info_summarized/grouped/", mode: 'copy'
input:
tuple val(group_name), path(summary_dirs) from summaries_by_group_ch
path summary_config
output:
path "${group_name}", type: 'dir'
script:
"""
merge_summaries.py $summary_config $summary_dirs -o "${group_name}"
"""
}
}