-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.nf
305 lines (247 loc) · 9.8 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
/**
* Copyright (c) 2021 DKFZ.
*
* Distributed under the MIT License (license terms are at https://github.com/DKFZ-ODCF/nf-bam2fastq/blob/master/LICENSE.txt).
*
* Author: Philip R. Kensche
*/
/** Comma-separated list of input BAMs */
params.input
/** Path to which data should be written. One subdirectory per input BAM. */
params.outputDir
/** Whether to sort the output FASTQs. */
params.sortFastqs = true
/** Alignments with these flags are excluded. Comma delimited list (interpreted as bash array) of the following values:
* secondary, supplementary. */
params.excludedFlags = "secondary,supplementary"
/** While reading in intermediate (yet unsorted) FASTQs, check that the MD5 is the same as in the accompanied '.md5'
* file. Only available for Picard, as Biobambam does not produce MD5 files for output files. */
params.checkIntermediateFastqMd5 = true
/** Whether to compress intermediate (yet unsorted) FASTQs. Applies only when sortFastqs = true.
* Note that the final FASTQs are always compressed. */
params.compressIntermediateFastqs = true
/** Compression binary or script used for (de)compression of sorted output FASTQs. gzip, ${TOOL_PIGZ}.
* API for $compressor:
* * $compressor < uncompressed > compressed
* * $compressor -d < compressed > uncompressed
* * compressorThreads=$threads $compressor # change number of threads
*/
String compressor = "pigz.sh"
params.compressorThreads = 4
/** Memory used for storing data while sorting. Is passed to the sorting tool and should follow its required syntax.
* WARNING: Also adapt the job requirements! */
params.sortMemory = "1 GB"
/** The number of parallel threads used for sorting. */
params.sortThreads = 4
/** Produce more output for debugging. */
params.debug = false
/** The Nextflow publish mode. See https://www.nextflow.io/docs/latest/process.html#publishdir */
params.publishMode = "rellink"
/** Parameter checking and casting code */
allowedParameters = ['input', 'outputDir',
'sortFastqs', 'sortMemory', 'sortThreads',
'compressIntermediateFastqs', 'compressorThreads',
'excludedFlags', 'checkIntermediateFastqMd5',
'debug', 'publishMode']
checkParameters(params, allowedParameters)
// The sorting memory is used as MemoryUnit in the code below.
sortMemory = new MemoryUnit(params.sortMemory)
/** See https://www.nextflow.io/docs/latest/process.html#publishdir */
enum PublishMode {
symlink, // Absolute symbolic link in the published directory for each process output file.
rellink, // Relative symbolic link in the published directory for each process output file.
link, // Hard link in the published directory for each process output file.
copy, // Copies the output files into the published directory.
copyNoFollow, // Copies the output files into the published directory without following
// symlinks ie. copies the links themselves.
move; // Moves the output files into the published directory. Note: this is only supposed
// to be used for a terminating process i.e. a process whose output is not consumed
// by any other downstream process.
static PublishMode fromString(String str) throws IllegalArgumentException {
return str.toLowerCase() as PublishMode
}
}
publishMode = PublishMode.fromString(params.publishMode)
log.info """
==================================
= nf-bam2fastq =
==================================
${allowedParameters.collect { "$it = ${params.get(it)}" }.join("\n")}
"""
String fastqSuffix(Boolean compressed) {
if (compressed)
return "fastq.gz"
else
return "fastq"
}
String sortedFastqFile(String outDir, Path unsortedFastq, Boolean compressed) {
String result = outDir + "/" + unsortedFastq.getFileName().toString().replaceFirst(/\.fastq(?:\.gz)?$/, ".sorted.fastq")
if (compressed)
return result + ".gz"
else
return result
}
/** Check whether parameters are correct (names and values)
*
* @param parameters
* @param allowedParameters
*/
void checkParameters(parameters, List<String> allowedParameters) {
Set<String> unknownParameters = parameters.
keySet().
grep {
!it.contains('-') // Nextflow creates hyphenated versions of camel-cased parameters.
}.
minus(allowedParameters)
if (!unknownParameters.empty) {
log.error "There are unrecognized parameters: ${unknownParameters}"
exit(1)
}
}
/** Workflow supplementary code */
/** Convert the configured sort memory from Nextflow's MemoryUnit to a string for the `sort` tool.
*
* @param mem
* */
String toSortMemoryString(MemoryUnit mem) {
def splitted = mem.toString().split(" ")
String size = splitted[0]
switch(splitted[1]) {
case "B":
return size + "b"
break
case "KB":
return size + "k"
break
case "MB":
return size + "m"
break
case "GB":
return size + "g"
break
case "PB":
return size + "p"
break
case "EB":
return size + "e"
break
case "ZB":
return size + "z"
break
default:
throw new RuntimeException("MemoryUnit produced unknown unit in '${mem.toString()}")
}
}
/** The actual workflow */
bamFiles_ch = Channel.
fromPath(params.input.split(',') as List<String>,
checkIfExists: true)
Boolean compressBamToFastqOutput = params.sortFastqs ? params.compressIntermediateFastqs : true
process bamToFastq {
// Just bamtofastq
cpus 1
// The biobambam paper states something like 133 MB.
memory 1.GB
time { 48.hours * 2**(task.attempt - 1) }
maxRetries 2
publishDir params.outputDir, enabled: !params.sortFastqs, mode: publishMode.toString()
input:
file bamFile from bamFiles_ch
output:
tuple file(bamFile), file("**/*.${fastqSuffix(compressBamToFastqOutput)}") into readsFiles_ch
shell:
"""
excludedFlags="(${params.excludedFlags.split(",").join(" ")})" \
compressor="$compressor" \
compressorThreads="$params.compressorThreads" \
compressFastqs="$compressBamToFastqOutput" \
bamFile="$bamFile" \
converter="biobambam" \
outputDir="${bamFile}_fastqs" \
bam2Fastq.sh
"""
}
// Create two channels of matched paired-end and unmatched or single-end reads, each of tuples of (bam, fastq).
readsFiles_ch.into { readsFilesA_ch; readsFilesB_ch }
pairedFastqs_ch = readsFilesA_ch.flatMap {
def (bam, fastqs) = it
fastqs.grep { it.getFileName() =~ /.+_R[12]\.fastq(?:\.[^.]*)?$/ }.
groupBy { fastq -> fastq.getFileName().toString().replaceFirst("_R[12].fastq(?:.gz)?\$", "") }.
collect { key, files ->
assert files.size() == 2
files.sort()
[bam, files[0], files[1]]
}
}
// Unpaired FASTQs are unmatched or orphaned paired-reads (1 or 2) and singletons, i.e. unpaired reads.
unpairedFastqs_ch = readsFilesB_ch.flatMap {
def (bam, fastqs) = it
fastqs.
grep { it.getFileName() =~ /.+_(U[12]|S)\.fastq(?:\.[^.]*)?$/ }.
collect { [bam, it] }
}
process nameSortUnpairedFastqs {
cpus { params.sortThreads + (params.compressIntermediateFastqs ? params.compressorThreads : 0 ) }
memory { (sortMemory + 100.MB) * params.sortThreads * 1.2 }
// TODO Make runtime dependent on file-size.
time { 24.hour * 2**(task.attempt - 1) }
maxRetries 2
publishDir params.outputDir, mode: publishMode.toString()
when:
params.sortFastqs
input:
tuple file(bam), file(fastq) from unpairedFastqs_ch
output:
tuple file(bam), file(sortedFastqFile) into sortedUnpairedFastqs_ch
script:
bamFileName = bam.getFileName().toString()
outDir = "${bamFileName}_sorted_fastqs"
sortedFastqFile = sortedFastqFile(outDir, fastq, true)
"""
mkdir -p "$outDir"
compressedInputFastqs="$compressBamToFastqOutput" \
compressor="$compressor" \
compressorThreads="$params.compressorThreads" \
sortThreads="$params.sortThreads" \
sortMemory="${toSortMemoryString(sortMemory)}" \
fastqFile="$fastq" \
sortedFastqFile="$sortedFastqFile" \
coreutilsSortFastqSingle.sh
"""
}
process nameSortPairedFastqs {
cpus { params.sortThreads + (params.compressIntermediateFastqs ? params.compressorThreads * 2 : 0) }
memory { (sortMemory + 100.MB) * params.sortThreads * 1.2 }
// TODO Make runtime dependent on file-size.
time { 24.hours * 2**(task.attempt - 1) }
maxRetries 2
publishDir params.outputDir
when:
params.sortFastqs
input:
tuple file(bam), file(fastq1), file(fastq2) from pairedFastqs_ch
output:
tuple file(bam), file(sortedFastqFile1), file(sortedFastqFile2) into sortedPairedFastqs_ch
script:
bamFileName = bam.getFileName().toString()
outDir = "${bamFileName}_sorted_fastqs"
sortedFastqFile1 = sortedFastqFile(outDir, fastq1, true)
sortedFastqFile2 = sortedFastqFile(outDir, fastq2, true)
"""
mkdir -p "$outDir"
compressedInputFastqs="$compressBamToFastqOutput" \
compressor="$compressor" \
compressorThreads="$params.compressorThreads" \
sortThreads="$params.sortThreads" \
sortMemory="${toSortMemoryString(sortMemory)}" \
fastqFile1="$fastq1" \
fastqFile2="$fastq2" \
sortedFastqFile1="$sortedFastqFile1" \
sortedFastqFile2="$sortedFastqFile2" \
coreutilsSortFastqPair.sh
"""
}
workflow.onComplete {
println "Workflow run $workflow.runName completed at $workflow.complete with status " +
"${ workflow.success ? 'success' : 'failure' }"
}