以下有两条序列,进行匹配,其中A对应T,C对应G。匹配规则如下匹配成功得五分,错配扣三分,空位扣四分,线性扣分。 序列1:CAATTCAGTTA 序列2:GGATCGA 请给出匹配分数最高的匹配。并用Python给出相应算法代码
时间: 2023-04-01 09:04:48 浏览: 56
我可以回答这个问题。最高匹配分数为1分,对应的匹配方式为:
序列1:C A A T T C A G T T A
序列2:G G A T C G A - - -
其中,匹配的部分用“-”表示空位,得分为1分。
以下是Python代码实现:
seq1 = "CAATTCAGTTA"
seq2 = "GGATCGA"
# 初始化匹配分数矩阵
score_matrix = [[ for j in range(len(seq2)+1)] for i in range(len(seq1)+1)]
# 填充矩阵
for i in range(1, len(seq1)+1):
for j in range(1, len(seq2)+1):
if seq1[i-1] == "C" and seq2[j-1] == "G":
score_matrix[i][j] = score_matrix[i-1][j-1] + 5
elif seq1[i-1] == "A" and seq2[j-1] == "T":
score_matrix[i][j] = score_matrix[i-1][j-1] + 5
elif seq1[i-1] == "A" and seq2[j-1] == "G":
score_matrix[i][j] = score_matrix[i-1][j-1] - 3
elif seq1[i-1] == "T" and seq2[j-1] == "C":
score_matrix[i][j] = score_matrix[i-1][j-1] - 3
else:
score_matrix[i][j] = max(score_matrix[i-1][j]-4, score_matrix[i][j-1]-4, score_matrix[i-1][j-1]-3)
# 找到最高分数及其位置
max_score =
max_i, max_j = ,
for i in range(1, len(seq1)+1):
for j in range(1, len(seq2)+1):
if score_matrix[i][j] > max_score:
max_score = score_matrix[i][j]
max_i, max_j = i, j
# 输出匹配结果
match1 = ""
match2 = ""
while max_i > and max_j > :
if seq1[max_i-1] == "C" and seq2[max_j-1] == "G":
match1 = "C" + match1
match2 = "G" + match2
max_i -= 1
max_j -= 1
elif seq1[max_i-1] == "A" and seq2[max_j-1] == "T":
match1 = "A" + match1
match2 = "T" + match2
max_i -= 1
max_j -= 1
elif score_matrix[max_i-1][max_j] >= score_matrix[max_i][max_j-1] and score_matrix[max_i-1][max_j] >= score_matrix[max_i-1][max_j-1]:
match1 = seq1[max_i-1] + match1
match2 = "-" + match2
max_i -= 1
elif score_matrix[max_i][max_j-1] >= score_matrix[max_i-1][max_j] and score_matrix[max_i][max_j-1] >= score_matrix[max_i-1][max_j-1]:
match1 = "-" + match1
match2 = seq2[max_j-1] + match2
max_j -= 1
else:
match1 = seq1[max_i-1] + match1
match2 = seq2[max_j-1] + match2
max_i -= 1
max_j -= 1
print("最高匹配分数为:", max_score)
print("匹配结果为:")
print(match1)
print(match2)
阅读全文