-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_alignment.py
38 lines (30 loc) · 1.12 KB
/
seq_alignment.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
from collections import deque
def all_alignments(x, y):
"""Return an iterable of all alignments of two
sequences ( x and y ).
"""
def F(x, y):
"""Function to recursively build the
alignments.
x, y -- Sequence indices for the original x and y.
"""
if len(x) == 0 and len(y) == 0:
yield deque()
scenarios = []
if len(x) > 0 and len(y) > 0:
scenarios.append((x[0], x[1:], y[0], y[1:]))
if len(x) > 0:
scenarios.append((x[0], x[1:], None, y))
if len(y) > 0:
scenarios.append((None, x, y[0], y[1:]))
# NOTE: "xh" and "xt" stand for "x-head" and "x-tail",
# with "head" being the front of the sequence, and
# "tail" being the rest of the sequence. Similarly for
# "yh" and "yt".
for xh, xt, yh, yt in scenarios:
for alignment in F(xt, yt):
alignment.appendleft((xh, yh))
yield alignment
alignments = F(range(len(x)), range(len(y)))
return map(list, alignments)
my_alignments = all_alignments("CAT", "CT")