-
Notifications
You must be signed in to change notification settings - Fork 0
/
long.py
executable file
·72 lines (55 loc) · 1.58 KB
/
long.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
#! /usr/bin/python3
import sys
import argparse
import signal
import itertools
import collections
import tools
def overlap(a, b):
l = 0
for i in range(len(a)):
suffix = a[-i-1:]
if b.startswith(suffix):
l = i+1
return l
def main(args):
reads = tools.io.read_fasta(sys.stdin)
reads = [''.join(seq) for _, seq in reads]
left_neighbours = dict()
right_neighbours = dict()
overlaps = dict()
def compute_overlap(i, j):
l = overlap(reads[i], reads[j])
if l >= min(len(reads[i]), len(reads[j])) // 2 + 1:
overlaps[i] = l
left_neighbours[j] = i
right_neighbours[i] = j
for i, j in itertools.combinations(range(len(reads)), 2):
compute_overlap(i, j)
compute_overlap(j, i)
def missing_key(d):
for i in range(len(d)+1):
if i not in d:
return i
assert False
superstring = []
i = missing_key(left_neighbours)
superstring.append(reads[i])
while True:
if i not in right_neighbours:
break
l = overlaps[i]
i = right_neighbours[i]
superstring.append(reads[i][l:])
superstring = ''.join(superstring)
print(superstring)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='description',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
args = parser.parse_args()
try:
main(args)
except BrokenPipeError:
sys.exit(128 + signal.SIGPIPE)
except KeyboardInterrupt:
sys.exit(128 + signal.SIGINT)