-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefficient_3.py
More file actions
200 lines (170 loc) · 5.14 KB
/
efficient_3.py
File metadata and controls
200 lines (170 loc) · 5.14 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
import sys
import time
import psutil
COST_TABLE = {
'delta' : 30,
'alpha' : {
'A' : {'A' : 0, 'C' : 110, 'G' : 48, 'T' : 94},
'C' : {'A' : 110, 'C' : 0, 'G' : 118, 'T' : 48},
'G' : {'A' : 48, 'C' : 118, 'G' : 0, 'T' : 110},
'T' : {'A' : 94, 'C' : 48, 'G' : 110, 'T' : 0},
},
}
total_cost = 0
cost = 0
def seq_generator(s_init, gen_list):
'''
Take a root seq and generate the long seq
'''
new_seq = s_init
for idx in gen_list:
new_seq = new_seq[:idx+1] + new_seq + new_seq[idx+1:]
return new_seq
def input_parser(input_file):
'''
Parse input file to a sequence of string
'''
X,Y = '',''
X_init, Y_init = '',''
X_gen_list, Y_gen_list = [],[]
with open(input_file) as fp:
lines = fp.read().splitlines()
cnt = 0
for line in lines:
if not line.isdigit():
if cnt == 0:
X_init = line
elif cnt == 1:
Y_init = line
cnt += 1
else:
if cnt == 1:
X_gen_list.append(int(line))
elif cnt == 2:
Y_gen_list.append(int(line))
X = seq_generator(X_init, X_gen_list)
Y = seq_generator(Y_init, Y_gen_list)
return X,Y
def basicAlign(str_1, str_2):
N = len(str_1)
M = len(str_2)
A = [[0 for i in range(M+1)] for i in range(N+1)]
for i in range(M+1):
A[0][i] = COST_TABLE['delta'] * i
for j in range(N+1):
A[j][0] = COST_TABLE['delta'] * j
for i in range(1, N+1):
for j in range(1, M+1):
A[i][j] = min(
A[i-1][j-1] + COST_TABLE['alpha'][str_1[i-1]][str_2[j-1]],
A[i-1][j] + COST_TABLE['delta'],
A[i][j-1] + COST_TABLE['delta']
)
res_1 = ''
res_2 = ''
j = M
i = N
while(i >= 1 and j >= 1):
x_i = str_1[i-1]
y_j = str_2[j-1]
if A[i][j] == A[i-1][j-1] + COST_TABLE['alpha'][x_i][y_j]:
res_1 = x_i + res_1
res_2 = y_j + res_2
i -= 1
j -= 1
elif A[i][j] == A[i][j-1] + COST_TABLE['delta']:
res_1 = '_' + res_1
res_2 = y_j + res_2
j -= 1
else:
res_1 = x_i + res_1
res_2 = '_' + res_2
i -= 1
while(i >= 1):
res_1 = str_1[i - 1] + res_1
res_2 = "_" + res_2
i -= 1
while(j >= 1):
res_1 = "_" + res_1
res_2 = str_2[j - 1] + res_2
j -= 1
global cost
cost = A[N][M]
return [res_1, res_2]
def dcAlign(str_1, str_2):
M = len(str_1)
N = len(str_2)
if (M <= 2 or N <= 2):
tmp = basicAlign(str_1, str_2)
global total_cost
global cost
total_cost += cost
return tmp
f = [[0 for i in range(2)] for j in range(M+1)]
g = [[0 for i in range(2)] for j in range(M+1)]
mid = int(N / 2)
for i in range(M+1):
f[i][0] = COST_TABLE['delta'] * i
for j in range(1, mid+1):
f[0][1] = COST_TABLE['delta'] * j
for i in range(1, M+1):
f[i][1] = min(
COST_TABLE['alpha'][str_1[i-1]][str_2[j-1]] + f[i-1][0],
COST_TABLE['delta'] + f[i-1][1],
COST_TABLE['delta'] + f[i][0]
)
for i in range(M+1):
f[i][0] = f[i][1]
str_1 = str_1[::-1]
str_2 = str_2[::-1]
for i in range(M+1):
g[i][0] = COST_TABLE['delta'] * i
for j in range(mid+1, N+1):
g[0][1] = COST_TABLE['delta'] * (j - mid)
for i in range(1, M+1):
x_i = str_1[i-1]
y_j = str_2[j - (mid + 1)]
g[i][1] = min(
COST_TABLE['alpha'][x_i][y_j] + g[i-1][0],
COST_TABLE['delta'] + g[i-1][1],
COST_TABLE['delta'] + g[i][0]
)
for i in range(M+1):
g[i][0] = g[i][1]
str_1 = str_1[::-1]
str_2 = str_2[::-1]
q = -1
sum = sys.maxsize
for i in range(M+1):
if f[i][1] + g[M-i][1] < sum:
q = i
sum = f[i][1] + g[M-i][1]
left = dcAlign(str_1[0: q], str_2[0: mid])
right = dcAlign(str_1[q: M], str_2[mid: N])
return [left[0]+right[0], left[1]+right[1]]
def main(input_file):
input_X, input_Y = input_parser(input_file)
res = dcAlign(input_X, input_Y)
return res[0], res[1]
if __name__ == "__main__":
# parse input augments
if len(sys.argv) != 3:
raise Exception("Must have 3 Augments!")
input_file = sys.argv[1]
output_file = sys.argv[2]
# process info. of memory
process = psutil.Process()
# main function
tik = time.time()
alignment_1, alignment_2 = main(input_file)
tok = time.time()
# memory info.
memory_info = process.memory_info()
memory_consumed = int(memory_info.rss/1024)
with open(output_file, 'w') as fp:
fp.write(f"{total_cost}\n")
fp.write(f"{alignment_1}\n")
fp.write(f"{alignment_2}\n")
fp.write(f"{(tok-tik) * 1000}ms\n")
fp.write(f"{memory_consumed}KB\n")