-
Notifications
You must be signed in to change notification settings - Fork 79
/
atac.bds
executable file
·1988 lines (1695 loc) · 73.4 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
type := "atac-seq" help Type of the pipeline. atac-seq or dnase-seq (default: atac-seq).
dnase_seq := false help DNase-Seq (no tn5 shifting).
align := false help Align only (no MACS2 peak calling or IDR or ataqc analysis).
subsample_xcor := "25M" help # reads to subsample for cross corr. analysis (default: 25M).
subsample := "0" help # reads to subsample exp. replicates. Subsampled tagalign will be used for steps downstream (default: 0; no subsampling).
true_rep := false help No pseudo-replicates.
no_ataqc := false help No ATAQC
no_xcor := false help No Cross-correlation analysis.
csem := false help Use CSEM for alignment.
smooth_win := "150" help Smoothing window size for MACS2 peak calling (default: 150).
idr_thresh := 0.1 help IDR threshold : -log_10(score) (default: 0.1).
ENCODE3 := false help Force to use parameter set (-smooth_win 73 -idr_thresh 0.05 -multimapping 4) for ENCODE3.
ENCODE := false help Force to use parameter set (-smooth_win 73 -idr_thresh 0.05 -multimapping 4) for ENCODE.
no_browser_tracks := false help Disable generation of genome browser tracks (workaround for bzip2 shared library issue).
overlap_pval_thresh := 0.01 help p-val threshold for overlapped peaks (default: 0.01).
macs2_pval_thresh := 0.01 help MACS2 p-val threshold for calling peaks (default: 0.1).
macs2_pval_thresh_bw := 0.01 help MACS2 p-val threshold for generating BIGWIG signal tracks (default: 0.1).
enable_idr := false help Enable IDR on called peaks.
auto_detect_adapter := false help Automatically find and trim adapters.
adjust_bedpe := false // help (BETA) adjust BEDPE (by Daniel Kim).
exclude_ppr_in_naive_overlap := false //help Exclude peaks from pooled pseudo replicates in naive overlap. This flag works for multiple replicate case only.
disable_gb_peak := true // disable gapped/broad peak generation
help() // show help contexts
include "modules/pipeline_template.bds"
include "modules/input.bds"
include "modules/input_adapter.bds"
include "modules/align_bowtie2.bds"
include "modules/align_trim_adapter.bds"
include "modules/align_etc.bds"
include "modules/postalign_bam.bds"
include "modules/postalign_bed.bds"
include "modules/postalign_xcor.bds"
include "modules/callpeak_macs2_atac.bds"
include "modules/callpeak_naive_overlap.bds"
include "modules/callpeak_idr.bds"
include "modules/callpeak_blacklist_filter.bds"
include "modules/callpeak_bigbed.bds"
include "modules/ataqc.bds"
include "modules/ENCODE_accession.bds"
// Important output file names are stored in global variables (usually a string map string{} with a key with replicate id, pair id)
// 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, flagstat_qc, bam, filt_bam, dup_qc, flagstat_nodup_qc, pbc_qc, xcor_qc, xcor_plot
string{} final_tag, final_tag_pr1, final_tag_pr2
string final_tag_pooled, final_tag_ppr1, final_tag_ppr2
string{} peak, peak_001, peak_pr1, peak_pr2
string peak_pooled, peak_pooled_001, peak_ppr1, peak_ppr2
string{} gpeak, gpeak_001, gpeak_pr1, gpeak_pr2
string gpeak_pooled, gpeak_pooled_001, gpeak_ppr1, gpeak_ppr2
string num_peak_log
string{} pval_bigwig_001, fc_bigwig_001
string{} idr_tr, idr_pr, idr_tr_png, idr_pr_png, idr_tr_log, idr_pr_log
string idr_ppr, idr_opt, idr_consv, idr_ppr_png, idr_ppr_log
string idr_qc
string{} idr_qc_FRiP
string{} peak_overlap_tr, peak_overlap_pr
string peak_overlap_ppr, peak_overlap_opt, peak_overlap_consv
string peak_overlap_qc
string{} peak_overlap_qc_FRiP
string{} ataqc_qc
main()
void main() { // atac pipeline starts here
init_atac()
chk_param() // check if parameters are valid
chk_adapters()
chk_input( true_rep, false )
do_align()
call_peaks()
read_input_peak() // if inputs are peaks, read them
do_naive_overlap()
do_idr()
wait
log_number_of_peaks()
ataqc()
// blacklist-filter peaks and also make ENCODE accession metadata for them
filter_peak_and_convert_to_bigbed()
tar_all_logs()
ENCODE3_overall_qc()
report()
}
void init_atac() {
read_conf_atac()
init_etc_atac()
print_atac()
init_filetable()
}
void read_conf_atac() {
type = get_conf_val( type, ["type"] )
align = get_conf_val_bool( align, ["align"] )
true_rep = get_conf_val_bool( true_rep, ["true_rep"] )
no_ataqc = get_conf_val_bool( no_ataqc, ["no_ataqc"] )
no_xcor = get_conf_val_bool( no_xcor, ["no_xcor"] )
csem = get_conf_val_bool( csem, ["csem"] )
smooth_win = get_conf_val( smooth_win, ["smooth_win"] )
dnase_seq = get_conf_val_bool( dnase_seq, ["dnase_seq"] )
idr_thresh = get_conf_val_real( idr_thresh, ["idr_thresh"] )
subsample_xcor = get_conf_val( subsample_xcor, ["subsample_xcor"] )
subsample = get_conf_val( subsample, ["subsample"] )
ENCODE3 = get_conf_val_bool( ENCODE3, ["ENCODE3"] )
ENCODE = get_conf_val_bool( ENCODE, ["ENCODE","ENCODE1"] )
no_browser_tracks = get_conf_val_bool( no_browser_tracks, ["no_browser_tracks"] )
overlap_pval_thresh = get_conf_val_real( overlap_pval_thresh, ["overlap_pval_thresh"] )
// exclude_ppr_in_naive_overlap = get_conf_val_bool( exclude_ppr_in_naive_overlap, ["exclude_ppr_in_naive_overlap"] )
macs2_pval_thresh = get_conf_val_real( macs2_pval_thresh, ["macs2_pval_thresh"] )
macs2_pval_thresh_bw = get_conf_val_real( macs2_pval_thresh_bw, ["macs2_pval_thresh_bw"] )
enable_idr = get_conf_val_bool( enable_idr, ["enable_idr"] )
auto_detect_adapter = get_conf_val_bool( auto_detect_adapter, ["auto_detect_adapter"] )
// adjust_bedpe = get_conf_val_bool( adjust_bedpe, ["adjust_bedpe"] )
}
void init_etc_atac() {
default_is_pe = true
speak_xcor = 0 // set fragment length explicitly as zero for cross corr. analysis
}
void print_atac() {
print( "\n\n== atac pipeline settings\n")
print( "Type of pipeline\t\t\t: $type\n")
print( "Align only\t\t\t\t: " + align + "\n")
print( "# reads to subsample replicates (0 if no subsampling)\t: "+parse_number( subsample )+"\n")
print( "# reads to subsample for cross-corr. analysis \t: " +parse_number( subsample_xcor)+"\n")
print( "No pseudo replicates\t\t\t: $true_rep\n")
print( "No ATAQC (advanced QC report)\t\t: $no_ataqc\n")
print( "No Cross-corr. analysis\t\t\t: $no_xcor\n")
print( "Use CSEM for alignment\t\t\t: $csem\n")
print( "Smoothing window for MACS2\t\t: $smooth_win\n")
print( "DNase Seq\t\t\t\t: $dnase_seq\n")
print( "IDR threshold\t\t\t\t: $idr_thresh\n" )
print( "Force to use ENCODE3 parameter set\t: $ENCODE3\n" )
print( "Force to use ENCODE parameter set\t: $ENCODE\n" )
print( "Disable genome browser tracks\t: $no_browser_tracks\n" )
print( "p-val thresh. for overlapped peaks\t: $overlap_pval_thresh\n")
// print( "Exclude ppr in naive overlap\t: $exclude_ppr_in_naive_overlap\n")
print( "MACS2 p-val thresh. for peaks\t: $macs2_pval_thresh\n")
print( "MACS2 p-val thresh. for BIGWIGs\t\t: $macs2_pval_thresh_bw\n")
print( "Enable IDR on called peaks\t: $enable_idr\n")
print( "Automatically find/trim adapters\t: $auto_detect_adapter\n")
// print( "(BETA) adjust BEDPE (by Daniel Kim)\t: $adjust_bedpe\n")
if ( dnase_seq ) type = "dnase-seq"
}
void init_filetable() { // init file table labels in HTML report
// add label to graphviz
// : Items in filetable will be sorted in the ascending order of rank
// : Items added later will have higher rank
// Level 1
add_label_to_table("Raw reads")
add_label_to_table("Alignment")
add_label_to_table("Signal tracks")
add_label_to_table("Peaks")
add_label_to_table("QC and logs")
// Level 2
for (int i=1; i<=100; i++) \
add_label_to_table("Replicate $i")
add_label_to_table("True replicates")
add_label_to_table("Pooled replicate")
add_label_to_table("Pseudo-replicates")
add_label_to_table("Pooled pseudo-replicate")
add_label_to_table("Pooled pseudo-replicates")
add_label_to_table("Optimal set")
add_label_to_table("Conservative set")
add_label_to_table("Naive overlap")
add_label_to_table("MACS2")
add_label_to_table("IDR")
// Level 2 or 3
add_label_to_table("Pseudo-replicate 1")
add_label_to_table("Pseudo-replicate 2")
add_label_to_table("Pooled pseudo-replicate 1")
add_label_to_table("Pooled pseudo-replicate 2")
for (int i=1; i<=20; i++) \
for (int j=i+1; j<=20; j++) \
add_label_to_table("Rep. $i vs Rep. $j")
// Higher levels
add_label_to_table("IDR QC")
add_label_to_table("Fastq")
add_label_to_table("Fastq 1")
add_label_to_table("Fastq 2")
add_label_to_table("Trimmed fastq")
add_label_to_table("Trimmed fastq 1")
add_label_to_table("Trimmed fastq 2")
add_label_to_table("Bowtie2 map. log")
add_label_to_table("Bam")
add_label_to_table("Filtered bam")
add_label_to_table("Sorted bam")
add_label_to_table("Dedup. log")
add_label_to_table("Bowtie2 map. flagstat log")
add_label_to_table("PBC log")
add_label_to_table("Bedpe")
add_label_to_table("Subsampled bedpe")
add_label_to_table("Tag-align")
add_label_to_table("Subsampled tag-align")
add_label_to_table("Cross-corr. log")
add_label_to_table("Cross-corr. plot")
add_label_to_table("P-value")
add_label_to_table("Fold enrichment")
add_label_to_table("Narrow peak")
add_label_to_table("Gapped peak")
add_label_to_table("Filtered narrow peak")
add_label_to_table("Filtered gapped peak")
add_label_to_table("IDR peak")
add_label_to_table("Peak")
add_label_to_table("Filtered peak")
add_label_to_table("Filtered gapped peak")
add_label_to_table("ATAQC")
add_label_to_table("IDR plot")
add_label_to_table("Unthresholded IDR peak")
// add label to graphviz (short name, long name)
for (int i=1; i<=50; i++) {
add_label_to_graph("rep$i", "Replicate $i")
add_label_to_graph("rep$i-pr1", "Pseudo-replicate 1 for rep. $i")
add_label_to_graph("rep$i-pr2", "Pseudo-replicate 2 for rep. $i")
add_label_to_graph("rep$i-pr", "Pseudo replicates for rep. $i")
for (int j=1; j<=20; j++) {
add_label_to_graph("rep$i-rep$j", "Rep. $i vs. Rep. $j")
}
}
add_label_to_graph("pooled_rep", "Pooled replicate")
add_label_to_graph("ppr", "Pooled pseudo-replicates")
add_label_to_graph("ppr1", "Pooled pseudo-replicate 1")
add_label_to_graph("ppr2", "Pooled pseudo-replicate 2")
}
void chk_param() {
print( "\n== checking atac parameters ...\n" );
if ( has_input_fastq() ) chk_align_bwt2()
if ( !align ) chk_callpeak_macs2()
if ( enable_idr ) chk_idr()
if ( !no_ataqc ) {
no_ataqc = !chk_ataqc()
}
if ( has_pe_input_tag() && subsample > 0 ) {
print("Warning: Cannot subsample paired end tagaligns. Disabling subsampling...\n")
subsample = 0
}
if ( !has_input_fastq() && !no_ataqc ) {
print("Warning: ATAQC is available for fastq inputs only. Disabling ATAQC...\n")
no_ataqc = true
}
if ( has_pe_input_fastq() && csem ) {
error("CSEM (-csem) is not available for paired end fastqs!\n")
}
if ( ENCODE ) {
print("Info: ENCODE flag is on (-smooth_win 73 -idr_thresh 0.05 -multimapping 4).\n")
smooth_win = 73
idr_thresh = 0.05
multimapping = 4
}
if ( ENCODE3 ) {
print("Info: ENCODE3 flag is on (-smooth_win 73 -idr_thresh 0.05 -multimapping 4).\n")
smooth_win = 73
idr_thresh = 0.05
multimapping = 4
}
//ENCODE_assay_category = "DNA accessibility"
if ( dnase_seq ) {
ENCODE_assay_title = "DNase-seq"
}
else {
ENCODE_assay_title = "ATAC-seq"
}
}
void chk_adapters() {
print( "\n== checking adapters to be trimmed ...\n" );
// check adapters
for ( int rep=1; rep <= get_num_rep(); rep++) {
if ( is_input_fastq( rep ) ) {
fastqs_R1 := get_fastq( 0, rep, 1 )
adapters_R1 := get_adapter( 0, rep, 1 )
if ( is_se( rep ) ) { //SE
print("Rep$rep adapters (SE) : \n")
for ( string pool_id : fastqs_R1.keys() ) {
if ( !adapters_R1.hasKey(pool_id) ) {
if ( auto_detect_adapter ) {
print("\t$pool_id: automatically detected and trimmed.\n")
}
else {
print("\t$pool_id: no adapter specified.\n")
}
}
else {
adapter := adapters_R1{pool_id}
print("\t$pool_id: $adapter\n")
}
}
}
else { //PE
print("Rep$rep R1 adapters (PE) : \n")
for ( string pool_id : fastqs_R1.keys() ) {
if ( !adapters_R1.hasKey(pool_id) ) {
if ( auto_detect_adapter ) {
print("\t$pool_id: automatically detected and trimmed.\n")
}
else {
print("\t$pool_id: no adapter specified.\n")
}
}
else {
adapter := adapters_R1{pool_id}
print("\t$pool_id: $adapter\n")
}
}
fastqs_R2 := get_fastq( 0, rep, 2 )
adapters_R2 := get_adapter( 0, rep, 2 )
print("Rep$rep R2 adapters (PE) : \n")
for ( string pool_id : fastqs_R2.keys() ) {
if ( !adapters_R2.hasKey(pool_id) ) {
if ( auto_detect_adapter ) {
print("\t$pool_id: automatically detected and trimmed.\n")
}
else {
print("\t$pool_id: no adapter specified.\n")
}
}
else {
adapter := adapters_R2{pool_id}
print("\t$pool_id: $adapter\n")
}
}
if ( !equals( adapters_R1.keys(), adapters_R2.keys() ) ) {
error("Match pooling IDs (-fastq[REP_ID]_[PAIR_ID]:[POOL_ID]) in both pairs.\n")
}
}
//if ( !is_se( rep ) && fastqs.size() < 2 ) \
// error("A pair of fastqs are needed for replicate $rep (if it's single-ended add '-se')\n")
}
}
}
void do_align() {
if ( is_input_peak() ) return
// filesize of input ( map with key $rep )
int{} filesize
for ( int rep=1; rep <= get_num_rep(); rep++) {
// check file size to distribute nth to each nth_app
// determine # threads for each app related to alignment
// get file size in bytes
if ( is_input_fastq( rep ) ) {
fastqs := get_fastqs( rep )
filesize{rep} = (fastqs[0]).size()
if ( fastqs.size() > 1) filesize{rep} += (fastqs[1]).size()*3 // multiply 3 to allocate more cpus for align
}
else if ( is_input_bam( rep ) ) filesize{rep} = (get_bam( 0, rep )).size()
else if ( is_input_filt_bam( rep ) ) filesize{rep} = (get_filt_bam( 0, rep )).size()
else if ( is_input_tag( rep ) ) filesize{rep} = (get_tag( 0, rep )).size()*10
}
//// distribute # threads for each replicate
nth_rep := distribute_nonzero( nth, filesize ) // distribute # threads according to input filesize
for (int rep=1; rep<=get_num_rep(); rep++) {
if ( no_par ) do_align( rep, nth_rep{rep} )
else par do_align( rep, nth_rep{rep} )
}
wait
print( "\n== Done do_align()\n" )
}
void do_align( int rep, int nth_rep ) {
if ( is_se( rep ) ) align_SE( rep, nth_rep )
else align_PE( rep, nth_rep )
}
void align_SE( int rep, int nth_rep ) {
group := get_group_name( rep )
long := get_long_group_name( rep )
aln_o_dir := mkdir( "$out_dir/align/$group" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$group" ) // create qc output dir.
string bam_, read_length_log, flagstat_qc_
string{} fastqs, trimmed_fastqs, adapters // format: key=pool_id, val=fastq_path or adapter
string[] fastqs_arr
if ( is_input_fastq( rep ) ) {
fastqs = get_fastq( 0, rep, 1 )
adapters = get_adapter( 0, rep, 1 )
fastqs_arr = map_to_array( fastqs )
// string[] trimmed_fastqs
for ( string pool_id : fastqs.keys() ) {
// if ( adapters.size()==0 && !auto_detect_adapter ) {
if ( !adapters.hasKey(pool_id) && !auto_detect_adapter ) {
trimmed_fastqs{pool_id} = fastqs{pool_id}
print("\nSpecified adapter for $group:$pool_id (SE) : None\n")
}
else {
string adapter
if ( !adapters.hasKey(pool_id) && auto_detect_adapter ) {
string adapter_log, tid
(adapter_log, tid) = detect_adapter( fastqs{pool_id}, qc_o_dir, group )
wait tid
adapter = parse_adapter_log( adapter_log )
if ( adapter ) {
print("\nDetected adapter for $group:$pool_id (SE) : $adapter\n")
}
else {
print("\nDetected adapter for $group:$pool_id (SE) : No adapter detected. Skip trimming.\n")
}
}
else {
adapter = adapters{pool_id}
print("\nSpecified adapter for $group:$pool_id (SE) : $adapter\n")
}
if ( adapter ) {
suffix := (pool_id == "00" ? "" : ":$pool_id")
trimmed_fastqs{pool_id} = \
trim_adapters( fastqs{pool_id}, adapter, aln_o_dir, group, suffix )
}
else {
trimmed_fastqs{pool_id} = fastqs{pool_id}
}
}
}
wait
string p1
trimmed_fastqs_arr := map_to_array(trimmed_fastqs)
if ( trimmed_fastqs.size() > 1 ) { // if multiple fastqs are given, pool trimmed fastqs
p1 = pool_fastq( trimmed_fastqs_arr, aln_o_dir, group )
// add_file_to_report( p1, "pooled\\nfastq" , group, "Raw reads/$long/Pooled fastq" )
wait
}
else {
p1 = trimmed_fastqs_arr[0]
}
fastq{rep} = p1
string align_log_
read_length_log = get_read_length_log( p1, qc_o_dir, group )
if ( csem ) {
( bam_, align_log_ ) = bowtie2_csem( p1, aln_o_dir, qc_o_dir, group, nth_rep )
}
else {
( bam_, align_log_ ) = bowtie2( p1, aln_o_dir, qc_o_dir, group, nth_rep )
}
wait
align_log{rep} = align_log_
add_file_to_table( align_log_, "QC and logs/$long/Bowtie2 map. log")
flagstat_qc_ = samtools_flagstat_bam( bam_, qc_o_dir, group )
wait
flagstat_qc{rep} = flagstat_qc_
add_file_to_table( flagstat_qc_, "QC and logs/$long/Bowtie2 map. flagstat log")
// add to report
tmp_log := parse_flagstat( flagstat_qc_ )
raw_reads := metric_prefix( parse_int( tmp_log{"total"} ) )
mapped_reads := metric_prefix( parse_int( tmp_log{"mapped"} ) )
if ( trimmed_fastqs_arr.size() > 1 ) { // if multiple fastqs are given, pool trimmed fastqs
for ( int i=0; i<fastqs_arr.size(); i++) {
suffix := fastqs_arr.size()==1 ? "" : ":$i"
add_file_to_report( fastqs_arr[i], "fastq$suffix", group, \
"Raw reads/$long/Fastq$suffix" )
if ( trimmed_fastqs_arr[i]!=fastqs_arr[i] ) {
add_file_to_report( trimmed_fastqs_arr[i], "trimmed\\nfastq$suffix", group, \
"Raw reads/$long/Trimmed fastq$suffix" )
}
}
add_file_to_report( p1, "pooled\\nfastq" + (raw_reads ? "\\n$raw_reads" : ""), group, \
"Raw reads/$long/Pooled fastq"+ (raw_reads ? " ($raw_reads)" : "") )
}
else {
for ( int i=0; i<fastqs_arr.size(); i++) {
if ( trimmed_fastqs_arr[i]==fastqs_arr[i] ) {
add_file_to_report( fastqs_arr[i], "fastq" + (raw_reads ? "\\n$raw_reads" : ""), group, \
"Raw reads/$long/Fastq"+ (raw_reads ? " ($raw_reads)" : "") )
}
else {
add_file_to_report( fastqs_arr[i], "fastq", group, \
"Raw reads/$long/Fastq" )
add_file_to_report( trimmed_fastqs_arr[i], "trimmed\\nfastq" + (raw_reads ? "\\n$raw_reads" : ""), group, \
"Raw reads/$long/Trimmed fastq"+ (raw_reads ? " ($raw_reads)" : "") )
}
}
}
bam{rep} = bam_
add_file_to_report( bam_, "bam" + (mapped_reads ? "\\n$mapped_reads" : ""), group, \
"Alignment/$long/Bam" + (mapped_reads ? " ($mapped_reads)" : "") )
}
string filt_bam_, dup_qc_, pbc_qc_, flagstat_nodup_qc_
if ( is_input_fastq( rep ) || is_input_bam( rep ) ) {
if ( is_input_bam( rep ) ) {
bam_ = get_bam( 0, rep )
bam{rep} = bam_
}
string deduped_reads
if ( no_dup_removal ) {
string tmp
( filt_bam_, tmp ) = dedup_bam( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
wait
}
else {
( filt_bam_, dup_qc_, flagstat_nodup_qc_, pbc_qc_ ) \
= dedup_bam( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
dup_qc{rep} = dup_qc_
pbc_qc{rep} = pbc_qc_
flagstat_nodup_qc{rep} = flagstat_nodup_qc_
add_file_to_table( dup_qc_, "QC and logs/$long/Dedup. log")
add_file_to_table( pbc_qc_, "QC and logs/$long/PBC log")
add_file_to_table( flagstat_nodup_qc_, "QC and logs/$long/Filtered flagstat log")
wait
tmp_log := parse_flagstat( flagstat_nodup_qc_ )
deduped_reads = metric_prefix( parse_int( tmp_log{"total"} ) )
}
// add to report
filt_bam{rep} = filt_bam_
add_file_to_report( filt_bam_, "filt. bam" + (deduped_reads ? "\\n$deduped_reads" : ""), group, \
"Alignment/$long/Filtered & deduped bam" + (deduped_reads ? " ($deduped_reads)" : "") )
// For ENCODE accession, use different step name for single rep case
if ( is_input_fastq( rep ) ) {
string ENCODE_step_name
if ( get_num_rep() == 1 ) ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-single-rep-v1"
else ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-v1"
if ( fastqs.size() > 0 ) \
add_ENCODE_metadata_to_summary_json( "bam", "", "alignments", \
ENCODE_step_name, filt_bam_, fastqs_arr )
if ( flagstat_qc_ ) {
add_ENCODE_quality_metrics_to_summary_json( "samtools_flagstats_quality_metric", \
ENCODE_step_name, [filt_bam_], [flagstat_qc_] )
}
}
}
string tag
if ( is_input_fastq( rep ) || is_input_bam( rep ) || is_input_filt_bam( rep ) ) {
if ( is_input_filt_bam( rep ) ) {
filt_bam_ = get_filt_bam( 0, rep )
filt_bam{rep} = filt_bam_
}
tag = bam_to_tag( filt_bam_, aln_o_dir, group )
wait
}
string final_tag_, final_tag_pr1_, final_tag_pr2_
if ( is_input_fastq( rep ) || is_input_bam( rep ) || is_input_filt_bam( rep ) || is_input_tag( rep ) ) {
if ( is_input_tag( rep ) ) tag = get_tag( 0, rep )
string subsampled_tag
if ( parse_number( subsample ) != 0 ) {
subsampled_tag = subsample_tag( tag, parse_number( subsample ), false, aln_o_dir, group )
wait
}
else {
subsampled_tag = tag
}
if ( is_dnase_seq() ) {
final_tag_ = subsampled_tag
}
else {
final_tag_ = tn5_shift_tag( subsampled_tag, aln_o_dir, group )
}
final_tag{rep} = final_tag_
add_file_to_report( final_tag_, "tag-align", group, "Alignment/$long/Tag-align" )
wait
if ( !true_rep ) {
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr2" )
( final_tag_pr1_, final_tag_pr2_ ) = spr( final_tag_, aln_pr1_o_dir, aln_pr2_o_dir, group )
final_tag_pr1{rep} = final_tag_pr1_
final_tag_pr2{rep} = final_tag_pr2_
add_file_to_report( final_tag_pr1_, "tag-align", "$group-pr1", "Alignment/Pseudo-replicates/$long/Pseudo-replicate 1/Tag-align" )
add_file_to_report( final_tag_pr2_, "tag-align", "$group-pr2", "Alignment/Pseudo-replicates/$long/Pseudo-replicate 2/Tag-align" )
wait
}
if ( !no_xcor ) {
// cross-corr. analysis
subsampled_tag_xcor := subsample_tag( final_tag_, parse_number( subsample_xcor ), true, aln_o_dir, group )
wait
// xcor
string xcor_qc_, xcor_plot_
( xcor_qc_, xcor_plot_ ) = xcor( subsampled_tag_xcor, qc_o_dir, group, nth_rep )
xcor_qc{rep} = xcor_qc_
xcor_plot{rep} = xcor_plot_
add_file_to_report( final_tag_, "tag-align", group, "Alignment/$long/Tag-align" )
add_file_to_table( xcor_plot_, "QC and logs/$long/Cross-corr. plot" )
wait
string ENCODE_step_name
if ( pbc_qc_ && read_length_log ) {
if ( get_num_rep() == 1 ) \
ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-single-rep-v1"
else \
ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-v1"
add_ENCODE_quality_metrics_to_summary_json( "complexity_xcorr_quality_metric", \
ENCODE_step_name, \
[filt_bam_], [pbc_qc_, xcor_qc_, read_length_log], [ "false", xcor_plot_] )
}
}
}
}
void align_PE( int rep, int nth_rep ) {
group := get_group_name( rep )
long := get_long_group_name( rep )
aln_o_dir := mkdir( "$out_dir/align/$group" ) // create align output directory
qc_o_dir := mkdir( "$out_dir/qc/$group" ) // create qc output dir.
string bam_, align_log_, read_length_log, flagstat_qc_
string{} fastqs_pair1, fastqs_pair2, adapters_pair1, adapters_pair2
string{} trimmed_fastqs_pair1, trimmed_fastqs_pair2
string[] fastqs_pair1_arr, fastqs_pair2_arr
if ( is_input_fastq( rep ) ) {
fastqs_pair1 = get_fastq( 0, rep, 1 )
fastqs_pair2 = get_fastq( 0, rep, 2 )
fastqs_pair1_arr = map_to_array(fastqs_pair1)
fastqs_pair2_arr = map_to_array(fastqs_pair2)
adapters_pair1 = get_adapter( 0, rep, 1 )
adapters_pair2 = get_adapter( 0, rep, 2 )
string{} trimmed_fastqs_pair1, trimmed_fastqs_pair2
if ( fastqs_pair1.size() != fastqs_pair2.size() ) {
error("Number of fastqs to be pooled for pair 1 and pair 2 do not match!\n")
}
// for ( int i=0; i<fastqs_pair1.size(); i++) {
for ( string pool_id : fastqs_pair1.keys() ) {
if ( !fastqs_pair2.hasKey(pool_id) ) error("Paired fastq not found for "+fastqs_pair1{pool_id}+"\n")
if ( !(adapters_pair1.hasKey(pool_id) && adapters_pair2.hasKey(pool_id)) \
&& !auto_detect_adapter ) {
trimmed_fastqs_pair1{pool_id} = fastqs_pair1{pool_id}
trimmed_fastqs_pair2{pool_id} = fastqs_pair2{pool_id}
print("\nSpecified adapter for $group:$pool_id (PE) : None\n")
}
else {
string adapter1, adapter2
if ( !(adapters_pair1.hasKey(pool_id) && adapters_pair2.hasKey(pool_id)) \
&& auto_detect_adapter ) {
string adapter_log1, adapter_log2, tid1, tid2
(adapter_log1, tid1) = detect_adapter( fastqs_pair1{pool_id}, qc_o_dir, group )
(adapter_log2, tid2) = detect_adapter( fastqs_pair2{pool_id}, qc_o_dir, group )
wait [tid1, tid2]
adapter1 = parse_adapter_log( adapter_log1 )
adapter2 = parse_adapter_log( adapter_log2 )
if ( adapter1 && adapter2 ) {
print("\nDetected adapter for $group:$pool_id (PE) : $adapter1, $adapter2\n")
}
else {
print("\nDetected adapter for $group:$pool_id (PE) : No adapter detected. Skip trimming (fastq1: $adapter1, fastq2: $adapter2).\n")
}
}
else if ( adapters_pair1.hasKey(pool_id) && adapters_pair2.hasKey(pool_id) ) {
adapter1 = adapters_pair1{pool_id}
adapter2 = adapters_pair2{pool_id}
print("\nSpecified adapter for $group:$pool_id (PE) : $adapter1, $adapter2\n")
}
if ( adapter1 && adapter2 ) {
suffix := fastqs_pair1.size()==1 ? "" : ":$pool_id"
( trimmed_fastqs_pair1{pool_id}, trimmed_fastqs_pair2{pool_id} ) \
= trim_adapters_PE( fastqs_pair1{pool_id}, fastqs_pair2{pool_id}, \
adapter1, adapter2, aln_o_dir, group, suffix )
// add_file_to_report( p1, "trimmed\\nfastq 1$suffix", group, "Raw reads/$long/Trimmed fastq 1$suffix" )
// add_file_to_report( p2, "trimmed\\nfastq 2$suffix", group, "Raw reads/$long/Trimmed fastq 2$suffix" )
}
else {
trimmed_fastqs_pair1{pool_id} = fastqs_pair1{pool_id}
trimmed_fastqs_pair2{pool_id} = fastqs_pair2{pool_id}
}
}
}
wait
string p1, p2
trimmed_fastqs_pair1_arr := map_to_array(trimmed_fastqs_pair1)
trimmed_fastqs_pair2_arr := map_to_array(trimmed_fastqs_pair2)
if ( trimmed_fastqs_pair1.size() > 1 ) { // if multiple fastqs are given, pool trimmed fastqs
p1 = pool_fastq( trimmed_fastqs_pair1_arr, aln_o_dir, group )
p2 = pool_fastq( trimmed_fastqs_pair2_arr, aln_o_dir, group )
// add_file_to_report( p1, "pooled\\nfastq 1" , group, "Raw reads/$long/Pooled fastq 1" )
// add_file_to_report( p2, "pooled\\nfastq 2" , group, "Raw reads/$long/Pooled fastq 2" )
wait
}
else {
p1 = trimmed_fastqs_pair1_arr[0]
p2 = trimmed_fastqs_pair2_arr[0]
}
fastq{rep+",1"} = p1
fastq{rep+",2"} = p2
read_length_log = get_read_length_log( p1, qc_o_dir, group )
( bam_, align_log_ ) = bowtie2_PE( p1, p2, aln_o_dir, qc_o_dir, group, nth_rep )
wait
align_log{rep} = align_log_
add_file_to_table( align_log_, "QC and logs/$long/Bowtie2 map. log")
flagstat_qc_ = samtools_flagstat_bam( bam_, qc_o_dir, group )
wait
flagstat_qc{rep} = flagstat_qc_
add_file_to_table( flagstat_qc_, "QC and logs/$long/Bowtie2 map. flagstat log")
// add to report
tmp_log := parse_flagstat( flagstat_qc_ )
raw_reads := metric_prefix( parse_int( tmp_log{"total"} ) )
half_raw_reads := metric_prefix( parse_int( tmp_log{"total"} )/2 )
if ( trimmed_fastqs_pair1.size() > 1 ) {
for ( string pool_id : fastqs_pair1.keys() ) {
suffix := fastqs_pair1.size()==1 ? "" : ":$pool_id"
add_file_to_report( fastqs_pair1{pool_id}, "fastq 1$suffix", group, \
"Raw reads/$long/Fastq 1$suffix" )
add_file_to_report( fastqs_pair2{pool_id}, "fastq 2$suffix", group, \
"Raw reads/$long/Fastq 2$suffix" )
if ( trimmed_fastqs_pair1{pool_id}!=fastqs_pair1{pool_id} ) {
add_file_to_report( trimmed_fastqs_pair1{pool_id}, "trimmed\\nfastq 1$suffix", group, \
"Raw reads/$long/Trimmed fastq 1$suffix" )
}
if ( trimmed_fastqs_pair2{pool_id}!=fastqs_pair2{pool_id} ) {
add_file_to_report( trimmed_fastqs_pair2{pool_id}, "trimmed\\nfastq 2$suffix", group, \
"Raw reads/$long/Trimmed fastq 2$suffix" )
}
}
add_file_to_report( p1, "pooled\\nfastq 1" + (half_raw_reads ? "\\n$half_raw_reads" : ""), group, \
"Raw reads/$long/Pooled fastq 1"+ (half_raw_reads ? " ($half_raw_reads)" : "") )
add_file_to_report( p2, "pooled\\nfastq 2" + (half_raw_reads ? "\\n$half_raw_reads" : ""), group, \
"Raw reads/$long/Pooled fastq 2"+ (half_raw_reads ? " ($half_raw_reads)" : "") )
}
else {
for ( string pool_id : fastqs_pair1.keys() ) {
if ( trimmed_fastqs_pair1{pool_id}==fastqs_pair1{pool_id} ) {
add_file_to_report( fastqs_pair1{pool_id}, "fastq 1" + (half_raw_reads ? "\\n$half_raw_reads" : ""), group, \
"Raw reads/$long/Fastq 1"+ (half_raw_reads ? " ($half_raw_reads)" : "") )
}
else {
add_file_to_report( fastqs_pair1{pool_id}, "fastq 1", group, \
"Raw reads/$long/Fastq 1" )
add_file_to_report( trimmed_fastqs_pair1{pool_id}, "trimmed\\nfastq 1" + (half_raw_reads ? "\\n$half_raw_reads" : ""), group, \
"Raw reads/$long/Trimmed fastq 1"+ (half_raw_reads ? " ($half_raw_reads)" : "") )
}
if ( trimmed_fastqs_pair2{pool_id}==fastqs_pair2{pool_id} ) {
add_file_to_report( fastqs_pair2{pool_id}, "fastq 2" + (half_raw_reads ? "\\n$half_raw_reads" : ""), group, \
"Raw reads/$long/Fastq 2"+ (half_raw_reads ? " ($half_raw_reads)" : "") )
}
else {
add_file_to_report( fastqs_pair2{pool_id}, "fastq 2", group, \
"Raw reads/$long/Fastq 2" )
add_file_to_report( trimmed_fastqs_pair2{pool_id}, "trimmed\\nfastq 2" + (half_raw_reads ? "\\n$half_raw_reads" : ""), group, \
"Raw reads/$long/Trimmed fastq 2"+ (half_raw_reads ? " ($half_raw_reads)" : "") )
}
}
}
mapped_reads := metric_prefix( parse_int( tmp_log{"mapped"} ) )
bam{rep} = bam_
add_file_to_report( bam_, "bam" + (mapped_reads ? "\\n$mapped_reads" : ""), group, \
"Alignment/$long/Bam" + (mapped_reads ? " ($mapped_reads)" : "") )
}
string filt_bam_, dup_qc_, pbc_qc_, flagstat_nodup_qc_
if ( is_input_fastq( rep ) || is_input_bam( rep ) ) {
if ( is_input_bam( rep ) ) {
bam_ = get_bam( 0, rep )
bam{rep} = bam_
}
string deduped_reads
if ( no_dup_removal ) {
string tmp
(filt_bam_, tmp ) \
= dedup_bam_PE( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
wait
}
else {
(filt_bam_, dup_qc_, flagstat_nodup_qc_, pbc_qc_ ) \
= dedup_bam_PE( bam_, aln_o_dir, qc_o_dir, group, nth_rep )
dup_qc{rep} = dup_qc_
pbc_qc{rep} = pbc_qc_
flagstat_nodup_qc{rep} = flagstat_nodup_qc_
add_file_to_table( dup_qc_, "QC and logs/$long/Dedup. log")
add_file_to_table( pbc_qc_, "QC and logs/$long/PBC log")
add_file_to_table( flagstat_nodup_qc_, "QC and logs/$long/Filtered flagstat log")
wait
tmp_log := parse_flagstat( flagstat_nodup_qc_ )
deduped_reads = metric_prefix( parse_int( tmp_log{"total"} ) )
}
// add to report
filt_bam{rep} = filt_bam_
add_file_to_report( filt_bam_, "filt. bam" + (deduped_reads ? "\\n$deduped_reads" : ""), group, \
"Alignment/$long/Filtered & deduped bam" + (deduped_reads ? " ($deduped_reads)" : "") )
if ( is_input_fastq( rep ) ) {
string ENCODE_step_name
if ( get_num_rep()==1 ) ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-single-rep-v1"
else ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-v1"
if ( fastqs_pair1.size() > 0 || fastqs_pair2.size() > 0 ) {
add_ENCODE_metadata_to_summary_json( "bam", "", "alignments", \
ENCODE_step_name, filt_bam_, fastqs_pair1_arr+fastqs_pair2_arr )
}
if ( flagstat_qc_) {
add_ENCODE_quality_metrics_to_summary_json( "samtools_flagstats_quality_metric", \
ENCODE_step_name, [filt_bam_], [flagstat_qc_] )
}
}
}
// string bedpe, subsampled_bedpe, tag
string bedpe, tag
if ( is_input_fastq( rep ) || is_input_bam( rep ) || is_input_filt_bam( rep ) ) {
if ( is_input_filt_bam( rep ) ) {
filt_bam_ = get_filt_bam( 0, rep )
filt_bam{rep} = filt_bam_
}
bedpe = bam_to_bedpe( filt_bam_, aln_o_dir, group )
wait
if ( adjust_bedpe ) {
tag = bedpe_to_tag_adj( bedpe, aln_o_dir, group )
}
else {
tag = bedpe_to_tag( bedpe, aln_o_dir, group )
}
wait
}
string final_tag_, final_tag_pr1_, final_tag_pr2_
if ( is_input_fastq( rep ) || is_input_bam( rep ) || is_input_filt_bam( rep ) || is_input_tag( rep ) ) {
if ( is_input_tag( rep ) ) tag = get_tag( 0, rep )
string subsampled_tag
if ( parse_number( subsample ) != 0 ) {
subsampled_tag = subsample_tag_PE( tag, parse_number( subsample ), false, aln_o_dir, group )
wait
}
else {
subsampled_tag = tag
}
if ( is_dnase_seq() ) {
final_tag_ = subsampled_tag
}
else {
final_tag_ = tn5_shift_tag( subsampled_tag, aln_o_dir, group )
}
final_tag{rep} = final_tag_
add_file_to_report( final_tag_, "tag-align", group, "Alignment/$long/Tag-align" )
if ( !true_rep ) {
aln_pr1_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr1" )
aln_pr2_o_dir := mkdir( "$out_dir/align/pseudo_reps/$group/pr2" )
( final_tag_pr1_, final_tag_pr2_ ) = spr_tag_PE( final_tag_, aln_pr1_o_dir, aln_pr2_o_dir, group )
final_tag_pr1{rep} = final_tag_pr1_
final_tag_pr2{rep} = final_tag_pr2_
add_file_to_report( final_tag_pr1_, "tag-align", "$group-pr1", \
"Alignment/Pseudo-replicates/$long/Pseudo-replicate 1/Tag-align" )
add_file_to_report( final_tag_pr2_, "tag-align", "$group-pr2", \
"Alignment/Pseudo-replicates/$long/Pseudo-replicate 2/Tag-align" )
wait
}
if ( !no_xcor ) {
subsampled_tag_xcor := subsample_tag_PE_for_xcor( final_tag_, parse_number( subsample_xcor ), true, aln_o_dir, group )
wait
// cross-corr. analysis
string xcor_qc_, xcor_plot_
( xcor_qc_, xcor_plot_ ) = xcor( subsampled_tag_xcor, qc_o_dir, group, nth_rep )
xcor_qc{rep} = xcor_qc_
xcor_plot{rep} = xcor_plot_
add_file_to_report( final_tag_, "tag-align", group, "Alignment/$long/Tag-align" )
add_file_to_table( xcor_plot_, "QC and logs/$long/Cross-corr. plot" )
wait
string ENCODE_step_name
if ( pbc_qc_ && read_length_log ) {
if ( get_num_rep() == 1 ) \
ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-single-rep-v1"
else \
ENCODE_step_name = "anshul-kundaje:atac-seq-trim-align-filter-step-run-v1"
add_ENCODE_quality_metrics_to_summary_json( "complexity_xcorr_quality_metric", \
ENCODE_step_name, \
[filt_bam_], [pbc_qc_, xcor_qc_, read_length_log], [ "true", xcor_plot_] )
}
}
}
}
void read_input_peak() {
if ( !is_input_peak() ) return // read peaks here
for ( int rep=0; rep<=get_num_rep_peak(); rep++) { // rep==0 : pooled
if ( get_num_rep_peak() == 1 && rep==0 ) continue // if only one replicate, skip reading pooled rep
for (int pse=0; pse<=2; pse++) { // pse==0 : true rep
if ( true_rep && pse > 0 ) continue