forked from petercombs/HybridSliceSeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseDelta.py
90 lines (72 loc) · 1.96 KB
/
ParseDelta.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
from numpy import (zeros, arange, sum, array, argwhere, abs)
def parse_records(delta_file):
while True:
try:
yield parse_record(delta_file)
except StopIteration:
break
def parse_record(delta_file, return_names=True):
line = next(delta_file)
while not line.startswith('>'):
line = next(delta_file)
# The first line is
# >seq1 seq2 length1 length2
line = line.strip().strip('>').split()
n1 = line[0]
n2 = line[1]
l1 = int(line[-2])
l2 = int(line[-1])
# The next line is
# start1 end1 start2 end2 ??? ???
line = next(delta_file).strip().split()
i1 = int(line[0])-1
p1 = i1
e1 = int(line[1])
i2 = int(line[2])-1
e2 = int(line[3])
p2 = i2
offset = i2 - i1
deltas = []
while line:
line = next(delta_file)
line = int(line)
deltas.append(line)
deltas = array(deltas)
longest = max(l1 + sum(deltas < 0) - offset, l2 + sum(deltas > 0)+offset)
pos1 = zeros(longest, dtype=int)
pos2 = zeros(longest, dtype=int)
offset1 = offset if offset > 0 else 0
offset2 = -offset if offset < 0 else 0
i1 += offset1
i2 += offset2
pos1[offset1:i1] = arange(p1)
pos2[offset2:i2] = arange(p2)
for d in deltas:
if d == 0:
break
ad = abs(d)
pos1[i1:i1+ad] = arange(p1, p1+ad)
pos2[i2:i2+ad] = arange(p2, p2+ad)
if d > 0:
p1 += ad
p2 += ad
i1 += ad
i2 += ad
p2 -= 1
elif d < 0:
p1 += ad
p2 += ad
i1 += ad
i2 += ad
p1 -= 1
else:
assert False
pos1[i1:i1+l1-p1] = arange(p1, l1)
pos2[i2:i2+l2-p2] = arange(p2, l2)
pos1[i1+l1-p1:] = pos1[i1+l1-p1-1]
pos2[i2+l2-p2:] = pos2[i2+l2-p2-1]
if return_names:
return (n1, pos1), (n2, pos2)
return pos1, pos2
if __name__ == "__main__":
pass