-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_alignment.py
185 lines (150 loc) · 6.63 KB
/
local_alignment.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
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
# -*- coding: utf-8 -*-
"""Local Alignment.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1S3OWs1MnW4yqS61VgznDc4_BlCqYxBod
"""
def capitalize(sequence):
cSeq=sequence.upper()
return cSeq #return Capitalized Sequence
def checkSeqValidty(seq1,seq2):
amino_acids=['C','S','T','P','A','G','N','D','E','Q','H','R','K','M','I','L','V','F','Y','W','-']
check_seq1_validty=[characters in amino_acids for characters in seq1 ]
check_seq2_validty=[characters in amino_acids for characters in seq2 ]
def isVaidSeq(list):
isValid=all(list)
return isValid
v1= isVaidSeq(check_seq1_validty)
if (v1== False):
print ("Sequence one is invalid protein sequence.")
v2=isVaidSeq(check_seq2_validty)
if(v2==False):
print("Sequence two is invalid protein sequence.")
if (v1 == False or v2==False):
return False
def substitution_matrix(aa1,aa2):
#we used BLOSUM62 MATRIX
blosum_matrix={('C','C'):9,('C','S'):-1,('C','T'):-1,('C','P'):-3,('C','A'):0, ('C','G'):-3,('C','N'):-3,('C','D'):-3,('C','E'):-4,('C','Q'):-3,('C','H'):-3,('C','R'):-3,('C','K'):-3,('C','M'):-1,('C','I'):-1,('C','L'):-1,('C','V'):-1,('C','F'):-2,('C','Y'):-2,('C','W'):-2,
('S','S'):4,('S','T'):-1,('S','P'):-1,('S','A'):1,('C','G'):0,('S','N'):1,('S','D'):0,('S','E'):0,('S','Q'):0,('S','H'):-1,('S','R'):-1,('S','K'):0,('S','M'):-1,('S','I'):-2,('S','L'):-2,('S','V'):-2,('S','F'):-2,('C','Y'):-2,('C','W'):-3,
('T','T'):4,('T','P'):1,('T','A'):-1,('T','G'):1,('T','N'):0,('T','D'):1,('T','E'):0,('T','Q'):0,('T','H'):0,('T','R'):-1,('T','K'):0,('T','M'):-1, ('T','I'):-1,('T','L'):-2,('T','V'):-2, ('T','F'):-2, ('T','Y'):-2,('T','W'):-3,
('P','P'):7, ('P','A'):-1, ('P','G'):-2,('P','N'):-2, ('P','D'):-1,('P','E'):-1, ('P','Q'):-1,('P','H'):-2,('P','R'):-2,('P','K'):-1,('P','M'):-2, ('P','I'):-3,('P','L'):-3,('P','V'):-2, ('P','F'):-4, ('P','Y'):-3,('P','W'):-4,
('A','A'):4, ('A','G'):0,('A','N'):-2, ('A','D'):-2,('A','E'):-1, ('A','Q'):-1,('A','H'):-2,('A','R'):-1,('A','K'):-1,('A','M'):-1, ('A','I'):-1,('A','L'):-1,('A','V'):0, ('A','F'):-2, ('A','Y'):-2,('A','W'):-3,
('G','G'):6,('G','N'):0, ('G','D'):-1,('G','E'):-2, ('G','Q'):-2,('G','H'):-2,('G','R'):-2,('G','K'):-2,('G','M'):-3, ('G','I'):-4,('G','L'):-4,('G','V'):-3, ('G','F'):-3, ('G','Y'):-3,('G','W'):-2,
('N','N'):6, ('N','D'):1,('N','E'):0, ('N','Q'):0,('N','H'):1,('N','R'):0,('N','K'):0,('N','M'):-2, ('N','I'):-3,('N','L'):-3,('N','V'):-3, ('N','F'):-3, ('N','Y'):-2,('N','W'):-4,
('D','D'):6,('D','E'):2, ('D','Q'):0,('D','H'):1,('D','R'):-2,('D','K'):-1,('D','M'):-3, ('D','I'):-3,('D','L'):-4,('D','V'):-3, ('D','F'):-3, ('D','Y'):-3,('D','W'):-4,
('E','E'):5, ('E','Q'):2,('E','H'):0,('E','R'):0,('E','K'):1,('E','M'):-2, ('E','I'):-3,('E','L'):-3,('E','V'):-2, ('E','F'):-3, ('E','Y'):-2,('E','W'):-3,
('Q','Q'):5,('Q','H'):0,('Q','R'):1,('Q','K'):1,('Q','M'):0,('Q','I'):-3,('Q','L'):-2,('Q','V'):-2, ('Q','F'):-3, ('Q','Y'):-1,('Q','W'):-2,
('H','H'):8,('H','R'):0,('H','K'):-1,('H','M'):-2, ('H','I'):-3,('H','L'):-3,('H','V'):-3,('H','F'):-1, ('H','Y'):-2,('H','W'):-2,
('R','R'):5, ('R','K'):2,('R','M'):-1, ('R','I'):-3,('R','L'):-2,('R','V'):-3, ('R','F'):-3, ('R','Y'):-2,('R','W'):-3,
('K','K'):5,('K','M'):-1, ('K','I'):-3,('K','L'):-2,('K','V'):-2, ('K','F'):-3, ('K','Y'):-2,('K','W'):-3,
('M','M'):5, ('M','I'):1,('M','L'):2,('M','V'):1, ('M','F'):0, ('M','Y'):-1,('M','W'):-1,
('I','I'):4,('I','L'):2,('I','V'):3, ('I','F'):0, ('I','Y'):-1,('I','W'):-3,
('L','L'):4,('L','V'):1, ('L','F'):0, ('L','Y'):-1,('L','W'):-2,
('V','V'):4, ('V','F'):-1, ('V','Y'):-1,('V','W'):-3,
('F','F'):6,('F','Y'):3,('F','W'):1,
('Y','Y'):7,('Y','W'):2,
('W','W'):11}
#capetalize amino acid
aa1=capitalize(aa1)
aa2=capitalize(aa2)
#getting the score between pair of amino acids
pair=('%s'%aa1,'%s'%aa2)
rPair=('%s'%aa2,'%s'%aa1)
#print (pair)
if pair in blosum_matrix:
score=blosum_matrix[aa1,aa2]
return score
elif rPair in blosum_matrix:
score=blosum_matrix[aa2,aa1]
return score
def local_alignment(seq1,seq2):
cSeq1=capitalize(seq1)
cSeq2=capitalize(seq2)
validation=checkSeqValidty(cSeq1,cSeq2)
if validation== False:
time.sleep(2)
sys.exit()
#getting the length for both sequences
col=len(seq1) #columns
row=len(seq2) #rows
#create a matrix
similarity_matrix=[[0 for i in range(col+1)]for j in range(row+1)]
#Alias
S=similarity_matrix
gapPenalty=int(input("Enter the value of the gap penalty: "))
#initializing the matrix
for i in range (row+1):
S[i][0]=0
for j in range (col+1):
S[0][j]=0
#filling the matrix
max_value=0
for i in range (1,row+1,1):
for j in range (1,col+1,1):
score=substitution_matrix('%s'%cSeq1[j-1],'%s'%cSeq2[i-1])
pair=(seq1[j-1],seq2[i-1])
up=S[i-1][j]+gapPenalty
left=S[i][j-1]+gapPenalty
if '-' in pair:
diagonal=S[i-1][j-1]+ gapPenalty
else:
diagonal=S[i-1][j-1]+score
#filling the cell by the max value fron up, left% diagonal
S[i][j]=max(up,left,diagonal,0)
#gettig the maximum value
if(S[i][j]>=max_value):
max_value=S[i][j]
mi=i
mj=j
print("The Maximum Score is %d" %max_value)
#traceback
fAlignment=''
sAlignment=''
matching_status=''
i=mi
j=mj
while S[i][j]>0:
score=substitution_matrix('%s'%cSeq1[j-1],'%s'%cSeq2[i-1])
up=S[i-1][j]+gapPenalty
left=S[i][j-1]+gapPenalty
pair=(cSeq1[j-1],cSeq2[i-1])
if '-' in pair:
diagonal=S[i-1][j-1]+ gapPenalty
else:
diagonal=S[i-1][j-1]+score
if diagonal==S[i][j]:
fAlignment+=cSeq1[j-1]
sAlignment+=cSeq2[i-1]
if cSeq1[j-1]==cSeq2[i-1]:
matching_status+=':'
i-=1
j-=1
else:
matching_status+='.'
elif up == S[i][j]:
fAlignment+='-'
matching_status+='.'
sAlignment+=cSeq2[i-1]
i-=1
else:
fAlignment+=seq1[j-1]
matching_status+='.'
sAlignment+='-'
j-=1
while j>0:
fAlignment+=cSeq1[j-1]
sAlignment+='-'
matching_status+='.'
j-=1
while i>0:
fAlignment+='-'
sAlignment+=cSeq2[i-1]
matching_status+='.'
i-=1
fAlignment=fAlignment[::-1]
matching_status=matching_status[::-1]
sAlignment=sAlignment[::-1]
print(fAlignment)
print(matching_status[i])
print(sAlignment)
local_alignment('ACTGH','ATCHN')