def align3(s, score, gap_score= -1, debug=False): '''Alignment of three strings.''' x, y, z = s c = np.zeros(tuple(len(sp) + 1 for sp in s), dtype=object) #--------------------- # Initial conditions #--------------------- # Align pairs of strings c[0, :, :] = ra.global_alignment_matrix((s[1], s[2]), score=score, gap_score=gap_score, debug=debug) c[:, 0, :] = ra.global_alignment_matrix((s[0], s[2]), score=score, gap_score=gap_score, debug=debug) c[:, :, 0] = ra.global_alignment_matrix((s[0], s[1]), score=score, gap_score=gap_score, debug=debug) # Add gap penalty to each c-face for i in xrange(c.shape[1]): for j in xrange(1, c.shape[2]): c[0, i, j] = (c[0, i, j][0] + (i + j) * gap_score, (0, c[0, i, j][1][0], c[0, i, j][1][1])) for i in xrange(c.shape[2]): for j in xrange(1, c.shape[0]): c[j, 0, i] = (c[j, 0, i][0] + (i + j) * gap_score, (c[j, 0, i][1][0], 0, c[j, 0, i][1][1])) for i in xrange(c.shape[0]): for j in xrange(1, c.shape[1]): c[i, j, 0] = (c[i, j, 0][0] + (i + j) * gap_score, (c[i, j, 0][1][0], c[i, j, 0][1][1], 0)) #--------------------- # Dynamic programming #--------------------- g2 = 2 * gap_score for i, xi in enumerate(x, 1): for j, yj in enumerate(y, 1): s_xy = score(xi, yj) for k, zk in enumerate(z, 1): s_xz, s_yz = score(xi, zk), score(yj, zk) c[i, j, k] = max((c[i - 1, j - 1, k - 1][0] + s_xy + s_xz + s_yz, (i - 1, j - 1, k - 1)), (c[i, j - 1, k - 1][0] + s_yz + g2, (i, j - 1, k - 1)), (c[i - 1, j, k - 1][0] + s_xz + g2, (i - 1, j, k - 1)), (c[i - 1, j - 1, k][0] + s_xy + g2, (i - 1, j - 1, k)), (c[i - 1, j, k][0] + g2, (i - 1, j, k)), (c[i, j - 1, k][0] + g2, (i, j - 1, k)), (c[i, j, k - 1][0] + g2, (i, j, k - 1))) return c
ip, jp, kp, lp = c[i, j, k, l][1] s, t, u, v = \ (gap_symbol if ip == i else x[i - 1]) + s, \ (gap_symbol if jp == j else y[j - 1]) + t, \ (gap_symbol if kp == k else z[k - 1]) + u, \ (gap_symbol if lp == l else w[l - 1]) + v i, j, k, l = ip, jp, kp, lp # print i, j, k,l return s, t, u, v def test_align2(f, (i, j), match_score=0, mismatch_score= -1, gap_score= -1, debug=False): '''Two-string test.''' s = ro.fafsa_values(f) score = lambda x, y: match_score if x == y else mismatch_score print (s[i], s[j]) c = ra.global_alignment_matrix((s[i], s[j]), score, gap_score=gap_score) # print c print ro.join_list([c[-1, -1][0]] + list(ra.alignment_from_matrix((s[i], s[j]), c)), delimiter='\n') def test_align3(f, (i, j, k), match_score=0, mismatch_score= -1, gap_score= -1, debug=False): '''Three-string test.''' s = ro.fafsa_values(f) score = lambda x, y: match_score if x == y else mismatch_score print (s[i], s[j], s[k]) c = align3((s[i], s[j], s[k]), score, gap_score=gap_score) # print c print ro.join_list([c[-1, -1, -1][0]] + list(alignment3((s[i], s[j], s[k]), c)), delimiter='\n') def mult(f, match_score=0, mismatch_score= -1, gap_score= -1, debug=False): '''Main driver to solve this problem.''' s = ro.fafsa_values(f)