Needleman-Wunsch演算法Python代碼實現

來源:互聯網
上載者:User

標籤:too   col   inpu   none   pre   reverse   logs   []   +=   

Needleman-Wunsch演算法是基於動態規划算法的序列比對演算法。

生信課上學的演算法,課下閑來無事,用Python實現一下。

def introduce():    print("*********************************************")    print("Welcome to use short sequence alignment tool!")    print("                Author : Chaz                ")    print("input1: long sequence!!!!!")    print("input2: short sequence!!!!!")    print("*********************************************")def matrix(seq1, seq2):    input_1 = []    input_2 = []    for i in range(len(seq1)):        input_1.append(i * -4)    for i in range(len(seq2)):        input_2.append(i * -4)    return input_1, input_2def matchBase(base1, base2):    if base1 == base2:        return "match"    else:        return "mismatch"def getScore(i, j, result_match):    num_s1 = "(" + str(j - 1) +"," +str(i - 1) + ")"    num_si = "(" + str(j - 1) +"," + str(i) + ")"    num_sj = "(" + str(j) + "," + str(i - 1) + ")"    if result_match == "match":        score1 = score[num_s1] + 4    else:        score1 = score[num_s1] - 3    score_i = score[num_si] - 4    score_j = score[num_sj] - 4    score_max = max(score1, score_i, score_j)    a = "(" + str(j) +"," +str(i) + ")"    if score_max == score1:        con[a] = score_max    elif score_max == score_i:        a_i[a] = score_max    else:        b_j[a] = score_max    score[a] = score_maxdef getPath(j, i,seq1, seq2,flag):    a = "(" + str(j) + "," + str(i) + ")"    score_res1 = con.get(a)    score_res2 = a_i.get(a)    score_res3 = b_j.get(a)    if score_res1 != None:        res1.append(seq1[i])        res2.append(seq2[j])        if j == 0:            return res2        res_j = getPath(j - 1, i - 1,seq1, seq2,flag)    elif score_res2:        res1.append("-")        res2.append(seq2[j])        if j == 0 :            return res2        res_j = getPath(j - 1, i ,seq1, seq2,flag)    else:        if score_res3 != None:            res2.append("-")            res1.append(seq1[i])            flag = False        else:            res2.append(seq2[j])            res1.append(seq1[i])            flag = True        if j == 0:            return res2        res_j = getPath(j,i- 1,seq1, seq2,flag)    return res_jdef run(seq1, seq2):    input_1, input_2 = matrix(seq1, seq2)    for i in range(len(input_1)):        s = "(0," + str(i) + ")"        score[s] = input_1[i]    for i in range(len(input_2)):        s = "(" + str(i) + ",0)"        score[s] = input_2[i]    for j in range(len(seq2) - 1):        j += 1        for i in range(len(seq1) - 1):            i += 1            result_match = matchBase(seq1[i],seq2[j])            getScore(i, j, result_match)    flag = True    res_j = getPath(len(seq2) - 1, len(seq1) - 1,seq1, seq2,flag)    return res_jif __name__ == "__main__":    flag = True    while(flag):        introduce()        seq1 = input("Please input long sequence:")        seq2 = input("Please input short sequence:")        #seq1 = "ATTC"        #seq2 = "TT"        # seq1 = "AAATTTCC"        # seq2 = "TTT"        res1 = []        res2 = []        con = {}        a_i = {}        b_j = {}        score = {}        seq1 = "0" + seq1.upper()        seq2 = "0" + seq2.upper()        res_j = run(seq1, seq2)        res_j.reverse()        res1.reverse()        print("  ".join(res1))        print("  ".join(res_j))        tmp = input("是否繼續判斷:[y/n]")        if tmp.strip() == "n":            flag = False        else:            flag = True

 

Needleman-Wunsch演算法Python代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.