-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipeline_atacseq.py
1921 lines (1499 loc) · 73.4 KB
/
pipeline_atacseq.py
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
from ruffus import follows, transform, add_inputs, mkdir, regex, formatter, merge, subdivide, files, collate, suffix, inputs
from ruffus.combinatorics import product
#from ruffus.combinatorics import *
import sys
import os
import sqlite3
import cgatcore.experiment as E
import cgatcore.pipeline as P
import pipelineAtacseq
import tempfile
import re
import cgatcore.iotools as IOTools
import cgatpipelines.tasks.rnaseq as rnaseq
import pandas
#sys.path.insert(0, "/home/mbp15ja/dev/AuxiliaryPrograms/Segmentations/")
#import compareSegmentations as compseg
#sys.path.insert(0, "/home/mbp15ja/dev/AuxiliaryPrograms/logParsers/")
#import logParser
#sys.path.insert(0, "/home/mbp15ja/dev/AuxiliaryPrograms/StringOperations/")
#import StringOperations
#sys.path.insert(0, '/home/mbp15ja/dev/AuxiliaryPrograms/Clusterings/')
#import clusterings
#sys.path.insert(0, '/home/mbp15ja/dev/AuxiliaryPrograms/File_operations/')
#import file_operations
import matplotlib as mpl
mpl.use('Agg') # So that matplotlib.pyplot doesn't try to find the X-server and give an error
import matplotlib.pyplot as plt
import math
PARAMS = P.get_parameters(
["%s/pipeline.yml" % os.path.splitext(__file__)[0],
"../pipeline.ini",
"pipeline.yml"])
#------------------------------------------------------------------------------
@follows(mkdir("stats.dir"))
@transform("*.bam",
regex("(.+).bam"),
r"stats.dir/\1.after_mapping.tsv")
def getInitialMappingStats(infile, outfile):
''' Gets the initial mapping rate in terms of total reads '''
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# The mapping file is sorted by coordinate, first sort by readname
temp_file = P.snip(outfile, ".tsv") + ".bam"
log_file = P.snip(outfile, ".tsv") + ".log"
# Samtools creates temporary files with a certain prefix, create a temporal directory name
samtools_temp_dir = tempfile.mkdtemp(dir=tmp_dir)
samtools_temp_file = os.path.join(samtools_temp_dir, "temp")
disk_space_file = P.snip(outfile, ".tsv") + ".txt"
statement = ''' mkdir -p %(samtools_temp_dir)s &&
samtools sort -n -o %(temp_file)s
-T %(samtools_temp_file)s %(infile)s
2> %(log_file)s
'''
# Execute the statement
P.run(statement)
# Get the stats
pipelineAtacseq.getMappedUnmappedReads(temp_file,
outfile,
submit=True)
# Remove the temporal file
statement = '''rm %(temp_file)s;
'''
# Execute the statement
P.run(statement)
#-----------------------------------------------------------------------------
@follows(mkdir("first_filtering.dir"))
@transform("*.bam",
regex("(.+).bam"),
r"first_filtering.dir/\1.bam")
def filterOutIncorrectPairsAndExcessiveMultimappers(infile, outfile):
'''Assuming a starting compressed coordinate sorted bam file.
Remove unmapped, mate unmapped and reads failing platform. Keep only
properly mapped pairs. Sort by name and filter out proper mapped pairs with
more than the defined number of proper pair alignments'''
allowed_multimappers = PARAMS["filtering_allowed_multimapper_proper_pairs"]
log_file = P.snip(outfile, ".bam") + ".log"
first_filtering_bam_output = P.snip(outfile, ".bam") + "_proper_pairs.bam"
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".bam") + "_temp.bam"
# Samtools creates temporary files with a certain prefix
samtools_temp_file = (tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
scripts_dir = os.path.join(os.path.dirname(__file__), "scripts")
statement = '''samtools view -F 524 -f 2 -u %(infile)s |
samtools sort -n - -o %(first_filtering_bam_output)s
-T %(samtools_temp_file)s 2> %(log_file)s &&
samtools view -h %(first_filtering_bam_output)s |
%(scripts_dir)s/assign_multimappers.py -k %(allowed_multimappers)s --paired-end |
samtools view -bS - -o %(temp_file)s 2>> %(log_file)s &&
mv %(temp_file)s %(outfile)s &&
rm %(first_filtering_bam_output)s
'''
P.run(statement)
#-------------------------------------------------------------------------
@follows(mkdir("stats.dir"))
@transform(filterOutIncorrectPairsAndExcessiveMultimappers,
regex(".+/(.+).bam"),
r"stats.dir/\1.after_first_filter.tsv")
def getFirstFilteringStats(infile, outfile):
''' Gets the mapping rate in terms of total reads after the first filtering '''
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# The mapping file is sorted by coordinate, first sort by readname
temp_file = P.snip(outfile, ".tsv") + ".bam"
log_file = P.snip(outfile, ".tsv") + ".log"
# Samtools creates temporary files with a certain prefix, create a temporal directory name
samtools_temp_dir = tempfile.mkdtemp(dir=tmp_dir)
samtools_temp_file = os.path.join(samtools_temp_dir, "temp")
disk_space_file = P.snip(outfile, ".tsv") + ".txt"
statement = ''' mkdir -p %(samtools_temp_dir)s &&
samtools sort -n -o %(temp_file)s -T %(samtools_temp_file)s %(infile)s 2> %(log_file)s;
'''
# Execute the statement
P.run(statement)
# Get the stats
pipelineAtacseq.getMappedUnmappedReads(temp_file,
outfile,
submit=True,
job_memory="6G")
# Remove the temporal file
statement = '''rm %(temp_file)s;
'''
# Execute the statement
P.run(statement)
#----------------------------------------------------------------------------------------
@follows(mkdir("second_filtering.dir"))
@transform(filterOutIncorrectPairsAndExcessiveMultimappers,
regex(".+/(.+).bam"),
r"second_filtering.dir/\1.bam")
def filterOutOrphanReadsAndDifferentChrPairs(infile, outfile):
''' Remove orphan reads (pair was removed) and read pairs mapping to different
chromosomes and read pairs which are "facing against one another" with no overlap.
Obtain position sorted BAM. Assumes a starting read name sorted BAM file.
'''
# Get the sample name
sample_name , _ = os.path.splitext(os.path.basename(outfile))
# Get samples details table
sample_details = PARAMS["samples_details_table"]
# Get trimmings in the 5' ends done previously (for example in qc).
five_prime_trim = 0 # pipelineAtacseq.getSampleQCShift(sample_name, sample_details)
integer_five_prime_correction = 0
# To avoid putting "--"
# Correction is going to be -correction on the start of the + strand
# Correction is going to be +correction on the end of the - strand
try:
integer_five_prime_correction = int(five_prime_trim)
except ValueError:
raise Exception("Five prime trimming argument needs to be an integer.")
# String with the correction to apply (Eg. "- 2", "+ 5")
positive_strand_correction = ""
negative_strand_correction = ""
if integer_five_prime_correction < 0:
positive_strand_correction = "+ "+str(abs(integer_five_prime_correction))
negative_strand_correction = "- "+str(abs(integer_five_prime_correction))
elif integer_five_prime_correction > 0:
positive_strand_correction = "- "+str(abs(integer_five_prime_correction))
negative_strand_correction = "+ "+str(abs(integer_five_prime_correction))
# 0 Case: no correction, empty string
log_file = P.snip(outfile, ".bam") + ".log"
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".bam") + "_temp.bam"
# Samtools creates temporary files with a certain prefix
samtools_temp_file = (tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
first_filtering_bam_output = P.snip(outfile, ".bam") + "_proper_pairs.bam"
# Fixmate seems to fix read pairs which are in different chromosomes but not
# read pairs "facing away" from each other
# Intermediate file outputted to process these cases
read_name_processing_input_bam = (
tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name + ".bam"
read_name_processing_problematic_reads = (
tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
read_name_processing_output_bam = (
tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name + ".bam"
# Reads are already read name sorted, so fixmate can be run
# Next filter the "wrong pairs" and get the read names of read pairs with
# each pair on a different chror "facing away" from each other with no
# overlap:
# <---------- <--------
# --------> Allowed --------> Not allowed
# Because the enzyme introduces 4 bp in the start of + strand and 5bp in the
# start of the - strand(end coordinate in bedpe file) a minumum overlap of
# 10
# The 5' ends of the reads is previously extended by any cuts performed in qc
# which are indicated.
# Get a bed with both ends of a read pair in each line
# and extract the problematic read names from there.
# Then filter them out with picard FilterSamReads
statement = '''samtools fixmate -r %(infile)s %(first_filtering_bam_output)s
2> %(log_file)s &&
samtools view -F 1804 -f 2 -u %(first_filtering_bam_output)s
-o %(read_name_processing_input_bam)s
2>> %(log_file)s &&
bedtools bamtobed -bedpe -i %(read_name_processing_input_bam)s
| awk '($1!=$4 ||
($10=="+" && $9=="-" &&
($3-1%(negative_strand_correction)s-5)<($5%(positive_strand_correction)s+4)))
{printf ("%%s\\n", $7)}'
> %(read_name_processing_problematic_reads)s
2>> %(log_file)s &&
if [ -s %(read_name_processing_problematic_reads)s ];
then
FilterSamReads I=%(read_name_processing_input_bam)s
O=%(read_name_processing_output_bam)s
READ_LIST_FILE=%(read_name_processing_problematic_reads)s
FILTER=excludeReadList 2>> %(log_file)s;
else
ln -s %(read_name_processing_input_bam)s %(read_name_processing_output_bam)s;
fi &&
samtools sort %(read_name_processing_output_bam)s
-o %(temp_file)s -T %(samtools_temp_file)s 2>> %(log_file)s &&
mv %(temp_file)s %(outfile)s &&
rm %(first_filtering_bam_output)s
%(read_name_processing_input_bam)s
%(read_name_processing_problematic_reads)s
%(read_name_processing_output_bam)s;
'''
job_memory="4G"
P.run(statement)
#------------------------------------------------------------------------------
# Assumes the files are coordinate sorted
@follows(mkdir("dedupped.dir"))
@transform(filterOutOrphanReadsAndDifferentChrPairs,
regex(".+/(.+).bam"),
r"dedupped.dir/\1.bam")
def markDuplicates(infile, outfile):
''' Use picard to mark duplicates in BAM files (not deleted).
The files are assumed to be coordinate sorted'''
# Used to be 5G
job_memory = "8G"
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Temporal dir, to prevent the "No space left on device" error
temp_dir = tempfile.mkdtemp(dir=tmp_dir)
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".bam") + "_temp.bam"
statement = ''' MarkDuplicates
ASSUME_SORTED=True
INPUT=%(infile)s
OUTPUT=%(temp_file)s
VALIDATION_STRINGENCY=LENIENT
METRICS_FILE=%(outfile)s.metrics
REMOVE_DUPLICATES=false
TMP_DIR=%(temp_dir)s
> %(outfile)s.log &&
mv %(temp_file)s %(outfile)s '''
P.run(statement)
#------------------------------------------------------------------------------
@follows(mkdir("stats.dir"))
@transform(markDuplicates,
regex(".+/(.+).bam"),
r"stats.dir/\1.after_marking_dups.tsv")
def getPostDuplicationStats(infile, outfile):
''' Assuming multimapping is allowed (multiple best alignments can occur)
Sort the reads by readname, make filterings and get the number
unique pair mappings:
1) Correctly mapped pairs and primary alignments only.
2) Correctly mapped pairs and primary or secondary alignments.
3) Correctly mapped pairs and secondary alignments only.
get initial statistics on the reads '''
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
sorted_bam = P.snip(outfile, ".tsv") + "_sorted.bam"
log_file = P.snip(outfile, ".tsv") + ".log"
bam_outfile_sec = P.snip(outfile, ".tsv") + "_sec.bam"
bam_outfile_primary = P.snip(outfile, ".tsv") + "_prim.bam"
# Samtools creates temporary files with a certain prefix
samtools_temp_file = (
tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
# First sort the bamfile
# Then get only the primary alignments
statement = '''samtools sort -n
-o %(sorted_bam)s
-T %(samtools_temp_file)s
%(infile)s
2> %(log_file)s &&
samtools view -h -F 256
%(sorted_bam)s
-o %(bam_outfile_primary)s
-U %(bam_outfile_sec)s
2>> %(log_file)s;
'''
P.run(statement)
# Now see the mapped pairs and PCR duplicates in each of the 3 files
primary_stats_file = P.snip(outfile, ".tsv") + "_primary.tsv"
secondary_stats_file = P.snip(outfile, ".tsv") + "_secondary.tsv"
# Where only primary alignments exist (1 read = 1 alignment)
pipelineAtacseq.getUniquelyMappedPairsNoMultimapping(bam_outfile_primary,
primary_stats_file,
submit=True,
job_memory="4G")
# Where multiple alignments can exist (primary + secondary)
pipelineAtacseq.getCorrectReadPairs(sorted_bam,
outfile,
submit=True,
job_memory="4G")
# Where multiple alignments can exist (secondary)
pipelineAtacseq.getCorrectReadPairs(bam_outfile_sec,
secondary_stats_file,
submit=True,
job_memory="4G")
#------------------------------------------------------------------------------
@subdivide(markDuplicates,
regex("(.+)/(.+).bam"),
[(r"\1/\2_pos_sorted.bam"),
(r"\1/\2_read_name_sorted.bam")],
r"\1/\2")
def deduplicate(infile, outfiles, sample):
'''Remove duplicates, create final name sorted BAM. Assumes a starting position sorted BAM'''
# Get both outfiles
position_sorted_bam = outfiles[0]
read_name_sorted_bam = outfiles[1]
log_file = sample + ".log"
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Create end temp file and intermediate temp file for position sorted and name sorted
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file_pos_sorted_bam = P.snip(position_sorted_bam, ".bam") + "_temp.bam"
# Samtools creates temporary files with a certain prefix
samtools_pos_sorted_temp_file = (tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
temp_file_name_sorted_bam = P.snip(read_name_sorted_bam, ".bam") + "_temp.bam"
# Samtools creates temporary files with a certain prefix
samtools_name_sorted_temp_file = (tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
statement = '''samtools view -F 1804
-f 2
-b %(infile)s
-o %(temp_file_pos_sorted_bam)s
2> %(log_file)s &&
samtools sort -n %(temp_file_pos_sorted_bam)s
-o %(temp_file_name_sorted_bam)s
-T %(samtools_name_sorted_temp_file)s
2>> %(log_file)s &&
mv %(temp_file_pos_sorted_bam)s %(position_sorted_bam)s &&
mv %(temp_file_name_sorted_bam)s %(read_name_sorted_bam)s;
'''
P.run(statement)
#------------------------------------------------------------------------------
@follows(mkdir("library_complexity.dir"))
@transform(markDuplicates,
regex(".+/(.+).bam"),
r"library_complexity.dir/\1.pbc.qc")
def calculateLibrarycomplexity(infile, outfile):
'''Calculates library complexity'''
# outfile temp file to ensure complete execution before writing outfile
temp_outfile = P.snip(outfile, ".pbc.qc") + "_temp.pbc.qc"
# outfile temp file to ensure complete execution before writing outfile
temp_header_outfile = P.snip(outfile, ".pbc.qc") + ".header"
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Samtools creates temporary files with a certain prefix
samtools_name_sorted_temp_file = (
tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
temp_file_name_sorted_bam = P.snip(infile, ".bam") + "_temp.bam"
log_file = P.snip(outfile, ".pbc.qc") + ".log"
# 1) Turns the read name sorted file to bed with both mapped segments in the pair
# 2) Gets the fields:
# -beginning of the most upstream segment
# -end of most downstream segment
# -mapping strand of each segment.
# 3) Removes the mitochondrial chromosome regions
# 4) Sees any repeated regions from 2), counting the times each region appears.
# 5) Performs calculations for distinct reads, total reads and ratios.
# 6) Creates a header file and appends the figures calculated
header_file = "TotalReadPairs\\tDistinctReadPairs\\tOneReadPair\\tTwoReadPairs\\tNRF=Distinct/Total\\tPBC1=OnePair/Distinct\\tPBC2=OnePair/TwoPair"
statement = '''samtools sort -n %(infile)s
-o %(temp_file_name_sorted_bam)s
-T %(samtools_name_sorted_temp_file)s
2>> %(log_file)s &&
bedtools bamtobed -bedpe -i %(temp_file_name_sorted_bam)s
| awk 'BEGIN{OFS="\\t"}
(($1==$4) && ($2==$5) && ($3==$6))
{$9="+";$10="-"}
{print $0}'
| awk 'BEGIN{OFS="\\t"}{print $1,$2,$4,$6,$9,$10}'
| grep -v 'chrM'
| sort
| uniq -c
| awk 'BEGIN{mt=0;m0=0;m1=0;m2=0}
($1==1)
{m1=m1+1}
($1==2){m2=m2+1}
{m0=m0+1}
{mt=mt+$1}
END{printf "%%d\\t%%d\\t%%d\\t%%d\\t%%f\\t%%f\\t%%f\\n",mt,m0,m1,m2,m0/mt,m1/m0,m1/m2}'
> %(temp_outfile)s &&
rm %(temp_file_name_sorted_bam)s %(samtools_name_sorted_temp_file)s* &&
echo -e '%(header_file)s' > %(temp_header_outfile)s &&
cat %(temp_outfile)s >> %(temp_header_outfile)s &&
rm %(temp_outfile)s &&
mv %(temp_header_outfile)s %(outfile)s;
'''
P.run(statement)
#------------------------------------------------------------------------------
@follows(mkdir("flagstats.dir"), deduplicate)
@transform(deduplicate,
formatter(".+/(?P<SAMPLE>.+)_pos_sorted\.bam"),
"flagstats.dir/{SAMPLE[0]}.flagstats")
def index(infile, outfile):
'''Index final position sorted BAM, get flag stats.'''
log_file = P.snip(outfile, ".flagstats") + ".log"
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".flagstats") + "_temp.flagstats"
# Index Final BAM file
statement = '''samtools index %(infile)s 2> %(log_file)s &&
samtools flagstat %(infile)s > %(temp_file)s &&
mv %(temp_file)s %(outfile)s;
'''
P.run(statement)
#------------------------------------------------------------------------------
@follows(mkdir("tag_align.dir"), index, deduplicate)
@transform(deduplicate,
formatter(".+/(?P<SAMPLE>.+)_pos_sorted\.bam"),
"tag_align.dir/{SAMPLE[0]}.PE2SE.tagAlign.gz")
def createTagAlign(infile, outfile):
'''creates tagAlign file (virtual single end) with (BED 3+3 format)'''
log_file = P.snip(outfile, ".PE2SE.tagAlign.gz") + ".log"
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".PE2SE.tagAlign.gz") + "_temp.PE2SE.tagAlign.gz"
# Create virtual SE file containing both read pairs
statement = '''bedtools bamtobed -i %(infile)s |
awk 'BEGIN{OFS="\\t"}{$4="N";$5="1000";print $0}' |
gzip -c > %(temp_file)s 2> %(log_file)s &&
mv %(temp_file)s %(outfile)s;
'''
P.run(statement)
#------------------------------------------------------------------
@follows(mkdir("final_tag_align.dir"), index)
@transform(createTagAlign,
regex(".+/(.+?).PE2SE.tagAlign.gz"),
r"final_tag_align.dir/\1.PE2SE.tagAlign.gz")
def excludeUnwantedContigsPE2SE(infile, outfile):
'''Exclude the contigs indicated, performs partial matching for each'''
excluded_chrs = PARAMS["filtering_contigs_to_remove"]
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".PE2SE.tagAlign.gz") + "_temp.PE2SE.tagAlign.gz"
statement = pipelineAtacseq.createExcludingChrFromBedStatement(infile,
excluded_chrs,
temp_file)
statement += '''&&
mv %(temp_file)s %(outfile)s'''
P.run(statement)
#----------------------------------------------------------------------
@follows(mkdir("final_tag_align.dir"))
@transform(excludeUnwantedContigsPE2SE,
regex(".+/(.+?).PE2SE.tagAlign.gz"),
r"final_tag_align.dir/\1.PE2SE.tn5_shifted.tagAlign.gz")
def shiftTagAlign(infile, outfile):
'''Shifts tag aligns by the TN5 sites and any 5' trimming from qc'''
# Eliminate .PE2SE.tn5_shifted.tagAlign.gz from the sample name
sample_name = re.sub('\.PE2SE\.tagAlign\.gz$', '', os.path.basename(infile))
# Get samples details table
sample_details = PARAMS["samples_details_table"]
# Get trimmings in the 5' ends done previously (for example in qc).
five_prime_trim = 0 # pipelineAtacseq.getSampleQCShift(sample_name, sample_details)
integer_five_prime_correction = 0
# To avoid putting "--"
# Correction is going to be -correction on the start of the + strand
# Correction is going to be +correction on the end of the - strand
try:
integer_five_prime_correction = int(five_prime_trim)
except ValueError:
raise Exception("Five prime trimming argument needs to be an integer.")
# String with the correction to apply (Eg. "- 2", "+ 5")
positive_strand_correction = ""
negative_strand_correction = ""
if integer_five_prime_correction < 0:
positive_strand_correction = "+ "+str(abs(integer_five_prime_correction))
negative_strand_correction = "- "+str(abs(integer_five_prime_correction))
elif integer_five_prime_correction > 0:
positive_strand_correction = "- "+str(abs(integer_five_prime_correction))
negative_strand_correction = "+ "+str(abs(integer_five_prime_correction))
# 0 Case: no correction, empty string
# Get the contigs
contigs = PARAMS["contigs"]
log_file = P.snip(outfile, ".PE2SE.tn5_shifted.tagAlign.gz") + ".tn5_shifted.log"
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file = P.snip(outfile, ".PE2SE.tn5_shifted.tagAlign.gz") + "_temp.tn5_shifted.tagAlign.gz"
# Shift the beginning of the elements in the + strand (where the enzume cuts) by +4
# Shift the end of the elements in the - strand (where the enzume cuts) by -5
# Apply qc corrections too.
statement = '''zcat %(infile)s
| awk -F $'\\t'
'BEGIN {OFS = FS}
{ if ($6 == "+") {
$2 = $2 + 4 %(positive_strand_correction)s
} else if ($6 == "-") {
$3 = $3 - 5 %(negative_strand_correction)s
}
print $0}'
| gzip -c
> %(temp_file)s
2> %(log_file)s;
'''
P.run(statement)
log_file_correction = P.snip(outfile, ".PE2SE.tn5_shifted.tagAlign.gz") + \
".tn5_shifted_slop_correction.log"
# Temp file: We create a temp file to make sure the whole process goes well
# before the actual outfile is created
temp_file2 = P.snip(outfile, ".PE2SE.tn5_shifted.tagAlign.gz") + "_temp_correction.tn5_shifted.tagAlign.gz"
# Check that the slop does not surpass the chromosome edges and that the starts are < than ends
pipelineAtacseq.correctSlopChromosomeEdges(temp_file,
contigs,
temp_file2,
log_file_correction,
submit=True,
job_memory="4G")
# Remove the temp file
statement = ''' rm %(temp_file)s &&
mv %(temp_file2)s %(outfile)s '''
P.run(statement)
#------------------------------------------------------------------------------
@follows(mkdir("filtered_tag_align.dir"))
@transform(shiftTagAlign,
formatter(".+\.dir/(?P<SAMPLE>(?!pooled_[control|treatment]).+)\.PE2SE\.tn5_shifted\.tagAlign\.gz"),
"filtered_tag_align.dir/{SAMPLE[0]}.single.end.shifted.filtered.tagAlign.gz")
def filterShiftTagAlign(infile, outfile):
''' Filters out regions of low mappability and excessive mappability in the shifted single ends'''
temp_file = P.snip(outfile, ".gz") + "_temp.gz"
excluded_beds = PARAMS["filtering_bed_exclusions"]
statement = pipelineAtacseq.createExcludingBedsFromBedStatement(infile,
excluded_beds,
temp_file)
statement += ''' && mv %(temp_file)s %(outfile)s'''
P.run(statement)
#--------------------------------------------------------------------------------------
@follows(mkdir("filtered_tag_align_count_balanced.dir"))
@transform(filterShiftTagAlign,
regex("filtered_tag_align.dir/(.+).single.end.shifted.filtered.tagAlign.gz"),
r"filtered_tag_align_count_balanced.dir/\1.tsv",
r"\1")
def calculateNumberOfSingleEnds(infile, outfile, sample):
'''Get the number of single end reads entering the peak calling'''
statement = '''echo %(sample)s,`zcat %(infile)s | wc -l` > %(outfile)s
'''
P.run(statement)
#----------------------------------------------------------------------------------------
@merge(calculateNumberOfSingleEnds, "filtered_tag_align_count_balanced.dir/reads_per_sample.csv")
def mergeSingleEndsCount(infiles, outfile):
''' Merge together all the SE counts into a single table '''
infiles = " ".join(infiles)
statement = ''' echo sample,n_se > %(outfile)s &&
cat %(infiles)s >> %(outfile)s '''
P.run(statement)
#----------------------------------------------------------------------------------------
@follows(mergeSingleEndsCount)
def get_single_ends():
pass
##########################################################################################
# Generate Peak sets
@follows(mkdir("pan_balanced_samples.dir"),
mkdir("subtype_balanced_samples.dir"))
@subdivide(filterShiftTagAlign,
regex("filtered_tag_align.dir/(.+).bowtie2.single.end.shifted.filtered.tagAlign.gz"),
add_inputs(mergeSingleEndsCount, "samples.tsv"),
[r"pan_balanced_samples.dir/\1.single.end.shifted.filtered.tagAlign.gz",
r"subtype_balanced_samples.dir/\1.single.end.shifted.filtered.tagAlign.gz"],
r"\1")
def get_balanced_sample_filtered_shifted_SE(infiles, outfiles, sample_name):
''' Samples a random sample of reads from each sample. Each sample contains the same
number of reads: the minimum sample single ends. Returns a file with the same sorting
as the input'''
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Separate the infiles
bed_tags, read_count_file, sample_info = infiles
sample_info = pandas.read_csv(sample_info, sep="\t")
read_counts = pandas.read_csv(read_count_file, sep=",")
read_counts["sample"] = read_counts["sample"].str.replace(".bowtie2","")
sample_info = sample_info.merge(read_counts, left_on="ATAC.sample.code", right_on="sample")
sample_info = sample_info.set_index("sample")
#min_per_status = sample_info.groupby("MM.ND").n_se.min()
#min_per_subtype = sample_info.groupby("Subgroup").n_se.min()
#get_reads_status = min_per_status[sample_info["MM.ND"][sample_name]]
#get_reads_subtype = min_per_subtype[sample_info["Subgroup"][sample_name]]
output_reads = sample_info[sample_info["MM.ND"].isin(["MM","ND"])].n_se.min()
if sample_name in ["A26.20", "A26.18"]:
output_reads=output_reads/2
# Extract the file and generate the sample
statement_template = '''zcat %(bed_tags)s > %(temp_extract_bed)s &&
sample -o --preserve-order -d 51 -k %(output_reads)s %(temp_extract_bed)s | gzip > %(outfile)s &&
rm %(temp_extract_bed)s
'''
statements = []
for outfile in outfiles:
temp_extract_bed = (tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False)).name
statements.append(statement_template % locals())
# Samples with number of single ends > 100M
job_memory = "8G"
P.run(statements)
#----------------------------------------------------------------------------------------
def get_files_for_pooling():
'''This generator makes the tuples for @files that will be used to pool the balanced samples
into pan-MM/ND and subtype read pools for peak calling'''
sample_info = pandas.read_csv("samples.tsv", sep="\t")
for pan in ["MM", "ND"]:
samples = sample_info[sample_info["MM.ND"] == pan]["ATAC.sample.code"]
dirname = "pan_balanced_samples.dir"
outfile = os.path.join(dirname, "%s_pooled_pan.single.end.shifted.filtered.tagAlign.gz"
% pan)
infiles = [os.path.join(dirname, "%s.single.end.shifted.filtered.tagAlign.gz") % s
for s in samples]
yield (infiles, outfile)
for subtype in set(sample_info["Subgroup"]):
if subtype == "UNKNOWN":
continue
if subtype =="Cell_line":
continue
samples = sample_info[sample_info["Subgroup"] == subtype]["ATAC.sample.code"]
dirname = "subtype_balanced_samples.dir"
outfile = os.path.join(dirname, "%s_pooled_subtype.single.end.shifted.filtered.tagAlign.gz"
% subtype)
infiles = [os.path.join(dirname, "%s.single.end.shifted.filtered.tagAlign.gz") % s
for s in samples]
yield (infiles, outfile)
@follows(get_balanced_sample_filtered_shifted_SE)
@files(get_files_for_pooling)
def pool_balanced_single_ends(infiles, outfile):
'''Pool the downsampled balanced single-end aligned tags for subgroup
and also for MM and ND'''
infiles = " ".join(infiles)
statement = '''zcat %(infiles)s | gzip > %(outfile)s'''
P.run(statement)
#----------------------------------------------------------------------------------------
@follows(mkdir("filtered_peaks_broad.dir"),
mkdir("pan_peaks_broad.dir"),
mkdir("subtype_peaks_broad.dir"))
@subdivide((filterShiftTagAlign,
pool_balanced_single_ends),
regex("([^_]+)_.+.dir/(.+).single.end.shifted.filtered.tagAlign.gz"),
[r"\1_peaks_broad.dir/\2_peaks.broadPeak.gz",
r"\1_peaks_broad.dir/\2_peaks.gappedPeak.gz"],
r"\2")
def call_peaks_broad(infile, outfiles, sample):
''' Use MACS2 to calculate broad peaks and gapped peaks.
Sorts the output by -log10pvalue,
formats the name of the broad and gapped peaks '''
# Get the thresholding values for MACS2
threshold_method = PARAMS["macs2_threshold_method"].lower()
# If nothing specified default to p (p-value)
if threshold_method == "":
threshold_method = "p"
elif threshold_method not in ["p", "q"]:
raise Exception("threshold method specified not valid")
threshold_quantity = PARAMS["macs2_threshold_quantity"]
# If nothing specified default to 0.01
if threshold_quantity == "":
threshold_quantity = "0.01"
# Get the read extending and shift values
shift_parameter = PARAMS["end_extending_shift"]
# If nothing specified default to -100
if shift_parameter == "":
shift_parameter = "-100"
extsize_parameter = PARAMS["end_extending_extsize"]
# If nothing specified default to 200
if extsize_parameter == "":
extsize_parameter = "200"
# Get the temporal dir specified
tmp_dir = PARAMS["general_temporal_dir"]
# Get the directory for the outfile
outdir = os.path.dirname(outfiles[0])
# Create a temporal directory name for the run
peaks_temp_dir = tempfile.mkdtemp(dir=tmp_dir)
# Get the name of the peak file (_peaks.broadPeak.gz)
outfile_basename = os.path.basename(outfiles[0])
outfile_basename_prefix = P.snip(outfile_basename, "_peaks.broadPeak.gz")
# Create the prefix to create the files in the temporal directory
# and with the name of the run
outfile_prefix = os.path.join(peaks_temp_dir, outfile_basename_prefix)
# Final outfile names for the post-processed files
broad_peaks_final_outfile = outfiles[0]
gappedPeak_peaks_final_outfile = outfiles[1]
# 1) Calculate peaks, will create .xls, .broadPeak and .gappedPeak in temp dir
# 2) Process the .broadPeak and .gappedPeak and output in outdir
# 3) Delete the .broadPeak and .gappedPeak temp files
# 4) Copy the remaining created files from temp dir to outdir
statement = '''macs2 callpeak
-t %(infile)s
-f BED
-n %(outfile_prefix)s
-g hs
-%(threshold_method)s %(threshold_quantity)s
--nomodel
--shift %(shift_parameter)s
--extsize %(extsize_parameter)s
--broad
--broad-cutoff %(threshold_quantity)s
--keep-dup all
--tempdir %(tmp_dir)s &&
sort -k 8gr,8gr %(outfile_prefix)s_peaks.broadPeak
| awk 'BEGIN{OFS="\\t"}{$4="Peak_"NR ; print $0}'
| gzip -c > %(broad_peaks_final_outfile)s &&
rm %(outfile_prefix)s_peaks.broadPeak &&
sort -k 14gr,14gr %(outfile_prefix)s_peaks.gappedPeak
| awk 'BEGIN{OFS="\\t"}{$4="Peak_"NR ; print $0}' |
gzip -c > %(gappedPeak_peaks_final_outfile)s &&
rm %(outfile_prefix)s_peaks.gappedPeak &&
mv %(outfile_prefix)s* %(outdir)s
'''
# The pooled datasets contain a lot of reads, it is advisable to up
# the memory in this case
job_memory = "4G"
# Pooled samples for this pipeline
if ("pooled" in sample):
job_memory = "10G"
P.run(statement, job_condaenv="macs2")
#----------------------------------------------------------------------------------------
@follows(mkdir("filtered_peaks_narrow.dir"),
mkdir("pan_peaks_narrow.dir"),
mkdir("subtype_peaks_narrow.dir"))
@subdivide((filterShiftTagAlign,
pool_balanced_single_ends),
regex("([^_]+)_.+.dir/(.+).single.end.shifted.filtered.tagAlign.gz"),
[r"\1_peaks_narrow.dir/\2_peaks.narrowPeak.gz",
r"\1_peaks_narrow.dir/\2_summits.bed"],
r"\2")
def call_peaks_narrow(infile, outfiles, sample):
''' Use MACS2 to calculate peaks '''
# Get the thresholding values for MACS2
threshold_method = PARAMS["macs2_threshold_method"].lower()
# If nothing specified default to p (p-value)
if threshold_method == "":
threshold_method = "p"
elif threshold_method not in ["p", "q"]:
raise Exception("threshold method specified not valid")
threshold_quantity = PARAMS["macs2_threshold_quantity"]
# If nothing specified default to p (p-value)
if threshold_quantity == "":
threshold_quantity = "0.01"
# Get the read extending and shift values
shift_parameter = PARAMS["end_extending_shift"]
# If nothing specified default to -100
if shift_parameter == "":
shift_parameter = "-100"
extsize_parameter = PARAMS["end_extending_extsize"]
# If nothing specified default to 200
if extsize_parameter == "":
extsize_parameter = "200"
# Get the temporal dir specified