-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathatac.bds
executable file
·1103 lines (796 loc) · 32.8 KB
/
atac.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 == atac pipeline settings
se := false help Single ended data.
trimmed_fastq := false help Skip fastq-trimming stage.
align := false help Align only (no MACS2 peak calling or IDR or ataqc analysis).
subsample := 0 help # of reads to subsample replicates. Subsampled tagalign will be used for steps downstream (default: 0; no subsampling).
nreads := 25000000 help # reads to be subsampled for cross corr. analysis (default: 25000000).
true_rep := false help No pseudo-replicates.
no_idr := false help No IDR analysis on called peaks. This will change p-value threshold (0.1->0.01) in MACS2 peak calling.
no_ataqc := false help No ATAQC
csem := false help Use CSEM for alignment.
mem_ataqc := "8G" help Max. memory for ATAQC (default: 8G).
smooth_win := "150" help Smoothing window size for MACS2 peak calling (default: 150).
dnase_seq := false help DNase-Seq (no tn5 shifting, subsampling 50M by default for cross-corr. analysis).
idr_thresh := "0.1" help IDR threshold : -log_10(score) (default: 0.1).
help() // show help contexts
include "modules/input_fastq.bds"
include "modules/input_bam.bds"
include "modules/input_tagalign.bds"
include "modules/align_bowtie2.bds"
include "modules/align_trim_adapter.bds"
include "modules/postalign_bam.bds"
include "modules/postalign_bed.bds"
include "modules/callpeak_macs2.bds"
include "modules/callpeak_etc.bds"
include "modules/idr.bds"
include "modules/report.bds"
input := ""
// Important output file names are stored in global variables (usually a string map string{} with a key with replicate id, pair id and peakcaller name)
// e.g. filt_bam{"1"} = filtered bam for replicate 1, peak_pr1{"2"} = peak file for pseudo replicate 1 of replicate 2
string{} fastq, align_log, bam, filt_bam, dup_qc, flagstat_nodup_qc, pbc_qc, xcor_qc, xcor_plot
string{} final_tag, final_tag_pr1, final_tag_pr2
string{} peak, peak_001, peak_pr1, peak_pr2
string peak_pooled, peak_pooled_001, peak_ppr1, peak_ppr2, peak_overlap
string{} gpeak, gpeak_001, gpeak_pr1, gpeak_pr2
string gpeak_pooled, gpeak_pooled_001, gpeak_ppr1, gpeak_ppr2, gpeak_overlap
string{} pval_bigwig_001, fc_bigwig_001
string{} idr_tr, idr_pr, idr_tr_png, idr_pr_png
string idr_ppr, idr_opt, idr_consv, idr_ppr_png
string idr_qc
main()
void main() { // atac pipeline starts here
init_atac()
chk_input_data()
atac()
post_atac()
do_idr()
ataqc()
report()
}
void init_atac() {
se = get_conf_val_bool( se, ["se"] )
trimmed_fastq = get_conf_val_bool( trimmed_fastq, ["trimmed_fastq"] )
align = get_conf_val_bool( align, ["align"] )
subsample = get_conf_val_int( subsample, ["subsample"] )
true_rep = get_conf_val_bool( true_rep, ["true_rep"] )
no_idr = get_conf_val_bool( no_idr, ["no_idr"] )
no_ataqc = get_conf_val_bool( no_ataqc, ["no_ataqc"] )
csem = get_conf_val_bool( csem, ["csem"] )
mem_ataqc = get_conf_val( mem_ataqc, ["mem_ataqc"] )
smooth_win = get_conf_val( smooth_win, ["smooth_win"] )
dnase_seq = get_conf_val_bool( dnase_seq, ["dnase_seq"] )
idr_thresh = get_conf_val( idr_thresh, ["idr_thresh"] )
nreads = get_conf_val_int( nreads, ["nreads"] )
if ( input == "" ) { // determine input type
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"
}
fraglen0 = true // set fragment length as zero for cross corr. analysis
rm_chr_from_tag = "chrM"; // remove lines with chrM in _bam_to_tag
print_atac()
}
void print_atac() {
print( "\n\n== atac pipeline settings\n")
print( "Single ended data set?\t\t: $se\n")
print( "Input data type\t\t\t: $input\n")
print( "Fastqs are trimmed?\t\t: $trimmed_fastq\n")
print( "# Replicates\t\t\t: "+ get_num_rep() + "\n")
print( "MACS2 peak calling\t\t: " + !align + "\n")
print( "Subsample # lines in replicates (0 if no subsampling)\t: $subsample\n")
print( "Subsample # read for cross-corr. analysis \t: $nreads\n")
print( "No pseudo replicates\t\t: $true_rep\n")
print( "No IDR analysis on peaks\t: $no_idr\n")
print( "No ATAQC (advanced QC report)\t: $no_ataqc\n")
print( "Use CSEM for alignment\t\t: $csem\n")
print( "Max. memory for ATAQC\t\t: $mem_ataqc\n")
print( "Smoothing window for MACS2\t: $smooth_win\n")
print( "DNase Seq\t\t\t: $dnase_seq\n")
print( "IDR threshold\t\t\t: $idr_thresh\n" )
}
void chk_input_data() {
print( "\n== checking atac inputs (data type = $input) ...\n" );
if ( is_input_tag() && !se && subsample > 0 ) {
print("Warning: Cannot subsample paried end tagaligns. Disabling subsampling...\n")
subsample = 0
}
if ( !is_input_fastq() && !no_ataqc ) {
print("Warning: ATAQC is available for fastq inputs only. Disabling ATAQC...\n")
no_ataqc = true
}
if ( !se && csem ) {
error("CSEM (-csem) is not available for paired end data set!\n")
}
if ( get_num_rep() > 2 && !no_idr ) {
print("Warning: IDR is available for one replicate or two replicates only. Disabling IDR...\n")
no_idr = true
}
if ( is_input_fastq() && !trimmed_fastq ) chk_align_trim_adapter()
if ( is_input_fastq() ) chk_align_bwt2()
if ( !align ) chk_callpeak_macs2()
if ( !no_idr ) chk_idr()
if ( !no_ataqc ) chk_ataqc()
print("\n")
// check data files
suffix := se ? " (SE)" : " (PE)"
string[] data_all
for ( int rep=1; rep <= get_num_rep(); rep++) {
string[] data
string prefix
if ( is_input_fastq() ) {
prefix = "Rep$rep fastq" + suffix
fastqs := get_fastqs( rep )
if ( !se && fastqs.size() < 2 ) error("A pair of fastqs are needed for replicate $rep (if it's single-ended add '-se')\n")
if ( fastqs.size()==0 ) {
data.push( "" )
}
else {
for ( string fastq : fastqs ) data.push( fastq )
}
if ( fastqs.size()==0 ) error("No fastq defined for replicate $rep\n")
}
else if ( is_input_bam() ) {
prefix = "Rep$rep bam" + suffix
data.push( get_bam( 0, rep ) )
}
else if ( is_input_filt_bam() ) {
prefix = "Rep$rep filt_bam" + suffix
data.push( get_filt_bam( 0, rep ) )
}
else if ( is_input_tag() ) {
prefix = "Rep$rep tagalign" + suffix
data.push( get_tag( 0, rep ) )
}
else {
error("Unspported input type (-input)!")
}
print("$prefix :\n")
for ( string s : data ) {
print("\t$s\n")
if ( (s != "") && !path_exists(s) ) error("\t\tFile not found!\n")
}
if ( data[0] == "" ) {
error( "\t$prefix missing!\n")
}
// 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 )
}
}
void chk_ataqc() {
print("\nChecking parameters and data files for ATAQC. If you don't want ATAQC then add '-no_ataqc'\n\n")
if ( species == "" ) error("Genome name is missing ( '-species [GENOME_NAME; hg19, mm9, ...]' )!\n" )
if ( ref_fa == "" ) error("Specify your reference genome .fa ('-ref_fa [FA]')!\n")
if ( tss_enrich == "" ) error("V plot index is missing ( '-tss_enrich' )!\n")
if ( dnase == "" ) error("DNase bed is missing ( '-dnase' )!\n")
if ( blacklist == "" ) error("Blacklist bed is missing ( '-blacklist' )!\n")
if ( prom == "" ) error("Promoter bed is missing ( '-prom' )!\n")
if ( enh == "" ) error("Enhancer bed is missing ( '-enh' )!\n")
if ( reg2map == "" ) error("reg2map is missing ( '-reg2map' )!\n")
if ( roadmap_meta == "" ) error("Roadmap metadata are missing ( '-roadmap_meta' )!\n")
}
void atac() {
// filesize of input ( map with key $rep )
int{} filesize
for ( int rep=1; rep <= get_num_rep(); rep++) {
// check file size to distribute_nth nth to each nth_app
// determine # threads for each app related to alignment
// get file size in bytes
if ( is_input_fastq() ) {
fastqs := get_fastqs( rep )
filesize{rep} = (fastqs[0]).size()
if ( fastqs.size() > 1) filesize{rep} += (fastqs[1]).size()
}
else if ( is_input_bam() ) {
filesize{rep} = (get_bam( 0, rep )).size()
}
else if ( is_input_filt_bam() ) {
filesize{rep} = (get_filt_bam( 0, rep )).size()
}
else if ( is_input_tag() ) {
filesize{rep} = (get_tag( 0, rep )).size()
}
}
//// distribute # threads for each replicate/control
nth_rep := distribute_nth( nth, filesize ) // distribute_nth # threads according to input filesize
for (int rep=1; rep<=get_num_rep(); rep++) {
par _atac( rep, nth_rep{rep} )
}
wait_clear_tids()
print( "\n== Done atac()\n" )
}
void _atac( int rep, int nth_rep ) {
if ( se ) _atac_SE( rep, nth_rep )
else _atac_PE( rep, nth_rep )
}
void _atac_SE( int rep, int nth_rep ) {
info := "rep$rep"
aln_o_dir := mkdir( "$out_dir/align/$info" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$info" ) // create qc output dir.
string bam_
if ( is_input_fastq() ) {
fastqs := get_fastqs( rep )
fastq{rep} = fastqs[0]
string p1
if ( trimmed_fastq ) {
p1 = fastqs[0]
}
else {
p1 = _trim_adapters( fastqs[0], aln_o_dir, info, nth_rep, "" )
}
wait
if ( csem ) {
( bam_, align_log{rep} ) = _bowtie2_csem( p1, aln_o_dir, qc_o_dir, info, nth_rep, !trimmed_fastq )
bam{rep} = bam_
}
else {
( bam_, align_log{rep} ) = _bowtie2( p1, aln_o_dir, qc_o_dir, info, nth_rep, !trimmed_fastq )
bam{rep} = bam_
}
wait
}
string filt_bam_
if ( is_input_fastq() || is_input_bam() ) {
if ( is_input_bam() ) {
bam_ = get_bam( 0, rep )
bam{rep} = bam_
}
( filt_bam_, dup_qc{rep}, flagstat_nodup_qc{rep}, pbc_qc{rep} ) \
= _dedup_bam( bam_, aln_o_dir, qc_o_dir, info, nth_rep )
filt_bam{rep} = filt_bam_
wait
}
string tag
if ( is_input_fastq() || is_input_bam() || is_input_filt_bam() ) {
if ( is_input_filt_bam() ) {
filt_bam_ = get_filt_bam( 0, rep )
filt_bam{rep} = filt_bam_
}
tag = _bam_to_tag( filt_bam_, aln_o_dir, info )
wait
}
string final_tag_, final_tag_pr1_, final_tag_pr2_
if ( is_input_fastq() || is_input_bam() || is_input_filt_bam() || is_input_tag() ) {
if ( is_input_tag() ) tag = get_tag( 0, rep )
string subsampled_tag
if ( subsample != 0 ) {
subsampled_tag = _subsample_tag( tag, subsample, aln_o_dir, info )
wait
}
else {
subsampled_tag = tag
}
if ( dnase_seq ) {
final_tag_ = subsampled_tag
}
else {
final_tag_ = _tn5_shift_tag( subsampled_tag, aln_o_dir, info )
}
final_tag{rep} = final_tag_
wait
if ( !true_rep ) {
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$info/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$info/pr2" )
( final_tag_pr1_, final_tag_pr2_ ) = _spr( final_tag_, aln_pr1_o_dir, aln_pr2_o_dir, info )
final_tag_pr1{rep} = final_tag_pr1_
final_tag_pr2{rep} = final_tag_pr2_
wait
}
}
// call peaks
if ( !align ) {
// cross-corr. analysis
subsampled_tag_xcor := _subsample_tag( tag, nreads, aln_o_dir, info )
wait
// distribute # threads (nth) to macs2 peak calling and xcor
int nth_peak_n_sig, nth_peak, nth_xcor // weight=2 for peak n sig, weight=1 for peak only and xcor
(nth_peak_n_sig, nth_peak, nth_xcor) = distribute_nth( nth, true_rep ? [2,1,1] : [2,1,1,1,1] )
// xcor
( xcor_qc{rep}, xcor_plot{rep} ) = _xcor( subsampled_tag_xcor, qc_o_dir, info, nth_xcor, info ) // 1 cpu
// call peaks
hrchy := info
peak_o_dir := mkdir( "$out_dir/peak/macs2/$info")
sig_o_dir := mkdir( "$out_dir/signal/macs2/$info" )
// macs2 pval thresh = 0.01
( peak_001{rep}, gpeak_001{rep}, fc_bigwig_001{rep}, pval_bigwig_001{rep} ) \
= _macs2_atac( final_tag_, "$smooth_win", 0.01, true, \
peak_o_dir, sig_o_dir, info, nth_peak_n_sig, hrchy )
( peak{rep}, gpeak{rep} ) \
= _macs2_atac( final_tag_, "$smooth_win", 0.1, false, \
peak_o_dir, sig_o_dir, info, nth_peak, hrchy )
if ( !true_rep ) {
pr1_hrchy := "pseudo_reps/$info/pr1"
pr2_hrchy := "pseudo_reps/$info/pr2"
peak_pr1_o_dir := mkdir( "$out_dir/peak/macs2/$pr1_hrchy" )
peak_pr2_o_dir := mkdir( "$out_dir/peak/macs2/$pr2_hrchy" )
sig_pr1_o_dir := mkdir( "$out_dir/signal/macs2/$pr1_hrchy" )
sig_pr2_o_dir := mkdir( "$out_dir/signal/macs2/$pr2_hrchy" )
( peak_pr1{rep}, gpeak_pr1{rep} ) \
= _macs2_atac( final_tag_pr1_, "$smooth_win", 0.1, false, \
peak_pr1_o_dir, sig_pr1_o_dir, info+"-pr1", nth_peak, pr1_hrchy )
( peak_pr2{rep}, gpeak_pr2{rep} ) \
= _macs2_atac( final_tag_pr2_, "$smooth_win", 0.1, false, \
peak_pr2_o_dir, sig_pr2_o_dir, info+"-pr2", nth_peak, pr2_hrchy )
}
}
}
void _atac_PE( int rep, int nth_rep ) {
info := "rep$rep"
aln_o_dir := mkdir( "$out_dir/align/$info" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$info" ) // create qc output dir.
string bam_
if ( is_input_fastq() ) {
fastqs := get_fastqs( rep )
fastq{rep+",1"} = fastqs[0]
fastq{rep+",2"} = fastqs[1]
string p1, p2
if ( trimmed_fastq ) {
p1 = fastqs[0]
p2 = fastqs[1]
}
else {
( p1, p2 ) = _trim_adapters_PE( fastqs[0], fastqs[1], aln_o_dir, info, nth_rep )
wait
}
( bam_, align_log{rep} ) = _bowtie2_PE( p1, p2, aln_o_dir, qc_o_dir, info, nth_rep, !trimmed_fastq )
bam{rep} = bam_
wait
}
string filt_bam_
if ( is_input_fastq() || is_input_bam() ) {
if ( is_input_bam() ) {
bam_ = get_bam( 0, rep )
bam{rep} = bam_
}
(filt_bam_, dup_qc{rep}, flagstat_nodup_qc{rep}, pbc_qc{rep} ) \
= _dedup_bam_PE( bam_, aln_o_dir, qc_o_dir, info, nth_rep )
filt_bam{rep} = filt_bam_
wait
}
string bedpe, subsampled_bedpe, tag
if ( is_input_fastq() || is_input_bam() || is_input_filt_bam() ) {
if ( is_input_filt_bam() ) {
filt_bam_ = get_filt_bam( 0, rep )
filt_bam{rep} = filt_bam_
}
bedpe = _bam_to_bedpe( filt_bam_, aln_o_dir, info )
wait
if ( subsample!=0 ) {
subsampled_bedpe = _subsample_bedpe( bedpe, subsample, aln_o_dir, info )
}
else {
subsampled_bedpe = bedpe
}
wait
tag = _bedpe_to_tag( subsampled_bedpe, aln_o_dir, info )
wait
}
string final_tag_, final_tag_pr1_, final_tag_pr2_
if ( is_input_fastq() || is_input_bam() || is_input_filt_bam() || is_input_tag() ) {
if ( is_input_tag() ) tag = get_tag( 0, rep )
string aln_pr1_o_dir, aln_pr2_o_dir
string tag_pr1, tag_pr2
if ( !true_rep ) {
aln_pr1_o_dir = mkdir( "$out_dir/align/pseudo_reps/$info/pr1" )
aln_pr2_o_dir = mkdir( "$out_dir/align/pseudo_reps/$info/pr2" )
if ( is_input_tag() ) {
( tag_pr1, tag_pr2 ) = _spr_tag_PE( tag, aln_pr1_o_dir, aln_pr2_o_dir, info )
}
else {
( tag_pr1, tag_pr2 ) = _spr_PE( subsampled_bedpe, aln_pr1_o_dir, aln_pr2_o_dir, info )
}
wait
}
if ( dnase_seq ) {
final_tag_ = tag
}
else {
final_tag_ = _tn5_shift_tag( tag, aln_o_dir, info )
}
final_tag{rep} = final_tag_
if ( !true_rep ) {
if ( dnase_seq ) {
final_tag_pr1_ = tag_pr1
final_tag_pr2_ = tag_pr2
}
else {
final_tag_pr1_ = _tn5_shift_tag( tag_pr1, aln_pr1_o_dir, info )
final_tag_pr2_ = _tn5_shift_tag( tag_pr2, aln_pr2_o_dir, info )
}
final_tag_pr1{rep} = final_tag_pr1_
final_tag_pr2{rep} = final_tag_pr2_
}
wait
}
if ( !align ) {
string subsampled_tag_xcor
if ( bedpe == "" ) {
subsampled_tag_xcor = _subsample_tag_PE_xcor( tag, nreads, aln_o_dir, info )
}
else {
subsampled_tag_xcor = _subsample_bedpe_to_tag_xcor( bedpe, nreads, aln_o_dir, info )
}
wait
// distribute # threads (nth) for macs2 peak calling and xcor
int nth_peak_n_sig, nth_peak, nth_xcor // weight=2 for peak n sig, weight=1 for peak only and xcor
(nth_peak_n_sig, nth_peak, nth_xcor) = distribute_nth( nth, true_rep ? [2,1,1] : [2,1,1,1,1] )
( xcor_qc{rep}, xcor_plot{rep} ) = _xcor( subsampled_tag_xcor, qc_o_dir, info, nth_xcor, info ) // 1 cpu
// call peaks
hrchy := info
peak_o_dir := mkdir( "$out_dir/peak/macs2/$info")
sig_o_dir := mkdir( "$out_dir/signal/macs2/$info" )
( peak_001{rep}, gpeak_001{rep}, fc_bigwig_001{rep}, pval_bigwig_001{rep} ) \
= _macs2_atac( final_tag_, "$smooth_win", 0.01, true, \
peak_o_dir, sig_o_dir, info, nth_peak_n_sig, hrchy )
( peak{rep}, gpeak{rep} ) = _macs2_atac( final_tag_, "$smooth_win", 0.1, false, \
peak_o_dir, sig_o_dir, info, nth_peak, hrchy )
if ( !true_rep ) {
pr1_hrchy := "pseudo_reps/$info/pr1"
pr2_hrchy := "pseudo_reps/$info/pr2"
peak_pr1_o_dir := mkdir( "$out_dir/peak/macs2/$pr1_hrchy" )
peak_pr2_o_dir := mkdir( "$out_dir/peak/macs2/$pr2_hrchy" )
sig_pr1_o_dir := mkdir( "$out_dir/signal/macs2/$pr1_hrchy" )
sig_pr2_o_dir := mkdir( "$out_dir/signal/macs2/$pr2_hrchy" )
( peak_pr1{rep}, gpeak_pr1{rep} ) \
= _macs2_atac( final_tag_pr1_, "$smooth_win", 0.1, false, \
peak_pr1_o_dir, sig_pr1_o_dir, info+"-pr1", nth_peak, pr1_hrchy )
( peak_pr2{rep}, gpeak_pr2{rep} ) \
= _macs2_atac( final_tag_pr2_, "$smooth_win", 0.1, false, \
peak_pr2_o_dir, sig_pr2_o_dir, info+"-pr2", nth_peak, pr2_hrchy )
}
}
}
void post_atac() { // for pooling two replicates and calling peaks on them
if ( align ) return
string[] tags, tags_pr1, tags_pr2
for ( int rep=1; rep<=get_num_rep(); rep++ ) {
tags.add( final_tag{rep} )
if ( !true_rep ) {
tags_pr1.add( final_tag_pr1{rep} )
tags_pr2.add( final_tag_pr2{rep} )
}
}
if ( get_num_rep() > 1 ) {
aln_pooled_o_dir := mkdir( "$out_dir/align/pooled_rep" )
final_tag_pooled := _pool_tag( tags, aln_pooled_o_dir, "reps" )
// graphviz, filetable for pooled tagalign
string[] graph_in
for ( int rep=1; rep<=get_num_rep(); rep++ ) graph_in.add("tagalign_(rep$rep)")
_add_to_graphviz( graph_in, tags, ["tagalign_(pooled)"], [final_tag_pooled] )
_add_to_filetable(["L1_align/pooled_rep/tagalign"], [final_tag_pooled] )
//
// Make shifted tags for pooled pseudo rep (ppr).
string final_tag_ppr1, final_tag_ppr2
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" )
final_tag_ppr1_ := _pool_tag( tags_pr1, aln_ppr1_o_dir, "reps-pr1" )
final_tag_ppr2_ := _pool_tag( tags_pr2, aln_ppr2_o_dir, "reps-pr2" )
final_tag_ppr1 = final_tag_ppr1_ // thread safety
final_tag_ppr2 = final_tag_ppr2_
// graphviz, filetable for ppr
string[] graph_in_pr1, graph_in_pr2
for ( int rep=1; rep<=get_num_rep(); rep++ ) {
graph_in_pr1.add("tagalign_(rep$rep-pr1)")
graph_in_pr2.add("tagalign_(rep$rep-pr2)")
}
_add_to_graphviz( graph_in_pr1, tags_pr1, ["tagalign_(ppr1)"], [final_tag_ppr1_] )
_add_to_graphviz( graph_in_pr2, tags_pr2, ["tagalign_(ppr2)"], [final_tag_ppr2_] )
_add_to_filetable(["L1_align/pooled_pseudo_reps/ppr1/tagalign"], [final_tag_ppr1_] )
_add_to_filetable(["L1_align/pooled_pseudo_reps/ppr2/tagalign"], [final_tag_ppr2_] )
//
}
wait_clear_tids()
// distribute nth to macs2 peak calling for pooled replicates
int nth_pooled_sig, nth_pooled, nth_ppr
if ( !true_rep ) (nth_pooled_sig, nth_pooled, nth_ppr) = distribute_nth( nth, [4,2,1,1] )
else (nth_pooled_sig, nth_pooled) = distribute_nth( nth, [4,2] )
peak_o_dir := mkdir( "$out_dir/peak/macs2")
sig_o_dir := mkdir( "$out_dir/signal/macs2")
pooled_hrchy := "pooled_rep"
pooled_o_dir := mkdir( "$peak_o_dir/$pooled_hrchy" )
pooled_sig_o_dir:= mkdir( "$sig_o_dir/$pooled_hrchy" )
( peak_pooled_001, gpeak_pooled_001, \
fc_bigwig_001{"pooled"}, \
pval_bigwig_001{"pooled"} ) \
= _macs2_atac( final_tag_pooled, "$smooth_win", 0.01, true, \
pooled_o_dir, pooled_sig_o_dir, "pooled", nth_pooled_sig, pooled_hrchy )
( peak_pooled, gpeak_pooled ) = _macs2_atac( final_tag_pooled, "$smooth_win", 0.1, false, \
pooled_o_dir, pooled_sig_o_dir, "pooled", nth_pooled, pooled_hrchy )
if ( !true_rep ) {
ppr1_hrchy := "pooled_pseudo_reps/ppr1"
ppr2_hrchy := "pooled_pseudo_reps/ppr2"
ppr1_o_dir := mkdir( "$peak_o_dir/$ppr1_hrchy" )
ppr2_o_dir := mkdir( "$peak_o_dir/$ppr2_hrchy" )
ppr1_sig_o_dir := mkdir( "$sig_o_dir/$ppr1_hrchy" )
ppr2_sig_o_dir := mkdir( "$sig_o_dir/$ppr2_hrchy" )
// call peaks on ppr
( peak_ppr1, gpeak_ppr1 ) = _macs2_atac( final_tag_ppr1, "$smooth_win", 0.1, false, \
ppr1_o_dir, ppr1_sig_o_dir, "ppr1", nth_ppr, ppr1_hrchy )
( peak_ppr2, gpeak_ppr2 ) = _macs2_atac( final_tag_ppr2, "$smooth_win", 0.1, false, \
ppr2_o_dir, ppr2_sig_o_dir, "ppr2", nth_ppr, ppr2_hrchy )
}
}
wait_clear_tids()
print( "\n== Done post_atac()\n" )
}
void do_idr() {
if ( align || no_idr ) return
string{} filt_peak, filt_peak_pr1, filt_peak_pr2
string filt_peak_pooled, filt_peak_ppr1, filt_peak_ppr2
string{} filt_gpeak, filt_gpeak_pr1, filt_gpeak_pr2
string filt_gpeak_pooled, filt_gpeak_ppr1, filt_gpeak_ppr2
// take top $npeak_filt lines from narrowpeaks for idr
for ( int rep=1; rep<=get_num_rep(); rep++ ) {
filt_peak{rep} = _filt_top_peaks( peak{rep}, "", "rep$rep", "peak_macs2", "macs2/rep$rep" )
filt_gpeak{rep} = _filt_top_peaks( gpeak{rep}, "", "rep$rep", "gpeak_macs2", "macs2/rep$rep" )
if ( !true_rep ) {
filt_peak_pr1{rep} = _filt_top_peaks( peak_pr1{rep}, "", "rep$rep-pr1", "peak_macs2", "macs2/pseudo_reps/rep$rep/pr1" )
filt_peak_pr2{rep} = _filt_top_peaks( peak_pr2{rep}, "", "rep$rep-pr2", "peak_macs2", "macs2/pseudo_reps/rep$rep/pr2" )
filt_gpeak_pr1{rep} = _filt_top_peaks( gpeak_pr1{rep}, "", "rep$rep-pr1", "gpeak_macs2", "macs2/pseudo_reps/rep$rep/pr1" )
filt_gpeak_pr2{rep} = _filt_top_peaks( gpeak_pr2{rep}, "", "rep$rep-pr2", "gpeak_macs2", "macs2/pseudo_reps/rep$rep/pr2" )
}
}
if ( get_num_rep() > 1 ) {
filt_peak_pooled = _filt_top_peaks( peak_pooled, "", "pooled", "peak_macs2", "macs2/pooled_rep" )
filt_gpeak_pooled= _filt_top_peaks( gpeak_pooled, "", "pooled", "gpeak_macs2", "macs2/pooled_rep" )
if ( !true_rep ) {
filt_peak_ppr1 = _filt_top_peaks( peak_ppr1, "", "ppr1", "peak_macs2", "macs2/pooled_pseudo_reps/ppr1" )
filt_peak_ppr2 = _filt_top_peaks( peak_ppr2, "", "ppr2", "peak_macs2", "macs2/pooled_pseudo_reps/ppr2" )
filt_gpeak_ppr1 = _filt_top_peaks( gpeak_ppr1, "", "ppr1", "gpeak_macs2", "macs2/pooled_pseudo_reps/ppr1" )
filt_gpeak_ppr2 = _filt_top_peaks( gpeak_ppr2, "", "ppr2", "gpeak_macs2", "macs2/pooled_pseudo_reps/ppr2" )
}
}
wait_clear_tids()
// naive overlap peak
overlap_o_dir := mkdir( "$out_dir/peak/macs2/overlap" )
if ( get_num_rep() == 1 ) {
if ( !true_rep ) {
peak_overlap = _naive_overlap_peak( "narrowPeak", filt_peak{1}, filt_peak_pr1{1}, filt_peak_pr2{1}, overlap_o_dir, \
"peak", "peak_macs2_filt", "macs2/overlap" )
gpeak_overlap = _naive_overlap_peak( "gappedPeak", filt_gpeak{1}, filt_gpeak_pr1{1}, filt_gpeak_pr2{1}, overlap_o_dir, \
"gpeak", "gpeak_macs2_filt", "macs2/overlap" )
}
}
else {
peak_overlap = _naive_overlap_peak( "narrowPeak", filt_peak_pooled, map_to_array( filt_peak ), \
filt_peak_ppr1, filt_peak_ppr2, overlap_o_dir, \
"peak", "peak_macs2_filt", "macs2/overlap" )
gpeak_overlap = _naive_overlap_peak( "gappedPeak", filt_gpeak_pooled, map_to_array( filt_gpeak ), \
filt_gpeak_ppr1, filt_gpeak_ppr2, overlap_o_dir, \
"gpeak", "gpeak_macs2_filt", "macs2/overlap" )
}
// do IDR
idr_o_dir := mkdir( "$out_dir/peak/idr" )
for ( int i=1; i<=get_num_rep(); i++ ) {
for ( int j=i+1; j<=get_num_rep(); j++ ) {
idr_true_o_dir := mkdir( "$idr_o_dir/true_reps/rep$i-rep$j" )
(idr_tr{"$i,$j"}, idr_tr_png{"$i,$j"} ) = _idr2( filt_peak{i}, filt_peak{j}, filt_peak_pooled, idr_thresh, "p.value", idr_true_o_dir, "rep$i-rep$j", \
"peak_macs2_filt", ["rep$i","rep$j","pooled"], "true_reps/rep$i-rep$j" )
}
if ( !true_rep ) {
idr_pr_o_dir := mkdir( "$idr_o_dir/pseudo_reps/rep$i" )
(idr_pr{i}, idr_pr_png{i}) = _idr2( filt_peak_pr1{i}, filt_peak_pr2{i}, filt_peak{i}, idr_thresh, "p.value", idr_pr_o_dir, "rep$i-pr", \
"peak_macs2_filt", ["rep$i-pr1","rep$i-pr2","rep$i"], "pseudo_reps/rep$i" )
}
}
if ( !true_rep && get_num_rep() > 1 ) {
idr_ppr_o_dir := mkdir( "$idr_o_dir/pooled_pseudo_reps" )
(idr_ppr, idr_ppr_png) = _idr2( filt_peak_ppr1, filt_peak_ppr2, filt_peak_pooled, idr_thresh, "p.value", idr_ppr_o_dir, "ppr", \
"peak_macs2_filt", ["ppr1","ppr2","pooled"], "pooled_pseudo_reps" )
}
wait_clear_tids()
qc_o_dir := mkdir( "$out_dir/qc" ) // create qc output dir.
// 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, idr_ppr, idr_o_dir, qc_o_dir, "" )
wait_clear_tids()
print( "\n== Done do_idr()\n" )
}
void ataqc() {
if ( no_ataqc || align ) return
for (int rep=1; rep<=get_num_rep(); rep++) {
par _ataqc( rep )
}
wait_clear_tids()
print( "\n== Done ataqc()\n" )
}
void _ataqc( int rep ) {
info := "rep$rep"
qc_o_dir := mkdir( "$out_dir/qc/$info" )
aln_o_dir := mkdir( "$out_dir/align/$info" ) // create align output directory
if ( bam.hasKey(rep) ) {
string idr_, graph_in_idr
if ( no_idr ) {
idr_ = ""
graph_in_idr = ""
}
else if ( get_num_rep() == 1 ) {
idr_ = idr_pr{1}
graph_in_idr = "idr_peak_(rep1-pr)"
}
else {
idr_ = idr_opt
graph_in_idr = "idr_peak_(opt)"
}
if ( se ) {
_ataqc( fastq{rep}, "", bam{rep}, align_log{rep}, pbc_qc{rep}, \
dup_qc{rep}, filt_bam{rep}, final_tag{rep}, pval_bigwig_001{rep}, peak_001{rep}, \
peak_overlap, idr_, qc_o_dir, info, graph_in_idr )
}
else {
_ataqc( fastq{rep+",1"}, fastq{rep+",2"}, bam{rep}, align_log{rep}, pbc_qc{rep}, \
dup_qc{rep}, filt_bam{rep}, final_tag{rep}, pval_bigwig_001{rep}, peak_001{rep}, \
peak_overlap, idr_, qc_o_dir, info, graph_in_idr )
}
}
}
string[] _ataqc( string fastq1, string fastq2, string bam, string align_log, string pbc_log, \
string dup_log, string filt_bam, string bed, string bigwig, string peak, \
string peak_naive_overlap, string idr_peak, \
string o_dir, string info, string graph_in_idr ) {
prefix := replace_dir( rm_ext( fastq1, ["fastq","fq"] ), o_dir ) + ( (fastq2!="") ? ".PE2SE" : "" )
html := "$prefix"+"_qc.html"
txt := "$prefix"+"_qc.txt"
prefix_basename := get_basename( prefix )
param_fastq := (fastq2!="") ? "--fastq1 $fastq1 --fastq2 $fastq2" : "--fastq1 $fastq1"
param_overlap := (peak_naive_overlap!="") ? "--naive_overlap_peaks $peak_naive_overlap" : ""
param_idr := (idr_peak!="") ? "--idr_peaks $idr_peak" : ""
in := (fastq2!="") ? [ fastq1, fastq2, bam, align_log, pbc_log, dup_log, filt_bam, bed, bigwig, peak ] \
: [ fastq1, bam, align_log, pbc_log, dup_log, filt_bam, bed, bigwig, peak ]
out := [ html, txt ] //, gc_plot, hist_graph, lg_vplot, vplot, signal ]
taskName:= "ataqc "+info
wait_par( cpus )
tid := task( out<-in ) {
sys $shcmd_init
// To prevent java heap space error (Exception in thread "main" java.lang.OutOfMemoryError: Java heap space)
// increae -Xmx if you still have it
sys export _JAVA_OPTIONS="-Xms256M -Xmx4096M -XX:ParallelGCThreads=1"
sys cd $o_dir
sys $script_dir/ataqc/run_ataqc.py \
--workdir $o_dir \
--outdir $o_dir \
--outprefix $prefix_basename \
--genome $species \
--ref $ref_fa \
--tss $tss_enrich \
--dnase $dnase \
--blacklist $blacklist \
--prom $prom \
--enh $enh \
--reg2map $reg2map \
--meta $roadmap_meta \
--pbc $pbc_log\
$param_fastq \
--alignedbam $bam \
--alignmentlog $align_log \
--coordsortbam $bam \
--duplog $dup_log \
--finalbam $filt_bam \
--finalbed $bed \
--bigwig $bigwig \
--peaks $peak \
$param_overlap \
$param_idr
}
register_par( tid, cpus )
path_in := ( fastq2!="" ) ? [fastq1, fastq2, bam, filt_bam, bed, bigwig, peak, peak_naive_overlap, idr_peak] \
: [fastq1, bam, filt_bam, bed, bigwig, peak, peak_naive_overlap, idr_peak]
graph_in := ( fastq2!="" ) ? ["fastq1_($info)","fastq2_($info)","bam_($info)","filt_bam_($info)",\
"tagalign_($info)","p-val_sig._0.01_($info)","peak_macs2_0.01_($info)",\
"peak_macs2_filt_(overlap)", graph_in_idr] : \
["fastq_($info)","bam_($info)","filt_bam_($info)",\
"tagalign_($info)","p-val_sig._0.01_($info)","peak_macs2_0.01_($info)", \
"peak_macs2_filt_(overlap)", graph_in_idr]
graph_out := ["ataqc rpt_($info)"]
_add_to_graphviz( graph_in, path_in, graph_out, out, "ataqc_($info)", grp_color_ataqc )
_add_to_filetable( ["L1_qc/$info/ataqc"], out )
return out
}
void report() {
wait_clear_tids()
html := _html_filetable() // treeview for directory and file structure
html += _html_atac_tracks() // epigenome browser tracks
html += _html_graphviz() // graphviz workflow diagram
html += _html_atac_QC() // show QC tables and images
report( html )
print( "\n== Done report()\n" )
}
string _html_atac_QC() {
string[] align_qcs, dup_qcs, flagstat_nodup_qcs, pbc_qcs, xcor_qcs, xcor_plots
string[] align_headers, dup_headers, flagstat_nodup_headers, pbc_headers, xcor_headers
for ( int rep=1; rep <= get_num_rep(); rep++) {
info := "rep$rep"
key := "$rep"
//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 ( align_log.hasKey( key ) ) align_qcs += align_log{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 ( align_log.hasKey( key ) ) align_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='atac_qc'>"
html += _parse_align_log_to_html( "all", align_headers, align_qcs, align_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 )