forked from kundajelab/chipseq_pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chipseq.bds
1058 lines (715 loc) · 32.2 KB
/
chipseq.bds
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
// Functions usually have the following structure. Output naming policy is included in the function.
// They find prefix of input and add suffix to output and then change output's directory (o_dir).
// Therefore, output naming is already defined in each file. e.g. in bwa, PREFIX.fastq.gz -> PREFIX.bam
// e.g. output (string or array of str.) = _do_something( input1, input2, ... , o_dir, info )
// Don't be confused with additional code lines for each underscored( _ ) function and 'hrchy' and 'graph' things.
// They are for automatic generation of graphviz diagram and HTML filetable
// Variables can be declared with help context, they are automatically set as 'parameter variables' and become parameters on shell command line.
// You can omit type of variable with ':='. e.g. str1 := "abc" is equivalent to string str1 = "abc".
// 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{"rep1"} = filtered bam for replicate 1, peak_pr1{"spp,2"} = peak file for pseudo replicate 1 of replicate 2 generated from spp
help == chipseq pipeline settings
pe := false help Paired end data.
final_stage := "" help Final stage for pipeline (bam, filt_bam, tag, xcor and peak).
callpeak := "spp,macs2" help Peak calling method : spp and macs2, can choose both (default: 'spp,macs2').
true_rep := false help Call peaks on true replicates only.
ctl_depth_ratio := 1.2 help Cut-off ratio of two control tagaligns for pooling (default: 1.2).
sigtrk := "" help (BETA) Signal track generation method : bam2bw (bamCoverage in deepTools) or tag2bw (align2rawsignal).
make_wig := false help Create wig (for '-sigtrk tag2bw' only).
help() // print help if no parameters are given
include "modules/input_fastq.bds"
include "modules/input_bam.bds"
include "modules/input_tagalign.bds"
include "modules/input_peak.bds"
include "modules/species.bds"
include "modules/report.bds"
include "modules/align_bwa.bds"
include "modules/postalign_bam.bds"
include "modules/postalign_bed.bds"
include "modules/callpeak_spp.bds"
include "modules/callpeak_macs2.bds"
include "modules/callpeak_etc.bds"
include "modules/idr.bds"
include "modules/signal.bds"
//////// Global variables /////
// system vars
input := ""
num_ctl := 1
num_callpeak := 2 // spp (1), macs2 (2)
peakcallers := ["spp","macs2"]
// filepath of outputs
string{} fastq, bam, filt_bam, tag, tag_pr1, tag_pr2 // replicate data: map with key ("$ctl,$rep" or "$ctl,$rep,$pe" for PE fastqs)
string{} peak, peak_pr1, peak_pr2, peak_pooled, peak_ppr1, peak_ppr2 // peaks: map with key ("$rep")
string peak_overlap
string tag_pooled, tag_ctl_pooled, tag_ppr1, tag_ppr2
string idr_tr, idr_pr_rep1, idr_pr_rep2, idr_ppr, idr_opt, idr_consv, idr_tr_png, idr_pr_rep1_png, idr_pr_rep2_png, idr_ppr_png
string{} signal_trk, signal_trk_pval, signal_trk_fc // signal tracks from deepTools or bamCoverage: map with key ("$ctl,$rep")
string{} flagstat_qc, dup_qc, flagstat_nodup_qc, pbc_qc, xcor_qc, xcor_plot // QC logs: map with key ("$ctl,$rep" or "$ctl,$rep,$pe" for PE fastqs)
string{} xcor_qc_pr1, xcor_qc_pr2
string xcor_qc_ppr1, xcor_qc_ppr2
string idr_qc
//////// chipseq pipeline starts here /////
init_chipseq() // read command line parameters or configruation file
chk_input() // check input files are valid
align() // align and postalign
call_peaks() // call peaks
naive_overlap() // naive overlap
do_idr() // IDR
report()
void init_chipseq() {
pe = get_conf_val_bool( pe, ["pe"] )
final_stage = get_conf_val( final_stage, ["final_stage"] )
callpeak = get_conf_val( callpeak, ["callpeak"] )
true_rep = get_conf_val_bool( true_rep, ["true_rep"] )
ctl_depth_ratio = get_conf_val_real( ctl_depth_ratio, ["ctl_depth_ratio"])
sigtrk = get_conf_val( sigtrk, ["sigtrk"] )
// determine input type
if ( get_peak(1,0) != "" ) input = "peak"
if ( get_tag(0,1) != "" ) input = "tag"
if ( get_filt_bam(0,1) != "" ) input = "filt_bam"
if ( get_bam(0,1) != "" ) input = "bam"
if ( get_fastq(0,1,1) != "" ) input = "fastq"
if ( !control_exists() ) num_ctl = 0
print_chipseq()
out_dir = mkdir( out_dir ) // create output directory and get absolute path for it
}
void print_chipseq() {
print( "\n\n== chipseq pipeline settings\n")
print( "Final stage for ChIP-Seq\t: $final_stage\n" )
print( "# replicates \t\t\t: "+get_num_rep()+"\n" )
print( "Input data type\t\t\t: $input\n")
print( "Peak calling method\t\t: $callpeak\n" )
print( "Peak calling for true reps only\t: $true_rep\n" )
print( "Control rep. depth ratio\t: $ctl_depth_ratio\n" )
print( "Sig. trk. generation method\t: $sigtrk\n" )
print( "Create wig\t\t\t: $make_wig\n" )
}
void chk_input() {
print( "\n\n== checking chipseq inputs (data type = $input) ...\n\n" );
if ( is_input_fastq() ) chk_align_bwa()
if ( is_callpeak_macs2() ) chk_callpeak_macs2()
if ( is_sigtrk_aln2rawsig() ) chk_signal_aln2rawsig()
if ( is_final_stage_idr() ) chk_idr()
print("\n")
if ( is_input_peak() ) {
for ( int rep=0; rep<3; rep++) {
for (int pse=0; pse<3; pse++) {
peak := get_peak(rep,pse)
prefix := (rep==0 ? "pooled " : "") + (pse==0 ? "replicate $rep" : "pseudo-replicate $pse for replicate $rep")
print( "$prefix: \n\t$peak\n")
}
}
if ( get_peak(0,0)=="" || get_peak(1,0)=="" || get_peak(2,0)=="" ) { // if (!can_idr_for_true_rep() ) {
error("Cannot do IDR for true replicates. Check your peak inputs.\n")
}
return
}
string[] data_all
for ( int ctl=0; ctl <= num_ctl; ctl++) { // iterate through replicats (0: not control, 1~: controls)
for ( int rep=1; rep <= get_num_rep(); rep++) {
string[] data
prefix := (ctl==1) ? "Control " : ""
suffix := is_paired_end( ctl, rep ) ? " (PE)" : " (SE)"
if ( is_input_fastq() ) {
prefix = prefix + "Rep$rep fastq" + suffix
fastqs := get_fastqs( ctl, rep )
if ( fastqs.size()==0 ) {
data.push( "" )
}
else {
for ( string fastq : fastqs ) data.push( fastq )
}
}
else if ( is_input_bam() ) {
prefix = prefix +"Rep$rep bam" + suffix
data.push( get_bam( ctl, rep ) )
}
else if ( is_input_filt_bam() ) {
prefix = prefix +"Rep$rep filt_bam" + suffix
data.push( get_filt_bam( ctl, rep ) )
}
else if ( is_input_tag() ) {
prefix = prefix + "Rep$rep tagalign" + suffix
data.push( get_tag( ctl, rep ) )
}
print("$prefix :\n")
for ( string s : data ) {
print("\t$s\n")
if ( (s != "") && !path_exists(s) ) error("\t\tFile not found!\n")
}
// if data is missing
if ( data[0] == "" ) {
if ( (rep==2) && (ctl==1) ) \
print( "\tWarning: $prefix missing! keep going... (using control 1 for calling peaks on replicate 2)\n")
else if ( (rep==2) && (ctl==0) ) \
print( "\tWarning: $prefix missing! keep going... (peak calling for replicate 1 only)\n")
else \
error( "\t$prefix missing!\n")
continue
}
// check any duplicate input filename
for ( string s : data ) {
if ( is_in_array( get_basename( s ), get_basename( data_all ) ) ) \
error( "\t$prefix has duplicate filename!\n")
}
data_all = concat( data_all, data )
}
}
if ( final_stage != "" ) \
print( "\n====== Final stage : $final_stage \n\n" );
}
void align() {
// parallel jobs for align() for each replicate and each control
for ( int ctl=0; ctl <= num_ctl; ctl++) { // iterate through inputs (ctl==0 : replicate, ctl==1 : control)
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( !is_data_available( ctl, rep ) ) continue
wait_par_rep()
tid := par _align( ctl, rep )
tids_rep.add( tid )
}
}
wait
}
void _align( int ctl, int rep ) {
if ( is_single_ended( ctl, rep ) ) _align_SE( ctl, rep )
else _align_PE( ctl, rep )
}
void _align_SE( int ctl, int rep ) {
info := get_info( ctl, rep )
key := "$ctl,$rep" // key name for global output variable (map)
aln_o_dir := mkdir( "$out_dir/align/$info" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$info" ) // create qc output dir.
if ( is_input_fastq() ) {
fastqs := get_fastqs( ctl, rep )
fastq{key} = fastqs[0]
( bam{key}, flagstat_qc{key} ) = _bwa( fastq{key}, aln_o_dir, qc_o_dir, info )
wait
if ( is_final_stage_bam() ) return
}
if ( is_input_bam() || is_input_fastq() ) {
if ( is_input_bam() ) bam{key} = get_bam( ctl, rep )
( filt_bam{key}, dup_qc{key}, flagstat_nodup_qc{key}, pbc_qc{key} ) \
= _dedup_bam( bam{key}, aln_o_dir, qc_o_dir, info )
wait
if ( is_sigtrk_deeptools() ) {
signal_trk{key} = _bam_to_bw( filt_bam{key}, aln_o_dir, info )
}
if ( is_final_stage_filt_bam() ) return
}
if ( is_input_filt_bam() || is_input_bam() || is_input_fastq() ) {
if ( is_input_filt_bam() ) filt_bam{key} = get_filt_bam( ctl, rep )
tag{key} = _bam_to_tag( filt_bam{key}, aln_o_dir, info )
wait
if ( is_final_stage_tag() ) return
}
if ( is_input_tag() || is_input_filt_bam() || is_input_bam() || is_input_fastq() ) {
if ( is_input_tag() ) tag{key} = get_tag( ctl, rep )
if ( ctl == 0 ) { // if replicate
subsampled_tag := _subsample_tag( tag{key}, aln_o_dir, info )
( xcor_qc{key}, xcor_plot{key} ) = _xcor( subsampled_tag, qc_o_dir, info, info )
wait
if ( is_sigtrk_aln2rawsig() ) {
sig_o_dir := mkdir( "$out_dir/signal/tag2bw/$info" )
fraglen := get_fraglen( xcor_qc{key} )
signal_trk{key} = _tag_to_bw( tag{key}, fraglen, sig_o_dir, info )
if ( make_wig ) _tag_to_wig( tag{key}, fraglen, sig_o_dir, info )
//wait
}
}
if ( is_final_stage_xcor() ) return
}
if ( ctl == 0 ) { // if replicate
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$info/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$info/pr2" )
qc_pr1_o_dir := mkdir( "$out_dir/qc/pseudo_reps/$info/pr1" ) // create qc output dir.
qc_pr2_o_dir := mkdir( "$out_dir/qc/pseudo_reps/$info/pr2" ) // create qc output dir.
(tag_pr1{key}, tag_pr2{key} ) = _spr( tag{key}, aln_pr1_o_dir, aln_pr2_o_dir, info ) // make self pseudo replicate
wait
// xcor for pseudo replicates
string tmp
( xcor_qc_pr1{key}, tmp ) = _xcor( tag_pr1{key}, qc_pr1_o_dir, info+"-pr1", "pseudo_reps/$info/pr1" )// suppress graphviz and filetable
( xcor_qc_pr2{key}, tmp ) = _xcor( tag_pr2{key}, qc_pr2_o_dir, info+"-pr2", "pseudo_reps/$info/pr2" )
wait
}
}
void _align_PE( int ctl, int rep ) {
info := get_info( ctl, rep )
key := "$ctl,$rep" // key name for global output variable (map)
aln_o_dir := mkdir( "$out_dir/align/$info" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$info" ) // create qc output dir.
if ( is_input_fastq() ) {
fastqs := get_fastqs( ctl, rep )
fastq1 := fastqs[0]
fastq2 := fastqs[1]
fastq{key+",1"} = fastq1
fastq{key+",2"} = fastq2
( bam{key}, flagstat_qc{key} ) = _bwa_PE( fastq1, fastq2, aln_o_dir, qc_o_dir, info )
wait
if ( is_final_stage_bam() ) return
}
if ( is_input_bam() || is_input_fastq() ) {
if ( is_input_bam() ) bam{key} = get_bam( ctl, rep )
( filt_bam{key}, dup_qc{key}, flagstat_nodup_qc{key}, pbc_qc{key} ) \
= _dedup_bam_PE( bam{key}, aln_o_dir, qc_o_dir, info )
wait
if ( is_sigtrk_deeptools() ) { // signal track generation (deeptools)
signal_trk{key} = _bam_to_bw( filt_bam{key}, aln_o_dir, info )
}
if ( is_final_stage_filt_bam() ) return
}
if ( is_input_filt_bam() || is_input_bam() || is_input_fastq() ) {
if ( is_input_filt_bam() ) filt_bam{key} = get_filt_bam( ctl, rep )
tag{key} = _bam_to_tag( filt_bam{key}, aln_o_dir, info )
wait
if ( is_final_stage_tag() ) return
}
string bedpe, subsampled_tag
if ( is_input_tag() || is_input_filt_bam() || is_input_bam() || is_input_fastq() ) {
if ( ctl == 0 ) { // if replicate
if ( is_input_tag() ) {
tag{key} = get_tag( ctl, rep )
subsampled_tag = _subsample_tag_PE_xcor( tag{key}, aln_o_dir, info )
wait
}
else {
bedpe = _bam_to_bedpe( filt_bam{key}, aln_o_dir, info )
wait
subsampled_tag = _subsample_bedpe_to_tag_xcor( bedpe, aln_o_dir, info )
wait
}
( xcor_qc{key}, xcor_plot{key} ) = _xcor( subsampled_tag, qc_o_dir, info, info )
wait
if ( is_sigtrk_aln2rawsig() ) {
sig_o_dir := mkdir( "$out_dir/signal/tag2bw/$info" )
fraglen := get_fraglen( xcor_qc{key} )
signal_trk{key} = _tag_to_bw( tag{key}, fraglen, sig_o_dir, info )
if ( make_wig ) _tag_to_wig( tag{key}, fraglen, sig_o_dir, info )
}
}
if ( is_final_stage_xcor() ) return
}
if ( ctl == 0 ) { // if replicate
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$info/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$info/pr2" )
qc_pr1_o_dir := mkdir( "$out_dir/qc/pseudo_reps/$info/pr1" ) // create qc output dir.
qc_pr2_o_dir := mkdir( "$out_dir/qc/pseudo_reps/$info/pr2" ) // create qc output dir.
if ( is_input_tag() ) { // if starting from tag, we don't have bedpe file
( tag_pr1{key}, tag_pr2{key} ) = _spr_tag_PE( tag{key}, aln_pr1_o_dir, aln_pr2_o_dir, info )
}
else {
( tag_pr1{key}, tag_pr2{key} ) = _spr_PE( bedpe, aln_pr1_o_dir, aln_pr2_o_dir, info )
}
wait
// xcor for pseudo replicates
string tmp
( xcor_qc_pr1{key}, tmp ) = _xcor( tag_pr1{key}, qc_pr1_o_dir, info+"-pr1", "pseudo_reps/$info/pr1" ) // suppress graphviz
( xcor_qc_pr2{key}, tmp ) = _xcor( tag_pr2{key}, qc_pr2_o_dir, info+"-pr2", "pseudo_reps/$info/pr2" )
wait
}
}
void call_peaks() {
if ( !(is_final_stage_peak() || is_final_stage_idr()) ) return
if ( is_input_peak() ) return
if ( get_num_rep() > 1 ) _pool_tags()
for ( int i=0; i<num_callpeak; i++ ) {
par _call_peaks( peakcallers[i] )
}
wait
}
void _pool_tags() {
// pool replicates and controls, then create ppr (pooled pseudoreplicates) before peak calling
if ( is_data_available( 0, 2 ) ) { // if replicate 2 exists, pool replicate 1 and replicate 2
aln_pooled_o_dir := mkdir( "$out_dir/align/pooled_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_pooled, tag_ppr1, tag_ppr2 ) = _ppr( tag{"0,1"}, tag_pr1{"0,1"}, tag_pr2{"0,1"}, \
tag{"0,2"}, tag_pr1{"0,2"}, tag_pr2{"0,2"}, \
aln_pooled_o_dir, aln_ppr1_o_dir, aln_ppr2_o_dir, "" ) // make pooled psudo replicate
wait
// cross correlation analysis to get fragment of ppr's
string tmp
(xcor_qc_ppr1, tmp) = _xcor( tag_ppr1, qc_ppr1_o_dir, "ppr1", "pooled_pseudo_reps/ppr1" )
(xcor_qc_ppr2, tmp) = _xcor( tag_ppr2, qc_ppr2_o_dir, "ppr2", "pooled_pseudo_reps/ppr2" )
wait
}
if ( is_data_available( 1, 2 ) ) { // if control 2 exists, pool control 1 and control 2
aln_pooled_o_dir := mkdir( "$out_dir/align/pooled_ctl" )
tag_ctl_pooled = _pool_tag( tag{"1,1"}, tag{"1,2"}, aln_pooled_o_dir, "ctl" )
wait
_add_to_graphviz( ["tagalign_(ctl1)","tagalign_(ctl2)"], [tag{"1,1"},tag{"1,2"}], \
["tagalign_(ctl, pooled)"], [tag_ctl_pooled] )
_add_to_filetable(["L1_align/pooled_ctl/tagalign"], [tag_ctl_pooled] )
}
}
void _call_peaks( string peakcaller ) {
if ( peakcaller == "spp" && !(is_callpeak_spp()) ) return
if ( peakcaller == "macs2" && !(is_callpeak_macs2()) ) return
if ( peakcaller == "gem" && !(is_callpeak_gem()) ) return
// get fragment length from cross corr. qc log
string fraglen_ppr1, fraglen_ppr2, fraglen_mean
string{} fraglen, fraglen_pr1, fraglen_pr2 // key = replicate id
fraglen{1} = get_fraglen( xcor_qc{"0,1"} ) // get fragment length of replicate 1
fraglen_pr1{1} = get_fraglen( xcor_qc_pr1{"0,1"} )
fraglen_pr2{1} = get_fraglen( xcor_qc_pr2{"0,1"} )
if ( is_data_available( 0, 2 ) ) { // if replicate 2 exists, get mean fraglen and fraglen for ppr
fraglen{2} = get_fraglen( xcor_qc{"0,2"} ) // get fragment length of replicate 2
fraglen_pr1{2} = get_fraglen( xcor_qc_pr1{"0,2"} )
fraglen_pr2{2} = get_fraglen( xcor_qc_pr2{"0,2"} )
fraglen_mean = round( (fraglen{1}.parseReal()+fraglen{2}.parseReal())*0.5 ) // compute fraglen mean for pooled
fraglen_ppr1 = get_fraglen( xcor_qc_ppr1 )
fraglen_ppr2 = get_fraglen( xcor_qc_ppr2 )
}
// choose appropriate control
string tag_ctl1_label, tag_ctl2_label, tag_ctl_pooled_label, tag_ctl1_, tag_ctl2_, tag_ctl_pooled_
if ( !control_exists() ) {
if ( peakcaller != "macs2" ) return
tag_ctl1_ = ""; tag_ctl2_ = ""; tag_ctl_pooled_ = ""
tag_ctl1_label = ""; tag_ctl2_label = ""; tag_ctl_pooled_label = ""
}
else if ( is_data_available( 1, 2 ) ) { // if control 2 exists
tag_ctl1_ = tag{"1,1"}; tag_ctl2_ = tag{"1,2"}; tag_ctl_pooled_ = tag_ctl_pooled
tag_ctl_pooled_label = "ctl, pooled"
real ntags_rep1 = get_no_lines( tag{"0,1"} )
real ntags_rep2 = get_no_lines( tag{"0,2"} )
real ntags_ctl1 = get_no_lines( tag{"1,1"} )
real ntags_ctl2 = get_no_lines( tag{"1,2"} )
ratio_ctl_reads := ntags_ctl1/ntags_ctl2
if ( ratio_ctl_reads < 1.0 ) ratio_ctl_reads = 1.0/ratio_ctl_reads
if ( ratio_ctl_reads > ctl_depth_ratio ) {
print("\n\nNumber of reads in controls differ by a factor of $ctl_depth_ratio. Using pooled controls.\n" )
tag_ctl1_ = tag_ctl_pooled; tag_ctl2_ = tag_ctl_pooled
tag_ctl1_label = "ctl, pooled"; tag_ctl2_label = "ctl, pooled"
}
else {
if ( ntags_ctl1 < ntags_rep1 ) {
print("\n\nFewer reads in control replicate 1 than experiment replicate 1. Using pooled controls for replicate 1.\n")
tag_ctl1_ = tag_ctl_pooled
tag_ctl1_label = "ctl, pooled"; tag_ctl2_label = "ctl2"
}
if ( ntags_ctl2 < ntags_rep2 ) {
print("\n\nFewer reads in control replicate 2 than experiment replicate 2. Using pooled controls for replicate 2.\n")
tag_ctl2_ = tag_ctl_pooled
tag_ctl1_label = "ctl1"; tag_ctl2_label = "ctl, pooled"
}
}
}
else { // otherwise, no pooling. use control 1 for all replicates
tag_ctl1_ = tag{"1,1"}; tag_ctl2_ = tag{"1,1"}; tag_ctl_pooled_ = tag{"1,1"}
tag_ctl1_label = "ctl1"; tag_ctl2_label = "ctl1"; tag_ctl_pooled_label = "ctl1"
}
//////////////// call peaks /////////////////
/// Note that two additional lines for each peak calling are for visualization and file table.
// create directories
peak_o_dir := mkdir( "$out_dir/peak/$peakcaller") // peak directory structure
sig_o_dir := mkdir( "$out_dir/signal/$peakcaller") // signal directory structure
rep1_o_dir := mkdir( "$peak_o_dir/rep1" )
rep1_sig_dir := mkdir( "$sig_o_dir/rep1" )
rep1_hrchy := "rep1"
peak{"$peakcaller,1"} = _call_peaks( peakcaller, tag{"0,1"}, tag_ctl1_, fraglen{1}, true, rep1_o_dir, rep1_sig_dir, "rep1", \
tag_ctl1_label, rep1_hrchy )
if ( !true_rep ) {
rep1_pr1_o_dir := mkdir( "$peak_o_dir/pseudo_reps/rep1/pr1" )
rep1_pr1_sig_o_dir := mkdir( "$sig_o_dir/pseudo_reps/rep1/pr1" )
rep1_pr2_o_dir := mkdir( "$peak_o_dir/pseudo_reps/rep1/pr2" )
rep1_pr2_sig_o_dir := mkdir( "$sig_o_dir/pseudo_reps/rep1/pr2" )
rep1_pr1_hrchy := "pseudo_reps/rep1/pr1"
rep1_pr2_hrchy := "pseudo_reps/rep1/pr2"
peak_pr1{"$peakcaller,1"} = _call_peaks( peakcaller, tag_pr1{"0,1"}, tag_ctl1_, fraglen_pr1{1}, false, rep1_pr1_o_dir, rep1_pr1_sig_o_dir, "rep1-pr1", \
tag_ctl1_label, rep1_pr1_hrchy )
peak_pr2{"$peakcaller,1"} = _call_peaks( peakcaller, tag_pr2{"0,1"}, tag_ctl1_, fraglen_pr2{1}, false, rep1_pr2_o_dir, rep1_pr2_sig_o_dir, "rep1-pr2", \
tag_ctl1_label, rep1_pr2_hrchy )
}
if ( is_data_available( 0, 2 ) ) { // if replicate 2 exists
rep2_o_dir := mkdir( "$peak_o_dir/rep2" )
rep2_sig_o_dir := mkdir( "$sig_o_dir/rep2" )
rep2_hrchy := "rep2"
peak{"$peakcaller,2"} = _call_peaks( peakcaller, tag{"0,2"}, tag_ctl2_, fraglen{2}, true, rep2_o_dir, rep2_sig_o_dir, "rep2", \
tag_ctl2_label, rep2_hrchy )
pooled_o_dir := mkdir( "$peak_o_dir/pooled_rep" )
pooled_sig_o_dir:= mkdir( "$sig_o_dir/pooled_rep" )
pooled_hrchy := "pooled_rep"
peak_pooled{"$peakcaller"}= _call_peaks( peakcaller, tag_pooled, tag_ctl_pooled_, fraglen_mean, true, pooled_o_dir, pooled_sig_o_dir, "pooled", \
tag_ctl_pooled_label, pooled_hrchy )
if ( !true_rep ) {
rep2_pr1_o_dir := mkdir( "$peak_o_dir/pseudo_reps/rep2/pr1" )
rep2_pr1_sig_o_dir := mkdir( "$sig_o_dir/pseudo_reps/rep2/pr1" )
rep2_pr2_o_dir := mkdir( "$peak_o_dir/pseudo_reps/rep2/pr2" )
rep2_pr2_sig_o_dir := mkdir( "$sig_o_dir/pseudo_reps/rep2/pr2" )
rep2_pr1_hrchy := "pseudo_reps/rep2/pr1"
rep2_pr2_hrchy := "pseudo_reps/rep2/pr2"
peak_pr1{"$peakcaller,2"} = _call_peaks( peakcaller, tag_pr1{"0,2"}, tag_ctl2_, fraglen_pr1{2}, false, rep2_pr1_o_dir, rep2_pr1_sig_o_dir, "rep2-pr1", \
tag_ctl2_label, rep2_pr1_hrchy )
peak_pr2{"$peakcaller,2"} = _call_peaks( peakcaller, tag_pr2{"0,2"}, tag_ctl2_, fraglen_pr2{2}, false, rep2_pr2_o_dir, rep2_pr2_sig_o_dir, "rep2-pr2", \
tag_ctl2_label, rep2_pr2_hrchy )
ppr1_o_dir := mkdir( "$peak_o_dir/pooled_pseudo_reps/ppr1" )
ppr1_sig_o_dir := mkdir( "$sig_o_dir/pooled_pseudo_reps/ppr1" )
ppr2_o_dir := mkdir( "$peak_o_dir/pooled_pseudo_reps/ppr2" )
ppr2_sig_o_dir := mkdir( "$sig_o_dir/pooled_pseudo_reps/ppr2" )
ppr1_hrchy := "pooled_pseudo_reps/ppr1"
ppr2_hrchy := "pooled_pseudo_reps/ppr2"
peak_ppr1{"$peakcaller"} = _call_peaks( peakcaller, tag_ppr1, tag_ctl_pooled_, fraglen_ppr1, false, ppr1_o_dir, ppr1_sig_o_dir, "ppr1", \
tag_ctl_pooled_label, ppr1_hrchy )
peak_ppr2{"$peakcaller"} = _call_peaks( peakcaller, tag_ppr2, tag_ctl_pooled_, fraglen_ppr2, false, ppr2_o_dir, ppr2_sig_o_dir, "ppr2", \
tag_ctl_pooled_label, ppr2_hrchy )
/*\
"tagalign_(ppr2)",tag_ctl_pooled_label, "peak_$peakcaller"+"_(ppr2)", "pval_signal_(ppr2)", "fc_signal_(ppr2)", \
"L1_peak/$ppr2_hrchy/peak", "L1_signal/$ppr2_hrchy/pval", "L1_signal/$ppr2_hrchy/fc" )
*/
}
}
}
string _call_peaks( string peakcaller, string tag1, string tag_ctl, string fraglen, bool make_sig, \
string peak_o_dir, string sig_o_dir, string label, string label_ctl, string hrchy ) {
if ( peakcaller == "spp" ) {
string narrowpeak, regionpeak, score, pdf
( narrowpeak, regionpeak, score, pdf ) = _spp( tag1, tag_ctl, fraglen, peak_o_dir, label, label_ctl, hrchy )
return regionpeak // use regionpeak instead of narrowpeak
}
else if ( peakcaller == "macs2" ) {
string narrowpeak, gpeak, fc_bw, pval_bw
( narrowpeak, gpeak, fc_bw, pval_bw ) = _macs2( tag1, tag_ctl, fraglen, make_sig, peak_o_dir, sig_o_dir, label, label_ctl, hrchy )
// macs2 generates signal tracks
if ( make_sig ) {
signal_trk_pval{label} = pval_bw
signal_trk_fc{label} = fc_bw
}
return narrowpeak // use narrowpeak
}
}
void naive_overlap() {
if ( !(is_final_stage_peak() || is_final_stage_idr()) ) return
if ( !is_callpeak_spp() ) return
// naive overlap peak
if ( !true_rep ) {
overlap_o_dir := mkdir( "$out_dir/peak/spp/overlap" )
if ( get_num_rep() == 1 ) {
peak_overlap = _naive_overlap_peak( "regionPeak", peak{"spp,1"}, peak_pr1{"spp,1"}, peak_pr2{"spp,1"}, overlap_o_dir, "peak", \
"peak_spp", "spp/overlap")
}
else {
peak_overlap = _naive_overlap_peak( "regionPeak", peak_pooled{"spp"}, peak{"spp,1"}, peak{"spp,2"}, \
peak_ppr1{"spp"}, peak_ppr2{"spp"}, overlap_o_dir, "peak",\
"peak_spp", "spp/overlap")
}
}
}
void do_idr() {
if ( !is_final_stage_idr() ) return
if ( !is_callpeak_spp() ) return
if ( is_input_peak() ) {
peak{"spp,1"} = get_peak(1,0); peak{"spp,2"} = get_peak(2,0); peak_pooled{"spp"} = get_peak(0,0)
peak_pr1{"spp,1"} = get_peak(1,1); peak_pr2{"spp,1"} = get_peak(1,2)
peak_pr1{"spp,2"} = get_peak(2,1); peak_pr2{"spp,2"} = get_peak(2,2)
peak_ppr1{"spp"} = get_peak(0,1); peak_ppr2{"spp"} = get_peak(0,2)
}
idr_o_dir := mkdir( "$out_dir/peak/idr" )
if ( get_num_rep() > 1 ) {
idr_true_o_dir := mkdir( "$idr_o_dir/true_reps/rep1-rep2" )
(idr_tr, idr_tr_png) = _idr2( peak{"spp,1"}, peak{"spp,2"}, peak_pooled{"spp"}, idr_true_o_dir, "rep1-rep2", \
"peak_spp", ["rep1","rep2","pooled"], "true_reps/rep1-rep2" )
}
if ( !true_rep ) {
idr_rep1_pr_o_dir := mkdir( "$idr_o_dir/pseudo_reps/rep1" )
(idr_pr_rep1, idr_pr_rep1_png) = _idr2( peak_pr1{"spp,1"}, peak_pr2{"spp,1"}, peak{"spp,1"}, idr_rep1_pr_o_dir, "rep1-pr", \
"peak_spp", ["rep1-pr1","rep1-pr2","rep1"], "pseudo_reps/rep1" )
if ( get_num_rep() > 1 ) {
idr_rep2_pr_o_dir := mkdir( "$idr_o_dir/pseudo_reps/rep2" )
(idr_pr_rep2, idr_pr_rep2_png) = _idr2( peak_pr1{"spp,2"}, peak_pr2{"spp,2"}, peak{"spp,2"}, idr_rep2_pr_o_dir, "rep2-pr", \
"peak_spp", ["rep2-pr1","rep2-pr2","rep2"], "pseudo_reps/rep2" )
idr_ppr_o_dir := mkdir( "$idr_o_dir/pooled_pseudo_reps" )
(idr_ppr, idr_ppr_png) = _idr2( peak_ppr1{"spp"}, peak_ppr2{"spp"}, peak_pooled{"spp"}, idr_ppr_o_dir, "ppr", \
"peak_spp", ["ppr1","ppr2","pooled"], "pooled_pseudo_reps" )
wait
qc_o_dir := mkdir( "$out_dir/qc" ) // create qc output dir.
string idr_opt_, idr_consv_
// get final idr qc score, use idr final idr narrow peak files from true, pseudo and pooled pseudo reps
(idr_qc, idr_opt_, idr_consv_) = _idr_final_qc( idr_tr, idr_pr_rep1, idr_pr_rep2, idr_ppr, qc_o_dir, "" )
wait
idr_opt_o_dir := mkdir( "$idr_o_dir/optimal_set" )
idr_consv_o_dir := mkdir( "$idr_o_dir/conservative_set" )
// make a copy of (because genome browser checks duplicate track URLs)
idr_opt = replace_dir( idr_opt_, idr_opt_o_dir )
idr_consv = replace_dir( idr_consv_, idr_consv_o_dir )
in := [idr_opt_,idr_consv_]
out := [idr_opt,idr_consv]
task ( out<-in ) {
sys cp $idr_opt_ $idr_opt
sys cp $idr_consv_ $idr_consv
}
//_add_to_filetable( ["L1_peak/idr/opt_set/idr_peak","L1_peak/idr/consv_set/idr_peak"], [idr_opt, idr_consv] )
}
}
wait
}
void report() {
html := _html_filetable() // treeview for directory and file structure
html += _html_chipseq_tracks() // epigenome browser tracks
html += _html_graphviz() // graphviz workflow diagram
html += _html_chipseq_QC() // show QC tables and images
report( html )
}
string _html_chipseq_QC() {
string[] flagstat_qcs, dup_qcs, flagstat_nodup_qcs, pbc_qcs, xcor_qcs, xcor_plots
string[] flagstat_headers, dup_headers, flagstat_nodup_headers, pbc_headers, xcor_headers
for ( int ctl=0; ctl <= num_ctl; ctl++) { // iterate through inputs (ctl==0 : replicate, ctl==1 : control)
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( !is_data_available( ctl, rep ) ) continue
info := get_info( ctl, rep )
key := "$ctl,$rep"
if ( ctl == 0 ) {
//html_rep_by_id += _html_xcor( info, [ info ], [ xcor_qc{key} ], [ xcor_plot{key} ], [ info ] )
if ( xcor_qc.hasKey( key ) ) {
xcor_qcs += xcor_qc{key}
xcor_plots += xcor_plot{key}
xcor_headers += info
}
}
if ( flagstat_qc.hasKey( key ) ) flagstat_qcs += flagstat_qc{key}
if ( dup_qc.hasKey( key ) ) dup_qcs += dup_qc{key}
if ( flagstat_nodup_qc.hasKey( key ) ) flagstat_nodup_qcs += flagstat_nodup_qc{key}
if ( pbc_qc.hasKey( key ) ) pbc_qcs += pbc_qc{key}
if ( flagstat_qc.hasKey( key ) ) flagstat_headers += info
if ( dup_qc.hasKey( key ) ) dup_headers += info
if ( flagstat_nodup_qc.hasKey( key ) ) flagstat_nodup_headers += info
if ( pbc_qc.hasKey( key ) ) pbc_headers += info
}
}
html := "<div id='chipseq_qc'>"
html += _parse_flagstat_to_html( "all", flagstat_headers, flagstat_qcs, flagstat_headers )
html += _parse_dup_to_html( "all", dup_headers, dup_qcs, dup_headers )
html += _parse_flagstat_to_html( "all, filtered",flagstat_nodup_headers, flagstat_nodup_qcs, flagstat_nodup_headers )
html += _parse_pbc_to_html( "all", pbc_headers, pbc_qcs, pbc_headers )
html += _parse_xcor_to_html( "all", xcor_headers, xcor_qcs, xcor_plots, xcor_headers )
// if idr qc's exists, add them to html
if ( idr_qc != "" ) html += _parse_idr_to_html( "idr", idr_qc )
if ( idr_tr_png != "" ) html += _html_img( idr_tr_png, 800, "true reps" ) + " "
if ( idr_pr_rep1_png != "" ) html += _html_img( idr_pr_rep1_png, 800, "rep1 pseudo-reps" ) + " "
if ( idr_pr_rep2_png != "" ) html += _html_img( idr_pr_rep2_png, 800, "rep2 pseudo-reps" ) + " "
if ( idr_ppr_png != "" ) html += _html_img( idr_ppr_png, 800, "pooled pseudo-reps" ) + " "
html += "</div><br>"
return html
}
string _html_chipseq_tracks() {
string[] track_files, track_types, track_names
if ( signal_trk_pval.hasKey( "pooled" ) ) { track_types += "bigwig"; track_names += "pval (pooled)"; track_files += signal_trk_pval{"pooled"} }
if ( signal_trk_fc.hasKey( "pooled" ) ) { track_types += "bigwig"; track_names += "fc (pooled)"; track_files += signal_trk_fc{"pooled"} }
if ( peak_overlap != "" ) { track_types += "hammock"; track_names += "peak overlap"; track_files += _peak_to_hammock( peak_overlap ) }
if ( idr_opt != "" ) { track_types += "hammock"; track_names += "peak idr (opt. set)"; track_files += _peak_to_hammock( idr_opt ) }
if ( idr_consv != "" ) {track_types += "hammock"; track_names += "peak idr (cons. set)"; track_files += _peak_to_hammock( idr_consv ) }
for (int rep=1; rep<=get_num_rep(); rep++) {
if ( signal_trk_pval.hasKey( "rep$rep" ) ) { track_types += "bigwig"; track_names += "pval (rep$rep)"; track_files += signal_trk_pval{"rep$rep"} }
if ( signal_trk_fc.hasKey( "rep$rep" ) ) { track_types += "bigwig"; track_names += "fc (rep$rep)"; track_files += signal_trk_fc{"rep$rep"} }
if ( rep==1 && idr_pr_rep1 != "" ) { track_types += "hammock"; track_names += "peak idr (rep1_pr)"; track_files += _peak_to_hammock( idr_pr_rep1 ) }
if ( rep==2 && idr_pr_rep2 != "" ) { track_types += "hammock"; track_names += "peak idr (rep2_pr)"; track_files += _peak_to_hammock( idr_pr_rep2 ) }
}
html := _html_epg_browser_viz( track_files, track_types, track_names )
return html
}
void help() {
if ( is_cmd_line_arg_empty() ) {
printHelp()
exit
}
}
// basic functions
bool is_callpeak_spp() {
return callpeak.toLower().indexOf("spp")>=0
}
bool is_callpeak_macs2() {
return callpeak.toLower().indexOf("macs")>=0
}
bool is_callpeak_gem() {
return callpeak.toLower().indexOf("gem")>=0
}
bool is_sigtrk_aln2rawsig() {
return sigtrk.toLower().startsWith( "aln" ) || sigtrk.toLower().startsWith( "align" ) || sigtrk.toLower().startsWith( "tag" )
}
bool is_sigtrk_deeptools() {
return sigtrk.toLower().startsWith( "deep" ) || sigtrk.toLower().startsWith( "bam" )
}
/*
bool is_align_only_mode() {
return !is_input_peak() && !control_exists() && !is_callpeak_macs2()
}
*/
bool is_input_fastq() {
return input.toLower() == "fastq"
}
bool is_input_bam() {
return input.toLower() == "bam"
}
bool is_input_filt_bam() {
return input.toLower() == "filt_bam"
}
bool is_input_tag() {
return input.toLower() == "tag" || input.toLower() == "tagalign"
}
bool is_input_peak() {
return input.toLower() == "peak"
}
int get_num_rep() {
if ( is_input_fastq() ) return get_num_rep_fastq()
else if ( is_input_bam() ) return get_num_rep_bam()
else if ( is_input_filt_bam() ) return get_num_rep_filt_bam()
else if ( is_input_tag() ) return get_num_rep_tag()
else if ( is_input_peak() ) return get_num_rep_peak()
return 0
}
bool is_final_stage_bam() {
return final_stage.toLower() == "bam"
}
bool is_final_stage_filt_bam() {
return final_stage.toLower() == "filt_bam"
}
bool is_final_stage_tag() {
return final_stage.toLower() == "tag" || final_stage.toLower() == "tagalign"
}
bool is_final_stage_xcor() {
return final_stage.toLower() == "xcor"