forked from angelovangel/nxf-ont
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
417 lines (348 loc) · 12.5 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
-------------------------
nextflow ont pipeline
-------------------------
A nextflow pipeline for processing raw Nanopore data
Homepage:
https://github.com/angelovangel/nxf-ont
Creator/Maintainer:
*/
/*
NXF ver 19.08+ needed because of the use of tuple instead of set
*/
if( !nextflow.version.matches('>=19.08') ) {
println "This workflow requires Nextflow version 19.08 or greater and you are running version $nextflow.version"
exit 1
}
def helpMessage() {
log.info"""
==================
N X F - O N T
==================
A simple nextflow pipeline for processing raw Nanopore data
USAGE:
A typical command for running the pipeline is as follows:
nextflow run angelovangel/nxf-ont \
--input /path/to/fast5/files/ \
--flowcell FLO-PRO001 \
--kit SQK-LSK109 \
--barcode_kit EXP-NBD104 \
-profile docker
Mandatory arguments
--input [dir] The directory contains raw FAST5 files.
--csv [file] Comma-separated file containing pairs of sample names and barcodes. Only required for renaming.
--cpus [int] Number of threads used for pipeline (default: 4)
-profile [str] Configuration profile to use, available: test.
Basecalling/Demultiplexing
--flowcell [str] Flowcell used to perform the sequencing e.g. FLO-MIN106.
Not required if '--config' is specified.
--kit [str] Kit used to perform the sequencing e.g. SQK-LSK109.
Not required if '--config' is specified.
--barcode_kits [str] Space separated list of barcoding kit(s) or
expansion kit(s) to detect against. Must be in double quotes.
Not required if '--skip_demultiplexing' is specified.
--trim_barcodes [bool] Trim the barcodes from the output sequencesin the FastQ files (default: false).
--config [file/str] Guppy config file used for basecalling e.g. dna_r9.4.1_450bps_fast.cfg.
Cannot be used in conjunction with '--flowcell' and '--kit'.
--cpu_threads_per_caller [int] Number of threads used for guppy_basecaller (default: 2, will overwrite '--cpus').
--num_callers [int] Number of callers used for guppy_basecaller (default: 1).
--skip_basecalling [bool] Skip basecalling with guppy_basecaller (default: false)
--skip_demultiplexing [bool] Skip demultiplexing with guppy_barcoder (default: false)
Adapter trimming
--skip_porechop [bool] Skip adapter trimming with porechop
(default: false, if '--skip_demultiplexing' is specified, adapter trimming will also be skipped.)
Quality control
--skip_pycoqc [bool] Skip pycoQC (default: false)
Other arguments
--help Show this help message and exit.
""".stripIndent()
}
if ( params.help ) {
helpMessage()
exit 0
}
// print run summary
def summary = [:]
summary['input'] = params.input
summary['cpus'] = params.cpus
summary['basecalling'] = params.skip_basecalling ? 'No' : 'Yes'
if (!params.skip_basecalling) {
if (params.flowcell) summary['flowcell'] = params.flowcell
if (params.kit) summary['kit'] = params.kit
if (params.config) summary['config'] = params.config
summary['cpus per caller'] = params.cpu_threads_per_caller ? params.cpu_threads_per_caller : params.cpus
summary['number of callers'] = params.num_callers
}
summary['demultiplexing'] = params.skip_demultiplexing ? 'No' : 'Yes'
if (!params.skip_demultiplexing) {
if (params.barcode_kits) summary['barcode kits'] = params.barcode_kits
summary['trim barcodes'] = params.trim_barcodes ? 'Yes' : 'No'
if (params.csv) summary['csv'] = params.csv
}
summary['adapter trimming'] = params.skip_porechop ? 'No' : 'Yes'
summary['quality control'] = params.skip_pycoqc ? 'seqkit': 'pycoQC & seqkit'
log.info summary.collect { k,v -> "${k.padRight(18)}: $v" }.join("\n")
log.info "-\033[2m--------------------------------------------------\033[0m-"
// define input channels, if params empty leave them empty
ch_input_csv = params.csv ? Channel.fromPath( params.csv, checkIfExists: true ) : Channel.empty()
if ( params.flowcell && !params.kit ) {
exit 1, "Error: no valid kit found."
}
if ( params.kit && !params.flowcell ) {
exit 1, "Error: no valid flowcell found."
}
/*
Guppy basecalling & demultiplexing
*/
if ( !params.skip_basecalling ) {
if (workflow.profile.contains('test')) {
process get_test_data {
publishDir path: "${params.outdir}/testdata", mode: 'copy'
output:
file "test-datasets" into ch_input_files
script:
"""
git clone https://github.com/ncct-mibi/test-datasets --branch nxf-ont
"""
}
} else {
if (params.input) {
ch_input_files = Channel.fromPath(params.input, checkIfExists: true)
} else {
exit 1, "Please specify a valid run directory to perform basecalling!"
}
}
process guppy_basecaller {
publishDir path: params.barcode_kits ? "${params.outdir}/barcodes" : "${params.outdir}/basecalled", mode:'copy',
saveAs: { filename -> if (!filename.endsWith("v_guppy_basecaller.txt")) filename }
publishDir path: "${params.outdir}/pipeline_info", mode:'copy',
saveAs: { filename -> if (filename.endsWith("v_guppy_basecaller.txt")) filename }
input:
file dir_fast5 from ch_input_files
file csv_file from ch_input_csv.ifEmpty([])
output:
file "fastq/*.fastq.gz" into ch_fastq, ch_for_seqkit
file "guppy_basecaller.log" into ch_log_guppy_basecaller
file "rename.log" optional true into ch_log_rename
file "sequencing_summary*" into ch_summary_guppy
file "v_guppy_basecaller.txt" into ch_version_guppy
script:
flowcell = params.flowcell ? "--flowcell $params.flowcell" : ""
kit = params.kit ? "--kits $params.kit" : ""
barcode_kits = params.barcode_kits ? "--barcode_kits $params.barcode_kits" : ""
config = params.config ? "--config $params.config" : ""
trim_barcodes = params.trim_barcodes ? "--trim_barcodes" : ""
cpu_threads_per_caller = params.cpu_threads_per_caller ? "--cpu_threads_per_caller $params.cpu_threads_per_caller" : "--cpu_threads_per_caller $params.cpus"
//num_callers = "--num_callers $params.num_callers"
"""
guppy_basecaller --version &> v_guppy_basecaller.txt
guppy_basecaller \\
--input_path $dir_fast5 \\
--save_path ./results-guppy-basecaller \\
--recursive \\
--records_per_fastq 0 \\
$flowcell \\
$kit \\
$barcode_kits \\
$trim_barcodes \\
$cpu_threads_per_caller \\
--num_callers $params.num_callers \\
--qscore_filtering \\
$config \\
--compress_fastq \\
&> guppy_basecaller.log
cp results-guppy-basecaller/sequencing_summary* .
mkdir fastq
cd results-guppy-basecaller/pass
if [ "\$(find . -type d -name "barcode*" )" != "" ]
then
for dir in barcode*/
do
dir=\${dir%*/}
cat \$dir/*.fastq.gz > ../../fastq/\$dir.fastq.gz
done
else
cat *.fastq.gz > ../../fastq/unclassified.fastq.gz
fi
if [ ! -z "$params.csv" ] && [ ! -z "$barcode_kits" ]
then
while IFS=, read -r ob nb
do
echo rename \$ob.fastq.gz to \$nb.fastq.gz &>> ../../rename.log
mv ../../fastq/\$ob.fastq.gz ../../fastq/\$nb.fastq.gz
done < ../../$csv_file
fi
"""
}
} else if ( params.skip_basecalling && ! params.skip_demultiplexing && params.barcode_kits ) {
if (params.input) {
ch_input_files = Channel.fromPath(params.input, checkIfExists: true)
} else {
exit 1, "Please specify a valid run directory to perform demultiplexing process!"
}
process guppy_barcoder {
publishDir path: "${params.outdir}/barcodes", mode:'copy'
input:
file basecalled_files from ch_input_files
file csv_file from ch_input_csv.ifEmpty([])
output:
file "fastq/*.fastq.gz" into ch_fastq, ch_for_seqkit
file "guppy_barcoder.log" into ch_log_guppy_barcoder
file "rename.log" optional true into ch_log_rename
file "sequencing_summary*" into ch_summary_guppy
file "v_guppy_barcoder.txt" into ch_version_guppy
script:
trim_barcodes = params.trim_barcodes ? "--trim_barcodes" : ""
//worker_threads = params.cpus ? "--worker_threads $params.cpus" : "--worker_threads 4"
"""
guppy_barcoder \\
--input_path $basecalled_files \\
--save_path ./results-guppy-barcoder \\
--recursive \\
--records_per_fastq 0 \\
--compress_fastq \\
--barcode_kits $params.barcode_kits \\
$trim_barcodes \\
--worker_threads $params.cpus \\
&> guppy_barcoder.log
cp results-guppy-barcoder/sequencing_summary* .
mkdir fastq
cd results-guppy-barcoder
if [ "\$(find . -type d -name "barcode*" )" != "" ]
then
for dir in barcode*/
do
dir=\${dir%*/}
cat \$dir/*.fastq.gz > ../fastq/\$dir.fastq.gz
done
else
cat *.fastq.gz > ../fastq/unclassified.fastq.gz
fi
if [ ! -z "$params.csv" ]
then
while IFS=, read -r ob nb
do
echo rename \$ob.fastq.gz to \$nb.fastq.gz &>> ../../rename.log
mv ../fastq/\$ob.fastq.gz ../fastq/\$nb.fastq.gz
done < ../$csv_file
fi
"""
}
} else if ( params.skip_basecalling && params.skip_demultiplexing ){
if (params.input) {
ch_input_files = Channel.fromPath(params.input, checkIfExists: true)
} else {
exit 1, "Please specify a valid run directory to perform rename process!"
}
process rename_barcode {
publishDir path: "${params.outdir}/renamed_barcodes", mode:'copy'
input:
file fastq_files from ch_input_files
file csv_file from ch_input_csv.ifEmpty([])
output:
file "fastq/*.fastq.gz" into ch_fastq, ch_for_seqkit
file "rename.log" into ch_log_rename
script:
"""
mkdir fastq
fastqdir=\$PWD
cd $fastq_files
if [ "\$(find . -type d -name "barcode*" )" != "" ]
then
for dir in barcode*/
do
dir=\${dir%*/}
cat \$dir/*.fastq.gz > \$fastqdir/fastq/\$dir.fastq.gz
done
else
cat *.fastq.gz > \$fastqdir/fastq/unclassified.fastq.gz
fi
if [ ! -z "$params.csv" ] && [ ! -z "$params.barcode_kits" ]
then
while IFS=, read -r ob nb
do
echo rename \$fastqdir/fastq/\$ob.fastq.gz to \$fastqdir/fastq/\$nb.fastq.gz &>> \$fastqdir/rename.log
mv \$fastqdir/fastq/\$ob.fastq.gz \$fastqdir/fastq/\$nb.fastq.gz
done < \$fastqdir/$csv_file
fi
"""
}
}
/*
Adapter trimming with porechop
*/
process porechop {
publishDir path: "${params.outdir}/porechop", mode:'copy'
input:
file fastq_file from ch_fastq.flatten()
output:
file "trimmed*.fastq.gz" into ch_porechop
file "logs/trimmed*.log" into ch_log_porechop
when:
!params.skip_porechop
script:
//threads = params.cpus ? "--threads $params.cpus" : "--threads 4"
"""
mkdir logs
porechop \\
--input $fastq_file \\
--output trimmed_$fastq_file \\
--threads $params.cpus \\
--no_split \\
&> logs/trimmed_"${fastq_file.simpleName}".log
"""
}
/*
Quality control with pycoQC
*/
process pycoqc {
publishDir path: "${params.outdir}/pycoqc", mode:'copy'
input:
file summary_file from params.skip_pycoqc ? Channel.empty(): ch_summary_guppy
output:
file "pycoQC.html"
when:
!params.skip_pycoqc
script:
"""
pycoQC --summary_file $summary_file --html_outfile pycoQC.html
"""
}
/*
Quality control with seqTools
*/
Channel.fromPath('bin/fastq-stats-report.Rmd').set{ fastq_stats_report_ch }
process seqtools {
publishDir path: "${params.outdir}/fastq-stats", mode:'copy'
input:
file fastq_file from !params.skip_porechop && !params.skip_demultiplexing ? ch_porechop.collect() : ch_for_seqkit.collect()
file 'fastq-stats-report.Rmd' from fastq_stats_report_ch
output:
file "fastq-stats.csv"
file "fastq-stats.xlsx"
file "fastq-stats-report.html"
script:
"""
seqtools.R $fastq_file
"""
}
/*
Get the software versions
*/
/*
process get_software_versions {
publishDir path: "${params.outdir}/pipeline_info", mode:'copy'
input:
file "*.txt" from params.skip_basecalling || skip_demultiplexing ? Channel.empty() : ch_version_guppy
output:
file "pipeline_info.txt"
script:
"""
echo porechop \$(porechop --version) &>> pipeline_info.txt
echo pycoQC && pycoQC --version &>> pipeline_info.txt
seqkit version &>> pipeline_info.txt
"""
}
*/