-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq30.py
More file actions
40 lines (32 loc) · 1010 Bytes
/
q30.py
File metadata and controls
40 lines (32 loc) · 1010 Bytes
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
from numpy import zeros
f = open("rosalind_edit.txt",'r') #input from file
seq = f.read().split(">") #extracting the strings from the input.
#print seq
inputv = []
for s in seq:
if(s!=""):
strings=s.split()
part1=strings[0]
part2=''.join(strings[1:])
inputv.append(part2)
#print inputv
lenc = len(inputv[0])+1
lenr = len(inputv[1])+1
str1 = inputv[0]
str2 = inputv[1]
mat = zeros((lenr,lenc),dtype=int) #creating a matrix of the order of lengths of the input string.
#print mat
for i in range(1,lenc):
mat[0][i] = i
#print mat
for i in range(1,lenr):
mat[i][0] = i
#print mat
for i in range(1,lenr): #To determine the edit distance.
for j in range(1,lenc):
if str2[i-1] != str1[j-1]:
mat[i,j] = min(mat[i - 1][j] + 1, mat[i][j - 1] + 1, mat[i - 1][j - 1] + 1)
else:
mat[i][j] = mat[i-1][j-1]
#print mat
print mat[-1][-1] #printing the edit diatance.