forked from ENCODE-DCC/chip-seq-pipeline2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip.wdl
executable file
·1510 lines (1418 loc) · 47.6 KB
/
chip.wdl
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
# ENCODE DCC TF/Histone ChIP-Seq pipeline
# Author: Jin Lee ([email protected])
workflow chip {
#### input file definition
# pipeline can start from any type of inputs and then leave all other types undefined
# supported types: fastq, bam, nodup_bam (filtered bam), ta (tagAlign), peak
# define up to 4 replicates
# [rep_id] is for each replicate
### fastqs
# define fastqs either with DNANexus style (1-dim array) or with default one (3-dim array)
# [merge_id] is for pooing fastqs
## DNANexus UI style fastq definition
Array[File] fastqs_rep1_R1 = [] # [merge_id]
Array[File] fastqs_rep1_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] fastqs_rep2_R1 = [] # do not define if you have a single replicate
Array[File] fastqs_rep2_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] fastqs_rep3_R1 = [] # do not define if you have <=2 replicates
Array[File] fastqs_rep3_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] fastqs_rep4_R1 = [] # do not define if you have <=3 replicates
Array[File] fastqs_rep4_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] fastqs_rep5_R1 = [] # do not define if you have <=4 replicates
Array[File] fastqs_rep5_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] fastqs_rep6_R1 = [] # do not define if you have <=5 replicates
Array[File] fastqs_rep6_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] ctl_fastqs_rep1_R1 = [] # [merge_id]
Array[File] ctl_fastqs_rep1_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] ctl_fastqs_rep2_R1 = [] # do not define if you have a single control
Array[File] ctl_fastqs_rep2_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] ctl_fastqs_rep3_R1 = [] # do not define if you have <=2 controls
Array[File] ctl_fastqs_rep3_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] ctl_fastqs_rep4_R1 = [] # do not define if you have <=3 controls
Array[File] ctl_fastqs_rep4_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] ctl_fastqs_rep5_R1 = [] # do not define if you have <=4 controls
Array[File] ctl_fastqs_rep5_R2 = [] # do not define _R2 array if your sample is not paired end
Array[File] ctl_fastqs_rep6_R1 = [] # do not define if you have <=5 controls
Array[File] ctl_fastqs_rep6_R2 = [] # do not define _R2 array if your sample is not paired end
## default style fastq definition
# [read_end_id] is for fastq R1 or fastq R2
Array[Array[Array[File]]] fastqs = [] # [rep_id][merge_id][read_end_id]
Array[Array[Array[File]]] ctl_fastqs = [] # [rep_id][merge_id][read_end_id]
### other input types (bam, nodup_bam, ta)
Array[File] bams = [] # [rep_id]
Array[File] ctl_bams = [] # [rep_id]
Array[File] nodup_bams = [] # [rep_id]
Array[File] ctl_nodup_bams = [] # [rep_id]
Array[File] tas = [] # [rep_id]
Array[File] ctl_tas = [] # [rep_id]
### other input types (peak)
Array[Int] fraglen = [] # [rep_id]. fragment length if inputs are peaks
Array[File] peaks = [] # [PAIR(rep_id1,rep_id2)]. example for 3 reps: [rep1_rep2, rep1_rep3, rep2_rep3]
Array[File] peaks_pr1 = [] # [rep_id]. do not define if true_rep=true
Array[File] peaks_pr2 = [] # [rep_id]. do not define if true_rep=true
File? peak_ppr1 # do not define if you have a single replicate or true_rep=true
File? peak_ppr2 # do not define if you have a single replicate or true_rep=true
File? peak_pooled # do not define if you have a single replicate or true_rep=true
### pipeline type
String pipeline_type # tf or histone chip-eq
String peak_caller = '' # default: spp for tf and macs2 for histone
### mandatory genome param
File genome_tsv # reference genome data TSV file including
# all important genome specific data file paths and parameters
Boolean paired_end
### optional but important
Boolean align_only = false # disable all post-align analysis (peak-calling, overlap, idr, ...)
Boolean true_rep_only = false # disable all analyses for pseudo replicates
# overlap and idr will also be disabled
Boolean no_jsd = false # no JSD plot generation (deeptools fingerprint)
### task-specific variables but defined in workflow level (limit of WDL)
## optional for MACS2
Int macs2_cap_num_peak = 500000 # cap number of raw peaks called from MACS2
Float pval_thresh = 0.01 # p.value threshold
Int? macs2_mem_mb # resource (memory in MB)
Int? macs2_time_hr # resource (walltime in hour)
String? macs2_disks # resource disks for cloud platforms
## optional for IDR
Float idr_thresh = 0.05 # IDR threshold
## optional for SPP
Int spp_cap_num_peak = 300000 # cap number of raw peaks called from SPP
Int? spp_cpu # resource (cpu)
Int? spp_mem_mb # resource (memory in MB)
Int? spp_time_hr # resource (walltime in hour)
String? spp_disks # resource disks for cloud platforms
### temp vars (do not define these)
String peak_caller_ = if peak_caller!='' then peak_caller
else if pipeline_type=='tf' then 'spp'
else 'macs2'
String peak_type = if peak_caller_=='spp' then 'regionPeak'
else if peak_caller_=='macs2' then 'narrowPeak'
else 'narrowPeak'
Boolean enable_idr = pipeline_type=='tf' # enable_idr for TF chipseq only
String idr_rank = if peak_caller_=='spp' then 'signal.value'
else if peak_caller_=='macs2' then 'p.value'
else 'p.value'
### read genome data and paths
call read_genome_tsv { input:genome_tsv = genome_tsv }
File bwa_idx_tar = read_genome_tsv.genome['bwa_idx_tar']
File blacklist = read_genome_tsv.genome['blacklist']
File chrsz = read_genome_tsv.genome['chrsz']
String gensz = read_genome_tsv.genome['gensz']
### pipeline starts here
# temporary 2-dim arrays for DNANexus style fastqs
Array[Array[File]] fastqs_rep1 = if length(fastqs_rep1_R2)>0 then transpose([fastqs_rep1_R1,fastqs_rep1_R2])
else transpose([fastqs_rep1_R1])
Array[Array[File]] fastqs_rep2 = if length(fastqs_rep2_R2)>0 then transpose([fastqs_rep2_R1,fastqs_rep2_R2])
else transpose([fastqs_rep2_R1])
Array[Array[File]] fastqs_rep3 = if length(fastqs_rep3_R2)>0 then transpose([fastqs_rep3_R1,fastqs_rep3_R2])
else transpose([fastqs_rep3_R1])
Array[Array[File]] fastqs_rep4 = if length(fastqs_rep4_R2)>0 then transpose([fastqs_rep4_R1,fastqs_rep4_R2])
else transpose([fastqs_rep4_R1])
Array[Array[File]] fastqs_rep5 = if length(fastqs_rep5_R2)>0 then transpose([fastqs_rep5_R1,fastqs_rep5_R2])
else transpose([fastqs_rep5_R1])
Array[Array[File]] fastqs_rep6 = if length(fastqs_rep6_R2)>0 then transpose([fastqs_rep6_R1,fastqs_rep6_R2])
else transpose([fastqs_rep6_R1])
Array[Array[Array[File]]] fastqs_ = if length(fastqs_rep1)<1 then fastqs
else if length(fastqs_rep2)<1 then [fastqs_rep1]
else if length(fastqs_rep3)<1 then [fastqs_rep1,fastqs_rep2]
else if length(fastqs_rep4)<1 then [fastqs_rep1,fastqs_rep2,fastqs_rep3]
else if length(fastqs_rep5)<1 then [fastqs_rep1,fastqs_rep2,fastqs_rep3,fastqs_rep4]
else if length(fastqs_rep6)<1 then [fastqs_rep1,fastqs_rep2,fastqs_rep3,fastqs_rep4,fastqs_rep5]
else [fastqs_rep1,fastqs_rep2,fastqs_rep3,fastqs_rep4,fastqs_rep5,fastqs_rep6]
scatter(fastq_set in fastqs_) {
# merge fastqs
call merge_fastq { input :
fastqs = fastq_set,
paired_end = paired_end,
}
# align merged fastqs with bwa
call bwa { input :
idx_tar = bwa_idx_tar,
fastqs = merge_fastq.merged_fastqs, #[R1,R2]
paired_end = paired_end,
}
}
# special treatment for xcor for paired end samples only
Array[Array[File]] fastqs_xcor = if !paired_end then [] else merge_fastq.merged_fastqs
scatter(fastq_set in fastqs_xcor) {
# for paired end dataset, map R1 only as SE for xcor analysis
call trim_fastq { input :
fastq = fastq_set[0],
}
}
Array[Array[File]] trimmed_fastqs_R1 = if length(trim_fastq.trimmed_fastq)<1 then []
else transpose([trim_fastq.trimmed_fastq])
scatter(fastq_set in trimmed_fastqs_R1) {
call bwa as bwa_R1 { input :
idx_tar = bwa_idx_tar,
fastqs = fastq_set,
paired_end = false,
}
# no bam filtering for xcor
call bam2ta as bam2ta_no_filt_R1 { input :
bam = bwa_R1.bam,
disable_tn5_shift = true,
paired_end = false,
}
}
Array[File] bams_ = flatten([bwa.bam, bams])
scatter(bam in bams_) {
# filter/dedup bam
call filter { input :
bam = bam,
paired_end = paired_end,
}
# make unfiltered bam for xcor
call bam2ta as bam2ta_no_filt { input :
bam = bam,
disable_tn5_shift = true,
paired_end = paired_end,
}
}
Array[File] nodup_bams_ = flatten([filter.nodup_bam, nodup_bams])
scatter(bam in nodup_bams_) {
# convert bam to tagalign and subsample it if necessary
call bam2ta { input :
bam = bam,
disable_tn5_shift = true,
paired_end = paired_end,
}
}
Array[File] tas_xcor = if length(bam2ta_no_filt_R1.ta)>0 then bam2ta_no_filt_R1.ta
else if length(bam2ta_no_filt.ta)>0 then bam2ta_no_filt.ta
else flatten([bam2ta.ta, tas])
Boolean paired_end_xcor = paired_end && length(bam2ta_no_filt_R1.ta)<1
scatter(ta in tas_xcor) {
# use trimmed/unfilitered R1 tagAlign for paired end dataset
# if not starting from fastqs, keep using old method
# (mapping with both ends for tag-aligns to be used for xcor)
# subsample tagalign (non-mito) and cross-correlation analysis
call xcor { input :
ta = ta,
paired_end = paired_end_xcor,
}
}
Array[File] tas_ = if align_only then [] else flatten([bam2ta.ta, tas])
if ( length(tas_)>1 ) {
# pool tagaligns from true replicates
call pool_ta { input :
tas = tas_,
}
}
if ( !true_rep_only ) {
scatter( ta in tas_ ) {
# make two self pseudo replicates per true replicate
call spr { input :
ta = ta,
paired_end = paired_end,
}
}
}
if ( !true_rep_only && length(tas_)>1 ) {
# pool tagaligns from pseudo replicates
call pool_ta as pool_ta_pr1 { input :
tas = spr.ta_pr1,
}
call pool_ta as pool_ta_pr2 { input :
tas = spr.ta_pr2,
}
}
# align controls
Array[Array[File]] ctl_fastqs_rep1 = if length(ctl_fastqs_rep1_R2)>0 then transpose([ctl_fastqs_rep1_R1,ctl_fastqs_rep1_R2])
else transpose([ctl_fastqs_rep1_R1])
Array[Array[File]] ctl_fastqs_rep2 = if length(ctl_fastqs_rep2_R2)>0 then transpose([ctl_fastqs_rep2_R1,ctl_fastqs_rep2_R2])
else transpose([ctl_fastqs_rep2_R1])
Array[Array[File]] ctl_fastqs_rep3 = if length(ctl_fastqs_rep3_R2)>0 then transpose([ctl_fastqs_rep3_R1,ctl_fastqs_rep3_R2])
else transpose([ctl_fastqs_rep3_R1])
Array[Array[File]] ctl_fastqs_rep4 = if length(ctl_fastqs_rep4_R2)>0 then transpose([ctl_fastqs_rep4_R1,ctl_fastqs_rep4_R2])
else transpose([ctl_fastqs_rep4_R1])
Array[Array[File]] ctl_fastqs_rep5 = if length(ctl_fastqs_rep5_R2)>0 then transpose([ctl_fastqs_rep5_R1,ctl_fastqs_rep5_R2])
else transpose([ctl_fastqs_rep5_R1])
Array[Array[File]] ctl_fastqs_rep6 = if length(ctl_fastqs_rep6_R2)>0 then transpose([ctl_fastqs_rep6_R1,ctl_fastqs_rep6_R2])
else transpose([ctl_fastqs_rep6_R1])
Array[Array[Array[File]]] ctl_fastqs_ = if length(ctl_fastqs_rep1)<1 then ctl_fastqs
else if length(ctl_fastqs_rep2)<1 then [ctl_fastqs_rep1]
else if length(ctl_fastqs_rep3)<1 then [ctl_fastqs_rep1,ctl_fastqs_rep2]
else if length(ctl_fastqs_rep4)<1 then [ctl_fastqs_rep1,ctl_fastqs_rep2,ctl_fastqs_rep3]
else if length(ctl_fastqs_rep5)<1 then [ctl_fastqs_rep1,ctl_fastqs_rep2,ctl_fastqs_rep3,ctl_fastqs_rep4]
else if length(ctl_fastqs_rep6)<1 then [ctl_fastqs_rep1,ctl_fastqs_rep2,ctl_fastqs_rep3,ctl_fastqs_rep4,ctl_fastqs_rep5]
else [ctl_fastqs_rep1,ctl_fastqs_rep2,ctl_fastqs_rep3,ctl_fastqs_rep4,ctl_fastqs_rep5,ctl_fastqs_rep6]
scatter(fastq_set in ctl_fastqs_) {
# merge fastqs
call merge_fastq as merge_fastq_ctl { input :
fastqs = fastq_set,
paired_end = paired_end,
}
# align merged fastqs with bwa
call bwa as bwa_ctl { input :
idx_tar = bwa_idx_tar,
fastqs = merge_fastq_ctl.merged_fastqs, #[R1,R2]
paired_end = paired_end,
}
}
Array[File] ctl_bams_ = flatten([bwa_ctl.bam, ctl_bams])
scatter(bam in ctl_bams_) {
# filter/dedup bam
call filter as filter_ctl { input :
bam = bam,
paired_end = paired_end,
}
}
Array[File] ctl_nodup_bams_ = flatten([filter_ctl.nodup_bam, ctl_nodup_bams])
scatter(bam in ctl_nodup_bams_) {
# convert bam to tagalign and subsample it if necessary
call bam2ta as bam2ta_ctl { input :
bam = bam,
disable_tn5_shift = true,
paired_end = paired_end,
}
}
Array[String] ctl_tas_ = if align_only then [] else flatten([bam2ta_ctl.ta, ctl_tas])
if ( length(ctl_tas_)>0 ) {
# pool tagaligns from true replicates
call pool_ta as pool_ta_ctl { input :
tas = ctl_tas_,
}
}
if ( !no_jsd && length(nodup_bams_)>0 && length(ctl_nodup_bams_)>0 && basename(blacklist)!='null' ) {
# fingerprint and JS-distance plot
call fingerprint { input :
nodup_bams = nodup_bams_,
ctl_bam = ctl_nodup_bams_[0], # use first control only
blacklist = blacklist,
}
}
if ( length(tas_)>0 && length(ctl_tas_)>0 ) {
# choose appropriate control for each exp IP replicate
# outputs:
# choose_ctl.idx : control replicate index for each exp replicate
# -1 means pooled ctl replicate
call choose_ctl { input:
tas = tas_,
ctl_tas = ctl_tas_,
ta_pooled = pool_ta.ta_pooled,
ctl_ta_pooled = pool_ta_ctl.ta_pooled,
}
}
# before peak calling, get fragment length from xcor analysis or given input
# if fraglen [] is defined in the input JSON, fraglen from xcor will be ignored
Array[Int] fraglen_ = if align_only then []
else if length(fraglen)>0 then fraglen
else xcor.fraglen
# make control ta array [[1,2,3,4]] -> [[1],[2],[3],[4]], will be zipped with exp ta array latter
Array[Array[File]] chosen_ctl_tas = if length(tas_)<1 || length(ctl_tas_)<1 then [[],[],[],[],[],[]]
else transpose(select_all([choose_ctl.chosen_ctl_tas]))
# we have all tas and ctl_tas (optional for histone chipseq) ready, let's call peaks
scatter(i in range(length(tas_))) {
# always call MACS2 peaks for true replicates to get signal tracks
# call peaks on tagalign
call macs2 { input :
tas = flatten([[tas_[i]], chosen_ctl_tas[i]]),
gensz = gensz,
chrsz = chrsz,
cap_num_peak = macs2_cap_num_peak,
pval_thresh = pval_thresh,
make_signal = true,
fraglen = fraglen_[i],
blacklist = blacklist,
# resource
mem_mb = macs2_mem_mb,
disks = macs2_disks,
time_hr = macs2_time_hr,
}
}
# SPP cannot call peaks without controls
if ( peak_caller_=='spp' ) {
scatter(i in range(length(tas_))) {
# call peaks on tagalign
call spp { input :
tas = flatten([[tas_[i]], chosen_ctl_tas[i]]),
chrsz = chrsz,
cap_num_peak = spp_cap_num_peak,
fraglen = fraglen_[i],
blacklist = blacklist,
# resource
cpu = spp_cpu,
mem_mb = spp_mem_mb,
disks = spp_disks,
time_hr = spp_time_hr,
}
}
}
if ( peak_caller_=='macs2' ) {
scatter(i in range(length(tas_))) {
# call peaks on 1st pseudo replicated tagalign
call macs2 as macs2_pr1 { input :
tas = flatten([[select_first([spr.ta_pr1])[i]], chosen_ctl_tas[i]]),
gensz = gensz,
chrsz = chrsz,
cap_num_peak = macs2_cap_num_peak,
pval_thresh = pval_thresh,
fraglen = fraglen_[i],
blacklist = blacklist,
# resource
mem_mb = macs2_mem_mb,
disks = macs2_disks,
time_hr = macs2_time_hr,
}
call macs2 as macs2_pr2 { input :
tas = flatten([[select_first([spr.ta_pr2])[i]], chosen_ctl_tas[i]]),
gensz = gensz,
chrsz = chrsz,
cap_num_peak = macs2_cap_num_peak,
pval_thresh = pval_thresh,
fraglen = fraglen_[i],
blacklist = blacklist,
# resource
mem_mb = macs2_mem_mb,
disks = macs2_disks,
time_hr = macs2_time_hr,
}
}
}
if ( peak_caller_=='spp' ) {
scatter(i in range(length(tas_))) {
# call peaks on 1st pseudo replicated tagalign
call spp as spp_pr1 { input :
tas = flatten([[select_first([spr.ta_pr1])[i]], chosen_ctl_tas[i]]),
chrsz = chrsz,
cap_num_peak = spp_cap_num_peak,
fraglen = fraglen_[i],
blacklist = blacklist,
# resource
cpu = spp_cpu,
mem_mb = spp_mem_mb,
disks = spp_disks,
time_hr = spp_time_hr,
}
# call peaks on 2nd pseudo replicated tagalign
call spp as spp_pr2 { input :
tas = flatten([[select_first([spr.ta_pr2])[i]], chosen_ctl_tas[i]]),
chrsz = chrsz,
cap_num_peak = spp_cap_num_peak,
fraglen = fraglen_[i],
blacklist = blacklist,
# resource
cpu = spp_cpu,
mem_mb = spp_mem_mb,
disks = spp_disks,
time_hr = spp_time_hr,
}
}
}
# rounded mean of fragment length, which will be used for
# 1) calling peaks for pooled true/pseudo replicates
# 2) calculating FRiP
call rounded_mean as fraglen_mean { input :
ints = fraglen_,
}
# actually not an array
Array[File] chosen_ctl_ta_pooled = if length(tas_)<2 || length(ctl_tas_)<1 then []
else if length(ctl_tas_)<2 then [ctl_tas_[0]] # choose first (only) control
else select_all([pool_ta_ctl.ta_pooled]) # choose pooled control
if ( length(tas_)>1 ) {
# call peaks on pooled replicate
# always call MACS2 peaks for pooled replicate to get signal tracks
call macs2 as macs2_pooled { input :
tas = flatten([select_all([pool_ta.ta_pooled]), chosen_ctl_ta_pooled]),
gensz = gensz,
chrsz = chrsz,
cap_num_peak = macs2_cap_num_peak,
pval_thresh = pval_thresh,
make_signal = true,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
# resource
mem_mb = macs2_mem_mb,
disks = macs2_disks,
time_hr = macs2_time_hr,
}
}
if ( length(tas_)>1 && peak_caller_=='spp' ) {
# call peaks on pooled replicate
call spp as spp_pooled { input :
tas = flatten([select_all([pool_ta.ta_pooled]), chosen_ctl_ta_pooled]),
chrsz = chrsz,
cap_num_peak = spp_cap_num_peak,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
# resource
cpu = spp_cpu,
mem_mb = spp_mem_mb,
disks = spp_disks,
time_hr = spp_time_hr,
}
}
if ( !true_rep_only && length(tas_)>1 && peak_caller_=='macs2' ) {
# call peaks on 1st pooled pseudo replicates
call macs2 as macs2_ppr1 { input :
tas = flatten([select_all([pool_ta_pr1.ta_pooled]), chosen_ctl_ta_pooled]),
gensz = gensz,
chrsz = chrsz,
cap_num_peak = macs2_cap_num_peak,
pval_thresh = pval_thresh,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
# resource
mem_mb = macs2_mem_mb,
disks = macs2_disks,
time_hr = macs2_time_hr,
}
}
if ( !true_rep_only && length(tas_)>1 && peak_caller_=='macs2' ) {
# call peaks on 2nd pooled pseudo replicates
call macs2 as macs2_ppr2 { input :
tas = flatten([select_all([pool_ta_pr2.ta_pooled]), chosen_ctl_ta_pooled]),
gensz = gensz,
chrsz = chrsz,
cap_num_peak = macs2_cap_num_peak,
pval_thresh = pval_thresh,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
# resource
mem_mb = macs2_mem_mb,
disks = macs2_disks,
time_hr = macs2_time_hr,
}
}
if ( !true_rep_only && length(tas_)>1 && peak_caller_=='spp' ) {
# call peaks on 1st pooled pseudo replicates
call spp as spp_ppr1 { input :
tas = flatten([select_all([pool_ta_pr1.ta_pooled]), chosen_ctl_ta_pooled]),
chrsz = chrsz,
cap_num_peak = spp_cap_num_peak,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
# resource
cpu = spp_cpu,
mem_mb = spp_mem_mb,
disks = spp_disks,
time_hr = spp_time_hr,
}
}
if ( !true_rep_only && length(tas_)>1 && peak_caller_=='spp' ) {
# call peaks on 2nd pooled pseudo replicates
call spp as spp_ppr2 { input :
tas = flatten([select_all([pool_ta_pr2.ta_pooled]), chosen_ctl_ta_pooled]),
chrsz = chrsz,
cap_num_peak = spp_cap_num_peak,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
# resource
cpu = spp_cpu,
mem_mb = spp_mem_mb,
disks = spp_disks,
time_hr = spp_time_hr,
}
}
# make peak arrays
Array[File] peaks_ = if align_only then [] else select_first([spp.rpeak ,flatten([macs2.npeak, peaks])])
# generate all possible pairs of true replicates (pair: left=prefix, right=[peak1,peak2])
Array[Pair[String,Array[File]]] peak_pairs =
if length(peaks_)<=1 then [] # 1 rep
else if length(peaks_)<=2 then # 2 reps
[('rep1-rep2',[peaks_[0],peaks_[1]])]
else if length(peaks_)<=3 then # 3 reps
[('rep1-rep2',[peaks_[0],peaks_[1]]), ('rep1-rep3',[peaks_[0],peaks_[2]]),
('rep2-rep3',[peaks_[1],peaks_[2]])]
else if length(peaks_)<=4 then # 4 reps
[('rep1-rep2',[peaks_[0],peaks_[1]]), ('rep1-rep3',[peaks_[0],peaks_[2]]), ('rep1-rep4',[peaks_[0],peaks_[3]]),
('rep2-rep3',[peaks_[1],peaks_[2]]), ('rep2-rep4',[peaks_[1],peaks_[3]]),
('rep3-rep4',[peaks_[2],peaks_[3]])]
else if length(peaks_)<=5 then # 5 reps
[('rep1-rep2',[peaks_[0],peaks_[1]]), ('rep1-rep3',[peaks_[0],peaks_[2]]), ('rep1-rep4',[peaks_[0],peaks_[3]]), ('rep1-rep5',[peaks_[0],peaks_[4]]),
('rep2-rep3',[peaks_[1],peaks_[2]]), ('rep2-rep4',[peaks_[1],peaks_[3]]), ('rep2-rep5',[peaks_[1],peaks_[4]]),
('rep3-rep4',[peaks_[2],peaks_[3]]), ('rep3-rep5',[peaks_[2],peaks_[4]]),
('rep4-rep5',[peaks_[3],peaks_[4]])]
else # 6 reps
[('rep1-rep2',[peaks_[0],peaks_[1]]), ('rep1-rep3',[peaks_[0],peaks_[2]]), ('rep1-rep4',[peaks_[0],peaks_[3]]), ('rep1-rep5',[peaks_[0],peaks_[4]]), ('rep1-rep6',[peaks_[0],peaks_[5]]),
('rep2-rep3',[peaks_[1],peaks_[2]]), ('rep2-rep4',[peaks_[1],peaks_[3]]), ('rep2-rep5',[peaks_[1],peaks_[4]]), ('rep2-rep6',[peaks_[1],peaks_[5]]),
('rep3-rep4',[peaks_[2],peaks_[3]]), ('rep3-rep5',[peaks_[2],peaks_[4]]), ('rep3-rep6',[peaks_[2],peaks_[5]]),
('rep4-rep5',[peaks_[3],peaks_[4]]), ('rep4-rep6',[peaks_[3],peaks_[5]]),
('rep5-rep6',[peaks_[4],peaks_[5]])]
scatter( pair in peak_pairs ) {
# Naive overlap on every pair of true replicates
call overlap { input :
prefix = pair.left,
peak1 = pair.right[0],
peak2 = pair.right[1],
peak_pooled = select_first([spp_pooled.rpeak, macs2_pooled.npeak, peak_pooled]),
fraglen = fraglen_mean.rounded_mean,
peak_type = peak_type,
blacklist = blacklist,
chrsz = chrsz,
ta = pool_ta.ta_pooled,
}
}
if ( enable_idr ) {
scatter( pair in peak_pairs ) {
# IDR on every pair of true replicates
call idr { input :
prefix = pair.left,
peak1 = pair.right[0],
peak2 = pair.right[1],
peak_pooled = select_first([spp_pooled.rpeak, macs2_pooled.npeak, peak_pooled]),
idr_thresh = idr_thresh,
peak_type = peak_type,
fraglen = fraglen_mean.rounded_mean,
rank = idr_rank,
blacklist = blacklist,
chrsz = chrsz,
ta = pool_ta.ta_pooled,
}
}
}
Array[File] peaks_pr1_ = select_first([spp_pr1.rpeak, macs2_pr1.npeak, peaks_pr1])
Array[File] peaks_pr2_ = select_first([spp_pr2.rpeak, macs2_pr2.npeak, peaks_pr2])
scatter( i in range(length(peaks_pr1_)) ) {
# Naive overlap on pseduo replicates
call overlap as overlap_pr { input :
prefix = "rep"+(i+1)+"-pr",
peak1 = peaks_pr1_[i],
peak2 = peaks_pr2_[i],
peak_pooled = peaks_[i],
fraglen = fraglen_[i],
peak_type = peak_type,
blacklist = blacklist,
chrsz = chrsz,
ta = if length(tas_)>0 then tas_[i] else pool_ta.ta_pooled,
}
}
if ( enable_idr ) {
scatter( i in range(length(peaks_pr1_)) ) {
# IDR on pseduo replicates
call idr as idr_pr { input :
prefix = "rep"+(i+1)+"-pr",
peak1 = peaks_pr1_[i],
peak2 = peaks_pr2_[i],
peak_pooled = peaks_[i],
fraglen = fraglen_[i],
idr_thresh = idr_thresh,
peak_type = peak_type,
rank = idr_rank,
blacklist = blacklist,
chrsz = chrsz,
ta = if length(tas_)>0 then tas_[i] else pool_ta.ta_pooled,
}
}
}
if ( length(peaks_pr1_)>1 ) {
# Naive overlap on pooled pseudo replicates
call overlap as overlap_ppr { input :
prefix = "ppr",
peak1 = select_first([spp_ppr1.rpeak, macs2_ppr1.npeak, peak_ppr1]), #peak_ppr1_[0],
peak2 = select_first([spp_ppr2.rpeak, macs2_ppr2.npeak, peak_ppr2]), #peak_ppr2_[0],
peak_pooled = select_first([spp_pooled.rpeak, macs2_pooled.npeak, peak_pooled]),
peak_type = peak_type,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
chrsz = chrsz,
ta = pool_ta.ta_pooled,
}
}
if ( enable_idr && length(peaks_pr1_)>1 ) {
# IDR on pooled pseduo replicates
call idr as idr_ppr { input :
prefix = "ppr",
peak1 = select_first([spp_ppr1.rpeak, macs2_ppr1.npeak, peak_ppr1]), #peak_ppr1_[0],
peak2 = select_first([spp_ppr2.rpeak, macs2_ppr2.npeak, peak_ppr2]), #peak_ppr2_[0],
peak_pooled = select_first([spp_pooled.rpeak, macs2_pooled.npeak, peak_pooled]),
idr_thresh = idr_thresh,
peak_type = peak_type,
rank = idr_rank,
fraglen = fraglen_mean.rounded_mean,
blacklist = blacklist,
chrsz = chrsz,
ta = pool_ta.ta_pooled,
}
}
if ( !align_only && !true_rep_only ) {
# reproducibility QC for overlapping peaks
call reproducibility as reproducibility_overlap { input :
prefix = 'overlap',
peaks = overlap.bfilt_overlap_peak,
peaks_pr = overlap_pr.bfilt_overlap_peak,
peak_ppr = overlap_ppr.bfilt_overlap_peak,
peak_type = peak_type,
chrsz = chrsz,
}
}
if ( !align_only && !true_rep_only && enable_idr ) {
# reproducibility QC for IDR peaks
call reproducibility as reproducibility_idr { input :
prefix = 'idr',
peaks = idr.bfilt_idr_peak,
peaks_pr = idr_pr.bfilt_idr_peak,
peak_ppr = idr_ppr.bfilt_idr_peak,
peak_type = peak_type,
chrsz = chrsz,
}
}
# Generate final QC report and JSON
call qc_report { input :
paired_end = paired_end,
pipeline_type = pipeline_type,
peak_caller = peak_caller_,
idr_thresh = idr_thresh,
flagstat_qcs = bwa.flagstat_qc,
nodup_flagstat_qcs = filter.flagstat_qc,
dup_qcs = filter.dup_qc,
pbc_qcs = filter.pbc_qc,
ctl_flagstat_qcs = bwa_ctl.flagstat_qc,
ctl_nodup_flagstat_qcs = filter_ctl.flagstat_qc,
ctl_dup_qcs = filter_ctl.dup_qc,
ctl_pbc_qcs = filter_ctl.pbc_qc,
xcor_plots = xcor.plot_png,
xcor_scores = xcor.score,
jsd_plot = fingerprint.plot,
jsd_qcs = select_first([fingerprint.jsd_qcs,[]]),
frip_macs2_qcs = macs2.frip_qc,
frip_macs2_qcs_pr1 = macs2_pr1.frip_qc,
frip_macs2_qcs_pr2 = macs2_pr2.frip_qc,
frip_macs2_qc_pooled = macs2_pooled.frip_qc,
frip_macs2_qc_ppr1 = macs2_ppr1.frip_qc,
frip_macs2_qc_ppr2 = macs2_ppr2.frip_qc,
frip_spp_qcs = spp.frip_qc,
frip_spp_qcs_pr1 = spp_pr1.frip_qc,
frip_spp_qcs_pr2 = spp_pr2.frip_qc,
frip_spp_qc_pooled = spp_pooled.frip_qc,
frip_spp_qc_ppr1 = spp_ppr1.frip_qc,
frip_spp_qc_ppr2 = spp_ppr2.frip_qc,
idr_plots = idr.idr_plot,
idr_plots_pr = idr_pr.idr_plot,
idr_plot_ppr = idr_ppr.idr_plot,
frip_idr_qcs = idr.frip_qc,
frip_idr_qcs_pr = idr_pr.frip_qc,
frip_idr_qc_ppr = idr_ppr.frip_qc,
frip_overlap_qcs = overlap.frip_qc,
frip_overlap_qcs_pr = overlap_pr.frip_qc,
frip_overlap_qc_ppr = overlap_ppr.frip_qc,
idr_reproducibility_qc = reproducibility_idr.reproducibility_qc,
overlap_reproducibility_qc = reproducibility_overlap.reproducibility_qc,
}
output {
File report = qc_report.report
File qc_json = qc_report.qc_json
Boolean qc_json_match = qc_report.qc_json_match
}
}
task merge_fastq { # merge trimmed fastqs
# parameters from workflow
Array[Array[File]] fastqs # [merge_id][read_end_id]
Boolean paired_end
# resource
Int? cpu
Int? mem_mb
Int? time_hr
String? disks
command {
python $(which encode_merge_fastq.py) \
${write_tsv(fastqs)} \
${if paired_end then "--paired-end" else ""} \
${"--nth " + select_first([cpu,2])}
}
output {
# WDL glob() globs in an alphabetical order
# so R1 and R2 can be switched, which results in an
# unexpected behavior of a workflow
# so we prepend merge_fastqs_'end'_ (R1 or R2)
# to the basename of original filename
# this prefix will be later stripped in bwa task
Array[File] merged_fastqs = glob("merge_fastqs_R?_*.fastq.gz")
}
runtime {
cpu : select_first([cpu,2])
memory : "${select_first([mem_mb,'12000'])} MB"
time : select_first([time_hr,6])
disks : select_first([disks,"local-disk 100 HDD"])
}
}
task trim_fastq { # trim fastq (for PE R1 only)
# parameters from workflow
File fastq
Int? trim_bp
command {
python $(which encode_trim_fastq.py) \
${fastq} \
--trim-bp ${select_first([trim_bp,50])}
}
output {
File trimmed_fastq = glob("*.fastq.gz")[0]
}
runtime {
cpu : 1
memory : "8000 MB"
time : 1
disks : "local-disk 50 HDD"
}
}
task bwa {
# parameters from workflow
File idx_tar # reference bwa index tar
Array[File] fastqs # [read_end_id]
Boolean paired_end
# resource
Int? cpu
Int? mem_mb
Int? time_hr
String? disks
command {
python $(which encode_bwa.py) \
${idx_tar} \
${sep=' ' fastqs} \
${if paired_end then "--paired-end" else ""} \
${"--nth " + select_first([cpu,4])}
}
output {
File bam = glob("*.bam")[0]
File bai = glob("*.bai")[0]
File flagstat_qc = glob("*.flagstat.qc")[0]
}
runtime {
cpu : select_first([cpu,4])
memory : "${select_first([mem_mb,'20000'])} MB"
time : select_first([time_hr,48])
disks : select_first([disks,"local-disk 100 HDD"])
preemptible: 0
}
}
task filter {
# parameters from workflow
File bam
Boolean paired_end
Int? multimapping
# optional
String? dup_marker # picard.jar MarkDuplicates (picard) or
# sambamba markdup (sambamba)
Int? mapq_thresh # threshold for low MAPQ reads removal
Boolean? no_dup_removal # no dupe reads removal when filtering BAM
# dup.qc and pbc.qc will be empty files
# and nodup_bam in the output is
# resource # filtered bam with dupes
Int? cpu
Int? mem_mb
Int? time_hr
String? disks
command {
python $(which encode_filter.py) \
${bam} \
${if paired_end then "--paired-end" else ""} \
${"--multimapping " + multimapping} \
${"--dup-marker " + select_first([dup_marker,'picard'])} \
${"--mapq-thresh " + select_first([mapq_thresh,30])} \
${if select_first([no_dup_removal,false]) then "--no-dup-removal" else ""} \
${"--nth " + cpu}
# ugly part to deal with optional outputs with Google JES backend
${if select_first([no_dup_removal,false]) then "touch null.dup.qc null.pbc.qc; " else ""}
touch null
}
output {
File nodup_bam = glob("*.bam")[0]
File nodup_bai = glob("*.bai")[0]
File flagstat_qc = glob("*.flagstat.qc")[0]
File dup_qc = if select_first([no_dup_removal,false]) then glob("null")[0] else glob("*.dup.qc")[0]
File pbc_qc = if select_first([no_dup_removal,false]) then glob("null")[0] else glob("*.pbc.qc")[0]
}
runtime {
#@docker : "quay.io/encode-dcc/atac-seq-pipeline:v1"
cpu : select_first([cpu,2])
memory : "${select_first([mem_mb,'20000'])} MB"
time : select_first([time_hr,24])
disks : select_first([disks,"local-disk 100 HDD"])
}
}
task bam2ta {
# parameters from workflow
File bam
Boolean paired_end
Boolean disable_tn5_shift # no tn5 shifting (it's for dnase-seq)
# optional
String? regex_grep_v_ta # Perl-style regular expression pattern
# to remove matching reads from TAGALIGN
Int? subsample # number of reads to subsample TAGALIGN
# this affects all downstream analysis
# resource
Int? cpu
Int? mem_mb
Int? time_hr
String? disks
command {
python $(which encode_bam2ta.py) \
${bam} \
${if paired_end then "--paired-end" else ""} \
${if disable_tn5_shift then "--disable-tn5-shift" else ""} \
${"--regex-grep-v-ta " +"'"+select_first([regex_grep_v_ta,'chrM'])+"'"} \
${"--subsample " + select_first([subsample,0])} \
${"--nth " + cpu}
}
output {
File ta = glob("*.tagAlign.gz")[0]
}
runtime {
#@docker : "quay.io/encode-dcc/atac-seq-pipeline:v1"
cpu : select_first([cpu,2])
memory : "${select_first([mem_mb,'10000'])} MB"
time : select_first([time_hr,6])
disks : select_first([disks,"local-disk 100 HDD"])
}
}
task spr { # make two self pseudo replicates
# parameters from workflow
File ta
Boolean paired_end
# resource
Int? mem_mb
command {
python $(which encode_spr.py) \
${ta} \
${if paired_end then "--paired-end" else ""}
}
output {
File ta_pr1 = glob("*.pr1.tagAlign.gz")[0]
File ta_pr2 = glob("*.pr2.tagAlign.gz")[0]
}
runtime {
cpu : 1
memory : "${select_first([mem_mb,'16000'])} MB"
time : 1
disks : "local-disk 50 HDD"
}
}
task pool_ta {
# parameters from workflow
Array[File] tas
command {
python $(which encode_pool_ta.py) \
${sep=' ' tas}
}
output {
File ta_pooled = glob("*.tagAlign.gz")[0]
}
runtime {
cpu : 1
memory : "4000 MB"
time : 1
disks : "local-disk 50 HDD"
}
}
task xcor {
# parameters from workflow
File ta
Boolean paired_end
# optional
Int? subsample # number of reads to subsample TAGALIGN
# this will be used for xcor only
# will not affect any downstream analysis
# resource
Int? cpu
Int? mem_mb
Int? time_hr
String? disks
command {
python $(which encode_xcor.py) \
${ta} \
${if paired_end then "--paired-end" else ""} \
${"--subsample " + select_first([subsample,15000000])} \
${"--nth " + select_first([cpu,2])}
}
output {
File plot_pdf = glob("*.cc.plot.pdf")[0]
File plot_png = glob("*.cc.plot.png")[0]
File score = glob("*.cc.qc")[0]
Int fraglen = read_int(glob("*.cc.fraglen.txt")[0])
}
runtime {
#@docker : "quay.io/encode-dcc/atac-seq-pipeline:v1"
cpu : select_first([cpu,2])
memory : "${select_first([mem_mb,'16000'])} MB"
time : select_first([time_hr,6])
disks : select_first([disks,"local-disk 100 HDD"])
}
}
task fingerprint {
# parameters from workflow
Array[File] nodup_bams
File ctl_bam # one control bam is required
File blacklist
# resource
Int? cpu