这篇文章将为大家详细讲解有关使用python实现BLAST的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
这次更新了打分函数如下,空位罚分改为-5,但不区分gap open 和 gap extend。
'''''
@author: JiuYu
'''
def score(a,b):#scoring function
score=0
lst=['AC','GT','CA','TG']
if a==b:
score +=2
elif a+b in lst:
score += -5
else:
score += -7
return score
def BLAST(seq1,seq2):#Basic Local Alignment Search Tool
l1 = len(seq1)
l2 = len(seq2)
GAP =-5 #-5 for any gap
scores =[]
point =[]
for j in range(l2+1):
if j == 0:
line1=[0]
line2=[0]
for i in range(1,l1+1):
line1.append(GAP*i)
line2.append(2)
else:
line1=[]
line2=[]
line1.append(GAP*j)
line2.append(3)
scores.append(line1)
point.append(line2)
#fill the blank of scores and point
for j in range(1,l2+1):
letter2 = seq2[j-1]
for i in range(1,l1+1):
letter1 = seq1[i-1]
diagonal_score = score(letter1, letter2) + scores[j-1][i-1]
left_score = GAP + scores[j][i-1]
up_score = GAP + scores[j-1][i]
max_score = max(diagonal_score, left_score, up_score)
scores[j].append(max_score)
if scores[j][i] == diagonal_score:
point[j].append(1)
elif scores[j][i] == left_score:
point[j].append(2)
else:
point[j].append(3)
#trace back
alignment1=''
alignment2=''
i = l2
j = l1
print 'scores =',scores[i][j]
while True:
if point[i][j] == 0:
break
elif point[i][j] == 1:
alignment1 += seq1[j-1]
alignment2 += seq2[i-1]
i -= 1
j -= 1
elif point[i][j] == 2:
alignment1 += seq1[j-1]
alignment2 += '-'
j -= 1
else:
alignment1 += '-'
alignment2 += seq2[i-1]
i -= 1
#reverse alignment
alignment1 = alignment1[::-1]
alignment2 = alignment2[::-1]
print 'The best alignment:'
print alignment1
print alignment2
seq1=raw_input('Please input your first sequences:\n')
seq2=raw_input('input second sequences:\n')
BLAST(seq1, seq2)
运行结果:
无疑python对字符串的处理更加强大,语言也更加简单,优雅。比如最后逆序输出alignment,java我是单独写了一个逆序函数,而python只用一个语句就可以完成相同任务。
关于“使用python实现BLAST的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。