forked from fedeoliv/Rosalind-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsub.py
28 lines (20 loc) · 750 Bytes
/
rsub.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
# http://rosalind.info/problems/rsub/
import newick, fasta
def find_rev(t,dnas):
r = []
for i in range(len(dnas[t.u])):
r += [(p[0],p[-1],i,dnas[p[0].u][i]) for p in t.find_rev(dnas,i)]
return r
if __name__ == '__main__':
with open('rosalind_rsub.txt') as f:
nw = f.readline()
nw.split()
tree = newick.newick_parse(nw)
fst = f.read()
dnas,_ = fasta.parse_fasta(fst)
nodes = tree.nodes()
for node in nodes:
revs = find_rev(node,dnas)
for fc, dest, pos, mid in revs:
print("%s %s %d %s->%s->%s" % (fc.u, dest.u, pos + 1, dnas[node.u][pos], mid, dnas[dest.u][pos]))
assert(dnas[node.u][pos] == dnas[dest.u][pos])