Repository navigation
Expand file tree
/
Copy pathparseBAMfield_fix.py
More file actions
158 lines (151 loc) · 4.8 KB
/
Copy pathparseBAMfield_fix.py
File metadata and controls
158 lines (151 loc) · 4.8 KB
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
#!/usr/bin/env python
import sys
def isSoftClipped(cigar):
#Op BAM Description
#M 0 alignment match (can be a sequence match or mismatch)
#I 1 insertion to the reference
#D 2 deletion from the reference
#N 3 skipped region from the reference
#S 4 soft clipping (clipped sequences present in SEQ)
#H 5 hard clipping (clipped sequences NOT present in SEQ)
#P 6 padding (silent deletion from padded reference)
#= 7 sequence match
#X 8 sequence mismatch
for (op,count) in cigar:
if op == 4: return True
return False
def aln_length(cigarlist):
tlength = 0
for operation,length in cigarlist:
if operation == 0 or operation == 2 or operation == 3 or operation >= 6: tlength += length
return tlength
def parseMD(MD):
MDfields = []
value = ""
chars = ""
for elem in MD:
if elem.isdigit() and chars == "":
value+=elem
elif not elem.isdigit() and value == "":
chars+=elem
elif elem.isdigit() and chars != "":
MDfields.append(chars)
chars = ""
value = elem
elif not elem.isdigit() and value != "":
MDfields.append(int(value))
value = ""
chars = elem
if value != "": MDfields.append(int(value))
if chars != "": MDfields.append(chars)
return MDfields
def parseMDwithCigar(MD,cigar,seq):
MDfields = parseMD(MD)
res = []
posRef = 0
posRead = 0
ind = 0 # Pointer in MDfields
#Op BAM Description
#M 0 alignment match (can be a sequence match or mismatch)
#I 1 insertion to the reference
#D 2 deletion from the reference
#N 3 skipped region from the reference
#S 4 soft clipping (clipped sequences present in SEQ)
#H 5 hard clipping (clipped sequences NOT present in SEQ)
#P 6 padding (silent deletion from padded reference)
#= 7 sequence match
#X 8 sequence mismatch
for (op,count) in cigar:
#print op,ind
if (op in [0,7,8]) and ind % 2 == 1:
while (count > 0):
for base in MDfields[ind]:
res.append((posRef,posRead,seq[posRead]))
posRef += 1
posRead += 1
count-= 1
ind += 1 # Next posRef should be a number
if (count > 0):
if MDfields[ind] > count:
posRef += count
posRead += count
MDfields[ind] -= count
count = 0
else:
count -= MDfields[ind]
posRef += MDfields[ind]
posRead += MDfields[ind]
ind += 1
if (ind < len(MDfields)) and (MDfields[ind] == 0):
ind += 1
elif (op in [0,7,8]) and ind % 2 == 0:
if MDfields[ind] > count:
posRef += count
posRead += count
MDfields[ind] -= count
else:
count -= MDfields[ind]
posRef += MDfields[ind]
posRead += MDfields[ind]
ind += 1 # Next posRef should be a base/string of bases
while (count > 0):
for base in MDfields[ind]:
res.append((posRef,posRead,seq[posRead]))
posRef+= 1
posRead+= 1
count-= 1
ind += 1 # Next posRef should be a number
if (count > 0):
if MDfields[ind] > count:
posRef += count
posRead += count
MDfields[ind] -= count
count = 0
else:
count -= MDfields[ind]
posRef += MDfields[ind]
posRead += MDfields[ind]
ind += 1
if (ind < len(MDfields)) and (MDfields[ind] == 0):
ind += 1
elif (op == 4):
posRead += count
elif (op == 5):
continue
elif (op == 1):
#ins=':'.join("INS",seq[posRead:posRead+count])
ins="INS:"+seq[posRead:posRead+count]
res.append((posRef,posRead,(ins)))
posRead += count
elif (op == 2) and (ind % 2 == 1):
#dels=''.join("DEL:",MDfields[ind][1:])
dels="DEL:"+MDfields[ind][1:]
print(dels)
res.append((posRef,posRead,(dels)))
posRef += count
ind += 1
else:
sys.stderr.write("Cigar case not implemented: (%d,%d) (%s) [%d (%s),%d]\n"%(op,count,str(cigar),ind,str(MDfields),posRef))
#sys.exit()
return []
#print MD,cigar,MDfields,res,posRef
return res
import pysam
import numpy as np
samfile = pysam.AlignmentFile(sys.argv[1], "rb",check_sq=False)
reflength = samfile.get_reference_length(samfile.get_reference_name(0)) - 37
for read in samfile:
if not read.is_unmapped:
cigar = read.cigar
MD = read.get_tag("MD")
# NM = read.get_tag("NM")
UMI = "."
if (len(cigar)>1) and (cigar[0][0]==4) and (cigar[0][1]<=8):
UMI = read.seq[0:cigar[0][1]]+UMI
if (len(cigar)>2) and (cigar[-1][0]==4) and (cigar[-1][1]<=8):
UMI = UMI + read.seq[(-1*cigar[-1][1]):]
a = parseMDwithCigar(MD,cigar,read.seq)
a=list(filter(lambda x:x[0]<=reflength,a))
# a=list(filter(lambda x:x[0]>27,a))
NM=len(a)
print("%d\t%d\t%s\t%s\t%s"%(read.pos,len(read.seq),UMI,NM,a))