forked from Sahra-Zhou/570FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_3.py
More file actions
193 lines (160 loc) · 5.47 KB
/
Copy pathbasic_3.py
File metadata and controls
193 lines (160 loc) · 5.47 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
import time
import psutil
import sys
#Define mismatch cost:
mismatch_dict = {
('A','C'):110,
('C','A'):110,
('A','G'):48,
('G','A'):48,
('A','T'):94,
('T','A'):94,
('C','G'):118,
('G','C'):118,
('C','T'):48,
('T','C'):48,
('G','T'):110,
('T','G'):110,
('A','A'):0,
('C','C'):0,
('G','G'):0,
('T','T'):0
}
def main():
input = sys.argv[1]
output = sys.argv[2]
file = open(input, "r")
lines = file.readlines()
str1 = lines[0].strip()
n1 = []
str2 = ""
n2 = []
parseStr2 = False
for l in lines[1:]:
l = l.strip()
if l.isalpha():
str2 = l
parseStr2 = True
continue
if parseStr2:
n2.append(int(l))
else:
n1.append(int(l))
str1 = generateStr(str1, n1)
str2 = generateStr(str2, n2)
time, score, a1, a2 = time_wrapper(str1, str2)
memory = process_memory()
file_output(output, score, a1, a2, time, memory)
return 0
def generateStr(str, n):
for i in n:
str = str[:i+1]+str+str[i+1:]
return str
def print_alignment(aligned_s1,aligned_s2):
num_to_print=len(aligned_s1)
for i in range(num_to_print):
print(aligned_s1[i],end="")
print()
for i in range(num_to_print):
if aligned_s1[i] == aligned_s2[i]:
print("|",end="")
else:
print(" ",end="")
print()
for i in range(num_to_print):
print(aligned_s2[i],end="")
#output txt
def file_output(f, score, aligned_s1, aligned_s2, time, memory):
with open(f, "+w") as file:
file.write(str(score))
file.write("\n")
num_to_print=len(aligned_s1)
for i in range(num_to_print):
file.write(aligned_s1[i])
file.write("\n")
for i in range(num_to_print):
file.write(aligned_s2[i])
file.write("\n")
file.write(str(time))
file.write("\n")
file.writelines(str(memory))
file.close()
#calculate memory usage:
def process_memory():
process = psutil.Process()
memory_info = process.memory_info()
memory_consumed = int(memory_info.rss/1024)
return memory_consumed
#calculate time usage:
def time_wrapper(s1, s2):
start_time = time.time()
score, a1, a2 = basic_dp(s1, s2) #solution func
end_time = time.time()
time_taken = (end_time - start_time)*1000
return time_taken, score, a1, a2
#solution function
def basic_dp(s1, s2, match_score=0, gap_score=30):
nrows = len(s1)+2
ncols = len(s2)+2
scores = [[ 0 for _ in range(ncols)] for _ in range(nrows)]
aligned = [[ 0 for _ in range(ncols)] for _ in range(nrows)]
scores[0][0] = ""
scores[1][0] = '_'
scores[0][1] = '_'
aligned[0][0] = ""
aligned[1][0] = '_'
aligned[0][1] = '_'
for i in range(len(s2)):
scores[0][i+2] = s2[:i+1]
aligned[0][i+2] = s2[:i+1]
for j in range(len(s1)):
scores[j+2][0] = s1[:j+1]
aligned[j+2][0] = s1[:j+1]
for s2_index in range(1, ncols):
if s2_index == 1:
scores[1][s2_index] = 0
aligned[1][s2_index] = ("","")
else:
s2_part = scores[0][s2_index]
scores[1][s2_index] = gap_score*len(s2_part)
aligned[1][s2_index] = ("".join(["_" for i in range(len(s2_part))]),s2_part)
for s1_index in range(1, nrows):
if s1_index == 1:
scores[s1_index][1] = 0
aligned[s1_index][1] = ("","")
else:
s1_part = scores[s1_index][0]
scores[s1_index][1] = gap_score*len(s1_part)
aligned[s1_index][1] = (s1_part, "".join(["_" for i in range(len(s1_part))]))
for i in range(2, nrows):
for j in range(2, ncols):
opt1_s1_index = i-1
opt1_s2_index = j-1
align_score = match_score if scores[i][0][-1] == scores[0][j][-1] else mismatch_dict[(scores[i][0][-1],scores[0][j][-1])]
score_opt1 = scores[opt1_s1_index][opt1_s2_index] + align_score
s1_aligned_opt1 = aligned[opt1_s1_index][opt1_s2_index][0]+aligned[i][0][-1]
s2_aligned_opt1 = aligned[opt1_s1_index][opt1_s2_index][1]+aligned[0][j][-1]
opt2_s1_index = i-1
opt2_s2_index = j
score_opt2 = scores[opt2_s1_index][opt2_s2_index] + gap_score
s1_aligned_opt2 = aligned[opt2_s1_index][opt2_s2_index][0]+aligned[i][0][-1]
s2_aligned_opt2 = aligned[opt2_s1_index][opt2_s2_index][1]+"_"
opt3_s1_index = i
opt3_s2_index = j-1
score_opt3 = scores[opt3_s1_index][opt3_s2_index] + gap_score
s1_aligned_opt3 = aligned[opt3_s1_index][opt3_s2_index][0]+"_"
s2_aligned_opt3 = aligned[opt3_s1_index][opt3_s2_index][1]+aligned[0][j][-1]
scores[i][j] = min(score_opt1,score_opt2,score_opt3)
if min(score_opt1,score_opt2,score_opt3) == score_opt1:
aligned[i][j] = (s1_aligned_opt1,s2_aligned_opt1)
elif min(score_opt1,score_opt2,score_opt3) == score_opt2:
aligned[i][j] = (s1_aligned_opt2,s2_aligned_opt2)
else:
aligned[i][j] = (s1_aligned_opt3,s2_aligned_opt3)
final_score = scores[nrows-1][ncols-1]
aligned_s1 = aligned[nrows-1][ncols-1][0]
aligned_s2 = aligned[nrows-1][ncols-1][1]
return final_score, aligned_s1, aligned_s2
#main:
if __name__=="__main__":
main()