-
Notifications
You must be signed in to change notification settings - Fork 0
/
chipseq.bds.achive
1997 lines (1641 loc) · 76.7 KB
/
chipseq.bds.achive
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 bds
#vim: syntax=java
help == chipseq pipeline settings
// general
type := "TF" help Type of ChIP-Seq pipeline. TF or histone (default: TF).
histone := false help (LEGACY PARAM) Histone ChIP-Seq. Equivalent to '-type histone'
final_stage := "" help Final stage for pipeline (bam, filt_bam, tag, xcor and peak).
// signal track generation
sig_trk_for_pooled_rep_only := false help Generate MACS2 signal tracks for pooled replicate only.
// mapping/filtering/aligning
aligner := "bwa" help Aligner to map raw reads in FASTQs (default: bwa).
subsample_xcor := "15M" help # reads to be subsampled for cross corr. analysis (default: 15M).
subsample_chip := "0" help # reads to subsample exp. replicate. Subsampled tagalign will be used for steps downstream (default: 0; no subsampling).
subsample := "0" help (LEGACY PARAM) # reads to subsample exp. replicate. Subsampled tagalign will be used for steps downstream (default: 0; no subsampling).
subsample_ctl := "0" help # reads to subsample control if non-zero (recommended: 40M or lower).
anon_filt_bam := false help Generate an annomymized filtered bam. This will not affect tasks downsteam.
// peak calling
peak_caller := "spp" help Peak caller for IDR analysis (default: spp for TF ChIP-seq and macs2 for Histone ChIP-seq ).
ctl_depth_ratio := 1.2 help Cut-off ratio of two control tagaligns for pooling (default: 1.2).
fraglen := 0 help (LEGACY PARAM) Manually specify fragment length (default: 0, 0 means parsing fraglen from cross-corr analysis log file).
idr_rank := "" help Scoring column in narrow peak files for IDR. If not specified, signal.value for SPP peaks (TF) and p.value for MACS2 peaks (histone) are used.
idr_thresh := 0.05 help IDR threshold : -log_10(score) (default: 0.05).
use_pooled_ctl := false help Force to use pooled control (ignoring criteria to choose control for each replicate).
true_rep := false help Call peaks on true replicates only.
no_pseudo_rep := false help Do not call peaks on self pseudo replicates.
no_xcor := false help Disable cross-correlation analysis.
no_gpeak_filt := false help Disable gapped peak filtering through narrow peak (for histone ChIP-Seq only).
no_browser_tracks := false help Disable generation of genome browser tracks (workaround for bzip2 shared library issue).
pe_xcor_only := false help (PE ONLY) Align R1 of paired end fastqs only and for cross-correlation analysis. All other analyses and QCs will be disabled.
pe_no_trim_fastq := false help (PE ONLY) No fastq trimming and use PE tagAlign for cross-correlation analysis.
no_jsd := false help Disable JSD plot generation
disable_gb_peak := true help Disable gapped/broad peak generation
no_chipqc := false help Disable advanced chipseq qc
help() // print help and exit if no parameters are given
include "modules/pipeline_template.bds"
include "modules/input.bds"
include "modules/align_bwa.bds"
include "modules/align_trim_fastq.bds"
include "modules/postalign_bam.bds"
include "modules/postalign_bed.bds"
include "modules/postalign_xcor.bds"
include "modules/callpeak_spp.bds"
include "modules/callpeak_gem.bds"
include "modules/callpeak_peakseq.bds"
include "modules/callpeak_macs2_chipseq.bds"
include "modules/callpeak_naive_overlap.bds"
include "modules/callpeak_idr.bds"
include "modules/callpeak_blacklist_filter.bds"
include "modules/chipqc.bds"
//include "modules/signal.bds"
// Important file names are stored in global variables (usually a string map string{} with a key with replicate id and peakcaller name)
// e.g. filt_bam{"Replicate 1"} = filtered bam for replicate 1, peak_pr1{"spp,2"} = peak file for pseudo replicate 1 of replicate 2 generated from spp
string{} flagstat_qc, dup_qc, flagstat_nodup_qc, pbc_qc, xcor_qc, xcor_plot, jsd_qc
string{} bam, filt_bam
string jsd_png
string{} tag, tag_pr1, tag_pr2
string tag_ppr1, tag_ppr2
string{} peak, peak_pr1, peak_pr2, peak_pooled, peak_ppr1, peak_ppr2
string{} gpeak, gpeak_pr1, gpeak_pr2, gpeak_pooled, gpeak_ppr1, gpeak_ppr2
string{} bpeak, bpeak_pr1, bpeak_pr2, bpeak_pooled, bpeak_ppr1, bpeak_ppr2
// string peak_overlap, gpeak_overlap, bpeak_overlap
string{} raw_peak_qc_FRiP
string{} idr_tr, idr_pr, idr_tr_png, idr_pr_png
string idr_ppr, idr_opt, idr_consv, idr_ppr_png
string idr_qc
string{} idr_qc_FRiP
string{} peak_overlap_tr, peak_overlap_pr
string peak_overlap_ppr, peak_overlap_opt, peak_overlap_consv
string peak_overlap_qc
string{} peak_overlap_qc_FRiP
string{} sig_trk // signal tracks from deepTools or bamCoverage: map with key ("$ctl,$rep")
string{} pval_bigwig, fc_bigwig // signal track from macs2
string num_peak_log
string{} chipqc_qc
main()
void main() { // chipseq pipeline starts here
init_chipseq() // read command line parameters or configruation file
chk_param() // check if parameters are valid
chk_input( true_rep, no_pseudo_rep ) // if inputs are fastq, bam, or tagalign, check if they exist
align() // align and postalign
jsd() // plot fingerprint and compute synthetic JS distance
chipqc()
if ( !pe_xcor_only ) {
pool_tags() // make pooled tagaligns and choose appropriate control for each replicate
call_peaks() // call peaks in parallel (MACS2,SPP)
read_input_peak() // if inputs are peaks, read them
naive_overlap() // get naive overlap peaks
log_number_of_peaks()
do_idr() // IDR
filter_peak() // blacklist-filter peaks
}
report()
}
void init_chipseq() {
read_conf_chipseq()
init_filetable()
}
void read_conf_chipseq() {
type = get_conf_val( type, ["type"] )
histone = get_conf_val_bool( histone, ["histone"] )
if ( histone ) type = "histone" // backward compatibility for LEGACY param
if ( type == "histone" && !cmd_line_arg_has_key("peak_caller") ) peak_caller = "macs2"
final_stage = get_conf_val( final_stage, ["final_stage"] )
sig_trk_for_pooled_rep_only = get_conf_val_bool( sig_trk_for_pooled_rep_only, ["sig_trk_for_pooled_rep_only"] )
aligner = get_conf_val( aligner, ["aligner"] )
subsample_xcor = get_conf_val( subsample_xcor, ["subsample_xcor"] )
subsample_chip = get_conf_val( subsample_chip, ["subsample_chip"] )
subsample = get_conf_val( subsample, ["subsample"] )
subsample_ctl = get_conf_val( subsample_ctl, ["subsample_ctl"] )
anon_filt_bam = get_conf_val_bool( anon_filt_bam, ["anon_filt_bam"] )
peak_caller = get_conf_val( peak_caller, ["peak_caller"] )
ctl_depth_ratio = get_conf_val_real( ctl_depth_ratio, ["ctl_depth_ratio"])
fraglen = get_conf_val_int( fraglen, ["fraglen"] )
idr_rank = get_conf_val( idr_rank, ["idr_rank"] )
idr_thresh = get_conf_val_real( idr_thresh, ["idr_thresh"] )
use_pooled_ctl = get_conf_val_bool( use_pooled_ctl, ["use_pooled_ctl"] )
true_rep = get_conf_val_bool( true_rep, ["true_rep"] )
no_chipqc = get_conf_val_bool( no_chipqc, ["no_chipqc"] )
no_pseudo_rep = get_conf_val_bool( no_pseudo_rep, ["no_pseudo_rep"] )
no_xcor = get_conf_val_bool( no_xcor, ["no_xcor"] )
no_gpeak_filt = get_conf_val_bool( no_gpeak_filt, ["no_gpeak_filt"] )
no_jsd = get_conf_val_bool( no_jsd, ["no_jsd"] )
no_browser_tracks = get_conf_val_bool( no_browser_tracks, ["no_browser_tracks"] )
pe_xcor_only = get_conf_val_bool( pe_xcor_only, ["pe_xcor_only"] )
pe_no_trim_fastq = get_conf_val_bool( pe_no_trim_fastq, ["pe_no_trim_fastq"] )
check_peak_caller_valid()
check_aligner_valid()
if ( pe_xcor_only ) final_stage = "xcor"
if ( idr_rank == "" ) {
if ( peak_caller == "spp" || peak_caller == "gem" ) {
idr_rank = "signal.value"
}
else if ( peak_caller == "macs2" ) {
idr_rank = "p.value"
}
else if ( peak_caller == "peakseq" ) {
idr_rank = "q.value"
}
else error("Unsupported peak_caller ($peak_caller)!")
idr_rank = (peak_caller == "spp" ? "signal.value" : "p.value")
}
// for backward compatibility for legacy params
if ( subsample_chip == "0" && subsample != "0" ) subsample_chip = subsample
if ( extsize_macs2 == -1 && fraglen > 0 ) extsize_macs2 = fraglen
if ( speak_spp == -1 && fraglen > 0 ) speak_spp = fraglen
if ( is_final_stage_after_xcor() && no_xcor ) {
if ( speak_spp == -1 ) error("For the case of no cross-corr. analysis (-no_xcor). You need to specify -speak_spp.")
if ( extsize_macs2 == -1 ) error("For the case of no cross-corr. analysis (-no_xcor). You need to specify -extsize_macs2.")
}
peak_caller = peak_caller.toLower()
print( "\n\n== chipseq pipeline settings\n")
print( "# replicates \t\t\t\t: "+get_num_rep()+"\n" )
print( "Type of ChIP-Seq pipeline\t\t: $type\n")
print( "Final stage for ChIP-Seq\t\t: $final_stage\n" )
print( "Signal tracks for pooled rep. only\t: $sig_trk_for_pooled_rep_only\n")
print( "Aligner to map raw reads\t\t\t: $aligner\n")
print( "# reads to subsample for cross-corr. analysis\t\t: "+parse_number( subsample_xcor)+"\n")
print( "# reads to subsample exp. replicates (0: no subsampling): "+parse_number( subsample_chip )+"\n")
print( "# reads to subsample controls (0: no subsampling)\t: "+parse_number( subsample_ctl)+"\n")
print( "Generate anonymized filt. bam\t\t: $anon_filt_bam\n")
print( "Peak caller for IDR analysis\t\t: $peak_caller\n")
print( "Control rep. depth ratio\t\t: $ctl_depth_ratio\n" )
// print( "Specified fraglen (0:auto)\t\t: $fraglen\n")
print( "Scoring column for IDR\t\t\t: $idr_rank\n")
print( "IDR threshold\t\t\t\t: $idr_thresh\n" )
print( "Force to use pooled ctl\t\t\t: $use_pooled_ctl\n")
print( "Peak calling for true reps only\t\t: $true_rep\n" )
print( "No peak calling for self pseudo reps\t: $no_pseudo_rep\n" )
print( "No CHIPQC (advanced QC report)\t\t: $no_chipqc\n")
print( "Disable cross-correlation analysis\t: $no_xcor\n" )
print( "Disable g. peak filt. thru. n. peak\t: $no_gpeak_filt\n" )
print( "Disable genome browser tracks\t: $no_browser_tracks\n" )
print( "Disable all QCs except cross-corr. (PE only)\t: $pe_xcor_only\n" )
print( "No fastq trimming and use PE tagAlign for cross-corr. (PE only)\t: $pe_no_trim_fastq\n" )
print( "Disable JSD plot generation\t\t: $no_jsd\n" )
}
void init_filetable() { // init file table labels in HTML report
// add label to graphviz
// : Items in filetable will be sorted in the ascending order of rank
// : Items added later will have higher rank
// Level 1
add_label_to_table("Raw reads")
add_label_to_table("Alignment")
add_label_to_table("Signal tracks")
add_label_to_table("Peaks")
add_label_to_table("QC and logs")
// Level 2
for (int i=1; i<=100; i++) \
add_label_to_table("Replicate $i")
for (int i=1; i<=100; i++) \
add_label_to_table("Control $i")
add_label_to_table("True replicates")
add_label_to_table("Pooled replicate")
add_label_to_table("Pooled control")
add_label_to_table("Pseudo-replicates")
add_label_to_table("Pooled pseudo-replicate")
add_label_to_table("Pooled pseudo-replicates")
add_label_to_table("Optimal set")
add_label_to_table("Conservative set")
add_label_to_table("Naive overlap")
add_label_to_table("SPP")
add_label_to_table("MACS2")
add_label_to_table("IDR")
// Level 2 or 3
add_label_to_table("Pseudo-replicate 1")
add_label_to_table("Pseudo-replicate 2")
add_label_to_table("Pooled pseudo-replicate 1")
add_label_to_table("Pooled pseudo-replicate 2")
for (int i=1; i<=20; i++) \
for (int j=i+1; j<=20; j++) \
add_label_to_table("Rep. $i vs Rep. $j")
// Higher levels
add_label_to_table("IDR QC")
add_label_to_table("Fastq")
add_label_to_table("Fastq 1")
add_label_to_table("Fastq 2")
add_label_to_table("BWA map. flagstat log")
add_label_to_table("Bam")
add_label_to_table("Filtered bam")
add_label_to_table("Sorted bam")
add_label_to_table("Dedup. log")
add_label_to_table("Map. flagstat log")
add_label_to_table("PBC log")
add_label_to_table("Bedpe")
add_label_to_table("Subsampled bedpe")
add_label_to_table("Tag-align")
add_label_to_table("Subsampled tag-align")
add_label_to_table("Cross-corr. log")
add_label_to_table("Cross-corr. plot")
add_label_to_table("P-value")
add_label_to_table("Fold enrichment")
add_label_to_table("Narrow peak")
add_label_to_table("Region peak")
add_label_to_table("Broad peak")
add_label_to_table("Gapped peak")
add_label_to_table("IDR peak")
add_label_to_table("Peak")
add_label_to_table("Filtered peak")
add_label_to_table("Filtered gapped peak")
add_label_to_table("Filtered broad peak")
add_label_to_table("CHIPQC")
add_label_to_table("IDR plot")
add_label_to_table("Unthresholded IDR peak")
// add label to graphviz (short name, long name)
for (int i=1; i<=50; i++) {
add_label_to_graph("rep$i", "Replicate $i")
add_label_to_graph("rep$i-pr1", "Pseudo-replicate 1 for rep. $i")
add_label_to_graph("rep$i-pr2", "Pseudo-replicate 2 for rep. $i")
add_label_to_graph("rep$i-pr", "Pseudo replicates for rep. $i")
for (int j=1; j<=20; j++) {
add_label_to_graph("rep$i-rep$j", "Rep. $i vs. Rep. $j")
}
}
for (int i=1; i<=100; i++) {
add_label_to_graph("ctl$i", "Control $i")
}
add_label_to_graph("pooled_rep", "Pooled replicate")
add_label_to_graph("pooled_ctl", "Pooled control")
add_label_to_graph("ppr", "Pooled pseudo-replicates")
add_label_to_graph("ppr1", "Pooled pseudo-replicate 1")
add_label_to_graph("ppr2", "Pooled pseudo-replicate 2")
}
void chk_param() {
print( "\n\n== checking chipseq parameters ...\n" )
if ( pe_xcor_only && !is_input_fastq(0,1) ) {
error("-pe_xcor_only is for fastqs only!\n")
}
if ( !no_chipqc ) {
no_chipqc = !chk_chipqc()
}
if ( has_input_fastq() ) chk_align_bwa()
if ( !has_input_fastq() && !no_chipqc ) {
print("Warning: CHIPQC is available for fastq inputs only. Disabling CHIPQC...\n")
no_chipqc = true
}
if ( is_final_stage_idr() ) chk_idr()
if ( is_final_stage_idr() || is_final_stage_peak() ) chk_callpeak_macs2()
// errors
if ( peak_caller == "gem" ) chk_callpeak_gem()
if ( peak_caller == "peakseq" ) chk_callpeak_peakseq()
if ( !is_input_peak() && peak_caller == "spp" && !ctl_exists() && !is_final_stage_before_peak() ) \
error("Cannot call peaks (SPP) without controls!\n")
if ( multimapping > 0 ) \
error("Multimapping is not available for chipseq pipeline!\n")
if ( is_input_peak() && true_rep && get_num_rep() == 1 && is_TF_chipseq() ) \
error("Cannot perform IDR on with only one replicate when '-true_rep' is turned on!")
// if ( is_histone_chipseq() && sig_trk_for_pooled_rep_only ) \
// sig_trk_for_pooled_rep_only = false
if ( has_pe_input_tag() && parse_number( subsample_xcor ) > 0 ) \
print("Warning: PE tagaligns cannot be subsampled for cross-corr. analysis.\n")
if ( has_pe_input_tag() && parse_number( subsample_chip ) > 0 ) \
print("Warning: PE tagaligns cannot be subsampled!\n")
if ( has_pe_input_tag(1) && parse_number( subsample_ctl ) > 0 ) \
error("PE contol tagaligns cannot be subsampled!\n")
// if ( sig_trk_for_pooled_rep_only && macs2_for_idr ) \
// error("-sig_trk_for_pooled_rep_only and -macs2_for_idr are mutally exclusive parameters!\n")
}
void align() {
if ( is_input_peak() ) return
// distribute # of threads to each replicate/control according to their file sizes
// different weight for each data type 3 for fastq, 5 for tagaligns, 1 for others
int{} filesize
for ( int ctl=0; ctl <= 1; ctl++) { // iterate through inputs (ctl==0 : exp. replicate, ctl==1 : control)
if ( ctl==1 && !ctl_exists() ) continue
for ( int rep=1; rep <= get_num_rep( ctl ); rep++) {
if ( !input_file_exists( ctl, rep ) ) continue
// check file size to distribute_nonzero nth to each nth_app
// determine # threads for each app related to alignment
group := get_group_name( ctl, rep )
// get file size in bytes
if ( is_input_fastq( ctl, rep ) ) {
fastqs := get_fastqs( ctl, rep )
filesize{group} = (fastqs[0]).size()*3 // multiply 3 (weight) to allocate more cpus for align
if ( fastqs.size() > 1) filesize{group} += (fastqs[1]).size()
}
else if ( is_input_bam( ctl, rep ) ) filesize{group} = (get_bam( ctl, rep )).size()
else if ( is_input_filt_bam( ctl, rep ) ) filesize{group} = (get_filt_bam( ctl, rep )).size()
else if ( is_input_tag( ctl, rep ) ) filesize{group} = (get_tag( ctl, rep )).size()*5
}
}
nth_rep := distribute_nonzero( nth, filesize )
// align
for ( int ctl=0; ctl <= 1; ctl++) { // iterate through inputs (ctl==0 : exp. replicate, ctl==1 : control)
if ( ctl==1 && !ctl_exists() ) continue
for ( int rep=1; rep <= get_num_rep( ctl ); rep++) {
if ( !input_file_exists( ctl, rep ) ) continue
group := get_group_name( ctl, rep )
if ( no_par ) align( ctl, rep, nth_rep{group} ) // parallel jobs for align() for each replicate and each control
else par align( ctl, rep, nth_rep{group} ) // parallel jobs for align() for each replicate and each control
}
}
wait
print( "\n== Done align()\n" )
}
void jsd() { // plot fingerprint
if ( no_jsd ) return
nth_sam_idx := min(2,nth)
string[] filt_bams, labels
for ( int rep=1; rep <= get_num_rep( 0 ); rep++) { // IP replicate
group := get_group_name( 0, rep )
filt_bam_ := filt_bam{group}
filt_bams.add(filt_bam_)
labels.add(group)
samtools_index(filt_bam_, group, nth_sam_idx)
}
nth_jsd := min(4,nth)
string jsd_qc_lib, jsd_qc_dat
(jsd_png, jsd_qc_lib, jsd_qc_dat) = \
jsd_single_lib(filt_bam, labels, "$out_dir/qc", nth_jsd )
wait
// jsd_qc_all_reps.rm()
add_file_to_table( jsd_png, "QC and logs/Fingerprint JSD Plot")
print( "\n== Done jsd()\n" )
}
void align( int ctl, int rep, int nth_rep ) {
if ( is_se( ctl, rep ) ) {
align_SE( ctl, rep, nth_rep )
}
else {
align_PE( ctl, rep, nth_rep )
}
}
void align_SE( int ctl, int rep, int nth_rep ) {
// ctl==0: exp. replicate, ctl==1: control
group := get_group_name( ctl, rep )
long := get_long_group_name( ctl, rep )
aln_o_dir := mkdir( "$out_dir/align/$group" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$group" ) // create qc output dir.
string bam_, flagstat_qc_
string mapped_reads // read count
if ( is_input_fastq( ctl, rep ) ) {
fastqs := get_fastqs( ctl, rep )
// pool if multiple fastqs
string pooled_fastq
if ( fastqs.size()==1 ) pooled_fastq = fastqs[0]
else {
pooled_fastq = pool_fastq( fastqs, aln_o_dir, group )
wait
}
if ( aligner == "bwa" ) {
( bam_, flagstat_qc_ ) = bwa( pooled_fastq, aln_o_dir, qc_o_dir, group, nth_rep )
}
else {
error("Unsupported aligner ($aligner)!")
}
bam{group} = bam_
flagstat_qc{group} = flagstat_qc_
add_file_to_table( flagstat_qc_, "QC and logs/$long/BWA map. flagstat log")
wait
// parse read counts
tmp_log := parse_flagstat( flagstat_qc_ )
raw_reads := metric_prefix( parse_int( tmp_log{"total"} ) )
mapped_reads = metric_prefix( parse_int( tmp_log{"mapped"} ) )
// apply read counts to report
if ( fastqs.size()==1 ) {
add_file_to_report( fastqs[0], "fastq\\n$raw_reads", group, "Raw reads/$long/Fastq ($raw_reads)" )
}
else {
for ( int i=0; i<fastqs.size(); i++) {
id := i+1
suffix := ":$id"
add_file_to_report( fastqs[i], "fastq$suffix", group, "Raw reads/$long/Fastq$suffix" )
}
add_file_to_report( pooled_fastq, "pooled\\nfastq\\n$raw_reads", group, \
"Raw reads/$long/Pooled fastq ($raw_reads)" )
}
}
wait
string filt_bam_, dup_qc_, pbc_qc_, flagstat_nodup_qc_
string deduped_reads
if ( is_input_bam( ctl, rep ) || is_input_fastq( ctl, rep ) ) {
if ( is_input_bam( ctl, rep ) ) bam_ = get_bam( ctl, rep )
add_file_to_report( bam_, "bam" + (mapped_reads ? "\\n$mapped_reads" : ""), group, \
"Alignment/$long/Bam" + (mapped_reads ? " ($mapped_reads)" : "") )
if ( is_final_stage_bam() ) return
if ( no_dup_removal ) {
string tmp
( filt_bam_, tmp ) \
= dedup_bam( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
wait
}
else {
( filt_bam_, dup_qc_, flagstat_nodup_qc_, pbc_qc_ ) \
= dedup_bam( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
dup_qc{group} = dup_qc_
add_file_to_table( dup_qc_, "QC and logs/$long/Dedup. log")
pbc_qc{group} = pbc_qc_
add_file_to_table( pbc_qc_, "QC and logs/$long/PBC log")
flagstat_nodup_qc{group} = flagstat_nodup_qc_
add_file_to_table( flagstat_nodup_qc_, "QC and logs/$long/Filtered flagstat log")
wait
// add to report
tmp_log := parse_flagstat( flagstat_nodup_qc_ )
deduped_reads = metric_prefix( parse_int( tmp_log{"total"} ) )
}
}
string tag_
if ( is_input_filt_bam( ctl, rep ) || is_input_bam( ctl, rep ) || is_input_fastq( ctl, rep ) ) {
if ( is_input_filt_bam( ctl, rep ) ) filt_bam_ = get_filt_bam( ctl, rep )
add_file_to_report( filt_bam_, "filt. bam" + (deduped_reads ? "\\n$deduped_reads" : ""), group, \
"Alignment/$long/Filtered & deduped bam" + (deduped_reads ? " ($deduped_reads)" : "") )
filt_bam{group} = filt_bam_
if ( anon_filt_bam ) anonymize_filt_bam( filt_bam_, aln_o_dir, group )
if ( is_final_stage_filt_bam() ) return
tag_ = bam_to_tag( filt_bam_, aln_o_dir, group )
wait
}
if ( is_input_tag( ctl, rep ) || is_input_filt_bam( ctl, rep ) || is_input_bam( ctl, rep ) || is_input_fastq( ctl, rep ) ) {
if ( is_input_tag( ctl, rep ) ) tag_ = get_tag( ctl, rep )
add_file_to_report( tag_, "tag-align", group, "Alignment/$long/Tag-align" )
if ( ctl == 0 && parse_number( subsample_chip ) > 0 ) {
tag_ = subsample_tag( tag_, parse_number( subsample_chip ), false, aln_o_dir, group )
wait
}
else if ( ctl > 0 && parse_number( subsample_ctl ) > 0 ) {
tag_ = subsample_tag( tag_, parse_number( subsample_ctl ), false, aln_o_dir, group )
wait
}
tag{group} = tag_
if ( is_final_stage_tag() ) return
string xcor_qc_
if ( ctl == 0 ) { // if replicate
if ( !true_rep ) { // pseudo replicates
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr2" )
string tag_pr1_, tag_pr2_
(tag_pr1_, tag_pr2_ ) = spr( tag_, aln_pr1_o_dir, aln_pr2_o_dir, group ) // make self pseudo replicate
tag_pr1{group} = tag_pr1_
tag_pr2{group} = tag_pr2_
add_file_to_report( tag_pr1_, "tag-align", "$group-pr1", \
"Alignment/Pseudo-replicates/$long/Pseudo-replicate 1/Tag-align" )
add_file_to_report( tag_pr2_, "tag-align", "$group-pr2", \
"Alignment/Pseudo-replicates/$long/Pseudo-replicate 2/Tag-align" )
}
// wait
if ( !no_xcor ) {
string tag_xcor
if ( bam_ ) {
tag_xcor = bam_to_tag( bam_, aln_o_dir, group )
}
else {
tag_xcor = tag_
}
subsampled_tag_xcor := subsample_tag( tag_xcor, parse_number( subsample_xcor ), true, aln_o_dir, group )
wait
// xcor for true rep
string xcor_qc_, xcor_plot_
( xcor_qc_, xcor_plot_ ) = xcor( subsampled_tag_xcor, qc_o_dir, group, nth_rep )
xcor_qc{group} = xcor_qc_
xcor_plot{group} = xcor_plot_
add_file_to_report( xcor_qc_, "xcor log", group, "QC and logs/$long/Cross-corr. log" )
add_file_to_table( xcor_plot_, "QC and logs/$long/Cross-corr. plot" )
}
}
if ( is_final_stage_xcor() ) return
}
}
void align_PE( int ctl, int rep, int nth_rep ) {
if ( pe_xcor_only && ctl > 0 ) return
// ctl==0: exp. replicate, ctl==1: control
group := get_group_name( ctl, rep )
long := get_long_group_name( ctl, rep )
aln_o_dir := mkdir( "$out_dir/align/$group" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$group" ) // create qc output dir.
string bam_, flagstat_qc_, bam_R1
string mapped_reads // read count
string tmp
if ( is_input_fastq( ctl, rep ) ) {
fastqs_pair1 := get_fastq( ctl, rep, 1 )
string[] fastqs_pair2
if ( !pe_xcor_only ) {
fastqs_pair2 = get_fastq( ctl, rep, 2 )
if ( fastqs_pair1.size() != fastqs_pair2.size() ) {
error("Number of fastqs to be pooled for pair 1 and pair 2 do not match!\n")
}
}
string pooled_fastq_pair1, pooled_fastq_pair2
if ( fastqs_pair1.size()==1 ) {
pooled_fastq_pair1 = fastqs_pair1[0]
if ( !pe_xcor_only ) {
pooled_fastq_pair2 = fastqs_pair2[0]
}
}
else {
pooled_fastq_pair1 = pool_fastq( fastqs_pair1, aln_o_dir, group )
if ( !pe_xcor_only ) {
pooled_fastq_pair2 = pool_fastq( fastqs_pair2, aln_o_dir, group )
}
wait
}
// allocate cpu for bwa_SE and bwa_PE
int[] nth_map
if ( !pe_xcor_only ) {
nth_map = distribute_nonzero( nth_rep, [1,2] )
}
else {
nth_map = [nth_rep]
}
// align on R1 fastq
if ( ctl==0 && !pe_no_trim_fastq ) {
trimmed_fastq_R1 := trim_fastq( pooled_fastq_pair1, aln_o_dir, group )
add_file_to_report( trimmed_fastq_R1, "trim. fastq R1", group, "Raw reads/$long/Trimmed fastq R1" )
wait
( bam_R1, tmp ) \
= bwa( trimmed_fastq_R1, aln_o_dir, qc_o_dir, group, nth_map[0] )
add_file_to_report( bam_R1, "bam R1", group, "Alignment/$long/Bam R1" )
}
if ( !pe_xcor_only ) {
if ( aligner == "bwa" ) {
( bam_, flagstat_qc_ ) \
= bwa_PE( pooled_fastq_pair1, pooled_fastq_pair2, aln_o_dir, qc_o_dir, group, nth_map[1] )
}
else {
error("Unsupported aligner ($aligner)!")
}
bam{group} = bam_
flagstat_qc{group} = flagstat_qc_
add_file_to_table( flagstat_qc_, "QC and logs/$long/BWA map. flagstat log")
wait
// parse read counts
tmp_log := parse_flagstat( flagstat_qc_ )
raw_reads := metric_prefix( parse_int( tmp_log{"total"} ) )
half_raw_reads := metric_prefix( parse_int( tmp_log{"total"} )/2 )
mapped_reads = metric_prefix( parse_int( tmp_log{"mapped"} ) )
// apply read counts to report
if ( fastqs_pair1.size()==1 ) {
add_file_to_report( fastqs_pair1[0], "fastq1\\n$half_raw_reads", group, \
"Raw reads/$long/Fastq 1 ($half_raw_reads)" )
add_file_to_report( fastqs_pair2[0], "fastq2\\n$half_raw_reads", group, \
"Raw reads/$long/Fastq 2 ($half_raw_reads)" )
}
else {
for ( int i=0; i<fastqs_pair1.size(); i++) {
id := i+1
suffix := ":$id"
add_file_to_report( fastqs_pair1[i], "fastq$suffix", group, "Raw reads/$long/Fastq 1$suffix" )
add_file_to_report( fastqs_pair2[i], "fastq$suffix", group, "Raw reads/$long/Fastq 2$suffix" )
}
add_file_to_report( pooled_fastq_pair1, "pooled\\nfastq1\\n$raw_reads", group, \
"Raw reads/$long/Pooled fastq 1 ($raw_reads)" )
add_file_to_report( pooled_fastq_pair2, "pooled\\nfastq2\\n$raw_reads", group, \
"Raw reads/$long/Pooled fastq 2 ($raw_reads)" )
}
}
}
wait
string filt_bam_, dup_qc_, pbc_qc_, flagstat_nodup_qc_
string deduped_reads
if ( is_input_bam( ctl, rep ) || is_input_fastq( ctl, rep ) ) {
if ( !pe_xcor_only ) {
if ( is_input_bam( ctl, rep ) ) bam_ = get_bam( ctl, rep )
add_file_to_report( bam_, "bam" + (mapped_reads ? "\\n$mapped_reads" : ""), group, \
"Alignment/$long/Bam" + (mapped_reads ? " ($mapped_reads)" : "") )
if ( is_final_stage_bam() ) return
if ( no_dup_removal ) {
( filt_bam_, tmp ) \
= dedup_bam_PE( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
wait
}
else {
( filt_bam_, dup_qc_, flagstat_nodup_qc_, pbc_qc_ ) \
= dedup_bam_PE( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
dup_qc{group} = dup_qc_
add_file_to_table( dup_qc_, "QC and logs/$long/Dedup. log")
pbc_qc{group} = pbc_qc_
add_file_to_table( pbc_qc_, "QC and logs/$long/PBC log")
flagstat_nodup_qc{group} = flagstat_nodup_qc_
add_file_to_table( flagstat_nodup_qc_, "QC and logs/$long/Filtered flagstat log")
wait
// add to report
tmp_log := parse_flagstat( flagstat_nodup_qc_ )
deduped_reads = metric_prefix( parse_int( tmp_log{"total"} ) )
}
}
}
string tag_, bedpe, tag_R1
if ( is_input_filt_bam( ctl, rep ) || is_input_bam( ctl, rep ) || is_input_fastq( ctl, rep ) ) {
if ( ctl==0 && bam_R1 ) {
tag_R1 = bam_to_tag( bam_R1, aln_o_dir, group )
add_file_to_report( tag_R1, "tag-align-R1", group, "Alignment/$long/Tag-align R1" )
}
if ( !pe_xcor_only ) {
if ( is_input_filt_bam( ctl, rep ) ) filt_bam_ = get_filt_bam( ctl, rep )
add_file_to_report( filt_bam_, "filt. bam" + (deduped_reads ? "\\n$deduped_reads" : ""), group, \
"Alignment/$long/Filtered & deduped bam" + (deduped_reads ? " ($deduped_reads)" : "") )
filt_bam{group} = filt_bam_
if ( anon_filt_bam ) anonymize_filt_bam( filt_bam_, aln_o_dir, group )
if ( is_final_stage_filt_bam() ) return
bedpe = bam_to_bedpe( filt_bam_, aln_o_dir, group )
wait
add_file_to_graph( bedpe, "bedpe", group )
tag_ = bedpe_to_tag( bedpe, aln_o_dir, group )
wait
}
}
if ( is_input_tag( ctl, rep ) || is_input_filt_bam( ctl, rep ) || is_input_bam( ctl, rep ) || is_input_fastq( ctl, rep ) ) {
if ( !pe_xcor_only ) {
if ( is_input_tag( ctl, rep ) ) tag_ = get_tag( ctl, rep )
add_file_to_report( tag_, "tag-align", group, "Alignment/$long/Tag-align" )
if ( ctl == 0 && parse_number( subsample_chip ) > 0 ) {
tag_ = subsample_tag_PE( tag_, parse_number( subsample_chip ), false, aln_o_dir, group )
wait
}
else if ( ctl > 0 && parse_number( subsample_ctl ) > 0 ) {
tag_ = subsample_tag_PE( tag_, parse_number( subsample_ctl ), false, aln_o_dir, group )
wait
}
tag{group} = tag_
if ( is_final_stage_tag() ) return
if ( ctl == 0 ) { // if replicate
if ( !true_rep ) { // if replicate
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr2" )
string tag_pr1_, tag_pr2_
( tag_pr1_, tag_pr2_ ) = spr_tag_PE( tag_, aln_pr1_o_dir, aln_pr2_o_dir, group )
tag_pr1{group} = tag_pr1_
tag_pr2{group} = tag_pr2_
add_file_to_report( tag_pr1_, "tag-align", "$group-pr1", \
"Alignment/Pseudo-replicates/$long/Pseudo-replicate 1/Tag-align" )
add_file_to_report( tag_pr2_, "tag-align", "$group-pr2", \
"Alignment/Pseudo-replicates/$long/Pseudo-replicate 2/Tag-align" )
}
}
// wait
}
if ( !no_xcor && ctl==0 ) {
string subsampled_tag_xcor
if ( tag_R1 ) {
subsampled_tag_xcor = subsample_tag( tag_R1, parse_number( subsample_xcor ), true, aln_o_dir, group )
}
else if ( bam_ ) {
bedpe_ := bam_to_bedpe( bam_, aln_o_dir, group )
wait
tag_xcor := bedpe_to_tag( bedpe_, aln_o_dir, group )
wait
subsampled_tag_xcor = subsample_tag_PE_for_xcor( tag_xcor, parse_number( subsample_xcor ), true, aln_o_dir, group )
}
else {
subsampled_tag_xcor = subsample_tag_PE_for_xcor( tag_, parse_number( subsample_xcor ), true, aln_o_dir, group )
}
wait
string xcor_qc_, xcor_plot_
( xcor_qc_, xcor_plot_ ) = xcor( subsampled_tag_xcor, qc_o_dir, group, nth_rep )
xcor_qc{group} = xcor_qc_
xcor_plot{group} = xcor_plot_
add_file_to_report( xcor_qc_, "xcor log", group, "QC and logs/$long/Cross-corr. log" )
add_file_to_table( xcor_plot_, "QC and logs/$long/Cross-corr. plot" )
}
if ( is_final_stage_xcor() ) return
}
}
void chipqc() {
if ( no_chipqc ) return
if ( is_input_peak() ) return
for (int rep=1; rep<=get_num_rep(); rep++) {
if ( no_par ) chipqc( rep )
else par chipqc( rep )
}
wait
print( "\n== Done chipqc()\n" )
}
void chipqc(int ctl, int rep ) {
// if ( true_rep ) {
// print("Warning: CHIPQC cannot run with a flag -true_rep\n");
// return
// }
if ( no_dup_removal ) {
print("Warning: CHIPQC cannot run with the flag -no_dup_removal\n");
return
}
group := get_group_name( ctl, rep )
long := get_long_group_name( ctl, rep )
fastqs := get_fastq(ctl,rep)
qc_o_dir := mkdir( "$out_dir/qc/$group" )
aln_o_dir := mkdir( "$out_dir/align/$group" ) // create align output directory
string chipqc_html
if ( is_se(ctl, rep ) ) {
( chipqc_html, chipqc_qc{group} ) = chipqc( fastqs[0], "", bam{group}, pbc_qc{group}, \
dup_qc{group}, filt_bam{group}, final_tag{group}, qc_o_dir, group )
}
else {
( chipqc_html, chipqc_qc{group} ) = chipqc( fastqs[0], fastqs[1], bam{group}, pbc_qc{group}, \
dup_qc{group}, filt_bam{group}, final_tag{group}, qc_o_dir, group )
}
add_file_to_report( chipqc_html, "CHIPQC\\nreport", group, "QC and logs/CHIPQC/$long/CHIPQC HTML report" )
}
void pool_tags() {
if ( !(is_final_stage_peak() || is_final_stage_idr()) ) return
if ( is_input_peak() ) return
////////////// pool replicates and controls, then create ppr (pooled pseudoreplicates) before peak calling
// make a temporary array of tagaligns, control tagaligns
string[] tags, tags_pr1, tags_pr2, tags_ctl
for ( int rep=1; rep<=get_num_rep(); rep++ ) {
tags.add( tag{"rep$rep"} )
if ( !true_rep ) {
tags_pr1.add( tag_pr1{"rep$rep"} )
tags_pr2.add( tag_pr2{"rep$rep"} )
}
if ( input_file_exists( 1, rep ) ) { // if control exists
tags_ctl.add( tag{"ctl$rep"} )
}
}
// if multiple tagalign for replicates, pool them
if ( tags.size() > 1 ) {
aln_pooled_o_dir := mkdir( "$out_dir/align/pooled_rep" )
tag_pooled := pool_tag( tags, aln_pooled_o_dir, "pooled_rep" )
add_file_to_report( tag_pooled, "tag-align", "pooled_rep", "Alignment/Pooled replicate/Tag-align" )
tag{"pooled_rep"} = tag_pooled
if ( !true_rep ) {
aln_ppr1_o_dir := mkdir( "$out_dir/align/pooled_pseudo_reps/ppr1" )
aln_ppr2_o_dir := mkdir( "$out_dir/align/pooled_pseudo_reps/ppr2" )
qc_ppr1_o_dir := mkdir( "$out_dir/qc/pooled_pseudo_reps/ppr1" ) // create qc output dir.
qc_ppr2_o_dir := mkdir( "$out_dir/qc/pooled_pseudo_reps/ppr2" ) // create qc output dir.
tag_ppr1_ := pool_tag( tags_pr1, aln_ppr1_o_dir, "ppr1" )
tag_ppr2_ := pool_tag( tags_pr2, aln_ppr2_o_dir, "ppr2" )
tag_ppr1 = tag_ppr1_ // for thread safety
tag_ppr2 = tag_ppr2_
add_file_to_report( tag_ppr1_, "tag-align", "ppr1", \
"Alignment/Pooled pseudo-replicates/Pooled pseudo-replicate 1/Tag-align" )
add_file_to_report( tag_ppr2_, "tag-align", "ppr2", \
"Alignment/Pooled pseudo-replicates/Pooled pseudo-replicate 2/Tag-align" )
wait
}
}
// if multiple tagalign for controls, pool them
if ( tags_ctl.size() > 1 ) {
aln_pooled_ctl_o_dir := mkdir( "$out_dir/align/pooled_ctl" )
tag_pooled := pool_tag( tags_ctl, aln_pooled_ctl_o_dir, "pooled_ctl" )
tag{"pooled_ctl"} = tag_pooled
add_file_to_report( tag_pooled, "tag-align\\n(pooled ctl.)", "pooled_ctl", "Alignment/Pooled control/Tag-align" )
}
wait
print( "\n== Done pool_tags()\n" )
}
void call_peaks() {
if ( !(is_final_stage_peak() || is_final_stage_idr()) ) return
if ( is_input_peak() ) return
// make a temporary array of tagaligns, control tagaligns
string[] tags, tags_pr1, tags_pr2, tags_ctl
for ( int rep=1; rep<=get_num_rep(); rep++ ) {
tags.add( tag{"rep$rep"} )
if ( !true_rep ) {
tags_pr1.add( tag_pr1{"rep$rep"} )
tags_pr2.add( tag_pr2{"rep$rep"} )
}
if ( input_file_exists( 1, rep ) ) { // if control exists
tags_ctl.add( tag{"ctl$rep"} )
}
}
// choose control (between true and pooled controls) for each replicate
string{} tag_ctl, tag_ctl_label // key: replicate id, value: actual control used for each replicate (according to control depth ratio)
if ( tags_ctl.size() == 0 ) { // if no control at all