-
Notifications
You must be signed in to change notification settings - Fork 2
/
paf.py
196 lines (178 loc) · 7.1 KB
/
paf.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
import re
re_tag = re.compile("(\S\S:[AZif]):(\S+)")
re_cs = re.compile("([:=*+-])(\d+|[A-Za-z]+)")
def get_edit_distance_pairs_from_tag(tag):
seq = re.findall(re_cs, tag)
pairs = []
current_pos = 0
for (operator, value) in seq:
if operator == ':':
for i in range(int(value)):
pairs.append([current_pos, 0])
current_pos = current_pos+1
elif operator == '=':
for i in range(len(value)):
pairs.append([current_pos, 0])
current_pos = current_pos+1
elif operator == '-':
for i in range(len(value)):
pairs.append([current_pos, 1])
current_pos = current_pos+1
elif operator == '*':
pairs.append([current_pos, 1])
current_pos = current_pos+1
elif operator == '+':
for i in range(len(value)):
if len(pairs) == 0:
pairs.append([current_pos, 0])
pair = pairs.pop()
pair[1] = pair[1]+1
pairs.append(pair)
else:
raise ValueError("Unknown operator {}".format(operator))
return pairs
def edit_distance_in_range(pairs, ref_start, ref_end):
edit_distance = 0
for pair in pairs:
if pair[0] >= ref_start and pair[0] < ref_end:
edit_distance = edit_distance + pair[1]
return edit_distance
def get_pos_pairs_from_tag(tag, strand):
seq = re.findall(re_cs, tag)
pairs = []
current_ref_pos = 0
current_query_pos = 0
step = 1
if strand == "-":
seq.reverse()
step = -1
current_ref_pos = calculate_ref_length(seq)-1
for (operator, value) in seq:
if operator == ':':
for i in range(int(value)):
pairs.append([current_ref_pos, current_query_pos])
current_ref_pos = current_ref_pos+step
current_query_pos = current_query_pos+1
elif operator == '=':
for i in range(len(value)):
pairs.append([current_ref_pos, current_query_pos])
current_ref_pos = current_ref_pos+step
current_query_pos = current_query_pos+1
elif operator == '-':
for i in range(len(value)):
pairs.append([current_ref_pos, current_query_pos])
current_ref_pos = current_ref_pos+step
elif operator == '*':
pairs.append([current_ref_pos, current_query_pos])
current_ref_pos = current_ref_pos+step
current_query_pos = current_query_pos+1
elif operator == '+':
for i in range(len(value)):
pairs.append([current_ref_pos, current_query_pos])
current_query_pos = current_query_pos+1
else:
raise ValueError("Unknown operator: {}".format(operator))
return pairs
def ref_pos_for_query_pos(pairs, query_pos):
ref_pos_set = set()
for pair in pairs:
if pair[1] == query_pos:
ref_pos_set.add(pair[0])
return ref_pos_set
def query_pos_for_ref_pos(pairs, ref_pos):
query_pos_set = set()
for pair in pairs:
if pair[0] == ref_pos:
query_pos_set.add(pair[1])
return query_pos_set
def calculate_ref_length(seq):
length = 0
for (operator, value) in seq:
if operator == ':':
length = length + int(value)
elif operator == '=' or operator == '-':
length = length + len(value)
elif operator == '*':
length = length + 1
elif operator == '+':
length = length + 0
else:
raise ValueError("Unknown operator {}".format(operator))
return length
class Paf:
def __init__(self, paf_line):
fields = paf_line.split()
self.qname = fields[0]
self.qlength = int(fields[1])
self.qstart = int(fields[2])
self.qend = int(fields[3])
self.strand = fields[4]
self.rname = fields[5]
self.rlength = int(fields[6])
self.rstart = int(fields[7])
self.rend = int(fields[8])
self.numMatches = int(fields[9])
self.alignmentLength = int(fields[10])
self.mapQ = int(fields[11])
self.tags = {}
for i in range(12, len(fields)):
m = re_tag.match(fields[i])
self.tags[m.group(1)] = m.group(2)
self.pairs = None
self.edit_distance_pairs = None
def get_tag(self, tag_name):
try:
tag = self.tags[tag_name]
except:
raise ValueError("{} tag not found".format(tag_name))
return tag
def get_pos_pairs(self):
if self.pairs != None:
return self.pairs
tag = self.get_tag("cs:Z")
self.pairs = get_pos_pairs_from_tag(tag, self.strand)
return self.pairs
def get_edit_distance_pairs(self):
if self.edit_distance_pairs != None:
return self.edit_distance_pairs
tag = self.get_tag("cs:Z")
self.edit_distance_pairs = get_edit_distance_pairs_from_tag(tag)
return self.edit_distance_pairs
def ref_pos_for_query_pos(self, query_pos):
pairs = self.get_pos_pairs()
return ref_pos_for_query_pos(pairs, query_pos)
def query_pos_for_ref_pos(self, ref_pos):
pairs = self.get_pos_pairs()
return query_pos_for_ref_pos(pairs, ref_pos)
def isInAlignedRangeRef(self, pos):
return pos >= self.rstart and pos < self.rend
def query_pos_for_ref_pos_in_paf(self, ref_pos):
if ref_pos < self.rstart or ref_pos >= self.rend:
raise ValueError("ref pos {} is not in aligned region: {}-{}".format(
ref_pos, self.rstart, self.rend))
pos = ref_pos - self.rstart
return_set = set()
for query_pos in self.query_pos_for_ref_pos(pos):
return_set.add(query_pos + self.qstart)
return return_set
def ref_pos_for_query_pos_in_paf_line(self, query_pos):
if query_pos < self.qstart or query_pos >= self.qend:
raise ValueError("query pos {} is not in aligned region: {}-{}".format(
query_pos, self.qstart, self.qend))
pos = query_pos - self.qstart
return_set = set()
for ref_pos in self.ref_pos_for_query_pos(pos):
return_set.add(ref_pos + self.rstart)
return return_set
def edit_distance_for_region_in_paf_line(self, ref_start, ref_end):
if ref_start < self.rstart or ref_start >= self.rend:
raise ValueError("start coordinate {} is not in aligned region: {}-{}".format(
ref_start, self.rstart, self.rend))
if ref_end <= self.rstart or ref_end > self.rend:
raise ValueError("end coordinate {} is not in aligned region: {}-{}".format(
ref_end, self.rstart, self.rend))
if ref_start >= ref_end:
raise ValueError("start coordinate is greater than end, please specify [start, end)")
start = ref_start - self.rstart
end = ref_end - self.rstart
return edit_distance_in_range(self.get_edit_distance_pairs(), start, end)