-
Notifications
You must be signed in to change notification settings - Fork 2
/
nus.py
42 lines (36 loc) · 1.33 KB
/
nus.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
# Dynamic programming routine definitions for the Nussinov algorithm
# http://ultrastudio.org/en/Nussinov_algorithm
def load_input(args):
input_f = open(args[0], 'r')
input_lines = input_f.readlines()
input_f.close()
seq = input_lines[0].strip()
return (seq, (len(seq), len(seq)))
def score_fn(x, y):
if (x == 'A' and y == 'T') or \
(x == 'T' and y == 'A') or \
(x == 'G' and y == 'C') or \
(x == 'C' and y == 'G'):
return 1
else:
return 0
def compute_cell(i, j, table, seq):
# This part is a little bit involved, since in the natural
# formulation of the Nussinov algorithm, the data flows
# from the top-right corner. Our implementation requires that
# the data flows from the top-left corner, so some translation
# between indiced has to be done.
i_pos = len(seq) - i - 1
j_pos = j
if i_pos == j_pos:
return 0
elif i_pos - 1 == j_pos:
return 0
else:
return reduce(max, [table[i, k_pos] + table[len(seq) - (k_pos + 1) - 1, j] \
for k_pos in xrange(i_pos, j_pos)],
table[i-1, j-1] + score_fn(seq[i_pos], seq[j_pos]))
def write_output(args, table, data):
output_f = open(args[1], 'w')
output_f.write('Alignment score: ' + str(table[-1,-1]) + '\n')
output_f.close()