# coding=utf-8 from numpy import * __author__ = 'denis' matrix = [ [10, -1, -3, -4], [-1, 7, -5, 3], [-3, -5, 9, 0], [-4, -3, 0, 1] ] d = -5 def main(): s1 = 'AGACTAGTTAC' s2 = 'CGA---GACGT' alphabet = ['A', 'G', 'C', 'T'] d = 5 start_smoothing(alphabet, s1, s2) def start_smoothing(alphabet, s1, s2): f = arange(len(s1) * len(s2)).reshape(len(s1), len(s2)) for i in range(0, len(s1)): for j in range(0, len(s2)): f[i][j] = 0 for i in range(0, len(s1)): f[i][0] = d * i for j in range(0, len(s2)): f[0][j] = d * j for i in range(1, len(s1)): for j in range(1, len(s2)): if s1[i] is '-' or s2[j] is '-': f[i][j] = max([f[i-1][j] + d, f[i][j-1] + d] ) else: f[i][j] = max([f[i-1][j-1] + matrix[alphabet.index(s1[i])][alphabet.index(s2[j])], f[i-1][j] + d, f[i][j-1] + d] ) do(s1, s2, f, alphabet) def do(s1, s2, f, alphabet): alignment_s1 = '' alignment_s2 = '' i = len(s1) - 1 j = len(s2) - 1 while i > 0 and j > 0: score = f[i][j] score_diag = f[i-1][j-1] score_up = f[i][j-1] score_left = f[i-1][j] if s1[i] is not '-' and s2[j] is not '-' and score == score_diag + matrix[alphabet.index(s1[i])][alphabet.index(s2[j])]: alignment_s1 = s1[i] + alignment_s1 alignment_s2 = s2[j] + alignment_s2 i -= 1 j -= 1 elif score == score_left + d: alignment_s1 = s1[i] + alignment_s1 alignment_s2 = "-" + alignment_s2 i -= 1 elif score == score_up + d: alignment_s1 = "-" + alignment_s1 alignment_s2 = s2[j] + alignment_s2 j -= 1 while i > 0: alignment_s1 = s1[i] + alignment_s1 alignment_s2 = "-" + alignment_s2 i -= 1 while j > 0: alignment_s1 = "-" + alignment_s1 alignment_s2 = s2[j] + alignment_s2 j -= 1 pass if __name__ == '__main__': main()