forked from brianpenghe/bamsam-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitBAMbyLength.py
124 lines (114 loc) · 4.16 KB
/
splitBAMbyLength.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
##################################
# #
# Last modified 06/06/2012 #
# #
# Georgi Marinov #
# #
##################################
import sys
from sets import Set
import pysam
import string
def main(argv):
if len(argv) < 4:
print 'usage: python %s BAM chrom.sizes sizes outfileprefix [-nomulti]' % argv[0]
print '\tsizes should be comma-separated and can be in from-to format as well as individual'
sys.exit(1)
BAM = argv[1]
outfileprefix= argv[4]
chrominfo=argv[2]
chromInfoList=[]
linelist=open(chrominfo)
for line in linelist:
fields=line.strip().split('\t')
chr=fields[0]
start=0
end=int(fields[1])
chromInfoList.append((chr,start,end))
doMulti=True
if '-nomulti' in argv:
doMulti=False
print 'will ignore multimappers'
samfile = pysam.Samfile(BAM, "rb" )
sizes = argv[3].split(',')
sizeDict={}
print sizes
for size in sizes:
if '-' in size:
s1 = int(size.split('-')[0])
s2 = int(size.split('-')[1])
for s in range(s1,s2+1):
sizeDict[s]= pysam.Samfile(outfileprefix + '.' + size + 'mers.bam', "wb", template=samfile)
else:
sizeDict[int(size)]= pysam.Samfile(outfileprefix + '.' + size + 'mers.bam', "wb", template=samfile)
if doMulti:
i=0
for (chr,start,end) in chromInfoList:
try:
for alignedread in samfile.fetch(chr, 0, 100):
a='b'
except:
continue
for alignedread in samfile.fetch(chr, start, end):
i+=1
if i % 5000000 == 0:
print str(i/1000000) + 'M alignments processed processed', chr,start,alignedread.pos,end
length = len(alignedread.seq)
if sizeDict.has_key(length):
outfile = sizeDict[length]
outfile.write(alignedread)
else:
MultiplicityDict={}
for (chr,start,end) in chromInfoList:
try:
for alignedread in samfile.fetch(chr, 0, 100):
a='b'
except:
continue
for alignedread in samfile.fetch(chr, start, end):
i+=1
if i % 5000000 == 0:
print 'examining read multiplicity', str(i/1000000) + 'M alignments processed processed', chr,start,alignedread.pos,end
fields=str(alignedread).split('\t')
ID=fields[0]
if alignedread.is_read1:
ID = ID + '/1'
if alignedread.is_read2:
ID = ID + '/2'
if MultiplicityDict.has_key(ID):
MultiplicityDict[ID]+=1
else:
MultiplicityDict[ID]=1
i=0
for (chr,start,end) in chromInfoList:
try:
for alignedread in samfile.fetch(chr, 0, 100):
a='b'
except:
continue
for alignedread in samfile.fetch(chr, start, end):
i+=1
if i % 5000000 == 0:
print 'outputting alignments', str(i/1000000) + 'M alignments processed processed', chr,start,alignedread.pos,end
fields=str(alignedread).split('\t')
ID=fields[0]
if alignedread.is_read1:
ID = ID + '/1'
if alignedread.is_read2:
ID = ID + '/2'
if nomulti and MultiplicityDict[ID] > 1:
continue
else:
length = len(alignedread.seq)
if sizeDict.has_key(length):
outfile = sizeDict[length]
outfile.write(alignedread)
outfilelist = []
for s in sizeDict.keys():
outfilelist.append(sizeDict[s])
outfilelist = list(Set(outfilelist))
for outfile in outfilelist:
print outfile
outfile.close()
if __name__ == '__main__':
main(sys.argv)