def _assign(self, root): """Traverse tree and align sequences""" if root.getValue()[2] == None: if root.getLeft().getValue()[2] == None: self._assign(root.getLeft()) if root.getRight().getValue()[2] == None: self._assign(root.getRight()) # Get sequences seq1 = root.getLeft().getValue()[2][1] seq2 = root.getRight().getValue()[2][1] (a1, a2, e1, e2, score, gaps) = \ align.NeedlemanWunsch(seq1, seq2, self.smx, 0, 0) v1 = root.getLeft().getValue() v2 = root.getRight().getValue() nv1 = (v1[0], v1[1], v1[2], e1) nv2 = (v2[0], v2[1], v2[2], e2) root.getLeft().setValue(nv1) root.getRight().setValue(nv2) # Choose the sequence with least gaps if e1 < e2: nseq = a1 else: nseq = a2 v1 = root.getValue() nseq = (v1[0], nseq) nv1 = (v1[0], v1[1], nseq, v1[3]) root.setValue(nv1)
import align str1 = [8, 0, 247, 166, 75, 130, 0, 1] str2 = [8, 0, 167, 210, 75, 130, 0, 12] (nseq1, nseq2, edits1, edits2, score, gaps) = align.NeedlemanWunsch(str1, str2, None, 0, 0) print nseq1 print nseq2
def test_NeedlemanWunsch(): # Test 1 print("Test 1:") s1 = "CCC" s2 = "CCC" S = readScoringMatrix("DNA1.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 1) test_alignment(a1, a2, s1, s2, score, 3, S, 1) print("Passed\n") # Test 2 print("Test 2:") s1 = "CCC" s2 = "CTC" S = readScoringMatrix("DNA1.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 1) test_alignment(a1, a2, s1, s2, score, 2, S, 1) print("Passed\n") # Test 3 print("Test 3:") s1 = "CTC" s2 = "CCC" S = readScoringMatrix("DNA1.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 1) test_alignment(a1, a2, s1, s2, score, 2, S, 1) print("Passed\n") # Test 4 print("Test 4:") s1 = "CCC" s2 = "CC" S = readScoringMatrix("DNA1.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 1) test_alignment(a1, a2, s1, s2, score, 1, S, 1) print("Passed\n") # Test 5 print("Test 5:") s1 = "CC" s2 = "CCC" S = readScoringMatrix("DNA1.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 1) test_alignment(a1, a2, s1, s2, score, 1, S, 1) print("Passed\n") # Test 6 print("Test 6:") s1 = "AGTA" s2 = "ACA" S = readScoringMatrix("DNA2.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 2) test_alignment(a1, a2, s1, s2, score, 7, S, 2) print("Passed\n") # Test 7 print("Test 7:") s1 = "ACA" s2 = "AGTA" S = readScoringMatrix("DNA2.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 2) test_alignment(a1, a2, s1, s2, score, 7, S, 2) print("Passed\n") # Test 8 print("Test 8:") s1 = "WWWGGWWW" s2 = "WWWLWWW" S = readScoringMatrix("Blosum62.txt") score, a1, a2 = align.NeedlemanWunsch(s1, s2, S, 2) test_alignment(a1, a2, s1, s2, score, 60, S, 2) print("Passed\n")