-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.nf
executable file
·1718 lines (1405 loc) · 58.3 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
/*
* =======================================================================
*
* Bidirectional Transcription Detection Pipeline
*
* =======================================================================
*
* This Source Code is distributed under the GPL-3.0 License
*/
/*
* 'Bidirectional-Flow' - A Nextflow pipeline for detecting regions of transcription
* from Nascent RNA sequencing
*
* The pipeline identifies regions of transcription with FStitch, TFit and/or dREG
*
* =============
* Authors
* =============
* Rutendo F. Sigauke : rutendo.sigauke@cuanschutz.edu
* Lynn Sanford : lynn.sanford@colorado.edu
*/
def helpMessage() {
log.info"""
=========================================
BidirectionalFlow v${params.version}
=========================================
Usage:
The typical command for running the pipeline is as follows:
nextflow run main.nf -profile slurm --crams '/project/*.sorted.cram' --workdir '/project/tempfiles' --outdir '/project/'
Required arguments:
-profile Configuration profile to use. <genome_user>
--crams Directory pattern for cram files: /project/*.sorted.cram (Required if --bams not specified).
--bams Directory pattern for bam files: /project/*.sorted.bam (Required if --crams not specified).
--workdir Nextflow working directory where all intermediate files are saved.
Input File options:
--singleEnd Specifies that the input files are not paired reads (default is paired-end).
--forwardStranded If input file is paired, specifies if read 1 has the 5 prime end (default R2 is five prime, must be manually determined)
Save options:
--outdir Specifies where to save the output from the nextflow run.
--savestats Saves tfit/dreg bidirectional statistics for all samples (default true)
--savebam Saves sorted bam files (if cram as input)
--savetfitbam Saves multimapped read filtered bamfiles used for tfit
--savebg Saves bedgraph files for tfit and/or dreg (whichever is run)
--savebw Saves dreg bigwig files
--savebidirs Saves bedfiles for promoter/intronic/intergenic bidirectional subsets
Analysis Options:
--gene_count Run featureCounts to obtain stranded and unstranded gene counts over an annotation.
--bidir_count Run featureCounts to obtain stranded and unstranded bidirectional counts over an annotation (SAF file)
--bedtools_count Run bedtools coverage counting for genes. Uses a multi-mapped read-filtered bam file for counting.
--fstitch Run FStitch. If used, you must also specify FS_path and FS_train params.
--tfit Run Tfit bidir. If used, you must also specify the Tfit_path parameter.
--tfit_prelim Run Tfit bidir. If used, you must also specify the Tfit_path parameter. Not compatible with --prelim_files flag.
--tfit_split_model Run Tfit model separately on <5kb prelim regions and 5-10kb prelim regions (with different k parameters). Not compatible with --tfit_model or --tfit flags, or --prelim_process=false
--tfit_model Run Tfit full model. If used, must specify the Tfit path parameter AND have prelim files from --tfit_prelim process or previous run via the --prelim_files flag. Not compatible with --tfit flag.
--prelim_files Directory pattern for tfit prelim files: /project/*-1_prelim_bidir_hits.bed (required for --tfit_model if --tfit_prelim is not also specified)
--prelim_process Run the prelim processing step for Tfit (default true)
--dreg Produce bigwigs formatted for input to dREG.
--dreg_results Do coverage filtering on existing dreg results
""".stripIndent()
}
// Configure Variables
params.bedGraphToBigWig = "$baseDir/bin/bedGraphToBigWig"
params.tfit_prelim_run = "$baseDir/bin/tfit_prelim.sh"
params.tfit_model_run = "$baseDir/bin/tfit_model.sh"
params.prelim_filter = "$baseDir/bin/prelim_filter.py"
software_versions = Channel.create()
strand_specific = 0
neg_strand = 0
import java.text.SimpleDateFormat
def date = new java.util.Date()
def sdf = new SimpleDateFormat("yyMMdd")
output_date = sdf.format(date)
String output_date = new java.text.SimpleDateFormat("yyMMdd").format(new Date())
// Header log info
log.info """=======================================================
Bidirectional-Flow v${params.version}"
======================================================="""
def summary = [:]
summary['Pipeline Name'] = 'Bidirectional-Flow'
summary['Help Message'] = params.help
summary['Pipeline Version'] = params.version
summary['Run Name'] = workflow.runName
if(params.crams) summary['Crams'] = params.crams
if(params.bams) summary['Bams'] = params.bams
summary['Genome Ref'] = params.genome
summary['Data Type'] = params.singleEnd ? 'Single-End' : 'Paired-End'
summary['Five prime read'] = params.r1_five_prime ? 'Read 1' : 'Read 2 or single end'
summary['Output dir'] = params.outdir
summary['FStitch'] = params.fstitch ? 'YES' : 'NO'
summary['Tfit'] = params.tfit ? 'YES' : 'NO'
summary['Tfit prelim'] = params.tfit_prelim ? 'YES' : 'NO'
summary['Tfit model'] = params.tfit_model ? 'YES' : 'NO'
summary['Tfit split model'] = params.tfit_split_model ? 'YES' : 'NO'
summary['Tfit prelim files'] = params.prelim_files ? 'YES' : 'NO'
summary['Process Tfit prelims'] = params.prelim_process ? 'YES' : 'NO'
summary['dREG'] = params.dreg ? 'YES' : 'NO'
summary['dREG postprocess'] = params.dreg ? 'YES' : params.dreg_results ? 'YES' : 'NO'
summary['Gene counting'] = params.gene_count ? 'YES' : 'NO'
summary['Bidir counting'] = params.bidir_count ? 'YES' : 'NO'
summary['Bedtools counting'] = params.bedtools_count ? 'YES' : 'NO'
if(params.fstitch)summary['FStitch dir'] = params.fstitch_path
if(params.fstitch)summary['FStitch train'] = params.fstitch_train
if(params.tfit)summary['Tfit dir'] = params.tfit_path
summary['Save bidir stats'] = params.savestats ? 'YES' : 'NO'
summary['Save bamfiles'] = params.savebam ? 'YES' : 'NO'
summary['Save Tfit bamfiles'] = params.savetfitbam ? 'YES' : 'NO'
summary['Save bedgraphs'] = params.savebg ? 'YES' : 'NO'
summary['Save bigwigs'] = params.savebw ? 'YES' : 'NO'
summary['Save bidir subsets'] = params.savebidirs ? 'YES' : 'NO'
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['Script dir'] = workflow.projectDir
summary['Config Profile'] = workflow.profile
log.info summary.collect { k,v -> "${k.padRight(15)}: $v" }.join("\n")
log.info "======================================================="
println "\nSTARTING PIPELINE"
// PART 0: Outputting software versions
println "[Log 0]: Getting software versions"
process get_software_versions {
time '1h'
output:
stdout into software_versions
script:
"""
printf "bidirectionalflow_version: %s\n" ${params.version}
printf "nextflow_version: %s\n" ${workflow.nextflow.version}
printf "samtools_version: %s\n" \$(samtools --version | head -1 | awk '{print \$NF}')
printf "bedtools_version: %s\n" \$(bedtools --version | head -1 | awk -F " v" '{print \$2}')
printf "openmpi_version: %s\n" \$(ompi_info | head -2 | tail -1 | awk '{print \$NF}')
printf "gcc_version: %s\n" \$(gcc --version | head -1 | awk '{print \$NF}')
printf "fstitch_version: %s\n" \$(${params.fstitch_path} train --version | head -1)
printf "tfit_version: %s\n" \$(${params.tfit_path} model --version | head -1)
printf "r_version: %s\n" \$(R --version | head -1 | awk '{print \$3}')
printf "rsubread_version: %s\n" \$(Rscript -e 'library("Rsubread");packageVersion("Rsubread")' 2>&1 | tail -1 | awk '{print \$NF}')
printf "boost_version: %s\n" \$(ls -d /Users/\$USER/.local/boost* | head -1 | awk -F "_" '{print \$(NF-2)"."\$(NF-1)"."\$(NF)}')
printf "dreg_version: %s\n" \$(Rscript -e 'library("dREG");packageVersion("dREG")' 2>&1 | tail -1 | awk '{print \$NF}')
printf "pipeline_hash: %s\n" ${workflow.scriptId}
"""
}
software_versions.collectFile(name: "software_versions_bidir_${output_date}_${workflow.runName}.yaml", storeDir: "${params.outdir}/pipeline_info")
println "[Log 0]: Software versions complete"
// PART 1: Converting cram files to bam files
if (params.crams) {
println "[Log 1]: Converting CRAM files to BAM files"
println "[Log 1]: Genome file being used ..... $params.genome "
println "[Log 1]: Cram file directory ........ $params.crams"
println "[Log 1]: Working directory ... $params.workdir"
println "[Log 1]: Output directory ... $params.outdir"
cramfiles = Channel
.fromPath(params.crams)
.map { file -> tuple((file.simpleName + '.sorted'), file)}
process cram_to_bam {
cpus 16
queue 'short'
memory '5 GB'
time '1h30m'
tag "$prefix"
publishDir "${params.outdir}" , mode: 'copy',
saveAs: {filename ->
if (params.savebam && (filename == "${prefix}.sorted.bam")) "bams/${prefix}.sorted.bam"
else null
}
input:
tuple val(prefix),file(cram) from cramfiles
output:
tuple val(prefix), file("${prefix}.sorted.bam"), file("${prefix}.sorted.bam.bai") into sorted_bam_file, bam_for_dreg, bam_for_gene_counting, bam_for_bidir_counting
script:
"""
samtools view -@ 16 -b -1 -T ${params.genome} ${cram} > ${prefix}.sorted.bam
samtools index ${prefix}.sorted.bam ${prefix}.sorted.bam.bai
"""
}
} else {
sorted_bam_file = Channel
.fromPath(params.bams)
.map { file -> tuple((file.simpleName + '.sorted'), file, (file + '.bai'))}
bam_for_dreg = Channel
.fromPath(params.bams)
.map { file -> tuple((file.simpleName + '.sorted'), file, (file + '.bai'))}
bam_for_gene_counting = Channel
.fromPath(params.bams)
.map { file -> tuple((file.simpleName + '.sorted'), file, (file + '.bai'))}
bam_for_bidir_counting = Channel
.fromPath(params.bams)
.map { file -> tuple((file.simpleName + '.sorted'), file, (file + '.bai'))}
}
process bam_conversion_tfit {
cpus 16
queue 'short'
memory '5 GB'
time '2h'
tag "$prefix"
publishDir "${params.outdir}" , mode: 'copy',
saveAs: {filename ->
if (params.savetfitbam && (filename == "${prefix}.mmfilt.sorted.bam")) "bams/${prefix}.mmfilt.sorted.bam"
else null
}
when:
params.tfit || params.fstitch || params.tfit_model || params.tfit_prelim || params.tfit_split_model || params.bedtools_count
input:
tuple val(prefix), file(bam), file(index) from sorted_bam_file
output:
tuple val(prefix), file("${prefix}.mmfilt.sorted.bam"), file("${prefix}.mmfilt.sorted.bam.bai") into bam_for_tfit, bam_for_bedtools_count
script:
"""
samtools view -@ 16 -h -q 1 ${bam} | \
grep -P '(NH:i:1|^@)' | \
samtools view -h -b > ${prefix}.mmfilt.sorted.bam
samtools index ${prefix}.mmfilt.sorted.bam ${prefix}.mmfilt.sorted.bam.bai
"""
// For use with bowtie mapper, which outputs different flag for multimapped
// script:
// """
// samtools view -@ 16 -h -q 1 ${bam} | \
// grep -v 'XS:i' | \
// samtools view -h -b > ${prefix}.mmfilt.sorted.bam
// samtools index ${prefix}.mmfilt.sorted.bam ${prefix}.mmfilt.sorted.bam.bai
// """
}
println "[Log 1]: Bam files are ready\n"
// PART 2: Generate bedgraphs
process bedgraphs {
println "[Log 2]: Generating BEDGRAPHS for TFit and FStitch"
println "[Log 2]: Genome information ..... $params.genome "
println "[Log 2]: Chromosome Sizes ....... $params.chrom_sizes"
tag "$prefix"
memory '40 GB'
queue 'short'
time '4h'
publishDir "${params.outdir}" , mode: 'copy',
saveAs: {filename ->
if (params.savebg && (filename == "${prefix}.bedGraph")) "bedgraphs/${prefix}.bedGraph"
else null
}
when:
params.tfit || params.fstitch || params.tfit_model || params.tfit_prelim || params.tfit_split_model
input:
tuple val(prefix), file(bam_file), file(index) from bam_for_tfit
output:
tuple val(prefix), file("${prefix}.bedGraph"), file("${prefix}.pos.bedGraph"), file("${prefix}.neg.bedGraph") into fstitch_bg
tuple val(prefix), file("${prefix}.bedGraph") into prelimtfit_bg, prelimtfit_process_bg, modeltfit_bg, modeltfit_bg_split_max5kb, modeltfit_bg_split_max10kb, post_tfit_bg_split, nqc_bg
script:
if (params.singleEnd) {
"""
genomeCoverageBed \
-bg \
-strand + \
-g ${params.chrom_sizes} \
-ibam ${bam_file} \
> ${prefix}.pos.bedGraph
genomeCoverageBed \
-bg \
-strand - \
-g ${params.chrom_sizes} \
-ibam ${bam_file} \
| awk 'BEGIN{FS=OFS="\t"} {\$4=-\$4}1' \
> ${prefix}.neg.bedGraph
cat ${prefix}.pos.bedGraph \
${prefix}.neg.bedGraph \
> ${prefix}.unsorted.bedGraph
sortBed \
-i ${prefix}.unsorted.bedGraph \
> ${prefix}.bedGraph
"""
} else if (params.forwardStranded) {
"""
samtools view \
-h -b -f 0x0040 \
${bam_file} \
> ${prefix}.first_pair.bam
genomeCoverageBed \
-bg \
-split \
-strand + \
-g ${params.chrom_sizes} \
-ibam ${prefix}.first_pair.bam \
| sortBed \
> ${prefix}.pos.bedGraph
genomeCoverageBed \
-bg \
-split \
-strand - \
-g ${params.chrom_sizes} \
-ibam ${prefix}.first_pair.bam \
| sortBed \
| awk 'BEGIN{FS=OFS="\t"} {\$4=-\$4}1' \
> ${prefix}.neg.bedGraph
cat ${prefix}.pos.bedGraph \
${prefix}.neg.bedGraph \
> ${prefix}.unsorted.bedGraph
sortBed \
-i ${prefix}.unsorted.bedGraph \
> ${prefix}.bedGraph
"""
} else {
"""
samtools view \
-h -b -f 0x0080 \
${bam_file} \
> ${prefix}.first_pair.bam
genomeCoverageBed \
-bg \
-split \
-strand + \
-g ${params.chrom_sizes} \
-ibam ${prefix}.first_pair.bam \
| sortBed \
> ${prefix}.pos.bedGraph
genomeCoverageBed \
-bg \
-split \
-strand - \
-g ${params.chrom_sizes} \
-ibam ${prefix}.first_pair.bam \
| sortBed \
| awk 'BEGIN{FS=OFS="\t"} {\$4=-\$4}1' \
> ${prefix}.neg.bedGraph
cat ${prefix}.pos.bedGraph \
${prefix}.neg.bedGraph \
> ${prefix}.unsorted.bedGraph
sortBed \
-i ${prefix}.unsorted.bedGraph \
> ${prefix}.bedGraph
"""
}
}
println "[Log 2]: Bedgraph files have been generated\n"
// PART 3: Running FStitch
process FStitch {
println "[Log 3]: Running FStitch"
println "[Log 3]: FStitch training file .. $params.fstitch_train"
println "[Log 3]: FStich source code ..... $params.fstitch_path"
tag "$prefix"
memory '50 GB'
queue 'short'
time '4h'
publishDir "${params.outdir}/fstitch/", mode: 'copy', pattern: "*.hmminfo"
publishDir "${params.outdir}/fstitch/segment/", mode: 'copy', pattern: "${prefix}.fstitch_seg.ON.merged.bed"
publishDir "${params.outdir}/fstitch/segment/", mode: 'copy', pattern: "${prefix}.fstitch_seg.bed"
publishDir "${params.outdir}/fstitch/segment/", mode: 'copy', pattern: "*.fstitch_seg.{pos,neg}.bed"
publishDir "${params.outdir}/fstitch/bidirs/", mode: 'copy', pattern: "${prefix}.fstitch_bidir.bed"
publishDir "${params.outdir}/fstitch/bidirs/", mode: 'copy', pattern: "*fstitch_bidir.{short,long}.bed"
publishDir "${params.outdir}/fstitch/bidirs/hist/", mode: 'copy', pattern: "*.html"
publishDir "${params.outdir}/fstitch/bidirs/stats/", mode: 'copy', pattern: "*.txt"
when:
params.fstitch
input:
tuple val(prefix), file(bg), file(pos_bg), file(neg_bg) from fstitch_bg
output:
file ("*.hmminfo") into fs_train_out
tuple val(prefix), file ("*.fstitch_seg.ON.merged.bed") into fs_seg_out
tuple val(prefix), file ("*.fstitch_bidir.bed") into fs_bidir_out
file ("*fstitch_bidir.{short,long}.bed") into fs_bidir_short_long_out
file ("*.html") into fs_bidir_plot_out
file ("*.txt") into fs_bidir_stats_out
script:
"""
${params.fstitch_path} train \
-s + \
-b ${bg} \
-t ${params.fstitch_train} \
-o ${prefix}.fstitch.hmminfo
${params.fstitch_path} segment \
-s + \
-b ${pos_bg} \
-p ${prefix}.fstitch.hmminfo \
-o ${prefix}.fstitch_seg.pos.bed
${params.fstitch_path} segment \
-s - \
-b ${neg_bg} \
-p ${prefix}.fstitch.hmminfo \
-o ${prefix}.fstitch_seg.neg.bed
cat ${prefix}.fstitch_seg.pos.bed \
${prefix}.fstitch_seg.neg.bed \
| sortBed > ${prefix}.fstitch_seg.bed
cat ${prefix}.fstitch_seg.bed | \
grep ON | \
bedtools merge -i stdin \
> ${prefix}.fstitch_seg.ON.merged.bed
bidir \
-b ${prefix}.fstitch_seg.bed \
-g ${params.genome_refseq} \
-o ${prefix}.fstitch_bidir.bed \
-p \
-s
"""
}
println "[Log 3]: Done Running FStitch\n"
// PART 4: Running Tfit
//Load in external prelim files
if (params.prelim_files) {
tfit_prelim_out = Channel
.fromPath(params.prelim_files)
.map { file -> tuple((file.simpleName + '.sorted'), file)}
// Or run Tfit bidir to make prelim files
} else {
process tfit_prelim {
println "[Log 4a]: Running Tfit prelim"
tag "$prefix"
memory '100 GB'
time '6h'
queue 'short'
clusterOptions = '-N 1 -c 64'
publishDir "${params.outdir}/tfit/prelim_logs", mode: 'copy', pattern: "*{log}"
publishDir "${params.outdir}/tfit/prelim", mode: 'copy', pattern: "*_prelim_bidir_hits.bed"
when:
params.tfit_prelim || params.tfit || params.tfit_model || params.tfit_split_model
input:
tuple val(prefix), file(bg) from prelimtfit_bg
output:
tuple val(prefix), file ("*.sorted-1_prelim_bidir_hits.bed") into tfit_prelim_out
file ("*.log") into prelimtfit_logs_out
script:
"""
${params.tfit_prelim_run} -t ${params.tfit_path} \
-c ${params.tfit_config} \
-b ${bg} \
-p ${prefix}
"""
}
println "[Log 4a]: Done Running Tfit prelim\n"
}
//Combine files with same prefix
if (params.tfit_split_model) {
tfit_prelim_bg_out_preprocess = prelimtfit_process_bg
.join(tfit_prelim_out)
process tfit_prelim_process_split {
println "[Log 4b]: Processing prelim file for split model run"
tag "$prefix"
memory '20 GB'
time '1h'
queue 'short'
publishDir "${params.outdir}/tfit/prelim", mode: 'copy', pattern: "*_prelim_coverage_filtered_diced.bed"
when:
params.tfit_split_model
input:
tuple val(prefix), file(bg), file(prelim) from tfit_prelim_bg_out_preprocess
output:
tuple val(prefix), file(bg), file ("*sorted_prelim_coverage_filtered_diced.bed") into tfit_prelim_bg_out
tuple val(prefix), file(bg), file ("*5kb_prelim_coverage_filtered_diced.bed") into tfit_prelim_bg_out_split_max5kb
tuple val(prefix), file(bg), file ("*10kb_prelim_coverage_filtered_diced.bed") into tfit_prelim_bg_out_split_max10kb
script:
"""
python3 ${params.prelim_filter} \
-p ${prelim} \
-b ${bg} \
-s ${prefix} \
-o . \
-g ${params.filtered_refseq} \
-c ${params.chrom_sizes}
awk '{if (\$3-\$2 < 5000) print \$0}' \
${prefix}_prelim_coverage_filtered_diced.bed \
> ${prefix}_max5kb_prelim_coverage_filtered_diced.bed
awk '{if (\$3-\$2 >= 5000) print \$0}' \
${prefix}_prelim_coverage_filtered_diced.bed \
> ${prefix}_max10kb_prelim_coverage_filtered_diced.bed
"""
}
process tfit_split_model_max5kb {
println "[Log 4b]: Running Tfit model on <5kb prelim regions (maxk=2)"
tag "$prefix"
memory '70 GB'
time '72h'
queue 'long'
clusterOptions = '-N 1 -n 32'
publishDir "${params.outdir}/tfit", mode: 'copy', pattern: "*_bidir_predictions.bed"
publishDir "${params.outdir}/tfit/logs", mode: 'copy', pattern: "*{tsv,log}"
when:
params.tfit_split_model
input:
tuple val(prefix), file(bg), file(prelim_max5kb) from tfit_prelim_bg_out_split_max5kb
output:
tuple val(prefix), file ("*_bidir_predictions.bed") into tfit_model_bed_out_split_max5kb
file ("*.tsv") into tfit_model_model_out_split_max5kb
file ("*.log") into tfit_model_logs_out_split_max5kb
script:
"""
${params.tfit_model_run} -t ${params.tfit_path} \
-c ${params.tfit_config_k2} \
-b ${bg} \
-k ${prelim_max5kb} \
-p ${prefix}_max5kb \
-n 32
"""
}
process tfit_split_model_max10kb {
println "[Log 4b]: Running Tfit model on 5-10kb prelim regions (maxk=5)"
tag "$prefix"
memory '70 GB'
time '96h'
queue 'long'
clusterOptions = '-N 1 -n 32'
publishDir "${params.outdir}/tfit", mode: 'copy', pattern: "*_bidir_predictions.bed"
publishDir "${params.outdir}/tfit/logs", mode: 'copy', pattern: "*{tsv,log}"
when:
params.tfit_split_model
input:
tuple val(prefix), file(bg), file(prelim_max10kb) from tfit_prelim_bg_out_split_max10kb
output:
set val(prefix), file ("*_bidir_predictions.bed") into tfit_model_bed_out_split_max10kb
file ("*.tsv") into tfit_model_model_out_split_max10kb
file ("*.log") into tfit_model_logs_out_split_max10kb
script:
"""
${params.tfit_model_run} -t ${params.tfit_path} \
-c ${params.tfit_config} \
-b ${bg} \
-k ${prelim_max10kb} \
-p ${prefix}_max10kb \
-n 32
"""
}
tfit_split_results = post_tfit_bg_split
.join(tfit_model_bed_out_split_max5kb)
.join(tfit_model_bed_out_split_max10kb)
process tfit_split_cat {
tag "$prefix"
memory '4 GB'
time '1h'
queue 'short'
publishDir "${params.outdir}/tfit", mode: 'copy', pattern: "*{split_bidir_predictions.bed,split_bidir_cov_filtered.bed}"
when:
params.tfit_split_model
input:
tuple val(prefix), file(bg), file(out_max5kb), file(out_max10kb) from tfit_split_results
output:
set val(prefix), file ("*split_bidir_predictions.bed"), file("*split_bidir_cov_filtered.bed") into tfit_model_out
script:
"""
grep -v '#' ${out_max5kb} > max5kb_noheader.bed
grep -v '#' ${out_max10kb} > max10kb_noheader.bed
cat max5kb_noheader.bed max10kb_noheader.bed | bedtools sort > ${prefix}_split_bidir_predictions.bed
bedtools coverage -a ${prefix}_split_bidir_predictions.bed -b ${bg} > ${prefix}_split_bidir_cov.bed
awk '{if (\$5 > 9) print \$0}' ${prefix}_split_bidir_cov.bed > ${prefix}_split_bidir_cov_filtered.bed
"""
}
} else if (params.prelim_process && (params.tfit || params.tfit_model)) {
tfit_prelim_bg_out_preprocess = prelimtfit_process_bg
.join(tfit_prelim_out)
process tfit_prelim_process {
println "[Log 4b]: Processing prelim file"
tag "$prefix"
memory '20 GB'
time '1h'
queue 'short'
publishDir "${params.outdir}/tfit/prelim", mode: 'copy', pattern: "*_prelim_coverage_filtered_diced.bed"
when:
params.prelim_process
input:
tuple val(prefix), file(bg), file(prelim) from tfit_prelim_bg_out_preprocess
output:
tuple val(prefix), file(bg), file ("*_prelim_coverage_filtered_diced.bed") into tfit_prelim_bg_out
script:
"""
python3 ${params.prelim_filter} \
-p ${prelim} \
-b ${bg} \
-s ${prefix} \
-o . \
-g ${params.filtered_refseq} \
-c ${params.chrom_sizes}
"""
}
} else if (params.tfit || params.tfit_model) {
tfit_prelim_bg_out = prelimtfit_process_bg
.join(tfit_prelim_out)
}
if (params.tfit || params.tfit_model) {
process tfit_model {
println "[Log 4b]: Running Tfit model"
tag "$prefix"
memory '70 GB'
time '72h'
queue 'long'
clusterOptions = '-N 1 -n 32'
publishDir "${params.outdir}/tfit", mode: 'copy', pattern: "*{_bidir_predictions.bed,_bidir_cov_filtered.bed}"
publishDir "${params.outdir}/tfit/logs", mode: 'copy', pattern: "*{tsv,log}"
when:
params.tfit_model || params.tfit
input:
tuple val(prefix), file(bg), file(prelim) from tfit_prelim_bg_out
output:
tuple val(prefix), file ("*_bidir_predictions.bed"), file("*_bidir_cov_filtered.bed") into tfit_model_out
file ("*.tsv") into tfit_model_model_out
file ("*.log") into tfit_model_logs_out
script:
"""
${params.tfit_model_run} -t ${params.tfit_path} \
-c ${params.tfit_config} \
-b ${bg} \
-k ${prelim} \
-p ${prefix} \
-n 32
bedtools coverage -a ${prefix}-1_bidir_predictions.bed -b ${bg} > ${prefix}_bidir_cov.bed
awk '{if (\$5 > 9) print \$0}' ${prefix}_bidir_cov.bed > ${prefix}_bidir_cov_filtered.bed
"""
}
}
println "[Log 4b]: Done Running Tfit model\n"
// PART 5: Preparing bigwig files for dREG
process dreg_prep {
println "[Log 5]: Generating bigwig files for dREG"
errorStrategy 'ignore'
tag "$prefix"
memory '60 GB'
cpus 16
queue 'short'
publishDir "${params.outdir}" , mode: 'copy',
saveAs: {filename ->
if (params.savebg && (filename == "${prefix}.bedGraph")) "bedgraphs/${prefix}.fivep.bedGraph"
else if (params.savebw && (filename.indexOf("bw") > 0)) "bigwigs/$filename"
else null
}
when:
params.dreg || params.dreg_results
input:
set val(prefix), file(bam_file), file(index) from bam_for_dreg
output:
tuple val(prefix), file("${prefix}.pos.bw"), file("${prefix}.neg.bw") into dreg_bigwig
tuple val(prefix), file("${prefix}.bedGraph") into dreg_bg
script:
if (params.singleEnd) {
"""
echo "Creating BigWigs suitable as inputs to dREG"
export CRAM_REFERENCE=${params.genome}
bamToBed -i ${bam_file} | awk 'BEGIN{OFS="\t"} (\$5 > 0){print \$0}' | \
awk 'BEGIN{OFS="\t"} (\$6 == "+") {print \$1,\$2,\$2+1,\$4,\$5,\$6}; (\$6 == "-") {print \$1, \$3-1,\$3,\$4,\$5,\$6}' \
> ${prefix}.dreg.bed
sortBed -i ${prefix}.dreg.bed > ${prefix}.dreg.sort.bed
echo "positive strand processed to bedGraph"
bedtools genomecov \
-bg \
-i ${prefix}.dreg.sort.bed \
-g ${params.chrom_sizes} \
-strand + \
> ${prefix}.pos.bedGraph
sortBed \
-i ${prefix}.pos.bedGraph \
> ${prefix}.pos.sort.bedGraph
bedtools genomecov \
-bg \
-i ${prefix}.dreg.sort.bed \
-g ${params.chrom_sizes} \
-strand - \
| awk 'BEGIN{FS=OFS="\t"} {\$4=-\$4}1' > ${prefix}.neg.bedGraph
sortBed \
-i ${prefix}.neg.bedGraph \
> ${prefix}.neg.sort.bedGraph
echo "negative strand processed to bedGraph"
${params.bedGraphToBigWig} ${prefix}.pos.sort.bedGraph ${params.chrom_sizes} ${prefix}.pos.bw
${params.bedGraphToBigWig} ${prefix}.neg.sort.bedGraph ${params.chrom_sizes} ${prefix}.neg.bw
cat ${prefix}.pos.bedGraph \
${prefix}.neg.bedGraph \
> ${prefix}.unsorted.bedGraph
sortBed \
-i ${prefix}.unsorted.bedGraph \
> ${prefix}.bedGraph
echo "bedGraph to bigwig done"
"""
} else {
if (params.forwardStranded) {
"""
samtools view -@ 16 -bf 0x2 ${bam_file} | samtools sort -n -@ 16 \
> ${prefix}.dreg.bam
bedtools bamtobed -bedpe -mate1 -i ${prefix}.dreg.bam \
| awk 'BEGIN{OFS="\t"} (\$9 == "+") {print \$1,\$2,\$2+1,\$7,\$8,\$9}; (\$9 == "-") {print \$1,\$3-1,\$3,\$7,\$8,\$9}' \
| sort -k 1,1 -k 2,2n > ${prefix}.dreg.sort.bed
bedtools genomecov -bg \
-i ${prefix}.dreg.sort.bed \
-g ${params.chrom_sizes} \
-strand + \
> ${prefix}.pos.bedGraph
bedtools genomecov -bg \
-i ${prefix}.dreg.sort.bed \
-g ${params.chrom_sizes} \
-strand - \
> ${prefix}.neg.noinv.bedGraph
cat ${prefix}.neg.noinv.bedGraph \
| awk 'BEGIN{OFS="\t"} {print \$1,\$2,\$3,-1*\$4}' \
> ${prefix}.neg.bedGraph
${params.bedGraphToBigWig} ${prefix}.pos.bedGraph \
${params.chrom_sizes} ${prefix}.pos.bw
${params.bedGraphToBigWig} ${prefix}.neg.bedGraph \
${params.chrom_sizes} ${prefix}.neg.bw
cat ${prefix}.pos.bedGraph \
${prefix}.neg.bedGraph \
> ${prefix}.unsorted.bedGraph
sortBed \
-i ${prefix}.unsorted.bedGraph \
> ${prefix}.bedGraph
"""
} else {
"""
samtools view -@ 16 -bf 0x2 ${bam_file} | samtools sort -n -@ 16 \
> ${prefix}.dreg.bam
bedtools bamtobed -bedpe -mate1 -i ${prefix}.dreg.bam \
| awk 'BEGIN{OFS="\t"} (\$10 == "+") {print \$1,\$5,\$5+1,\$7,\$8,\$10}; (\$10 == "-") {print \$1,\$6-1,\$6,\$7,\$8,\$10}' \
| sort -k 1,1 -k 2,2n > ${prefix}.dreg.sort.bed
bedtools genomecov -bg \
-i ${prefix}.dreg.sort.bed \
-g ${params.chrom_sizes} \
-strand + \
> ${prefix}.pos.bedGraph
bedtools genomecov -bg \
-i ${prefix}.dreg.sort.bed \
-g ${params.chrom_sizes} \
-strand - \
> ${prefix}.neg.noinv.bedGraph
cat ${prefix}.neg.noinv.bedGraph \
| awk 'BEGIN{OFS="\t"} {print \$1,\$2,\$3,-1*\$4}' \
> ${prefix}.neg.bedGraph
${params.bedGraphToBigWig} ${prefix}.pos.bedGraph \
${params.chrom_sizes} ${prefix}.pos.bw
${params.bedGraphToBigWig} ${prefix}.neg.bedGraph \
${params.chrom_sizes} ${prefix}.neg.bw
cat ${prefix}.pos.bedGraph \
${prefix}.neg.bedGraph \
> ${prefix}.unsorted.bedGraph
sortBed \
-i ${prefix}.unsorted.bedGraph \
> ${prefix}.bedGraph
"""
}
}
}
println "[Log 5]: Bigwig files are ready \n"
// PART 6: Running dREG
process dreg_run {
println "Log[6]: Running dREG"
println "Log[6]: N.B. Requires GPUs"
tag "$prefix"
memory '50 GB'
time '48h'
cpus 4
queue 'titan'
clusterOptions '--gres=gpu'
publishDir "${params.outdir}/dreg/", mode: 'copy', pattern: "*dREG*"
when:
params.dreg
input:
tuple val(prefix), file(pos_bw), file(neg_bw) from dreg_bigwig
output:
tuple val(prefix), file ("${prefix}.*") into dREG_out
script:
"""
bash ${params.dreg_path} \
${pos_bw} \
${neg_bw} \
${prefix} \
${params.dreg_train} \
4 1
"""
}
if (params.dreg_results) {
dreg_res = Channel
.fromPath(params.dreg_results)
.map { file -> tuple((file.simpleName + '.sorted'), file)}
dreg_res_process = dreg_bg
.join(dreg_res)
} else if (params.dreg) {
dreg_res_process = dreg_bg
.join(dREG_out)
}
if (params.dreg_results || params.dreg) {
process dreg_postprocess {
println "Log[6]: Running dREG postprocessing"
tag "$prefix"
memory '8 GB'
time '1h'
cpus 1
queue 'short'
publishDir "${params.outdir}/dreg/", mode: 'copy', pattern: "*covfiltered.bed"
stageInMode 'copy'
when:
params.dreg || params.dreg_results
input:
tuple val(prefix), file(bg), file(dreg_resfile) from dreg_res_process
output:
tuple val(prefix), file ("${prefix}.dREG.full.covfiltered.bed") into dREG_res_out
script:
"""
gunzip ${prefix}.dREG.peak.full.bed.gz
bedtools merge -i ${prefix}.dREG.peak.full.bed -d 20 > ${prefix}.dREG.peak.full.merge_distance20bp.bed
bedtools coverage -a ${prefix}.dREG.peak.full.merge_distance20bp.bed -b ${bg} > ${prefix}.dREG.bidir.cov.bed
awk '{if (\$4 > 9) print \$0}' ${prefix}.dREG.bidir.cov.bed > ${prefix}.dREG.full.covfiltered.bed
gzip ${prefix}.dREG.peak.full.bed
"""
}
}