forked from nf-core/chipseq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.nf
executable file
·1025 lines (870 loc) · 34.9 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
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
/*
========================================================================================
C H I P - S E Q B E S T P R A C T I C E
========================================================================================
ChIP-seq Best Practice Analysis Pipeline. Started May 2016.
#### Homepage / Documentation
https://github.com/nf-core/chipseq
@#### Authors
Chuan Wang <chuan.wang@scilifelab.se>
Phil Ewels <phil.ewels@scilifelab.se>
Alex Peltzer <alexander.peltzer@qbic.uni-tuebingen.de>
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
Pipeline overview:
- 1: FastQC for raw sequencing reads quality control
- 2: Trim Galore! for adapter trimming
- 3.1: BWA alignment against reference genome
- 3.2: Post-alignment processing and format conversion
- 3.3: Statistics about mapped reads
- 4: Picard for duplicate read identification
- 5: Statistics about read counts
- 6.1: Phantompeakqualtools for normalized strand cross-correlation (NSC) and relative strand cross-correlation (RSC)
- 6.2: Summarize NSC and RSC
- 7: deepTools for fingerprint, coverage bigwig, and correlation plots of reads over genome-wide bins
- 8: NGSplot for distribution of reads around transcription start sites (TSS) and gene bodies
- 9.1: MACS for peak calling
- 9.2: Saturation analysis using MACS when specified
- 10: Post peak calling processing: blacklist filtering and annotation
- 11: MultiQC
- 12: Output Description HTML
----------------------------------------------------------------------------------------
*/
def helpMessage() {
log.info"""
=========================================
nf-core/chipseq : ChIP-Seq Best Practice v${params.version}
=========================================
Usage:
The typical command for running the pipeline is as follows:
nextflow run nf-core/chipseq --reads '*_R{1,2}.fastq.gz' --genome GRCh37 --macsconfig 'macssetup.config' -profile uppmax
Mandatory arguments:
--reads Path to input data (must be surrounded with quotes).
--genome Name of iGenomes reference
--macsconfig Configuration file for peaking calling using MACS. Format: ChIPSampleID,CtrlSampleID,AnalysisID
-profile Hardware config to use. uppmax / uppmax_modules / docker / aws
Options:
--singleEnd Specifies that the input is single end reads
--allow_multi_align Secondary alignments and unmapped reads are also reported in addition to primary alignments
--saturation Run saturation analysis by peak calling with subsets of reads
--broad Run MACS with the --broad flag
--blacklist_filtering Filter ENCODE blacklisted regions from ChIP-seq peaks. It only works when --genome is set as GRCh37 or GRCm38
Presets:
--extendReadsLen [int] Number of base pairs to extend the reads for the deepTools analysis. Default: 100
References
--fasta Path to Fasta reference
--bwa_index Path to BWA index
--gtf Path to GTF file (Ensembl format)
--blacklist Path to blacklist regions (.BED format), used for filtering out called peaks. Note that --blacklist_filtering is required
--saveReference Save the generated reference files in the Results directory.
--saveAlignedIntermediates Save the intermediate BAM files from the Alignment step - not done by default
Trimming options
--notrim Specifying --notrim will skip the adapter trimming step.
--saveTrimmed Save the trimmed Fastq files in the the Results directory.
--clip_r1 [int] Instructs Trim Galore to remove bp from the 5' end of read 1 (or single-end reads)
--clip_r2 [int] Instructs Trim Galore to remove bp from the 5' end of read 2 (paired-end reads only)
--three_prime_clip_r1 [int] Instructs Trim Galore to remove bp from the 3' end of read 1 AFTER adapter/quality trimming has been performed
--three_prime_clip_r2 [int] Instructs Trim Galore to re move bp from the 3' end of read 2 AFTER adapter/quality trimming has been performed
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
--rlocation Location to save R-libraries used in the pipeline. Default value is ~/R/nxtflow_libs/
--clusterOptions Extra SLURM options, used in conjunction with Uppmax.config
-name Name for the pipeline run. If not specified, Nextflow will automatically generate a random mnemonic
""".stripIndent()
}
/*
* SET UP CONFIGURATION VARIABLES
*/
// Show help emssage
params.help = false
if (params.help){
helpMessage()
exit 0
}
// Configurable variables
params.name = false
params.project = false
params.genome = false
params.genomes = false
params.fasta = params.genome ? params.genomes[ params.genome ].fasta ?: false : false
params.bwa_index = params.genome ? params.genomes[ params.genome ].bwa ?: false : false
params.gtf = params.genome ? params.genomes[ params.genome ].gtf ?: false : false
params.blacklist = params.genome ? params.genomes[ params.genome ].blacklist ?: false : false
// R library locations
params.rlocation = false
if (params.rlocation){
nxtflow_libs = file(params.rlocation)
nxtflow_libs.mkdirs()
}
multiqc_config = file(params.multiqc_config)
output_docs = file("$baseDir/docs/output.md")
// Custom trimming options
params.clip_r1 = 0
params.clip_r2 = 0
params.three_prime_clip_r1 = 0
params.three_prime_clip_r2 = 0
// Validate inputs
macsconfig = file(params.macsconfig)
if( !macsconfig.exists() ) exit 1, "Missing MACS config: '$macsconfig'. Specify path with --macsconfig"
if( params.bwa_index ){
bwa_index = Channel
.fromPath(params.bwa_index)
.ifEmpty { exit 1, "BWA index not found: ${params.bwa_index}" }
} else if ( params.fasta ){
fasta = file(params.fasta)
if( !fasta.exists() ) exit 1, "Fasta file not found: ${params.fasta}"
} else {
exit 1, "No reference genome specified!"
}
gtf = false
if( params.gtf ){
gtf = file(params.gtf)
if( !gtf.exists() ) exit 1, "GTF file not found: ${params.gtf}."
}
if ( params.blacklist_filtering ){
blacklist = file(params.blacklist)
if( !blacklist.exists() ) exit 1, "Blacklist file not found: ${params.blacklist}"
}
if( workflow.profile == 'standard' && !params.project ) exit 1, "No UPPMAX project ID found! Use --project"
// 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(params.readPaths){
if(params.singleEnd){
Channel
.from(params.readPaths)
.map { row -> [ row[0], [file(row[1][0])]] }
.ifEmpty { exit 1, "params.readPaths was empty - no input files supplied" }
.into { raw_reads_fastqc; raw_reads_trimgalore }
} else {
Channel
.from(params.readPaths)
.map { row -> [ row[0], [file(row[1][0]), file(row[1][1])]] }
.ifEmpty { exit 1, "params.readPaths was empty - no input files supplied" }
.into { raw_reads_fastqc; raw_reads_trimgalore }
}
} else {
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!\nIf this is single-end data, please specify --singleEnd on the command line." }
.into { raw_reads_fastqc; raw_reads_trimgalore }
}
/*
* Create a channel for macs config file
*/
Channel
.from(macsconfig.readLines())
.map { line ->
list = line.split(',')
chip_sample_id = list[0]
ctrl_sample_id = list[1]
analysis_id = list[2]
[ chip_sample_id, ctrl_sample_id, analysis_id ]
}
.into{ macs_para; saturation_para }
/*
* Reference to use for MACS, ngs.plot.r and annotation
*/
def REF_macs = false
def REF_ngsplot = false
if (params.genome == 'GRCh37'){ REF_macs = 'hs'; REF_ngsplot = 'hg19' }
else if (params.genome == 'GRCm38'){ REF_macs = 'mm'; REF_ngsplot = 'mm10' }
log.info """=======================================================
,--./,-.
___ __ __ __ ___ /,-._.--~\'
|\\ | |__ __ / ` / \\ |__) |__ } {
| \\| | \\__, \\__/ | \\ |___ \\`-._,-`-,
`._,._,\'
nf-core/chipseq : ChIP-Seq Best Practice v${params.version}
======================================================="""
def summary = [:]
summary['Run Name'] = custom_runName ?: workflow.runName
summary['Reads'] = params.reads
summary['Data Type'] = params.singleEnd ? 'Single-End' : 'Paired-End'
summary['Genome'] = params.genome
if(params.bwa_index) summary['BWA Index'] = params.bwa_index
else if(params.fasta) summary['Fasta Ref'] = params.fasta
if(params.gtf) summary['GTF File'] = params.gtf
summary['Multiple alignments'] = params.allow_multi_align
summary['MACS Config'] = params.macsconfig
summary['Saturation analysis'] = params.saturation
summary['MACS broad peaks'] = params.broad
summary['Blacklist filtering'] = params.blacklist_filtering
if( params.blacklist_filtering ) summary['Blacklist BED'] = params.blacklist
summary['Extend Reads'] = "$params.extendReadsLen bp"
summary['Container'] = workflow.container
if(workflow.revision) summary['Pipeline Release'] = workflow.revision
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Working dir'] = workflow.workDir
summary['Output dir'] = params.outdir
summary['R libraries'] = params.rlocation
summary['Script dir'] = workflow.projectDir
summary['Save Reference'] = params.saveReference
summary['Save Trimmed'] = params.saveTrimmed
summary['Save Intermeds'] = params.saveAlignedIntermediates
if( params.notrim ){
summary['Trimming Step'] = 'Skipped'
} else {
summary['Trim R1'] = params.clip_r1
summary['Trim R2'] = params.clip_r2
summary["Trim 3' R1"] = params.three_prime_clip_r1
summary["Trim 3' R2"] = params.three_prime_clip_r2
}
summary['Config Profile'] = workflow.profile
if(params.project) summary['UPPMAX Project'] = params.project
if(params.email) summary['E-mail Address'] = params.email
if(workflow.commitId) summary['Pipeline Commit']= workflow.commitId
log.info summary.collect { k,v -> "${k.padRight(21)}: $v" }.join("\n")
log.info "===================================="
// Check that Nextflow version is up to date enough
// try / throw / catch works for NF versions < 0.30.1 when this was implemented
nf_required_version = '0.30.1'
try {
if( ! nextflow.version.matches(">= $nf_required_version") ){
throw GroovyException('Nextflow version too old')
}
} catch (all) {
log.error "====================================================\n" +
" Nextflow version $nf_required_version required! You are running v$workflow.nextflow.version.\n" +
" Pipeline execution will continue, but things may break.\n" +
" Please run `nextflow self-update` to update Nextflow.\n" +
"============================================================"
}
// Show a big error message if we're running on the base config and an uppmax cluster
if( workflow.profile == 'standard'){
if ( "hostname".execute().text.contains('.uppmax.uu.se') ) {
log.error "====================================================\n" +
" WARNING! You are running with the default 'standard'\n" +
" pipeline config profile, which runs on the head node\n" +
" and assumes all software is on the PATH.\n" +
" ALL JOBS ARE RUNNING LOCALLY and stuff will probably break.\n" +
" Please use `-profile uppmax` to run on UPPMAX clusters.\n" +
"============================================================"
}
}
// Show a big warning message if we're not running MACS
if (!REF_macs){
def warnstring = params.genome ? "Reference '${params.genome}' not supported by" : 'No reference supplied for'
log.warn "=======================================================\n" +
" WARNING! $warnstring MACS, ngs_plot\n" +
" and annotation. Steps for MACS, ngs_plot and annotation\n" +
" will be skipped. Use '--genome GRCh37' or '--genome GRCm38'\n" +
" to run these steps.\n" +
"==============================================================="
}
/*
* PREPROCESSING - Build BWA index
*/
if(!params.bwa_index && fasta){
process makeBWAindex {
tag fasta
publishDir path: { params.saveReference ? "${params.outdir}/reference_genome" : params.outdir },
saveAs: { params.saveReference ? it : null }, mode: 'copy'
input:
file fasta from fasta
output:
file "BWAIndex" into bwa_index
script:
"""
bwa index -a bwtsw $fasta
mkdir BWAIndex && mv ${fasta}* BWAIndex
"""
}
}
/*
* STEP 1 - FastQC
*/
process fastqc {
tag "$name"
publishDir "${params.outdir}/fastqc", mode: 'copy',
saveAs: {filename -> filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"}
input:
set val(name), file(reads) from raw_reads_fastqc
output:
file '*_fastqc.{zip,html}' into fastqc_results
file '.command.out' into fastqc_stdout
script:
"""
fastqc -q $reads
"""
}
/*
* STEP 2 - Trim Galore!
*/
if(params.notrim){
trimmed_reads = read_files_trimming
trimgalore_results = []
trimgalore_fastqc_reports = []
} else {
process trim_galore {
tag "$name"
publishDir "${params.outdir}/trim_galore", mode: 'copy',
saveAs: {filename ->
if (filename.indexOf("_fastqc") > 0) "FastQC/$filename"
else if (filename.indexOf("trimming_report.txt") > 0) "logs/$filename"
else params.saveTrimmed ? filename : null
}
input:
set val(name), file(reads) from raw_reads_trimgalore
output:
file '*.fq.gz' into trimmed_reads
file '*trimming_report.txt' into trimgalore_results
file "*_fastqc.{zip,html}" into trimgalore_fastqc_reports
script:
c_r1 = params.clip_r1 > 0 ? "--clip_r1 ${params.clip_r1}" : ''
c_r2 = params.clip_r2 > 0 ? "--clip_r2 ${params.clip_r2}" : ''
tpc_r1 = params.three_prime_clip_r1 > 0 ? "--three_prime_clip_r1 ${params.three_prime_clip_r1}" : ''
tpc_r2 = params.three_prime_clip_r2 > 0 ? "--three_prime_clip_r2 ${params.three_prime_clip_r2}" : ''
if (params.singleEnd) {
"""
trim_galore --fastqc --gzip $c_r1 $tpc_r1 $reads
"""
} else {
"""
trim_galore --paired --fastqc --gzip $c_r1 $c_r2 $tpc_r1 $tpc_r2 $reads
"""
}
}
}
/*
* STEP 3.1 - align with bwa
*/
process bwa {
tag "$prefix"
publishDir path: { params.saveAlignedIntermediates ? "${params.outdir}/bwa" : params.outdir }, mode: 'copy',
saveAs: {filename -> params.saveAlignedIntermediates ? filename : null }
input:
file reads from trimmed_reads
file index from bwa_index.first()
output:
file '*.bam' into bwa_bam
script:
prefix = reads[0].toString() - ~/(.R1)?(_1)?(_R1)?(_trimmed)?(_val_1)?(\.fq)?(\.fastq)?(\.gz)?$/
filtering = params.allow_multi_align ? '' : "| samtools view -b -q 1 -F 4 -F 256"
"""
bwa mem -M ${index}/genome.fa $reads | samtools view -bT $index - $filtering > ${prefix}.bam
"""
}
/*
* STEP 3.2 - post-alignment processing
*/
process samtools {
tag "${bam.baseName}"
publishDir path: "${params.outdir}/bwa", mode: 'copy',
saveAs: { filename ->
if (filename.indexOf(".stats.txt") > 0) "stats/$filename"
else params.saveAlignedIntermediates ? filename : null
}
input:
file bam from bwa_bam
output:
file '*.sorted.bam' into bam_picard, bam_for_mapped
file '*.sorted.bam.bai' into bwa_bai, bai_for_mapped
file '*.sorted.bed' into bed_total
file '*.stats.txt' into samtools_stats
script:
"""
samtools sort $bam -o ${bam.baseName}.sorted.bam
samtools index ${bam.baseName}.sorted.bam
bedtools bamtobed -i ${bam.baseName}.sorted.bam | sort -k 1,1 -k 2,2n -k 3,3n -k 6,6 > ${bam.baseName}.sorted.bed
samtools stats ${bam.baseName}.sorted.bam > ${bam.baseName}.stats.txt
"""
}
/*
* STEP 3.3 - Statistics about mapped and unmapped reads against ref genome
*/
process bwa_mapped {
tag "${input_files[0].baseName}"
publishDir "${params.outdir}/bwa/mapped", mode: 'copy'
input:
file input_files from bam_for_mapped.collect()
file bai from bai_for_mapped.collect()
output:
file 'mapped_refgenome.txt' into bwa_mapped
script:
"""
for i in $input_files
do
samtools idxstats \${i} | awk -v filename="\${i}" '{mapped+=\$3; unmapped+=\$4} END {print filename,"\t",mapped,"\t",unmapped}'
done > mapped_refgenome.txt
"""
}
/*
* STEP 4 Picard
*/
process picard {
tag "$prefix"
publishDir "${params.outdir}/picard", mode: 'copy'
input:
file bam from bam_picard
output:
file '*.dedup.sorted.bam' into bam_dedup_spp, bam_dedup_ngsplot, bam_dedup_deepTools, bam_dedup_macs, bam_dedup_saturation
file '*.dedup.sorted.bam.bai' into bai_dedup_deepTools, bai_dedup_ngsplot, bai_dedup_macs, bai_dedup_saturation
file '*.dedup.sorted.bed' into bed_dedup
file '*.picardDupMetrics.txt' into picard_reports
script:
prefix = bam[0].toString() - ~/(\.sorted)?(\.bam)?$/
if( task.memory == null ){
log.warn "[Picard MarkDuplicates] Available memory not known - defaulting to 6GB ($prefix)"
avail_mem = 6000
} else {
avail_mem = task.memory.toMega()
if( avail_mem <= 0){
avail_mem = 6000
log.warn "[Picard MarkDuplicates] Available memory 0 - defaulting to 6GB ($prefix)"
} else if( avail_mem < 250){
avail_mem = 250
log.warn "[Picard MarkDuplicates] Available memory under 250MB - defaulting to 250MB ($prefix)"
}
}
"""
picard MarkDuplicates \\
INPUT=$bam \\
OUTPUT=${prefix}.dedup.bam \\
ASSUME_SORTED=true \\
REMOVE_DUPLICATES=true \\
METRICS_FILE=${prefix}.picardDupMetrics.txt \\
VALIDATION_STRINGENCY=LENIENT \\
PROGRAM_RECORD_ID='null'
samtools sort ${prefix}.dedup.bam -o ${prefix}.dedup.sorted.bam
samtools index ${prefix}.dedup.sorted.bam
bedtools bamtobed -i ${prefix}.dedup.sorted.bam | sort -k 1,1 -k 2,2n -k 3,3n -k 6,6 > ${prefix}.dedup.sorted.bed
"""
}
/*
* STEP 5 Read_count_statistics
*/
process countstat {
tag "${input[0].baseName}"
publishDir "${params.outdir}/countstat", mode: 'copy'
input:
file input from bed_total.mix(bed_dedup).toSortedList()
output:
file 'read_count_statistics.txt' into countstat_results
script:
"""
countstat.pl $input
"""
}
/*
* STEP 6.1 Phantompeakqualtools
* TODO: The "run_spp.R" script is still missing here!
*/
process phantompeakqualtools {
tag "$prefix"
publishDir "${params.outdir}/phantompeakqualtools", mode: 'copy',
saveAs: {filename -> filename.indexOf(".out") > 0 ? "logs/$filename" : "$filename"}
input:
file bam from bam_dedup_spp
output:
file '*.pdf' into spp_results
file '*.spp.out' into spp_out, spp_out_mqc
script:
prefix = bam[0].toString() - ~/(\.dedup)?(\.sorted)?(\.bam)?$/
"""
run_spp.r -c="$bam" -savp -out="${prefix}.spp.out"
"""
}
/*
* STEP 6.2 Combine and calculate NSC & RSC
*/
process calculateNSCRSC {
tag "${spp_out_list[0].baseName}"
publishDir "${params.outdir}/phantompeakqualtools", mode: 'copy'
input:
file spp_out_list from spp_out.collect()
output:
file 'cross_correlation_processed.txt' into calculateNSCRSC_results
script:
"""
cat $spp_out_list > cross_correlation.txt
calculateNSCRSC.r cross_correlation.txt
"""
}
/*
* STEP 7 deepTools
*/
process deepTools {
tag "${bam[0].baseName}"
publishDir "${params.outdir}/deepTools", mode: 'copy'
input:
file bam from bam_dedup_deepTools.collect()
file bai from bai_dedup_deepTools.collect()
output:
file '*.{txt,pdf,png,npz,bw}' into deepTools_results
file '*.txt' into deepTools_multiqc
script:
if (!params.singleEnd) {
"""
bamPEFragmentSize \\
--binSize 1000 \\
--bamfiles $bam \\
--histogram fragment_length_distribution_histogram.png \\
--plotTitle "Fragment Length Distribution"
"""
}
if(bam instanceof Path){
log.warn("Only 1 BAM file - skipping multiBam deepTool steps")
"""
plotFingerprint \\
-b $bam \\
--plotFile ${bam.baseName}_fingerprints.pdf \\
--outRawCounts ${bam.baseName}_fingerprint.txt \\
--extendReads ${params.extendReadsLen} \\
--skipZeros \\
--ignoreDuplicates \\
--numberOfSamples 50000 \\
--binSize 500 \\
--plotFileFormat pdf \\
--plotTitle "${bam.baseName} Fingerprints"
bamCoverage \\
-b $bam \\
--extendReads ${params.extendReadsLen} \\
--normalizeUsing RPKM \\
-o ${bam}.bw
"""
} else {
"""
plotFingerprint \\
-b $bam \\
--plotFile fingerprints.pdf \\
--outRawCounts fingerprint.txt \\
--extendReads ${params.extendReadsLen} \\
--skipZeros \\
--ignoreDuplicates \\
--numberOfSamples 50000 \\
--binSize 500 \\
--plotFileFormat pdf \\
--plotTitle "Fingerprints"
for bamfile in ${bam}
do
bamCoverage \\
-b \$bamfile \\
--extendReads ${params.extendReadsLen} \\
--normalizeUsing RPKM \\
-o \${bamfile}.bw
done
multiBamSummary \\
bins \\
--binSize 10000 \\
--bamfiles $bam \\
-out multiBamSummary.npz \\
--extendReads ${params.extendReadsLen} \\
--ignoreDuplicates \\
--centerReads
plotCorrelation \\
-in multiBamSummary.npz \\
-o scatterplot_PearsonCorr_multiBamSummary.png \\
--outFileCorMatrix scatterplot_PearsonCorr_multiBamSummary.txt \\
--corMethod pearson \\
--skipZeros \\
--removeOutliers \\
--plotTitle "Pearson Correlation of Read Counts" \\
--whatToPlot scatterplot
plotCorrelation \\
-in multiBamSummary.npz \\
-o heatmap_SpearmanCorr_multiBamSummary.png \\
--outFileCorMatrix heatmap_SpearmanCorr_multiBamSummary.txt \\
--corMethod spearman \\
--skipZeros \\
--plotTitle "Spearman Correlation of Read Counts" \\
--whatToPlot heatmap \\
--colorMap RdYlBu \\
--plotNumbers
plotPCA \\
-in multiBamSummary.npz \\
-o pcaplot_multiBamSummary.png \\
--plotTitle "Principal Component Analysis Plot" \\
--outFileNameData pcaplot_multiBamSummary.txt
"""
}
}
/*
* STEP 8 Ngsplot
* TODO ngs.plot.R is missing too!
*/
process ngsplot {
tag "${input_bam_files[0].baseName}"
publishDir "${params.outdir}/ngsplot", mode: 'copy'
input:
file input_bam_files from bam_dedup_ngsplot.collect()
file input_bai_files from bai_dedup_ngsplot.collect()
output:
file '*.pdf' into ngsplot_results
when: REF_ngsplot
script:
"""
ngs_config_generate.r $input_bam_files
ngs.plot.r \\
-G $REF_ngsplot \\
-R genebody \\
-C ngsplot_config \\
-O Genebody \\
-D ensembl \\
-FL 300
ngs.plot.r \\
-G $REF_ngsplot \\
-R tss \\
-C ngsplot_config \\
-O TSS \\
-FL 300
"""
}
/*
* STEP 9.1 MACS
*/
process macs {
tag "${bam_for_macs[0].baseName}"
publishDir "${params.outdir}/macs", mode: 'copy'
input:
file bam_for_macs from bam_dedup_macs.collect()
file bai_for_macs from bai_dedup_macs.collect()
set chip_sample_id, ctrl_sample_id, analysis_id from macs_para
output:
file '*.{bed,r,narrowPeak}' into macs_results
file '*.xls' into macs_peaks
when: REF_macs
script:
def ctrl = ctrl_sample_id == '' ? '' : "-c ${ctrl_sample_id}.dedup.sorted.bam"
broad = params.broad ? "--broad" : ''
"""
macs2 callpeak \\
-t ${chip_sample_id}.dedup.sorted.bam \\
$ctrl \\
$broad \\
-f BAM \\
-g $REF_macs \\
-n $analysis_id \\
-q 0.01
"""
}
/*
* STEP 9.2 Saturation analysis
*/
if (params.saturation) {
process saturation {
tag "${bam_for_saturation[0].baseName}"
publishDir "${params.outdir}/macs/saturation", mode: 'copy'
input:
file bam_for_saturation from bam_dedup_saturation.collect()
file bai_for_saturation from bai_dedup_saturation.collect()
set chip_sample_id, ctrl_sample_id, analysis_id from saturation_para
each sampling from 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
output:
file '*.xls' into saturation_results
when: REF_macs
script:
def ctrl = ctrl_sample_id == '' ? '' : "-c ${ctrl_sample_id}.dedup.sorted.bam"
broad = params.broad ? "--broad" : ''
"""
samtools view -b -s ${sampling} ${chip_sample_id}.dedup.sorted.bam > ${chip_sample_id}.${sampling}.dedup.sorted.bam
macs2 callpeak \\
-t ${chip_sample_id}.${sampling}.dedup.sorted.bam \\
$ctrl \\
$broad \\
-f BAM \\
-g $REF_macs \\
-n ${analysis_id}.${sampling} \\
-q 0.01
"""
}
process saturation_r {
tag "${saturation_results_collection[0].baseName}"
publishDir "${params.outdir}/macs/saturation", mode: 'copy'
input:
file macsconfig from macsconfig
file countstat from countstat_results
file saturation_results_collection from saturation_results.collect()
output:
file '*.{txt,pdf}' into saturation_summary
when: REF_macs
script:
"""
saturation_results_processing.r $params.rlocation $macsconfig $countstat $saturation_results_collection
"""
}
}
/*
* STEP 10 Post peak calling processing
*/
process chippeakanno {
tag "${macs_peaks_collection[0].baseName}"
publishDir "${params.outdir}/macs/chippeakanno", mode: 'copy'
input:
file macs_peaks_collection from macs_peaks.collect()
file gtf from gtf
output:
file '*.{txt,bed}' into chippeakanno_results
when: REF_macs
script:
filtering = params.blacklist_filtering ? "${params.blacklist}" : "No-filtering"
"""
post_peak_calling_processing.r $params.rlocation $REF_macs $filtering $gtf $macs_peaks_collection
"""
}
/*
* Parse software version numbers
*/
process get_software_versions {
output:
file 'software_versions_mqc.yaml' into software_versions_yaml
script:
"""
echo ${params.version} > v_ngi_chipseq.txt
echo $workflow.nextflow.version > v_nextflow.txt
fastqc --version > v_fastqc.txt
trim_galore --version > v_trim_galore.txt
echo \$(bwa 2>&1) > v_bwa.txt
samtools --version > v_samtools.txt
bedtools --version > v_bedtools.txt
echo "version" \$(java -Xmx2g -jar \$PICARD_HOME/picard.jar MarkDuplicates --version 2>&1) >v_picard.txt
echo \$(plotFingerprint --version 2>&1) > v_deeptools.txt
echo \$(ngs.plot.r 2>&1) > v_ngsplot.txt
echo \$(macs2 --version 2>&1) > v_macs2.txt
multiqc --version > v_multiqc.txt
scrape_software_versions.py > software_versions_mqc.yaml
"""
}
/*
* STEP 11 MultiQC
*/
process multiqc {
tag "$prefix"
publishDir "${params.outdir}/MultiQC", mode: 'copy'
input:
file multiqc_config
file (fastqc:'fastqc/*') from fastqc_results.collect()
file ('trimgalore/*') from trimgalore_results.collect()
file ('samtools/*') from samtools_stats.collect()
file ('picard/*') from picard_reports.collect()
file ('deeptools/*') from deepTools_multiqc.collect()
file ('phantompeakqualtools/*') from spp_out_mqc.collect()
file ('phantompeakqualtools/*') from calculateNSCRSC_results.collect()
file ('software_versions/*') from software_versions_yaml.collect()
output:
file '*multiqc_report.html' into multiqc_report
file '*_data' into multiqc_data
file '.command.err' into multiqc_stderr
val prefix into multiqc_prefix
script:
prefix = fastqc[0].toString() - '_fastqc.html' - 'fastqc/'
rtitle = custom_runName ? "--title \"$custom_runName\"" : ''
rfilename = custom_runName ? "--filename " + custom_runName.replaceAll('\\W','_').replaceAll('_+','_') + "_multiqc_report" : ''
"""
multiqc -f $rtitle $rfilename --config $multiqc_config . 2>&1
"""
}
/*
* STEP 12 - Output Description HTML
*/
process output_documentation {
tag "$prefix"
publishDir "${params.outdir}/Documentation", mode: 'copy'
input:
val prefix from multiqc_prefix
file output from output_docs
output:
file "results_description.html"
script:
def rlocation = params.rlocation ?: ''
"""
markdown_to_html.r $output results_description.html $rlocation
"""
}
/*
* Completion e-mail notification
*/
workflow.onComplete {
// Set up the e-mail variables
def subject = "[nf-core/chipseq] Successful: $workflow.runName"
if(!workflow.success){
subject = "[nf-core/chipseq] FAILED: $workflow.runName"
}
def email_fields = [:]
email_fields['version'] = params.version
email_fields['runName'] = custom_runName ?: workflow.runName
email_fields['success'] = workflow.success
email_fields['dateComplete'] = workflow.complete
email_fields['duration'] = workflow.duration
email_fields['exitStatus'] = workflow.exitStatus
email_fields['errorMessage'] = (workflow.errorMessage ?: 'None')
email_fields['errorReport'] = (workflow.errorReport ?: 'None')
email_fields['commandLine'] = workflow.commandLine
email_fields['projectDir'] = workflow.projectDir
email_fields['summary'] = summary
email_fields['summary']['Date Started'] = workflow.start
email_fields['summary']['Date Completed'] = workflow.complete
email_fields['summary']['Nextflow Version'] = workflow.nextflow.version
email_fields['summary']['Nextflow Build'] = workflow.nextflow.build
email_fields['summary']['Nextflow Compile Timestamp'] = workflow.nextflow.timestamp
email_fields['summary']['Pipeline script file path'] = workflow.scriptFile
email_fields['summary']['Pipeline script hash ID'] = workflow.scriptId
if(workflow.repository) email_fields['summary']['Pipeline repository Git URL'] = workflow.repository
if(workflow.commitId) email_fields['summary']['Pipeline repository Git Commit'] = workflow.commitId
if(workflow.revision) email_fields['summary']['Pipeline Git branch/tag'] = workflow.revision
if(workflow.container) email_fields['summary']['Docker image'] = workflow.container
// Render the TXT template
def engine = new groovy.text.GStringTemplateEngine()
def tf = new File("$baseDir/assets/email_template.txt")
def txt_template = engine.createTemplate(tf).make(email_fields)
def email_txt = txt_template.toString()
// Render the HTML template
def hf = new File("$baseDir/assets/email_template.html")
def html_template = engine.createTemplate(hf).make(email_fields)
def email_html = html_template.toString()
// Render the sendmail template
def smail_fields = [ email: params.email, subject: subject, email_txt: email_txt, email_html: email_html, baseDir: "$baseDir" ]
def sf = new File("$baseDir/assets/sendmail_template.txt")
def sendmail_template = engine.createTemplate(sf).make(smail_fields)
def sendmail_html = sendmail_template.toString()
// Send the HTML e-mail
if (params.email) {
try {
if( params.plaintext_email ){ throw GroovyException('Send plaintext e-mail, not HTML') }
// Try to send HTML e-mail using sendmail
[ 'sendmail', '-t' ].execute() << sendmail_html
log.info "[nf-core/chipseq] Sent summary e-mail to $params.email (sendmail)"
} catch (all) {
// Catch failures and try with plaintext
[ 'mail', '-s', subject, params.email ].execute() << email_txt
log.info "[nf-core/chipseq] Sent summary e-mail to $params.email (mail)"
}
}
// Switch the embedded MIME images with base64 encoded src
nfcorechipseqlogo = new File("$baseDir/assets/nf-core_chipseq_logo.png").bytes.encodeBase64().toString()
scilifelablogo = new File("$baseDir/assets/SciLifeLab_logo.png").bytes.encodeBase64().toString()
ngilogo = new File("$baseDir/assets/NGI_logo.png").bytes.encodeBase64().toString()
email_html = email_html.replaceAll(~/cid:nfcorechipseqlogo/, "data:image/png;base64,$nfcorechipseqlogo")
email_html = email_html.replaceAll(~/cid:scilifelablogo/, "data:image/png;base64,$scilifelablogo")
email_html = email_html.replaceAll(~/cid:ngilogo/, "data:image/png;base64,$ngilogo")
// Write summary e-mail HTML to a file