Back to index

python-biopython  1.60
test_pairwise2.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 
00007 import unittest
00008 
00009 from Bio import pairwise2
00010 
00011 
00012 class TestPairwiseGlobal(unittest.TestCase):
00013 
00014     def test_globalxx_simple(self):
00015         aligns = pairwise2.align.globalxx("GAACT", "GAT")
00016         self.assertEqual(len(aligns), 2)
00017         aligns.sort()
00018         seq1, seq2, score, begin, end = aligns[0]
00019         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00020         self.assertEqual(alignment, """\
00021 GAACT
00022 |||||
00023 G-A-T
00024   Score=3
00025 """)
00026         seq1, seq2, score, begin, end = aligns[1]
00027         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00028         self.assertEqual(alignment, """\
00029 GAACT
00030 |||||
00031 GA--T
00032   Score=3
00033 """)
00034 
00035 
00036 class TestPairwiseLocal(unittest.TestCase):
00037 
00038     def test_localxs(self):
00039         aligns = pairwise2.align.localxs("AxBx", "zABz", -0.1, 0)
00040         aligns.sort()
00041         seq1, seq2, score, begin, end = aligns[0]
00042         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00043         self.assertEqual(alignment, """\
00044 -AxBx
00045  |||
00046 zA-Bz
00047   Score=1.9
00048 """)
00049         seq1, seq2, score, begin, end = aligns[1]
00050         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00051         self.assertEqual(alignment, """\
00052 -AxBx
00053  ||||
00054 zA-Bz
00055   Score=1.9
00056 """)
00057 
00058 
00059 class TestPairwiseOpenPenalty(unittest.TestCase):
00060 
00061     def test_match_score_open_penalty1(self):
00062         aligns = pairwise2.align.globalms("AA", "A", 2.0, -1, -0.1, 0)
00063         self.assertEqual(len(aligns), 2)
00064         aligns.sort()
00065         seq1, seq2, score, begin, end = aligns[0]
00066         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00067         self.assertEqual(alignment, """\
00068 AA
00069 ||
00070 -A
00071   Score=1.9
00072 """)
00073         seq1, seq2, score, begin, end = aligns[1]
00074         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00075         self.assertEqual(alignment, """\
00076 AA
00077 ||
00078 A-
00079   Score=1.9
00080 """)
00081 
00082     def test_match_score_open_penalty2(self):
00083         aligns = pairwise2.align.globalms("GAA", "GA", 1.5, 0, -0.1, 0)
00084         self.assertEqual(len(aligns), 2)
00085         aligns.sort()
00086         seq1, seq2, score, begin, end = aligns[0]
00087         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00088         self.assertEqual(alignment, """\
00089 GAA
00090 |||
00091 G-A
00092   Score=2.9
00093 """)
00094         seq1, seq2, score, begin, end = aligns[1]
00095         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00096         self.assertEqual(alignment, """\
00097 GAA
00098 |||
00099 GA-
00100   Score=2.9
00101 """)
00102 
00103     def test_match_score_open_penalty3(self):
00104         aligns = pairwise2.align.globalxs("GAACT", "GAT", -0.1, 0)
00105         self.assertEqual(len(aligns), 1)
00106         seq1, seq2, score, begin, end = aligns[0]
00107         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00108         self.assertEqual(alignment, """\
00109 GAACT
00110 |||||
00111 GA--T
00112   Score=2.9
00113 """)
00114 
00115     def test_match_score_open_penalty4(self):
00116         aligns = pairwise2.align.globalms("GCT", "GATA", 1, -2, -0.1, 0)
00117         self.assertEqual(len(aligns), 1)
00118         seq1, seq2, score, begin, end = aligns[0]
00119         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00120         self.assertEqual(alignment, """\
00121 GCT-
00122 ||||
00123 GATA
00124   Score=-0.1
00125 """)
00126 
00127 
00128 class TestPairwiseExtendPenalty(unittest.TestCase):
00129 
00130     def test_extend_penalty1(self):
00131         aligns = pairwise2.align.globalxs("GACT", "GT", -0.2, -0.5)
00132         self.assertEqual(len(aligns), 1)
00133         seq1, seq2, score, begin, end = aligns[0]
00134         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00135         self.assertEqual(alignment, """\
00136 GACT
00137 ||||
00138 G--T
00139   Score=1.3
00140 """)
00141 
00142     def test_extend_penalty2(self):
00143         aligns = pairwise2.align.globalxs("GACT", "GT", -0.2, -1.5)
00144         self.assertEqual(len(aligns), 2)
00145         aligns.sort()
00146         seq1, seq2, score, begin, end = aligns[0]
00147         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00148         self.assertEqual(alignment, """\
00149 GACT
00150 ||||
00151 -G-T
00152   Score=0.6
00153 """)
00154         seq1, seq2, score, begin, end = aligns[1]
00155         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00156         self.assertEqual(alignment, """\
00157 GACT
00158 ||||
00159 G-T-
00160   Score=0.6
00161 """)
00162 
00163 
00164 class TestPairwisePenalizeExtendWhenOpening(unittest.TestCase):
00165 
00166     def test_penalize_extend_when_opening(self):
00167         aligns = pairwise2.align.globalxs("GACT", "GT", -0.2, -1.5, penalize_extend_when_opening=1)
00168         self.assertEqual(len(aligns), 1)
00169         seq1, seq2, score, begin, end = aligns[0]
00170         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00171         self.assertEqual(alignment, """\
00172 GACT
00173 ||||
00174 G--T
00175   Score=-1.2
00176 """)
00177 
00178 
00179 class TestPairwisePenalizeEndgaps(unittest.TestCase):
00180 
00181     def test_penalize_end_gaps(self):
00182         aligns = pairwise2.align.globalxs("GACT", "GT", -0.2, -0.8, penalize_end_gaps=0)
00183         self.assertEqual(len(aligns), 3)
00184         aligns.sort()
00185         seq1, seq2, score, begin, end = aligns[0]
00186         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00187         self.assertEqual(alignment, """\
00188 GACT
00189 ||||
00190 --GT
00191   Score=1
00192 """)
00193         seq1, seq2, score, begin, end = aligns[1]
00194         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00195         self.assertEqual(alignment, """\
00196 GACT
00197 ||||
00198 G--T
00199   Score=1
00200 """)
00201         seq1, seq2, score, begin, end = aligns[2]
00202         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00203         self.assertEqual(alignment, """\
00204 GACT
00205 ||||
00206 GT--
00207   Score=1
00208 """)
00209 
00210 
00211 class TestPairwiseSeparateGapPenalties(unittest.TestCase):
00212 
00213     def test_separate_gap_penalties1(self):
00214         aligns = pairwise2.align.localxd("GAT", "GTCT", -0.3, 0, -0.8, 0)
00215         self.assertEqual(len(aligns), 2)
00216         aligns.sort()
00217         seq1, seq2, score, begin, end = aligns[0]
00218         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00219         self.assertEqual(alignment, """\
00220 G-AT
00221 ||||
00222 GTCT
00223   Score=1.7
00224 """)
00225         seq1, seq2, score, begin, end = aligns[1]
00226         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00227         self.assertEqual(alignment, """\
00228 GA-T
00229 ||||
00230 GTCT
00231   Score=1.7
00232 """)
00233 
00234     def test_separate_gap_penalties2(self):
00235         aligns = pairwise2.align.localxd("GAT", "GTCT", -0.5, 0, -0.2, 0)
00236         self.assertEqual(len(aligns), 1)
00237         seq1, seq2, score, begin, end = aligns[0]
00238         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00239         self.assertEqual(alignment, """\
00240 GAT--
00241 |||
00242 G-TCT
00243   Score=1.8
00244 """)
00245 
00246 
00247 
00248 class TestPairwiseSeparateGapPenaltiesWithExtension(unittest.TestCase):
00249 
00250     def test_separate_gap_penalties_with_extension(self):
00251         aligns = pairwise2.align.localxd(list("GAAT"), list("GTCCT"), -0.1, 0, -0.1, -0.1, gap_char=["-"])
00252         self.assertEqual(len(aligns), 3)
00253         aligns.sort()
00254         seq1, seq2, score, begin, end = aligns[0]
00255         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00256         self.assertEqual(alignment, """\
00257 ['G', '-', 'A', 'A', 'T']
00258 |||||
00259 ['G', 'T', 'C', 'C', 'T']
00260   Score=1.9
00261 """)
00262         seq1, seq2, score, begin, end = aligns[1]
00263         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00264         self.assertEqual(alignment, """\
00265 ['G', 'A', '-', 'A', 'T']
00266 |||||
00267 ['G', 'T', 'C', 'C', 'T']
00268   Score=1.9
00269 """)
00270         seq1, seq2, score, begin, end = aligns[2]
00271         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00272         self.assertEqual(alignment, """\
00273 ['G', 'A', 'A', '-', 'T']
00274 |||||
00275 ['G', 'T', 'C', 'C', 'T']
00276   Score=1.9
00277 """)
00278 
00279 
00280 class TestPairwiseMatchDictionary(unittest.TestCase):
00281 
00282     match_dict = {
00283         ("A", "A") : 1.5,
00284         ("A", "T") : 0.5,
00285         ("T", "T") : 1.0
00286         }
00287 
00288     def test_match_dictionary1(self):
00289         aligns = pairwise2.align.localds("ATAT", "ATT", self.match_dict, -.5, 0)
00290         self.assertEqual(len(aligns), 2)
00291         aligns.sort()
00292         seq1, seq2, score, begin, end = aligns[0]
00293         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00294         self.assertEqual(alignment, """\
00295 ATAT
00296 ||||
00297 AT-T
00298   Score=3
00299 """)
00300         seq1, seq2, score, begin, end = aligns[1]
00301         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00302         self.assertEqual(alignment, """\
00303 ATAT
00304 |||
00305 ATT-
00306   Score=3
00307 """)
00308 
00309     def test_match_dictionary2(self):
00310         aligns = pairwise2.align.localds("ATAT", "ATT", self.match_dict, -1, 0)
00311         self.assertEqual(len(aligns), 1)
00312         seq1, seq2, score, begin, end = aligns[0]
00313         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00314         self.assertEqual(alignment, """\
00315 ATAT
00316 |||
00317 ATT-
00318   Score=3
00319 """)
00320 
00321     def test_match_dictionary3(self):
00322         aligns = pairwise2.align.localds("ATT", "ATAT", self.match_dict, -1, 0)
00323         self.assertEqual(len(aligns), 1)
00324         seq1, seq2, score, begin, end = aligns[0]
00325         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00326         self.assertEqual(alignment, """\
00327 ATT-
00328 |||
00329 ATAT
00330   Score=3
00331 """)
00332 
00333 
00334 class TestPairwiseOneCharacter(unittest.TestCase):
00335 
00336     def test_align_one_char1(self):
00337         aligns = pairwise2.align.localxs("abcde", "c", -0.3, -0.1)
00338         self.assertEqual(len(aligns), 1)
00339         seq1, seq2, score, begin, end = aligns[0]
00340         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00341         self.assertEqual(alignment, """\
00342 abcde
00343   |
00344 --c--
00345   Score=1
00346 """)
00347 
00348     def test_align_one_char2(self):
00349         aligns = pairwise2.align.localxs("abcce", "c", -0.3, -0.1)
00350         self.assertEqual(len(aligns), 2)
00351         aligns.sort()
00352         seq1, seq2, score, begin, end = aligns[0]
00353         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00354         self.assertEqual(alignment, """\
00355 abcce
00356    |
00357 ---c-
00358   Score=1
00359 """)
00360         seq1, seq2, score, begin, end = aligns[1]
00361         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00362         self.assertEqual(alignment, """\
00363 abcce
00364   |
00365 --c--
00366   Score=1
00367 """)
00368 
00369     def test_align_one_char3(self):
00370         aligns = pairwise2.align.globalxs("abcde", "c", -0.3, -0.1)
00371         self.assertEqual(len(aligns), 1)
00372         seq1, seq2, score, begin, end = aligns[0]
00373         alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
00374         self.assertEqual(alignment, """\
00375 abcde
00376 |||||
00377 --c--
00378   Score=0.2
00379 """)
00380 
00381 
00382 if __name__ == '__main__':
00383     runner = unittest.TextTestRunner(verbosity = 2)
00384     unittest.main(testRunner=runner)