-
Notifications
You must be signed in to change notification settings - Fork 2
/
readMultiplicityBAM.py
59 lines (49 loc) · 1.41 KB
/
readMultiplicityBAM.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
##################################
# #
# Last modified 09/14/2012 #
# #
# Georgi Marinov #
# #
##################################
import sys
import os
import pysam
from sets import Set
def main(argv):
if len(argv) < 3:
print 'usage: python %s BAMfilename samtools outputfilename' % argv[0]
print 'BAM file need not be indexed or sorted'
sys.exit(1)
SAM = argv[1]
samtools = argv[2]
outputfilename = argv[3]
ReadMultiplicityDict={}
print 'examining read multiplicty'
i=0
cmd = samtools + ' view ' + SAM
p = os.popen(cmd, "r")
line = '...'
while line != '':
line = p.readline()
if line == '':
continue
i+=1
if i % 5000000 == 0:
print str(i/1000000) + 'M alignments processed'
fields = line.strip().split('\t')
ID=fields[0]
if fields[2] == '*':
continue
if ReadMultiplicityDict.has_key(ID):
pass
else:
ReadMultiplicityDict[ID]=0
ReadMultiplicityDict[ID]+=1
IDList = ReadMultiplicityDict.keys()
IDList.sort()
outfile=open(outputfilename,'w')
for ID in IDList:
outfile.write(ID + '\t' + str(ReadMultiplicityDict[ID]) + '\n')
outfile.close()
if __name__ == '__main__':
main(sys.argv)