使用python實現(xiàn)BLAST
最近在自學python,又用python實現(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)
運行結果:
無疑python對字符串的處理更加強大,語言也更加簡單,優(yōu)雅。比如最后逆序輸出alignment,java我是單獨寫了一個逆序函數(shù),而python只用一個語句就可以完成相同任務。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用scrapy ImagesPipeline爬取圖片資源的示例代碼
這篇文章主要介紹了使用scrapy ImagesPipeline爬取圖片資源的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Python sklearn中的.fit與.predict的用法說明
這篇文章主要介紹了Python sklearn中的.fit與.predict的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python中openpyxl和xlsxwriter對Excel的操作方法
這篇文章主要介紹了python中openpyxl和xlsxwriter對Excel的操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Python爬蟲實現(xiàn)抓取京東店鋪信息及下載圖片功能示例
這篇文章主要介紹了Python爬蟲實現(xiàn)抓取京東店鋪信息及下載圖片功能,涉及Python頁面請求、響應、解析等相關操作技巧,需要的朋友可以參考下2018-08-08編譯 pycaffe時報錯:fatal error: numpy/arrayobject.h沒有那個文件或目錄
這篇文章主要介紹了編譯 pycaffe時報錯:fatal error: numpy/arrayobject.h沒有那個文件或目錄,需要的朋友可以參考下2020-11-11Windows下安裝python2和python3多版本教程
這篇文章主要介紹下Windows(我用的Win10)環(huán)境下的python2.x 和 python3.x 的安裝,以及python2.x 與 python3.x 共存時的配置問題。2017-03-03