forked from nf-core/eager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.nf
More file actions
1462 lines (1188 loc) · 53.4 KB
/
main.nf
File metadata and controls
1462 lines (1188 loc) · 53.4 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env nextflow
/*
========================================================================================
nf-core/eager
========================================================================================
EAGER Analysis Pipeline. Started 2018-06-05
#### Homepage / Documentation
https://github.com/nf-core/eager
#### Authors
Alexander Peltzer apeltzer <alex.peltzer@gmail.com> - https://github.com/apeltzer>
James A. Fellows Yates <jfy133@gmail.com> - https://github.com/jfy133
Stephen Clayton <clayton@shh.mpg.de> - https://github.com/sc13-bioinf
Maxime Borry <borry@shh.mpg.de.de> - https://github.com/maxibor
========================================================================================
*/
def helpMessage() {
log.info nfcoreHeader()
log.info"""
=========================================
eager v${workflow.manifest.version}
=========================================
Usage:
The typical command for running the pipeline is as follows:
nextflow run nf-core/eager --reads '*_R{1,2}.fastq.gz' -profile docker
Mandatory arguments:
--reads Path to input data (must be surrounded with quotes)
-profile Institution or personal hardware config to use (e.g. standard, docker, singularity, conda, aws). Ask your system admin if unsure, or check documentation.
--singleEnd Specifies that the input is single end reads (required if not pairedEnd)
--pairedEnd Specifies that the input is paired end reads (required if not singleEnd)
--bam Specifies that the input is in BAM format
--fasta Path to Fasta reference (required if not iGenome reference)
--genome Name of iGenomes reference (required if not fasta reference)
Input Data Additional Options:
--snpcapture Runs in SNPCapture mode (specify a BED file if you do this!)
References If not specified in the configuration file, or you wish to overwrite any of the references.
--bwa_index Prefix of the BWA index files including the full path (everything before the endings '.amb' '.ann' '.bwt' most likely the same value supplied with the --fasta option)
--bedfile Path to BED file for SNPCapture methods
--seq_dict Path to picard sequence dictionary file (typically ending in '.dict')
--fasta_index Path to samtools FASTA index (typically ending in '.fai')
--saveReference Saves reference genome indices for later reusage
Skipping Skip any of the mentioned steps
--skip_fastqc Skips both pre- and post-Adapter Removal FastQC steps.
--skip_adapterremoval
--skip_preseq
--skip_damage_calculation
--skip_qualimap
--skip_deduplication
Complexity Filtering
--complexity_filter_poly_g Run poly-G removal on FASTQ files
--complexity_filter_poly_g_min Specify length of poly-g min for clipping to be performed (default: 10)
Clipping / Merging
--clip_forward_adaptor Specify adapter sequence to be clipped off (forward)
--clip_reverse_adaptor Specify adapter sequence to be clipped off (reverse)
--clip_readlength Specify read minimum length to be kept for downstream analysis
--clip_min_read_quality Specify minimum base quality for not trimming off bases
--min_adap_overlap Specify minimum adapter overlap
--skip_collapse Skip merging forward and reverse reads together. (Only for PE samples)
--skip_trim Skip adaptor and quality trimming
BWA Mapping
--bwaalnn Specify the -n parameter for BWA aln.
--bwaalnk Specify the -k parameter for BWA aln
--bwaalnl Specify the -l parameter for BWA aln
Stripping
--strip_input_fastq Create pre-Adapter Removal FASTQ files without reads that mapped to reference (e.g. for public upload of privacy sensitive non-host data)
--strip_mode Stripping mode. Remove mapped reads completely from FASTQ (strip) or just mask mapped reads sequence by N (replace)
CircularMapper
--circularmapper Turn on CircularMapper (CM)
--circularextension Specify the number of bases to extend reference by
--circulartarget Specify the target chromosome for CM
--circularfilter Specify to filter off-target reads
BWA Mem Mapping
--bwamem Turn on BWA Mem instead of BWA aln for mapping
BAM Filtering
--bam_mapping_quality_threshold Minimum mapping quality for reads filter, default 0.
--bam_discard_unmapped Discards unmapped reads in either FASTQ or BAM format, depending on choice (see --bam_unmapped_type).
--bam_unmapped_type Defines whether to discard all unmapped reads, keep only bam and/or keep only fastq format (options: discard, bam, fastq, both).
DeDuplication
--dedupper Deduplication method to use (options: dedup, markduplicates). Default: dedup
--dedup_all_merged Treat all reads as merged reads
Library Complexity Estimation
--preseq_step_size Specify the step size of Preseq
(aDNA) Damage Analysis
--damageprofiler_length Specify length filter for DamageProfiler
--damageprofiler_threshold Specify number of bases to consider for damageProfiler
--run_pmdtools Turn on PMDtools
--udg_type Specify here if you have UDG half treated libraries, Set to 'half' in that case, or 'full' for UDG+. If not set, libraries are set to UDG-.
--pmdtools_range Specify range of bases for PMDTools
--pmdtools_threshold Specify PMDScore threshold for PMDTools
--pmdtools_reference_mask Specify a reference mask for PMDTools
--pmdtools_max_reads Specify the max. number of reads to consider for metrics generation
BAM Trimming
--trim_bam Turn on BAM trimming for UDG(+ or 1/2) protocols
--bamutils_clip_left / --bamutils_clip_right Specify the number of bases to clip off reads
--bamutils_softclip Use softclip instead of hard masking
Other options:
--outdir The output directory where the results will be saved
--email Set this parameter to your e-mail address to get a summary e-mail with details of the run sent to you when the workflow exits
--plaintext_email Receive plain text emails rather than HTML
--maxMultiqcEmailFileSize Threshold size for MultiQC report to be attached in notification email. If file generated by pipeline exceeds the threshold, it will not be attached (Default: 25MB)
-name Name for the pipeline run. If not specified, Nextflow will automatically generate a random mnemonic.
--max_memory Memory limit for each step of pipeline. Should be in form e.g. --max_memory '8.GB'
--max_time Time limit for each step of the pipeline. Should be in form e.g. --max_memory '2.h'
--max_cpus Maximum number of CPUs to use for each step of the pipleine. Should be in form e.g. --max_cpus 1
For a full list and more information of available parameters, consider the documentation (https://github.com/nf-core/eager/).
""".stripIndent()
}
/*
* SET UP CONFIGURATION VARIABLES
*/
// Show help message
params.help = false
if (params.help){
helpMessage()
exit 0
}
// Configurable variables
params.name = false
params.singleEnd = false
params.pairedEnd = false
params.genome = "Custom"
params.snpcapture = false
params.bedfile = ''
params.fasta = ''
params.seq_dict = false
params.fasta_index = false
params.saveReference = false
params.pmd_udg_type = 'half'
params.multiqc_config = "$baseDir/conf/multiqc_config.yaml"
params.email = false
params.plaintext_email = false
// Skipping parts of the pipeline for impatient users
params.skip_fastqc = false
params.skip_adapterremoval = false
params.skip_preseq = false
params.skip_damage_calculation = false
params.skip_qualimap = false
params.skip_deduplication = false
//Complexity filtering reads
params.complexity_filter_poly_g = false
params.complexity_filter_poly_g_min = 10
//Read clipping and merging parameters
params.clip_forward_adaptor = "AGATCGGAAGAGCACACGTCTGAACTCCAGTCAC"
params.clip_reverse_adaptor = "AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGTA"
params.clip_readlength = 30
params.clip_min_read_quality = 20
params.min_adap_overlap = 1
params.skip_collapse = false
params.skip_trim = false
//Read mapping parameters (default = BWA aln default)
params.bwaalnn = 0.04
params.bwaalnk = 2
params.bwaalnl = 32
//Mapper to use, by default BWA aln will be used
params.circularmapper = false
params.circularextension = 500
params.circulartarget = 'MT'
params.circularfilter = false
//BWAMem Specific Settings
params.bwamem = false
//BAM Filtering steps (default = keep mapped and unmapped in BAM file)
params.bam_discard_unmapped = false
params.bam_unmapped_type = ''
params.bam_mapping_quality_threshold = 0
//DamageProfiler settings
params.damageprofiler_length = 100
params.damageprofiler_threshold = 15
//DeDuplication settings
params.dedupper = 'dedup' //default value dedup
params.dedup_all_merged = false
//Preseq settings
params.preseq_step_size = 1000
//PMDTools settings
params.run_pmdtools = false
params.pmdtools_range = 10
params.pmdtools_threshold = 3
params.pmdtools_reference_mask = ''
params.pmdtools_max_reads = 10000
//bamUtils trimbam settings
params.trim_bam = false
params.bamutils_clip_left = 1
params.bamutils_clip_right = 1
params.bamutils_softclip = false
//unmap
params.strip_input_fastq = false
params.strip_mode = 'strip'
multiqc_config = file(params.multiqc_config)
output_docs = file("$baseDir/docs/output.md")
where_are_my_files = file("$baseDir/assets/where_are_my_files.txt")
// Validate inputs
if ( params.fasta.isEmpty () ){
exit 1, "Please specify --fasta with the path to your reference"
} else if("${params.fasta}".endsWith(".gz")){
//Put the zip into a channel, then unzip it and forward to downstream processes. DONT unzip in all steps, this is inefficient as NXF links the files anyways from work to work dir
zipped_fasta = file("${params.fasta}")
rm_gz = params.fasta - '.gz'
lastPath = rm_gz.lastIndexOf(File.separator)
bwa_base = rm_gz.substring(lastPath+1)
process unzip_reference{
tag "${zipped_fasta}"
input:
file zipped_fasta
output:
file "*.{fa,fn,fna,fasta}" into fasta_for_indexing
script:
rm_zip = zipped_fasta - '.gz'
"""
pigz -f -d -p ${task.cpus} $zipped_fasta
"""
}
} else {
fasta_for_indexing = file("${params.fasta}")
lastPath = params.fasta.lastIndexOf(File.separator)
bwa_base = params.fasta.substring(lastPath+1)
}
//Index files provided? Then check whether they are correct and complete
if (params.aligner != 'bwa' && !params.circularmapper && !params.bwamem){
exit 1, "Invalid aligner option. Default is bwa, but specify --circularmapper or --bwamem to use these."
}
if( params.bwa_index && (params.aligner == 'bwa' | params.bwamem)){
lastPath = params.bwa_index.lastIndexOf(File.separator)
bwa_dir = params.bwa_index.substring(0,lastPath+1)
bwa_base = params.bwa_index.substring(lastPath+1)
Channel
.fromPath(bwa_dir, checkIfExists: true)
.ifEmpty { exit 1, "BWA index directory not found: ${bwa_dir}" }
.into {bwa_index; bwa_index_bwamem}
}
//Validate that either pairedEnd or singleEnd has been specified by the user!
if( params.singleEnd || params.pairedEnd || params.bam){
} else {
exit 1, "Please specify either --singleEnd, --pairedEnd to execute the pipeline on FastQ files and --bam for previously processed BAM files!"
}
//Validate that skip_collapse is only set to True for pairedEnd reads!
if (params.skip_collapse && params.singleEnd){
exit 1, "--skip_collapse can only be set for pairedEnd samples!"
}
//Strip mode sanity checking
if (params.strip_input_fastq){
if (!(['strip','replace'].contains(params.strip_mode))) {
exit 1, "--strip_mode can only be set to strip or replace"
}
}
// Has the run name been specified by the user?
// this has the bonus effect of catching both -name and --name
custom_runName = params.name
if( !(workflow.runName ==~ /[a-z]+_[a-z]+/) ){
custom_runName = workflow.runName
}
if( workflow.profile == 'awsbatch') {
// AWSBatch sanity checking
if (!params.awsqueue || !params.awsregion) exit 1, "Specify correct --awsqueue and --awsregion parameters on AWSBatch!"
// Check outdir paths to be S3 buckets if running on AWSBatch
// related: https://github.com/nextflow-io/nextflow/issues/813
if (!params.outdir.startsWith('s3:')) exit 1, "Outdir not on S3 - specify S3 Bucket to run on AWSBatch!"
// Prevent trace files to be stored on S3 since S3 does not support rolling files.
if (workflow.tracedir.startsWith('s3:')) exit 1, "Specify a local tracedir or run without trace! S3 cannot be used for tracefiles."
}
/*
* Create a channel for input read files
* Dump can be used for debugging purposes, e.g. using the -dump-channels operator on run
*/
if( params.readPaths ){
if( params.singleEnd && !params.bam) {
Channel
.from( params.readPaths )
.map { row -> [ row[0], [ file( row[1][0] ) ] ] }
.ifEmpty { exit 1, "params.readPaths or params.bams was empty - no input files supplied!" }
.into { ch_read_files_clip; ch_read_files_fastqc; ch_read_files_complexity_filter_poly_g ; ch_read_unmap}
ch_bam_to_fastq_convert = Channel.empty()
} else if (!params.bam){
Channel
.from( params.readPaths )
.map { row -> [ row[0], [ file( row[1][0] ), file( row[1][1] ) ] ] }
.ifEmpty { exit 1, "params.readPaths or params.bams was empty - no input files supplied!" }
.into { ch_read_files_clip; ch_read_files_fastqc; ch_read_files_complexity_filter_poly_g; ch_read_unmap }
ch_bam_to_fastq_convert = Channel.empty()
} else {
Channel
.from( params.readPaths )
.map { row -> [ file( row ) ] }
.ifEmpty { exit 1, "params.readPaths or params.bams was empty - no input files supplied!" }
.dump()
.set { ch_bam_to_fastq_convert }
//Set up clean channels
ch_read_files_fastqc = Channel.empty()
ch_read_files_complexity_filter_poly_g = Channel.empty()
ch_read_files_clip = Channel.empty()
ch_read_unmap = Channel.empty()
}
} else if (!params.bam){
Channel
.fromFilePairs( params.reads, size: params.singleEnd ? 1 : 2 )
.ifEmpty { exit 1, "Cannot find any reads matching: ${params.reads}\nNB: Path needs" +
"to be enclosed in quotes!\nNB: Path requires at least one * wildcard!\nIf this is single-end data, please specify --singleEnd on the command line." }
.into { ch_read_files_clip; ch_read_files_fastqc; ch_read_files_complexity_filter_poly_g; ch_read_unmap }
ch_bam_to_fastq_convert = Channel.empty()
} else {
Channel
.fromPath( params.reads )
.map { row -> [ file( row ) ] }
.ifEmpty { exit 1, "Cannot find any bam file matching: ${params.reads}\nNB: Path needs" +
"to be enclosed in quotes!\n" }
.dump() //For debugging purposes
.set { ch_bam_to_fastq_convert }
//Set up clean channels
ch_read_files_fastqc = Channel.empty()
ch_read_files_complexity_filter_poly_g = Channel.empty()
ch_read_files_clip = Channel.empty()
ch_read_unmap = Channel.empty()
}
// Header log info
log.info nfcoreHeader()
def summary = [:]
summary['Pipeline Name'] = 'nf-core/eager'
summary['Pipeline Version'] = workflow.manifest.version
summary['Run Name'] = custom_runName ?: workflow.runName
summary['Reads'] = params.reads
summary['Fasta Ref'] = params.fasta
summary['BAM Index Type'] = (params.large_ref == "") ? 'BAI' : 'CSI'
if(params.bwa_index) summary['BWA Index'] = params.bwa_index
summary['Data Type'] = params.singleEnd ? 'Single-End' : 'Paired-End'
summary['Skip Collapsing'] = params.skip_collapse ? 'Yes' : 'No'
summary['Skip Trimming'] = params.skip_trim ? 'Yes' : 'No'
summary['Output stripped fastq'] = params.strip_input_fastq ? 'Yes' : 'No'
if (params.strip_input_fastq){
summary['Strip mode'] = params.strip_mode
}
summary['Max Memory'] = params.max_memory
summary['Max CPUs'] = params.max_cpus
summary['Max Time'] = params.max_time
summary['Output dir'] = params.outdir
summary['Working dir'] = workflow.workDir
summary['Container Engine'] = workflow.containerEngine
if(workflow.containerEngine) summary['Container'] = workflow.container
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Working dir'] = workflow.workDir
summary['Output dir'] = params.outdir
summary['Script dir'] = workflow.projectDir
summary['Config Profile'] = workflow.profile
if(workflow.profile == 'awsbatch'){
summary['AWS Region'] = params.awsregion
summary['AWS Queue'] = params.awsqueue
}
if(params.email) summary['E-mail Address'] = params.email
summary['Config Profile'] = workflow.profile
if(params.config_profile_description) summary['Config Description'] = params.config_profile_description
if(params.config_profile_contact) summary['Config Contact'] = params.config_profile_contact
if(params.config_profile_url) summary['Config URL'] = params.config_profile_url
if(params.email) {
summary['E-mail Address'] = params.email
summary['MultiQC maxsize'] = params.maxMultiqcEmailFileSize
}
log.info summary.collect { k,v -> "${k.padRight(18)}: $v" }.join("\n")
log.info "\033[2m----------------------------------------------------\033[0m"
// Check the hostnames against configured profiles
checkHostname()
def create_workflow_summary(summary) {
def yaml_file = workDir.resolve('workflow_summary_mqc.yaml')
yaml_file.text = """
id: 'nf-core-eager-summary'
description: " - this information is collected when the pipeline is started."
section_name: 'nf-core/eager Workflow Summary'
section_href: 'https://github.com/nf-core/eager'
plot_type: 'html'
data: |
<dl class=\"dl-horizontal\">
${summary.collect { k,v -> " <dt>$k</dt><dd><samp>${v ?: '<span style=\"color:#999999;\">N/A</a>'}</samp></dd>" }.join("\n")}
</dl>
""".stripIndent()
return yaml_file
}
/*
* Create BWA indices if they are not present
*/
if(!params.bwa_index && !params.fasta.isEmpty() && (params.aligner == 'bwa' || params.bwamem)){
process makeBWAIndex {
tag {fasta}
publishDir path: "${params.outdir}/reference_genome/bwa_index", mode: 'copy', saveAs: { filename ->
if (params.saveReference) filename
else if(!params.saveReference && filename == "where_are_my_files.txt") filename
else null
}
input:
file fasta from fasta_for_indexing
file where_are_my_files
output:
file "BWAIndex" into (bwa_index, bwa_index_bwamem)
file "where_are_my_files.txt"
script:
"""
bwa index $fasta
mkdir BWAIndex && mv ${fasta}* BWAIndex
"""
}
}
/*
* PREPROCESSING - Index Fasta file if not specified on CLI
*/
process makeFastaIndex {
tag {fasta}
publishDir path: "${params.outdir}/reference_genome/fasta_index", mode: 'copy', saveAs: { filename ->
if (params.saveReference) filename
else if(!params.saveReference && filename == "where_are_my_files.txt") filename
else null
}
when: !params.fasta_index && !params.fasta.isEmpty() && params.aligner == 'bwa'
input:
file fasta from fasta_for_indexing
file where_are_my_files
output:
file "*.fai" into ch_fasta_faidx_index
file "where_are_my_files.txt"
script:
"""
samtools faidx $fasta
"""
}
/*
* PREPROCESSING - Create Sequence Dictionary for FastA if not specified on CLI
*/
process makeSeqDict {
tag {fasta}
publishDir path: "${params.outdir}/reference_genome/seq_dict", mode: 'copy', saveAs: { filename ->
if (params.saveReference) filename
else if(!params.saveReference && filename == "where_are_my_files.txt") filename
else null
}
when: !params.seq_dict && !params.fasta.isEmpty()
input:
file fasta from fasta_for_indexing
file where_are_my_files
output:
file "*.dict" into ch_seq_dict
file "where_are_my_files.txt"
script:
"""
picard -Xmx${task.memory.toMega()}M -Xms${task.memory.toMega()}M CreateSequenceDictionary R=$fasta O="${fasta.baseName}.dict"
"""
}
/*
* Convert BAM to FastQ if BAM input is specified instead of FastQ file(s)
*
*/
process convertBam {
tag "$bam"
when: params.bam
input:
file bam from ch_bam_to_fastq_convert
output:
set val("${base}"), file("*.fastq.gz") into (ch_read_files_converted_fastqc, ch_read_files_converted_fastp, ch_read_files_converted_mapping_bwa, ch_read_files_converted_mapping_cm, ch_read_files_converted_mapping_bwamem,
ch_read_unmap_convertBam)
script:
base = "${bam.baseName}"
"""
samtools fastq -tn ${bam} | pigz -p ${task.cpus} > ${base}.fastq.gz
"""
}
/*
* STEP 1 - FastQC
*/
process fastqc {
tag "$name"
publishDir "${params.outdir}/FastQC", mode: 'copy',
saveAs: {filename -> filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"}
when: !params.skip_fastqc
input:
set val(name), file(reads) from ch_read_files_fastqc.mix(ch_read_files_converted_fastqc)
output:
file "*_fastqc.{zip,html}" into ch_fastqc_results
script:
"""
fastqc -q $reads
rename 's/_fastqc\\.zip\$/_raw_fastqc.zip/' *_fastqc.zip
rename 's/_fastqc\\.html\$/_raw_fastqc.html/' *_fastqc.html
"""
}
/* STEP 2.0 - FastP
* Optional poly-G complexity filtering step before read merging/adapter clipping etc
* Note: Clipping, Merging, Quality Trimning are turned off here - we leave this to adapter removal itself!
*/
process fastp {
tag "$name"
publishDir "${params.outdir}/FastP", mode: 'copy'
when: params.complexity_filter_poly_g
input:
set val(name), file(reads) from ch_read_files_complexity_filter_poly_g.mix(ch_read_files_converted_fastp)
output:
set val(name), file("*pG.fq.gz") into ch_clipped_reads_complexity_filtered_poly_g
file("*.json") into ch_fastp_for_multiqc
script:
if(params.singleEnd){
"""
fastp --in1 ${reads[0]} --out1 "${reads[0].baseName}.pG.fq.gz" -A -g --poly_g_min_len "${params.complexity_filter_poly_g_min}" -Q -L -w ${task.cpus} --json "${reads[0].baseName}"_fastp.json
"""
} else {
"""
fastp --in1 ${reads[0]} --in2 ${reads[1]} --out1 "${reads[0].baseName}.pG.fq.gz" --out2 "${reads[1].baseName}.pG.fq.gz" -A -g --poly_g_min_len "${params.complexity_filter_poly_g_min}" -Q -L -w ${task.cpus} --json "${reads[0].baseName}"_fastp.json
"""
}
}
/*
* STEP 2 - Adapter Clipping / Read Merging
*/
//Initialize empty channel if we skip adapterremoval entirely
if(params.skip_adapterremoval) {
//No logs if no AR is run
ch_adapterremoval_logs = Channel.empty()
//Either coming from complexity filtering, or directly use reads normally directed to clipping first and push them through to the other channels downstream!
ch_clipped_reads_complexity_filtered_poly_g.mix(ch_read_files_clip).into { ch_clipped_reads;ch_clipped_reads_for_fastqc;ch_clipped_reads_circularmapper;ch_clipped_reads_bwamem }
} else {
process adapter_removal {
tag "$name"
publishDir "${params.outdir}/read_merging", mode: 'copy'
when: !params.bam && !params.skip_adapterremoval
input:
set val(name), file(reads) from ( params.complexity_filter_poly_g ? ch_clipped_reads_complexity_filtered_poly_g : ch_read_files_clip )
output:
set val(base), file("output/*.gz") into (ch_clipped_reads,ch_clipped_reads_for_fastqc,ch_clipped_reads_circularmapper,ch_clipped_reads_bwamem)
file("*.settings") into ch_adapterremoval_logs
script:
base = reads[0].baseName
//This checks whether we skip trimming and defines a variable respectively
trim_me = params.skip_trim ? '' : "--trimns --trimqualities --adapter1 ${params.clip_forward_adaptor} --adapter2 ${params.clip_reverse_adaptor} --minlength ${params.clip_readlength} --minquality ${params.clip_min_read_quality} --minadapteroverlap ${params.min_adap_overlap}"
collapse_me = params.skip_collapse ? '' : '--collapse'
//PE mode, dependent on trim_me and collapse_me the respective procedure is run or not :-)
if (!params.singleEnd && !params.skip_collapse && !params.skip_trim){
"""
mkdir -p output
AdapterRemoval --file1 ${reads[0]} --file2 ${reads[1]} --basename ${base} ${trim_me} --gzip --threads ${task.cpus} ${collapse_me}
#Combine files
zcat *.collapsed.gz *.collapsed.truncated.gz *.singleton.truncated.gz *.pair1.truncated.gz *.pair2.truncated.gz | gzip > output/${base}.combined.fq.gz
"""
//PE, don't collapse, but trim reads
} else if (!params.singleEnd && params.skip_collapse && !params.skip_trim) {
"""
mkdir -p output
AdapterRemoval --file1 ${reads[0]} --file2 ${reads[1]} --basename ${base} --gzip --threads ${task.cpus} ${trim_me} ${collapse_me}
mv ${base}.pair*.truncated.gz output/
"""
//PE, collapse, but don't trim reads
} else if (!params.singleEnd && !params.skip_collapse && params.skip_trim) {
"""
mkdir -p output
AdapterRemoval --file1 ${reads[0]} --file2 ${reads[1]} --basename ${base} --gzip --threads ${task.cpus} --basename ${base} ${collapse_me} ${trim_me}
mv ${base}.pair*.truncated.gz output/
"""
} else {
//SE, collapse not possible, trim reads
"""
mkdir -p output
AdapterRemoval --file1 ${reads[0]} --basename ${base} --gzip --threads ${task.cpus} ${trim_me}
mv *.truncated.gz output/
"""
}
}
}
/*
* STEP 2.1 - FastQC after clipping/merging (if applied!)
*/
process fastqc_after_clipping {
tag "${name}"
publishDir "${params.outdir}/FastQC/after_clipping", mode: 'copy',
saveAs: {filename -> filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"}
when: !params.bam && !params.skip_adapterremoval && !params.skip_fastqc
input:
set val(name), file(reads) from ch_clipped_reads_for_fastqc
output:
file "*_fastqc.{zip,html}" optional true into ch_fastqc_after_clipping
script:
"""
fastqc -q $reads
"""
}
/*
Step 3: Mapping with BWA, SAM to BAM, Sort BAM
*/
process bwa {
tag "${name}"
publishDir "${params.outdir}/mapping/bwa", mode: 'copy'
when: !params.circularmapper && !params.bwamem
input:
set val(name), file(reads) from ch_clipped_reads.mix(ch_read_files_converted_mapping_bwa)
file index from bwa_index
output:
file "*.sorted.bam" into ch_mapped_reads_idxstats,ch_mapped_reads_filter,ch_mapped_reads_preseq, ch_mapped_reads_damageprofiler, ch_bwa_mapped_reads_strip
file "*.{bai,csi}" into ch_bam_index_for_damageprofiler
script:
size = "${params.large_ref}" ? '-c' : ''
fasta = "${index}/${bwa_base}"
//PE data without merging, PE data without any AR applied
if (!params.singleEnd && (params.skip_collapse || params.skip_adapterremoval || params.skip_trim)){
prefix = "${reads[0].baseName}"
"""
bwa aln -t ${task.cpus} $fasta ${reads[0]} -n ${params.bwaalnn} -l ${params.bwaalnl} -k ${params.bwaalnk} -f ${prefix}.r1.sai
bwa aln -t ${task.cpus} $fasta ${reads[1]} -n ${params.bwaalnn} -l ${params.bwaalnl} -k ${params.bwaalnk} -f ${prefix}.r2.sai
bwa sampe -r "@RG\\tID:ILLUMINA-${prefix}\\tSM:${prefix}\\tPL:illumina" $fasta ${prefix}.r1.sai ${prefix}.r2.sai ${reads[0]} ${reads[1]} | samtools sort -@ ${task.cpus} -O bam - > ${prefix}.sorted.bam
samtools index "${size}" "${prefix}".sorted.bam
"""
} else {
//PE collapsed, or SE data
prefix = "${reads.baseName}"
"""
bwa aln -t ${task.cpus} $fasta $reads -n ${params.bwaalnn} -l ${params.bwaalnl} -k ${params.bwaalnk} -f ${prefix}.sai
bwa samse -r "@RG\\tID:ILLUMINA-${prefix}\\tSM:${prefix}\\tPL:illumina" $fasta ${prefix}.sai $reads | samtools sort -@ ${task.cpus} -O bam - > "${prefix}".sorted.bam
samtools index "${size}" "${prefix}".sorted.bam
"""
}
}
process circulargenerator{
tag "$prefix"
publishDir "${params.outdir}/reference_genome/circularmapper_index", mode: 'copy', saveAs: { filename ->
if (params.saveReference) filename
else if(!params.saveReference && filename == "where_are_my_files.txt") filename
else null
}
when: params.circularmapper
input:
file fasta from fasta_for_indexing
output:
file "${prefix}.{amb,ann,bwt,sa,pac}" into ch_circularmapper_indices
script:
prefix = "${fasta.baseName}_${params.circularextension}.fasta"
"""
circulargenerator -e ${params.circularextension} -i $fasta -s ${params.circulartarget}
bwa index $prefix
"""
}
process circularmapper{
tag "$prefix"
publishDir "${params.outdir}/mapping/circularmapper", mode: 'copy'
when: params.circularmapper
input:
set val(name), file(reads) from ch_clipped_reads_circularmapper.mix(ch_read_files_converted_mapping_cm)
file index from ch_circularmapper_indices
file fasta from fasta_for_indexing
output:
file "*.sorted.bam" into ch_mapped_reads_idxstats_cm,ch_mapped_reads_filter_cm,ch_mapped_reads_preseq_cm, ch_mapped_reads_damageprofiler_cm, ch_circular_mapped_reads_strip
file "*.{bai,csi}"
script:
filter = "${params.circularfilter}" ? '' : '-f true -x false'
elongated_root = "${fasta.baseName}_${params.circularextension}.fasta"
size = "${params.large_ref}" ? '-c' : ''
if (!params.singleEnd && params.skip_collapse ){
prefix = reads[0].toString().tokenize('.')[0]
"""
bwa aln -t ${task.cpus} $elongated_root ${reads[0]} -n ${params.bwaalnn} -l ${params.bwaalnl} -k ${params.bwaalnk} -f ${prefix}.r1.sai
bwa aln -t ${task.cpus} $elongated_root ${reads[1]} -n ${params.bwaalnn} -l ${params.bwaalnl} -k ${params.bwaalnk} -f ${prefix}.r2.sai
bwa sampe -r "@RG\\tID:ILLUMINA-${prefix}\\tSM:${prefix}\\tPL:illumina" $elongated_root ${prefix}.r1.sai ${prefix}.r2.sai ${reads[0]} ${reads[1]} > tmp.out
realignsamfile -e ${params.circularextension} -i tmp.out -r $fasta $filter
samtools sort -@ ${task.cpus} -O bam tmp_realigned.bam > ${prefix}.sorted.bam
samtools index "${size}" ${prefix}.sorted.bam
"""
} else {
prefix = reads[0].toString().tokenize('.')[0]
"""
bwa aln -t ${task.cpus} $elongated_root $reads -n ${params.bwaalnn} -l ${params.bwaalnl} -k ${params.bwaalnk} -f ${prefix}.sai
bwa samse -r "@RG\\tID:ILLUMINA-${prefix}\\tSM:${prefix}\\tPL:illumina" $elongated_root ${prefix}.sai $reads > tmp.out
realignsamfile -e ${params.circularextension} -i tmp.out -r $fasta $filter
samtools sort -@ ${task.cpus} -O bam tmp_realigned.bam > "${prefix}".sorted.bam
samtools index "${size}" "${prefix}".sorted.bam
"""
}
}
process bwamem {
tag "$prefix"
publishDir "${params.outdir}/mapping/bwamem", mode: 'copy'
when: params.bwamem && !params.circularmapper
input:
set val(name), file(reads) from ch_clipped_reads_bwamem.mix(ch_read_files_converted_mapping_bwamem)
file index from bwa_index_bwamem
output:
file "*.sorted.bam" into ch_bwamem_mapped_reads_idxstats,ch_bwamem_mapped_reads_filter,ch_bwamem_mapped_reads_preseq, ch_bwamem_mapped_reads_damageprofiler, ch_bwamem_mapped_reads_strip
file "*.{bai,csi}"
script:
fasta = "${index}/${bwa_base}"
prefix = reads[0].toString() - ~/(_R1)?(\.combined\.)?(prefixed)?(_trimmed)?(_val_1)?(\.fq)?(\.fastq)?(\.gz)?$/
size = "${params.large_ref}" ? '-c' : ''
if (!params.singleEnd && params.skip_collapse){
"""
bwa mem -t ${task.cpus} $fasta ${reads[0]} ${reads[1]} -R "@RG\\tID:ILLUMINA-${prefix}\\tSM:${prefix}\\tPL:illumina" | samtools sort -@ ${task.cpus} -O bam - > "${prefix}".sorted.bam
samtools index "${size}" -@ ${task.cpus} "${prefix}".sorted.bam
"""
} else {
"""
bwa mem -t ${task.cpus} $fasta $reads -R "@RG\\tID:ILLUMINA-${prefix}\\tSM:${prefix}\\tPL:illumina" | samtools sort -@ ${task.cpus} -O bam - > "${prefix}".sorted.bam
samtools index "${size}" -@ ${task.cpus} "${prefix}".sorted.bam
"""
}
}
/*
* Step 4 - IDXStats
*/
process samtools_idxstats {
tag "$prefix"
publishDir "${params.outdir}/samtools/stats", mode: 'copy'
input:
file(bam) from ch_mapped_reads_idxstats.mix(ch_mapped_reads_idxstats_cm,ch_bwamem_mapped_reads_idxstats)
output:
file "*.stats" into ch_idxstats_for_multiqc
script:
prefix = "$bam" - ~/(\.bam)?$/
"""
samtools flagstat $bam > ${prefix}.stats
"""
}
/*
* Step 5: Keep unmapped/remove unmapped reads
*/
process samtools_filter {
tag "$prefix"
publishDir "${params.outdir}/samtools/filter", mode: 'copy',
saveAs: {filename ->
if (filename.indexOf(".fq.gz") > 0) "unmapped/$filename"
else if (filename.indexOf(".unmapped.bam") > 0) "unmapped/$filename"
else if (filename.indexOf(".filtered.bam")) filename
else null
}
input:
file bam from ch_mapped_reads_filter.mix(ch_mapped_reads_filter_cm,ch_bwamem_mapped_reads_filter)
output:
file "*filtered.bam" into ch_bam_filtered_qualimap, ch_bam_filtered_dedup, ch_bam_filtered_markdup, ch_bam_filtered_pmdtools, ch_bam_filtered_angsd, ch_bam_filtered_gatk
file "*.fastq.gz" optional true
file "*.unmapped.bam" optional true
file "*.{bai,csi}"
script:
prefix="$bam" - ~/(\.bam)?/
size = "${params.large_ref}" ? '-c' : ''
if("${params.bam_discard_unmapped}" && "${params.bam_unmapped_type}" == "discard"){
"""
samtools view -h -b $bam -@ ${task.cpus} -F4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam
samtools index "${size}" ${prefix}.filtered.bam
"""
} else if("${params.bam_discard_unmapped}" && "${params.bam_unmapped_type}" == "bam"){
"""
samtools view -h $bam | tee >(samtools view - -@ ${task.cpus} -f4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.unmapped.bam) >(samtools view - -@ ${task.cpus} -F4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
samtools index "${size}" ${prefix}.filtered.bam
"""
} else if("${params.bam_discard_unmapped}" && "${params.bam_unmapped_type}" == "fastq"){
"""
samtools view -h $bam | tee >(samtools view - -@ ${task.cpus} -f4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.unmapped.bam) >(samtools view - -@ ${task.cpus} -F4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
samtools index "${size}" ${prefix}.filtered.bam
samtools fastq -tn ${prefix}.unmapped.bam | pigz -p ${task.cpus} > ${prefix}.unmapped.fastq.gz
rm ${prefix}.unmapped.bam
"""
} else if("${params.bam_discard_unmapped}" && "${params.bam_unmapped_type}" == "both"){
"""
samtools view -h $bam | tee >(samtools view - -@ ${task.cpus} -f4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.unmapped.bam) >(samtools view - -@ ${task.cpus} -F4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
samtools index "${size}" ${prefix}.filtered.bam
samtools fastq -tn ${prefix}.unmapped.bam | pigz -p ${task.cpus} > ${prefix}.unmapped.fastq.gz
"""
} else { //Only apply quality filtering, default
"""
samtools view -h -b $bam -@ ${task.cpus} -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam
samtools index "${size}" ${prefix}.filtered.bam
"""
}
}
process strip_input_fastq {
tag "${bam.baseName}"
publishDir "${params.outdir}/samtools/stripped_fastq", mode: 'copy'
when: params.strip_input_fastq
input:
set val(name), file(fq) from ch_read_unmap.mix(ch_read_unmap_convertBam)
file bam from ch_bwa_mapped_reads_strip.mix(ch_circular_mapped_reads_strip, ch_bwamem_mapped_reads_strip)
output:
file "*.fq.gz" into unmapped_fq_ch
script:
if (params.singleEnd) {
out_fwd = bam.baseName+'.stripped.fq.gz'
"""
samtools index $bam
extract_map_reads.py $bam ${fq[0]} -m ${params.strip_mode} -of $out_fwd -p ${task.cpus}
"""
} else {
out_fwd = bam.baseName+'.stripped.fwd.fq.gz'
out_rev = bam.baseName+'.stripped.rev.fq.gz'
"""
samtools index $bam
extract_map_reads.py $bam ${fq[0]} -rev ${fq[1]} -m ${params.strip_mode} -of $out_fwd -or $out_rev -p ${task.cpus}
"""
}
}
/*
Step 6: DeDup / MarkDuplicates
*/
process dedup{
tag "${bam.baseName}"
publishDir "${params.outdir}/deduplication/dedup", mode: 'copy',
saveAs: {filename -> "${prefix}/$filename"}
when:
!params.skip_deduplication && params.dedupper == 'dedup'
input:
file bam from ch_bam_filtered_dedup
output:
file "*.hist" into ch_hist_for_preseq
file "*.log" into ch_dedup_results_for_multiqc
file "${prefix}.sorted.bam" into ch_dedup_bam
file "*.{bai,csi}"
script:
prefix="${bam.baseName}"
treat_merged="${params.dedup_all_merged}" ? '-m' : ''
size = "${params.large_ref}" ? '-c' : ''
if(params.singleEnd) {
"""
dedup -i $bam $treat_merged -o . -u
mv *.log dedup.log
samtools sort -@ ${task.cpus} "$prefix"_rmdup.bam -o "$prefix".sorted.bam
samtools index "${size}" "$prefix".sorted.bam
"""
} else {
"""
dedup -i $bam $treat_merged -o . -u
mv *.log dedup.log
samtools sort -@ ${task.cpus} "$prefix"_rmdup.bam -o "$prefix".sorted.bam