生物信息学原理作业第二弹:利用Needleman–Wunsch算法进行DNA序列全局比对。
具体原理:https://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm。
利用Needleman–Wunsch算法进行DNA序列全局比对
转载请保留出处!
贴上python代码:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 25 18:20:01 2017 @author: zxzhu
后需修改:
1.加命令行参数
2.给出多种比对结果
""" import numpy as np
import pandas as pd
sequence1 = 'AACGTACTCA'
sequence2 = 'TCGTACTCA'
s1 = ''
s2 = ''
gap = -4
score_matrix = pd.read_excel('score.xlsx') #score matrix
best_matrix = np.empty(shape= (len(sequence2)+1,len(sequence1)+1),dtype = int) def get_match_score(s1,s2):
score = score_matrix[s1][s2]
return score for i in range(len(sequence2)+1):
for j in range(len(sequence1)+1):
if i == 0:
best_matrix[i][j] = gap * j elif j == 0:
best_matrix[i][j] = gap *i
else:
match = get_match_score(sequence2[i-1],sequence1[j-1])
gap1_score = best_matrix[i-1][j]+gap
gap2_score = best_matrix[i][j-1]+gap
match_score = best_matrix[i-1][j-1]+match
best_matrix[i][j] = max(gap1_score,gap2_score,match_score)
print(best_matrix)
i,j = len(sequence2),len(sequence1)
while(i>0 or j>0):
match = get_match_score(sequence2[i-1],sequence1[j-1])
if i>0 and j>0 and best_matrix[i][j] == best_matrix[i-1][j-1]+match:
s1 += sequence1[j-1]
s2 += sequence2[i-1]
i-=1;j-=1
elif i>0 and best_matrix[i,j] == best_matrix[i-1,j]+gap:
s1+='-'
s2+=sequence2[i-1]
i-=1
else:
s1+=sequence1[j-1]
s2+='-'
j-=1
print(s1[::-1]+'\n'+s2[::-1])
后面会加入命令行。
多种结果这里只取了一种,这个问题有待解决。
如果有其他的方法我会及时添加。