使用python實(shí)現(xiàn)BLAST
最近在自學(xué)python,又用python實(shí)現(xiàn)了一下BLAST。
這次更新了打分函數(shù)如下,空位罰分改為-5,但不區(qū)分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)
運(yùn)行結(jié)果:
無(wú)疑python對(duì)字符串的處理更加強(qiáng)大,語(yǔ)言也更加簡(jiǎn)單,優(yōu)雅。比如最后逆序輸出alignment,java我是單獨(dú)寫(xiě)了一個(gè)逆序函數(shù),而python只用一個(gè)語(yǔ)句就可以完成相同任務(wù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用scrapy ImagesPipeline爬取圖片資源的示例代碼
這篇文章主要介紹了使用scrapy ImagesPipeline爬取圖片資源的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Python sklearn中的.fit與.predict的用法說(shuō)明
這篇文章主要介紹了Python sklearn中的.fit與.predict的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06python中openpyxl和xlsxwriter對(duì)Excel的操作方法
這篇文章主要介紹了python中openpyxl和xlsxwriter對(duì)Excel的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03python學(xué)習(xí)之基于Python的人臉識(shí)別技術(shù)學(xué)習(xí)
面部識(shí)別技術(shù)的應(yīng)用越來(lái)越廣泛,它廣泛應(yīng)用于安全系統(tǒng)、人機(jī)交互、社交媒體、醫(yī)療保健等領(lǐng)域。本文介紹了基于Python的人臉識(shí)別技術(shù),感興趣的小伙伴可以參考閱讀2023-03-03python用for循環(huán)求和的方法總結(jié)
在本篇文章里小編給各位分享了關(guān)于python用for循環(huán)求和的方法以及相關(guān)實(shí)例代碼,需要的朋友們參考學(xué)習(xí)下。2019-07-07Pycharm github配置實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了Pycharm github配置實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Python爬蟲(chóng)實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能示例
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能,涉及Python頁(yè)面請(qǐng)求、響應(yīng)、解析等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08編譯 pycaffe時(shí)報(bào)錯(cuò):fatal error: numpy/arrayobject.h沒(méi)有那個(gè)文件或目錄
這篇文章主要介紹了編譯 pycaffe時(shí)報(bào)錯(cuò):fatal error: numpy/arrayobject.h沒(méi)有那個(gè)文件或目錄,需要的朋友可以參考下2020-11-11Windows下安裝python2和python3多版本教程
這篇文章主要介紹下Windows(我用的Win10)環(huán)境下的python2.x 和 python3.x 的安裝,以及python2.x 與 python3.x 共存時(shí)的配置問(wèn)題。2017-03-03