forked from kundajelab/chipseq_pipeline
/
chipseq.bds
1277 lines (872 loc) · 39.1 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
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" help Peak calling method : spp, macs2 and gem (default: spp).
subsample := false help Subsample taglign for cross corr. analysis. Use '-nreads [NO_READS]' for # reads to subsample.
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 Signal track generation method : aln2rawsig and deeptools.
no_bw := false help Prevent bigwig generation.
make_wig := false help Create wig (only for '-sigtrk aln2rawsig').
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/report_graphviz.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_gem.bds"
include "modules/idr.bds"
include "modules/signal.bds"
//////// Global variables /////
// system vars
input := ""
num_ctl := 1
// 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 // peaks: map with key ("$rep")
string tag_pooled, tag_ctl_pooled, tag_ppr1, tag_ppr2
string peak_pooled, peak_ppr1, peak_ppr2
string idr_Nt, idr_N1, idr_N2, idr_Np
string idr_Nt_png, idr_N1_png, idr_N2_png, idr_Np_png
string{} signal_trk // signal tracks from deepTools or bamCoverage: map with key ("$ctl,$rep")
string {} signal_trk_pval, signal_trk_fc // signal tracks from MACS2: map with key ("$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 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
do_idr() // IDR
report()
void init_chipseq() {
pe = get_conf_val_bool( pe, ["pe"] )
final_stage = get_conf_val( final_stage, ["final_stage"] )
subsample = get_conf_val_bool( subsample, ["subsample"] )
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"] )
make_wig = get_conf_val_bool( make_wig, ["make_wig"] )
no_bw = get_conf_val_bool( no_bw, ["no_bw"] )
// 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( "\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 replicates only?: $true_rep\n" )
print( "Control rep. depth ratio\t: $ctl_depth_ratio\n" )
print( "Subsampling for tagaligns\t: $subsample\n")
print( "Sig. trk. generation method\t: $sigtrk\n" )
print( "Create wig\t\t\t: $make_wig\n" )
print( "No bigwig\t\t\t: $no_bw\n" )
out_dir = mkdir_path( out_dir ) // create output directory and get absolute path for it
}
void chk_input() {
print( "\n\n== checking chipseq inputs (data type = $input) ...\n\n" );
if ( is_input_fastq() ) chk_align_bwa()
if ( is_callpeak_gem() ) chk_callpeak_gem()
if ( is_sigtrk_macs2() || is_callpeak_macs2() ) chk_callpeak_macs2()
if ( is_sigtrk_aln2rawsig() ) chk_signal_aln2rawsig()
if ( is_final_stage_idr() ) chk_idr()
if ( is_align_only_mode() ) {
print("Align only mode (no peak calling) for " + get_num_rep() + " replicates.\n")
}
else if ( get_num_rep() > 2 ) {
error("For peak calling, # replicates (" + get_num_rep() + ") should be <= 2.\n")
}
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 repliactes. 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
if ( no_par ) _align( ctl, rep ) // go serial
else par _align( ctl, rep ) // go parallel
}
}
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)
align_out_dir := mkdir_path( "$out_dir/align_$info" ) // create align output directory
if ( is_input_fastq() ) {
fastqs := get_fastqs( ctl, rep )
fastq{key} = fastqs[0]
( bam{key}, flagstat_qc{key} ) = _bwa( fastq{key}, align_out_dir, info )
wait
_add_to_graphviz( ["fastq\\n($info)"], [fastq{key}], \
["bam\\n($info)"], [bam{key}], \
"bwa\\n($info)", "salmon" )
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}, align_out_dir, info )
wait
_add_to_graphviz( ["bam\\n($info)"], [bam{key}], \
["filt_bam\\n($info)"], [filt_bam{key}], \
"filter\\n($info)", "lightcoral" )
if ( is_sigtrk_deeptools() && !no_bw ) {
signal_trk{key} = _bam_to_bw( filt_bam{key}, align_out_dir, info )
_add_to_graphviz( ["filt_bam\\n($info)"], [filt_bam{key}], \
["signal\\n($info)"], [signal_trk{key}], \
"deepTools\\n($info)", "yellow" )
}
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}, align_out_dir, info )
wait
_add_to_graphviz( ["filt_bam\\n($info)"], [filt_bam{key}], \
["tagalign\\n($info)"], [tag{key}], \
"align\\n($info)", "orange" )
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 )
string subsampled_tag
if ( subsample ) {
subsampled_tag = _subsample_tag( tag{key}, align_out_dir, info )
wait
}
else {
subsampled_tag = tag{key}
}
if ( ctl == 0 ) {
( xcor_qc{key}, xcor_plot{key} ) = _xcor( subsampled_tag, align_out_dir, info )
wait
_add_to_graphviz( ["tagalign\\n($info)"], [tag{key}], \
["xcor_qc\\n($info)"], [xcor_qc{key}], \
"cross-corr.\\n($info)", "yellowgreen" )
if ( is_sigtrk_aln2rawsig() ) {
fraglen := get_fraglen( xcor_qc{key} )
string tmp
( signal_trk{key}, tmp ) = _sigtrk_aln2rawsig( tag{key}, fraglen, align_out_dir, info )
//wait
_add_to_graphviz( ["tagalign\\n($info)"], [tag{key}], \
["signal\\n($info)"], [signal_trk{key}], \
"align2rawsignal\\n($info)", "yellow" )
}
}
if ( is_final_stage_xcor() ) return
}
if ( ctl == 0 ) { // if replicate
(tag_pr1{key}, tag_pr2{key} ) = _spr( tag{key}, align_out_dir, info ) // make self pseudo replicate
wait
// xcor for pseudo replicates
string tmp
( xcor_qc_pr1{key}, tmp ) = _xcor( tag_pr1{key}, align_out_dir, info+"_pr1" )
( xcor_qc_pr2{key}, tmp ) = _xcor( tag_pr2{key}, align_out_dir, info+"_pr2" )
wait
_add_to_graphviz( ["tagalign\\n($info)"], [tag{key}], \
["tagalign\\n($info pr1)","tagalign\\n($info pr2)"], [tag_pr1{key},tag_pr2{key}] )
}
}
void _align_PE( int ctl, int rep ) {
info := get_info( ctl, rep )
key := "$ctl,$rep" // key name for global output variable (map)
align_out_dir := mkdir_path( "$out_dir/align_$info" ) // create align output directory
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, align_out_dir, info )
wait
_add_to_graphviz( ["fastq\\n($info"+"_1)","fastq\\n($info"+"_2)"], [fastq{key+",1"},fastq{key+",2"}], \
["bam\\n($info)"], [bam{key}], \
"bwa\\n($info)", "salmon" )
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}, align_out_dir, info )
wait
_add_to_graphviz( ["bam\\n($info)"], [bam{key}], \
["filt_bam\\n($info)"], [filt_bam{key}], \
"filter\\n($info)", "lightcoral" )
if ( is_sigtrk_deeptools() && !no_bw ) { // signal track generation (deeptools)
signal_trk{key} = _bam_to_bw( filt_bam{key}, align_out_dir, info )
_add_to_graphviz( ["filt_bam\\n($info)"], [filt_bam{key}], \
["signal\\n($info)"], [signal_trk{key}], \
"deepTools\\n($info)", "yellow" )
}
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}, align_out_dir, info )
wait
_add_to_graphviz( ["filt_bam\\n($info)"], [filt_bam{key}], \
["tagalign\\n($info)"], [tag{key}], \
"align\\n($info)", "orange" )
if ( is_final_stage_tag() ) return
}
string bedpe
if ( is_input_tag() || is_input_filt_bam() || is_input_bam() || is_input_fastq() ) {
string subsampled_tag
if ( is_input_tag() ) {
tag{key} = get_tag( ctl, rep )
if ( subsample ) {
subsampled_tag = _subsample_tag_PE( tag{key}, align_out_dir, info )
wait
}
else {
subsampled_tag = tag{key}
}
}
else {
if ( subsample ) {
nmsrt_bam := _nmsrt_bam( filt_bam{key}, align_out_dir, info )
wait
bedpe = _bam_to_bedpe( nmsrt_bam, align_out_dir, info )
wait
subsampled_tag = _subsample_bedpe_to_tag( bedpe, align_out_dir, info )
wait
}
else {
subsampled_tag = tag{key}
}
}
if ( ctl == 0 ) {
( xcor_qc{key}, xcor_plot{key} ) = _xcor( subsampled_tag, align_out_dir, info )
wait
_add_to_graphviz( ["tagalign\\n($info)"], [tag{key}], \
["xcor_qc\\n($info)"], [xcor_qc{key}], \
"cross-corr.\\n($info)", "yellowgreen" )
if ( is_sigtrk_aln2rawsig() ) {
fraglen := get_fraglen( xcor_qc{key} )
string tmp
( signal_trk{key}, tmp ) = _sigtrk_aln2rawsig( tag{key}, fraglen, align_out_dir, info )
//wait
_add_to_graphviz( ["tagalign\\n($info)"], [tag{key}], \
["signal\\n($info)"], [signal_trk{key}], \
"align2rawsignal\\n($info)", "yellow" )
}
}
if ( is_final_stage_xcor() ) return
}
if ( ctl == 0 ) { // if replicate
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}, align_out_dir, info )
}
else {
( tag_pr1{key}, tag_pr2{key} ) = _spr_PE( bedpe, align_out_dir, info )
}
wait
// xcor for pseudo replicates
string tmp
( xcor_qc_pr1{key}, tmp ) = _xcor( tag_pr1{key}, align_out_dir, info+"_pr1" )
( xcor_qc_pr2{key}, tmp ) = _xcor( tag_pr2{key}, align_out_dir, info+"_pr2" )
wait
_add_to_graphviz( ["tagalign\\n($info)"], [tag{key}], \
["tagalign\\n($info pr1)", "tagalign\\n($info pr2)"], [tag_pr1{key},tag_pr2{key}] )
}
}
string[] _sigtrk_aln2rawsig( string tag, string fraglen, string out_dir, string info ) {
string wig, bigwig
if ( !no_bw ) {
prefix := change_dir( remove_ext( tag, ["tagAlign","tag","bed"] ), out_dir )
bigwig = "$prefix.bigwig"
in := tag
out := bigwig
if ( out <- in ) { // if bigwig is newer than tag
bedgraph := _tag_to_bdg( tag, fraglen, out_dir, info )
wait
bigwig = _bdg_to_bw( bedgraph, out_dir, info )
wait
bedgraph.rm()
}
}
if ( make_wig ) wig = _tag_to_wig( tag, fraglen, out_dir, info )
return [bigwig, wig]
}
string _callpeak( string tag1, string tag_ctl, string fraglen, string out_dir, string info ) {
if ( is_callpeak_spp() ) {
string narrowpeak, rpeak, score, pdf
( narrowpeak, rpeak, score, pdf ) = _spp( tag1, tag_ctl, fraglen, out_dir, info )
return rpeak // use regionpeak instead of narrowpeak
}
else if ( is_callpeak_macs2() ) {
string narrowpeak, bpeak, gpeak, fc_bw, pval_bw
( narrowpeak, bpeak, gpeak, fc_bw, pval_bw ) = _macs2( tag1, tag_ctl, fraglen, out_dir, info )
// macs2 generates signal tracks
if ( info == "Rep1" ) {
signal_trk_pval{1} = pval_bw
signal_trk_fc{1} = fc_bw
_add_to_graphviz( ["tagalign\\n(rep1)"], [tag{"0,1"}], \
["signal pval\\n(rep1)","signal fc\\n(rep1)"], [pval_bw,fc_bw] )
}
else if ( info == "Rep2" ) {
signal_trk_pval{2} = pval_bw
signal_trk_fc{2} = fc_bw
_add_to_graphviz( ["tagalign\\n(rep2)"], [tag{"0,2"}], \
["signal pval\\n(rep2)","signal fc\\n(rep2)"], [pval_bw,fc_bw] )
}
return narrowpeak // use narrowpeak
}
else if ( is_callpeak_gem() ) {
string peak, ksm, pfm, html
( peak, ksm, pfm, html ) = _gem( tag1, tag_ctl, out_dir, info )
return peak
}
}
void call_peaks() {
if ( is_align_only_mode() || is_input_peak() ) return
if ( !is_final_stage_peak() && !is_final_stage_idr() ) return
if ( !control_exists() ) { // if there is no control, show warning or exit
if ( is_callpeak_macs2() ) {
print("\n\nIMPORTANT! call peaks with MACS2 without control!\n")
}
else {
error("Cannot call peaks without control data! Instead choose MACS2 peak calling (add -callpeak macs2 to cmd. line).\n")
}
}
align_pooled_dir := mkdir_path( "$out_dir/align_pooled" )
// pool replicates and controls, then create ppr (pooled pseudoreplicates) before peak calling
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, pool replicate 1 and replicate 2
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
(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"}, align_pooled_dir, "" ) // make pooled psudo replicate
wait
_add_to_graphviz( ["tagalign\\n(rep1)","tagalign\\n(rep2)"], \
[tag{"0,1"},tag{"0,2"}], ["tagalign\\n(pooled)"], [tag_pooled] )
_add_to_graphviz( ["tagalign\\n(rep1 pr1)","tagalign\\n(rep2 pr1)"], [tag_pr1{"0,1"},tag_pr1{"0,2"}], \
["tagalign\\n(ppr1)"], [tag_ppr1] )
_add_to_graphviz( ["tagalign\\n(rep1 pr2)","tagalign\\n(rep2 pr2)"], [tag_pr2{"0,1"},tag_pr2{"0,2"}], \
["tagalign\\n(ppr2)"], [tag_ppr2] )
// cross correlation analysis to get fragment of ppr's
xcor_qc_ppr1 := _xcor( tag_ppr1, align_pooled_dir, "ppr1" )
xcor_qc_ppr2 := _xcor( tag_ppr2, align_pooled_dir, "ppr2" )
wait
fraglen_ppr1 = get_fraglen( xcor_qc_ppr1[0] )
fraglen_ppr2 = get_fraglen( xcor_qc_ppr2[0] )
}
string tag_ctl1_graphviz_id, tag_ctl2_graphviz_id, tag_ctl_pooled_graphviz_id
if ( !control_exists() ) {
tag{"1,1"} = ""
tag_ctl1_graphviz_id = ""
tag_ctl2_graphviz_id = ""
tag_ctl_pooled_graphviz_id = ""
}
else if ( is_data_available( 1, 2 ) ) { // if control 2 exists
tag_ctl_pooled = _pool_tag( tag{"1,1"}, tag{"1,2"}, align_pooled_dir, "ctl" )
wait
_add_to_graphviz( ["tagalign\\n(ctl1)","tagalign\\n(ctl2)"], [tag{"1,1"},tag{"1,2"}], \
["tagalign\\n(ctl, pooled)"], [tag_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"} )
wait
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{"1,1"} = tag_ctl_pooled
tag{"1,2"} = tag_ctl_pooled
tag_ctl1_graphviz_id = "tagalign\\n(ctl, pooled)"
tag_ctl2_graphviz_id = "tagalign\\n(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{"1,1"} = tag_ctl_pooled
tag_ctl1_graphviz_id = "tagalign\\n(ctl, pooled)"
tag_ctl2_graphviz_id = "tagalign\\n(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{"1,2"} = tag_ctl_pooled
tag_ctl1_graphviz_id = "tagalign\\n(ctl1)"
tag_ctl2_graphviz_id = "tagalign\\n(ctl, pooled)"
}
}
tag_ctl_pooled_graphviz_id = "tagalign\\n(ctl, pooled)"
}
else { // otherwise, no pooling. use control 1 for all replicates
tag_ctl_pooled = tag{"1,1"}
tag{"1,2"} = tag{"1,1"}
tag_ctl1_graphviz_id = "tagalign\\n(ctl1)"
tag_ctl2_graphviz_id = "tagalign\\n(ctl1)"
tag_ctl_pooled_graphviz_id = "tagalign\\n(ctl1)"
}
// call peaks
peak_out_dir := mkdir_path( "$out_dir/peaks"+"_$callpeak") // peak directory structure
truerep_dir := mkdir_path( "$peak_out_dir/true_replicates" )
string pseudorep_dir, ppr_dir
if ( !true_rep ) {
pseudorep_dir = mkdir_path( "$peak_out_dir/pseudo_replicates" )
ppr_dir = mkdir_path( "$peak_out_dir/pooled_pseudo_replicates" )
}
peak{1} = _callpeak( tag{"0,1"}, tag{"1,1"}, fraglen{1}, truerep_dir, "Rep1" )
_add_to_graphviz( ["tagalign\\n(rep1)",tag_ctl1_graphviz_id], [tag{"0,1"},tag{"1,1"}], \
["peak\\n(true rep1)"], [peak{1}], \
"$callpeak\\n(true rep1)", "skyblue" )
if ( !true_rep ) {
peak_pr1{1} = _callpeak( tag_pr1{"0,1"}, tag{"1,1"}, fraglen_pr1{1}, pseudorep_dir, "Rep1_PR1" )
_add_to_graphviz( ["tagalign\\n(rep1 pr1)",tag_ctl1_graphviz_id], \
[tag_pr1{"0,1"}, tag{"1,1"}], ["peak\\n(rep1 pr1)"], [peak_pr1{1}], \
"$callpeak\\n(rep1 pr1)", "skyblue" )
peak_pr2{1} = _callpeak( tag_pr2{"0,1"}, tag{"1,1"}, fraglen_pr2{1}, pseudorep_dir, "Rep1_PR2" )
_add_to_graphviz( ["tagalign\\n(rep1 pr2)",tag_ctl1_graphviz_id], \
[tag_pr2{"0,1"}, tag{"1,1"}], ["peak\\n(rep1 pr2)"], [peak_pr2{1}], \
"$callpeak\\n(rep1 pr2)", "skyblue" )
}
if ( is_data_available( 0, 2 ) ) { // if replicate 2 exists
peak{2} = _callpeak( tag{"0,2"}, tag{"1,2"}, fraglen{2}, truerep_dir, "Rep2" )
_add_to_graphviz( ["tagalign\\n(rep2)",tag_ctl2_graphviz_id], [tag{"0,2"},tag{"1,2"}], \
["peak\\n(true rep2)"], [peak{2}], \
"$callpeak\\n(true rep2)", "skyblue" )
peak_pooled = _callpeak( tag_pooled, tag_ctl_pooled, fraglen_mean, truerep_dir, "Pooled" )
_add_to_graphviz( ["tagalign\\n(pooled)",tag_ctl_pooled_graphviz_id], [tag_pooled,tag_ctl_pooled], \
["peak\\n(pooled)"], [peak{2}], \
"$callpeak\\n(pooled)", "skyblue" )
if ( !true_rep ) {
peak_pr1{2} = _callpeak( tag_pr1{"0,2"}, tag{"1,2"}, fraglen_pr1{2}, pseudorep_dir, "Rep2_PR1" )
_add_to_graphviz( ["tagalign\\n(rep2 pr1)",tag_ctl2_graphviz_id], [tag_pr1{"0,2"},tag{"1,2"}], \
["peak\\n(rep2 pr1)"], [peak_pr1{2}], \
"$callpeak\\n(rep2 pr1)", "skyblue" )
peak_pr2{2} = _callpeak( tag_pr2{"0,2"}, tag{"1,2"}, fraglen_pr2{2}, pseudorep_dir, "Rep2_PR2" )
_add_to_graphviz( ["tagalign\\n(rep2 pr2)",tag_ctl2_graphviz_id], [tag_pr2{"0,2"},tag{"1,2"}], \
["peak\\n(rep2 pr2)"], [peak_pr2{2}], \
"$callpeak\\n(rep2 pr2)", "skyblue" )
peak_ppr1 = _callpeak( tag_ppr1, tag_ctl_pooled, fraglen_ppr1, ppr_dir, "PPR1" )
_add_to_graphviz( ["tagalign\\n(ppr1)",tag_ctl_pooled_graphviz_id], [tag_ppr1,tag_ctl_pooled], \
["peak\\n(ppr1)"], [peak_ppr1], \
"$callpeak\\n(ppr1)", "skyblue" )
peak_ppr2 = _callpeak( tag_ppr2, tag_ctl_pooled, fraglen_ppr2, ppr_dir, "PPR2" )
_add_to_graphviz( ["tagalign\\n(ppr2)",tag_ctl_pooled_graphviz_id], [tag_ppr2,tag_ctl_pooled], \
["peak\\n(ppr2)"], [peak_ppr2], \
"$callpeak\\n(ppr2)", "skyblue" )
}
}
wait
}
void do_idr() {
if ( !is_final_stage_idr() ) return
string idr_out_dir
if ( is_input_peak() ) {
idr_out_dir = mkdir_path( "$out_dir/" + (use_idr1 ? "idr1" : "idr2") )
peak{1} = get_peak(1,0)
peak{2} = get_peak(2,0)
peak_pooled = get_peak(0,0)
peak_pr1{1} = get_peak(1,1)
peak_pr2{1} = get_peak(1,2)
peak_pr1{2} = get_peak(2,1)
peak_pr2{2} = get_peak(2,2)
peak_ppr1 = get_peak(0,1)
peak_ppr2 = get_peak(0,2)
}
else {
idr_out_dir = mkdir_path( "$out_dir/peaks"+"_$callpeak/" + (use_idr1 ? "idr1" : "idr2") )
if ( !is_data_available( 0, 2 ) ) return
}
string tmp
// take 2nd and 3rd output (narrowpeak.gz, and png plot) of _idr
(tmp, idr_Nt, idr_Nt_png) = _idr( peak{1}, peak{2}, peak_pooled, idr_out_dir, "true_rep_Rep1_vs_Rep2" )
_add_to_graphviz( ["peak\\n(true rep1)","peak\\n(true rep2)","peak\\n(pooled)"], [peak{1},peak{2},peak_pooled], \
["peak_idr\\n(Nt)"], [idr_Nt], \
"idr\\n(true rep)", "limegreen" )
if ( !true_rep ) {
(tmp, idr_N1, idr_N1_png) = _idr( peak_pr1{1}, peak_pr2{1}, peak{1}, idr_out_dir, "pseudo_rep_Rep1_PR1_vs_Rep1_PR2" )
_add_to_graphviz( ["peak\\n(rep1 pr1)","peak\\n(rep1 pr2)","peak\\n(true rep1)"], [peak_pr1{1},peak_pr2{1},peak{1}], \
["peak_idr\\n(N1)"], [idr_N1], \
"idr\\n(pseudo rep1)", "limegreen" )
(tmp, idr_N2, idr_N2_png) = _idr( peak_pr1{2}, peak_pr2{2}, peak{2}, idr_out_dir, "pseudo_rep_Rep2_PR1_vs_Rep2_PR2" )
_add_to_graphviz( ["peak\\n(rep2 pr1)","peak\\n(rep2 pr2)","peak\\n(true rep2)"], [peak_pr1{2},peak_pr2{2},peak{2}], \
["peak_idr\\n(N2)"], [idr_N1], \
"idr\\n(pseudo rep2)", "limegreen" )
(tmp, idr_Np, idr_Np_png) = _idr( peak_ppr1, peak_ppr2, peak_pooled, idr_out_dir, "pooled_pseudo_rep_PPR1_vs_PPR2" )
_add_to_graphviz( ["peak\\n(ppr1)","peak\\n(ppr2)","peak\\n(pooled)"], [peak_ppr1,peak_ppr2,peak_pooled], \
["peak_idr\\n(Np)"], [idr_Np], \
"idr\\n(pooled pseudo rep)", "limegreen" )
wait
if ( path_exists( blacklist_idr ) && \
idr_Nt!="" && idr_N1!="" && idr_N2!="" && idr_Np!="" ) { // if we have all four narrow peaks, do idr qc
// get final idr qc score, use idr final idr narrow peak files from true, pseudo and pooled pseudo reps
idr_qc = _idr_final_qc( idr_Nt, idr_N1, idr_N2, idr_Np, idr_out_dir, "" )
_add_to_graphviz( ["idr\\n(Nt)","idr\\n(N1)","idr\\n(N2)","idr\\n(Np)"], [idr_Nt,idr_N1,idr_N2,idr_Np], \
["idr_qc"], [idr_qc] )
}
}
wait
}
void report() {
wait
script_dir := programPath.dirName() // directory where chipseq.bds exists
sys cp $script_dir/html/jquery.treetable.* $out_dir # move report js/css files to out_dir
report_file := "$out_dir/Report.html"
report_header := "$script_dir/rpt_header.html"
html := report_header.read()
html += _html_chipseq_files() // treeview for directory and file structure
html += _html_chipseq_tracks() // epigenome browser tracks
html += _html_chipseq_graphviz()// graphviz workflow diagram
html += _html_chipseq_QC() // show QC tables and images
html += "<br></body></html>"
report_file.write( html )
}
string _html_chipseq_graphviz() {
html := "<div id='chipseq_graphviz'><b>Workflow diagram</b>"
html += _html_graphviz()
html += "</div><br>\n"
return html
}
string _html_chipseq_files() {
html := "<div id='chipseq_files'><b>Directories and files</b>"
html += "<table id='chipseq_filetable'> <caption>"+\
"<a href='#' onclick=\"jQuery('#chipseq_filetable').treetable('expandAll'); return false;\">Expand all</a>   " + \
"<a href='#' onclick=\"jQuery('#chipseq_filetable').treetable('collapseAll'); return false;\">Collapse all</a>" + \
"</caption> <thead><tr><th>Files</th><th>Path</th></tr></thead> <tbody>"
for ( int ctl=0; ctl <= num_ctl; ctl++) {
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( !is_data_available( ctl, rep ) ) continue
info := get_info( ctl, rep )
key := "$ctl,$rep"
align_out_dir := "$out_dir/align_$info"
if ( ctl == 0 ) \
html += "<tr data-tt-id='$key'><td>" + "Replicate $rep ($info)"+ "</td><td>"+ _html_link_url( align_out_dir ) +"</td></tr>"
else \
html += "<tr data-tt-id='$key'><td>" + "Control $rep ($info)" + "</td><td>"+ _html_link_url( align_out_dir ) +"</td></tr>"
// replicates and controls
if ( is_paired_end( ctl, rep ) ) {
if ( fastq.hasKey(key+",1") ) {
html += "<tr data-tt-id='fastq_$key"+"_1' data-tt-parent-id='$key'><td>" + "fastq pair 1" + "</td><td>"+_html_link_url( fastq{key+",1"} )+"</td></tr>"
html += "<tr data-tt-id='fastq_$key"+"_2' data-tt-parent-id='$key'><td>" + "fastq pair 2" + "</td><td>"+_html_link_url( fastq{key+",2"} )+"</td></tr>"
}
}
else {
if ( fastq.hasKey(key) ) {
html += "<tr data-tt-id='fastq_$key' data-tt-parent-id='$key'><td>" + "fastq" + "</td><td>"+_html_link_url( fastq{key} )+"</td></tr>"
}
}
if ( bam.hasKey(key) ) html += "<tr data-tt-id='bam_$key' data-tt-parent-id='$key'><td>" + "bam" + "</td><td>"+_html_link_url( bam{key} )+"</td></tr>"
if ( filt_bam.hasKey(key) ) html += "<tr data-tt-id='filt_bam_$key' data-tt-parent-id='$key'><td>" + "filtered bam" + "</td><td>"+_html_link_url( filt_bam{key} )+"</td></tr>"
if ( tag.hasKey(key) ) html += "<tr data-tt-id='tag_$key' data-tt-parent-id='$key'><td>" + "tagalign" + "</td><td>"+_html_link_url( tag{key} )+"</td></tr>"
}
}
// signal track
if ( signal_trk.hasKey("0,1") || signal_trk_pval.hasKey(1) ) {
html += "<tr data-tt-id='signal'><td>" + "Signal track"+ "</td><td>"+ "" +"</td></tr>"
for ( int ctl=0; ctl <= num_ctl; ctl++) {
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( !is_data_available( 0, rep ) ) continue
info := get_info( ctl, rep )
key := "$ctl,$rep"
if ( signal_trk.hasKey(key) ) html += "<tr data-tt-id='signal_$key' data-tt-parent-id='signal'><td>" + "pval ($info)" + "</td><td>"+ _html_link_url( signal_trk{key} )+"</td></tr>"
}
}
if ( signal_trk_pval.hasKey(1) ) html += "<tr data-tt-id='signal_pval1' data-tt-parent-id='signal'><td>" + "pval (macs2, rep1)" + "</td><td>"+ _html_link_url( signal_trk_pval{1} )+"</td></tr>"
if ( signal_trk_pval.hasKey(2) ) html += "<tr data-tt-id='signal_pval2' data-tt-parent-id='signal'><td>" + "pval (macs2, rep2)" + "</td><td>"+ _html_link_url( signal_trk_pval{1} )+"</td></tr>"
if ( signal_trk_fc.hasKey(1) ) html += "<tr data-tt-id='signal_fc1' data-tt-parent-id='signal'><td>" + "fc (macs2, rep1)" + "</td><td>"+ _html_link_url( signal_trk_fc{1} )+"</td></tr>"
if ( signal_trk_fc.hasKey(2) ) html += "<tr data-tt-id='signal_fc2' data-tt-parent-id='signal'><td>" + "fc (macs2, rep2)" + "</td><td>"+ _html_link_url( signal_trk_fc{1} )+"</td></tr>"
}
// peaks
if ( peak.hasKey(1) ) { // if peak output exists
peak_out_dir := "$out_dir/peaks"+"_$callpeak"
html += "<tr data-tt-id='peak'><td>" + "Peaks"+ "</td><td>"+ _html_link_url( peak_out_dir ) +"</td></tr>"
// true rep
html += "<tr data-tt-id='peak_truerep' data-tt-parent-id='peak'><td>" + "True replicates"+ "</td><td>"+ _html_link_url( peak_out_dir+"/true_replicates" ) +"</td></tr>"
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( peak.hasKey(rep) ) html += "<tr data-tt-id='peak_$rep' data-tt-parent-id='peak_truerep'><td>" + "rep$rep"+ "</td><td>"+ _html_link_url( peak{rep} ) +"</td></tr>"
}
// pseudo rep
if ( peak_pr1.hasKey(1) ) \
html += "<tr data-tt-id='peak_pr' data-tt-parent-id='peak'><td>" + "Pseudo replicates"+ "</td><td>"+ _html_link_url( peak_out_dir+"/pseudo_replicates" ) +"</td></tr>"
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( peak_pr1.hasKey(rep) ) html += "<tr data-tt-id='peak_pr1_$rep' data-tt-parent-id='peak_pr'><td>" + "rep$rep"+ "</td><td>"+ _html_link_url( peak_pr1{rep} ) +"</td></tr>"
if ( peak_pr2.hasKey(rep) ) html += "<tr data-tt-id='peak_pr2_$rep' data-tt-parent-id='peak_pr'><td>" + "rep$rep"+ "</td><td>"+ _html_link_url( peak_pr2{rep} ) +"</td></tr>"
}
if ( peak_ppr1 != "" ) {
html += "<tr data-tt-id='peak_ppr' data-tt-parent-id='peak'><td>" + "Pooled pseudo replicates"+ "</td><td>"+ _html_link_url( peak_out_dir+"/pooled_pseudo_replicates" ) +"</td></tr>"
html += "<tr data-tt-id='peak_ppr1' data-tt-parent-id='peak_ppr'><td>" + "ppr1"+ "</td><td>"+ _html_link_url( peak_ppr1 ) +"</td></tr>"
html += "<tr data-tt-id='peak_ppr2' data-tt-parent-id='peak_ppr'><td>" + "ppr2"+ "</td><td>"+ _html_link_url( peak_ppr2 ) +"</td></tr>"
}
}
// idr