def test_needleman_wunsch_dist_abs_nialls(self): """Test abydos.distance.NeedlemanWunsch.dist_abs (Nialls set).""" # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=2, skew=2) nw_vals = (5, 0, -2, 3, 1, 1, -2, -2, -1, -3, -3, -5, -3, -7, -7, -19) nw2 = NeedlemanWunsch(2, _sim_nw) for i in range(len(NIALL)): self.assertEqual(nw2.sim_score(NIALL[0], NIALL[i]), nw_vals[i])
def test_needleman_wunsch_dist_abs_nialls(self): """Test abydos.distance.NeedlemanWunsch.dist_abs (Nialls set).""" # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=2, skew=2) nw_vals = (5, 0, -2, 3, 1, 1, -2, -2, -1, -3, -3, -5, -3, -7, -7, -19) nw2 = NeedlemanWunsch(2, _sim_nw) for i in range(len(NIALL)): self.assertEqual(nw2.dist_abs(NIALL[0], NIALL[i]), nw_vals[i])
def test_needleman_wunsch_dist_abs(self): """Test abydos.distance.NeedlemanWunsch.dist_abs.""" self.assertEqual(NeedlemanWunsch().dist_abs('', ''), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual( NeedlemanWunsch(1, _sim_nw).dist_abs('GATTACA', 'GCATGCU'), 0 ) self.assertEqual( NeedlemanWunsch(5, _sim_wikipedia).dist_abs( 'AGACTAGTTAC', 'CGAGACGT' ), 16, ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) nw5 = NeedlemanWunsch(5, _sim_nw) self.assertEqual(nw5.dist_abs('CGATATCAG', 'TGACGSTGC'), -5) self.assertEqual(nw5.dist_abs('AGACTAGTTAC', 'TGACGSTGC'), -7) self.assertEqual(nw5.dist_abs('AGACTAGTTAC', 'CGAGACGT'), -15) # test wrapper self.assertEqual( needleman_wunsch('AGACTAGTTAC', 'CGAGACGT', 5, _sim_nw), -15 )
def test_gotoh_sim_score(self): """Test abydos.distance.Gotoh.sim_score.""" self.assertEqual(Gotoh().sim_score('', ''), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual( Gotoh(1, 1, _sim_nw).sim_score('GATTACA', 'GCATGCU'), 0) self.assertGreaterEqual( Gotoh(1, 0.5, _sim_nw).sim_score('GATTACA', 'GCATGCU'), NeedlemanWunsch(1, _sim_nw).sim_score('GATTACA', 'GCATGCU'), ) self.assertEqual( Gotoh(5, 5, _sim_wikipedia).sim_score('AGACTAGTTAC', 'CGAGACGT'), 16, ) self.assertGreaterEqual( Gotoh(5, 2, _sim_wikipedia).sim_score('AGACTAGTTAC', 'CGAGACGT'), NeedlemanWunsch(5, _sim_wikipedia).sim_score('AGACTAGTTAC', 'CGAGACGT'), ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) self.assertEqual( Gotoh(5, 5, _sim_nw).sim_score('CGATATCAG', 'TGACGSTGC'), -5) self.assertGreaterEqual( Gotoh(5, 2, _sim_nw).sim_score('CGATATCAG', 'TGACGSTGC'), NeedlemanWunsch(5, _sim_nw).sim_score('CGATATCAG', 'TGACGSTGC'), ) self.assertEqual( Gotoh(5, 5, _sim_nw).sim_score('AGACTAGTTAC', 'TGACGSTGC'), -7) self.assertGreaterEqual( Gotoh(5, 2, _sim_nw).sim_score('AGACTAGTTAC', 'TGACGSTGC'), NeedlemanWunsch(5, _sim_nw).sim_score('AGACTAGTTAC', 'TGACGSTGC'), ) self.assertEqual( Gotoh(5, 5, _sim_nw).sim_score('AGACTAGTTAC', 'CGAGACGT'), -15) self.assertGreaterEqual( Gotoh(5, 2, _sim_nw).sim_score('AGACTAGTTAC', 'CGAGACGT'), NeedlemanWunsch(5, _sim_nw).sim_score('AGACTAGTTAC', 'CGAGACGT'), ) # Test cases where one of the strings has length 1 self.assertAlmostEqual(Gotoh().sim_score('', 'a'), -1.0) self.assertAlmostEqual(Gotoh().sim_score('abc', 'a'), -0.4) self.assertAlmostEqual(Gotoh().sim_score('a', ''), -1.0) self.assertAlmostEqual(Gotoh().sim_score('a', 'abc'), -0.4) # Test wrapper self.assertEqual( gotoh('AGACTAGTTAC', 'CGAGACGT', 5, 5, _sim_wikipedia), 16)
def test_needleman_wunsch_sim_score(self): """Test abydos.distance.NeedlemanWunsch.sim_score.""" self.assertEqual(NeedlemanWunsch().sim_score('', ''), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual( NeedlemanWunsch(1, _sim_nw).sim_score('GATTACA', 'GCATGCU'), 0 ) self.assertEqual( NeedlemanWunsch(5, _sim_wikipedia).sim_score( 'AGACTAGTTAC', 'CGAGACGT' ), 16, ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) nw5 = NeedlemanWunsch(5, _sim_nw) self.assertEqual(nw5.sim_score('CGATATCAG', 'TGACGSTGC'), -5) self.assertEqual(nw5.sim_score('AGACTAGTTAC', 'TGACGSTGC'), -7) self.assertEqual(nw5.sim_score('AGACTAGTTAC', 'CGAGACGT'), -15) # test wrapper self.assertEqual( needleman_wunsch('AGACTAGTTAC', 'CGAGACGT', 5, _sim_nw), -15 )
def _sim_wikipedia(src, tar): """Return a similarity score for two DNA base pairs. Values copied from: https://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm Parameters ---------- src : str Source string for comparison tar : str Target string for comparison Returns ------- int: similarity of two DNA base pairs """ nw_matrix = { ('A', 'A'): 10, ('G', 'G'): 7, ('C', 'C'): 9, ('T', 'T'): 8, ('A', 'G'): -1, ('A', 'C'): -3, ('A', 'T'): -4, ('G', 'C'): -5, ('G', 'T'): -3, ('C', 'T'): 0, } return NeedlemanWunsch.sim_matrix( src, tar, nw_matrix, symmetric=True, alphabet='CGAT' )
def test_gotoh_sim_score_nialls(self): """Test abydos.distance.Gotoh.sim_score (Nialls set).""" # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=2, skew=2) nw_vals = (5, 0, -2, 3, 1, 1, -2, -2, -1, -3, -3, -5, -3, -7, -7, -19) g22 = Gotoh(2, 2, _sim_nw) for i in range(len(NIALL)): self.assertEqual(g22.sim_score(NIALL[0], NIALL[i]), nw_vals[i]) nw_vals2 = (5, 0, -2, 3, 1, 1, -2, -2, -1, -2, -3, -3, -2, -6, -6, -8) g21 = Gotoh(2, 1, _sim_nw) g205 = Gotoh(2, 0.5, _sim_nw) nw2 = NeedlemanWunsch(2, _sim_nw) for i in range(len(NIALL)): self.assertEqual(g21.sim_score(NIALL[0], NIALL[i]), nw_vals2[i]) self.assertGreaterEqual( g205.sim_score(NIALL[0], NIALL[i]), nw2.sim_score(NIALL[0], NIALL[i]), )
def test_gotoh_dist_abs_nialls(self): """Test abydos.distance.Gotoh.dist_abs (Nialls set).""" # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=2, skew=2) nw_vals = (5, 0, -2, 3, 1, 1, -2, -2, -1, -3, -3, -5, -3, -7, -7, -19) g22 = Gotoh(2, 2, _sim_nw) for i in range(len(NIALL)): self.assertEqual(g22.dist_abs(NIALL[0], NIALL[i]), nw_vals[i]) nw_vals2 = (5, 0, -2, 3, 1, 1, -2, -2, -1, -2, -3, -3, -2, -6, -6, -8) g21 = Gotoh(2, 1, _sim_nw) g205 = Gotoh(2, 0.5, _sim_nw) nw2 = NeedlemanWunsch(2, _sim_nw) for i in range(len(NIALL)): self.assertEqual(g21.dist_abs(NIALL[0], NIALL[i]), nw_vals2[i]) self.assertGreaterEqual( g205.dist_abs(NIALL[0], NIALL[i]), nw2.dist_abs(NIALL[0], NIALL[i]), )
def test_sim_matrix(self): """Test abydos.distance.NeedlemanWunsch.sim_matrix.""" self.assertEqual(NeedlemanWunsch.sim_matrix('', ''), 1) self.assertEqual(NeedlemanWunsch.sim_matrix('', 'a'), 0) self.assertEqual(NeedlemanWunsch.sim_matrix('a', ''), 0) self.assertEqual(NeedlemanWunsch.sim_matrix('a', 'a'), 1) self.assertEqual(NeedlemanWunsch.sim_matrix('abcd', 'abcd'), 1) self.assertEqual(NeedlemanWunsch.sim_matrix('abcd', 'dcba'), 0) self.assertEqual(NeedlemanWunsch.sim_matrix('abc', 'cba'), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual(_sim_wikipedia('A', 'C'), -3) self.assertEqual(_sim_wikipedia('G', 'G'), 7) self.assertEqual(_sim_wikipedia('A', 'A'), 10) self.assertEqual(_sim_wikipedia('T', 'A'), -4) self.assertEqual(_sim_wikipedia('T', 'C'), 0) self.assertEqual(_sim_wikipedia('A', 'G'), -1) self.assertEqual(_sim_wikipedia('C', 'T'), 0) self.assertRaises( ValueError, NeedlemanWunsch.sim_matrix, 'abc', 'cba', alphabet='ab' ) self.assertRaises( ValueError, NeedlemanWunsch.sim_matrix, 'abc', 'ba', alphabet='ab' ) self.assertRaises( ValueError, NeedlemanWunsch.sim_matrix, 'ab', 'cba', alphabet='ab' )
def test_gotoh_sim(self): """Test abydos.distance.Gotoh.sim.""" self.assertEqual(Gotoh().sim('', ''), 1.0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual(Gotoh(1, 1, _sim_nw).sim('GATTACA', 'GCATGCU'), 0) self.assertGreaterEqual( Gotoh(1, 0.5, _sim_nw).sim('GATTACA', 'GCATGCU'), NeedlemanWunsch(1, _sim_nw).sim('GATTACA', 'GCATGCU'), ) self.assertEqual( Gotoh(5, 5, _sim_wikipedia).sim('AGACTAGTTAC', 'CGAGACGT'), 0.19950186722152657, ) self.assertGreaterEqual( Gotoh(5, 2, _sim_wikipedia).sim('AGACTAGTTAC', 'CGAGACGT'), NeedlemanWunsch(5, _sim_wikipedia).sim('AGACTAGTTAC', 'CGAGACGT'), )
def test_gotoh_dist_abs(self): """Test abydos.distance.Gotoh.dist_abs.""" self.assertEqual(Gotoh().dist_abs('', ''), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual( Gotoh(1, 1, _sim_nw).dist_abs('GATTACA', 'GCATGCU'), 0) self.assertGreaterEqual( Gotoh(1, 0.5, _sim_nw).dist_abs('GATTACA', 'GCATGCU'), NeedlemanWunsch(1, _sim_nw).dist_abs('GATTACA', 'GCATGCU'), ) self.assertEqual( Gotoh(5, 5, _sim_wikipedia).dist_abs('AGACTAGTTAC', 'CGAGACGT'), 16) self.assertGreaterEqual( Gotoh(5, 2, _sim_wikipedia).dist_abs('AGACTAGTTAC', 'CGAGACGT'), NeedlemanWunsch(5, _sim_wikipedia).dist_abs('AGACTAGTTAC', 'CGAGACGT'), ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) self.assertEqual( Gotoh(5, 5, _sim_nw).dist_abs('CGATATCAG', 'TGACGSTGC'), -5) self.assertGreaterEqual( Gotoh(5, 2, _sim_nw).dist_abs('CGATATCAG', 'TGACGSTGC'), NeedlemanWunsch(5, _sim_nw).dist_abs('CGATATCAG', 'TGACGSTGC'), ) self.assertEqual( Gotoh(5, 5, _sim_nw).dist_abs('AGACTAGTTAC', 'TGACGSTGC'), -7) self.assertGreaterEqual( Gotoh(5, 2, _sim_nw).dist_abs('AGACTAGTTAC', 'TGACGSTGC'), NeedlemanWunsch(5, _sim_nw).dist_abs('AGACTAGTTAC', 'TGACGSTGC'), ) self.assertEqual( Gotoh(5, 5, _sim_nw).dist_abs('AGACTAGTTAC', 'CGAGACGT'), -15) self.assertGreaterEqual( Gotoh(5, 2, _sim_nw).dist_abs('AGACTAGTTAC', 'CGAGACGT'), NeedlemanWunsch(5, _sim_nw).dist_abs('AGACTAGTTAC', 'CGAGACGT'), ) # Test wrapper self.assertEqual( gotoh('AGACTAGTTAC', 'CGAGACGT', 5, 5, _sim_wikipedia), 16)
def test_needleman_wunsch_sim(self): """Test abydos.distance.NeedlemanWunsch.sim.""" self.assertEqual(NeedlemanWunsch().sim('', ''), 1.0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual( NeedlemanWunsch(1, _sim_nw).sim('GATTACA', 'GCATGCU'), 0) self.assertEqual( NeedlemanWunsch(5, _sim_wikipedia).sim('AGACTAGTTAC', 'CGAGACGT'), 0.19950186722152657, ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) nw5 = NeedlemanWunsch(5, _sim_nw) self.assertEqual(nw5.sim('CGATATCAG', 'TGACGSTGC'), 0) self.assertEqual(nw5.sim('AGACTAGTTAC', 'TGACGSTGC'), 0) self.assertEqual(nw5.sim('AGACTAGTTAC', 'CGAGACGT'), 0)
class NeedlemanWunschTestCases(unittest.TestCase): """Test Needleman-Wunsch functions. abydos.distance.NeedlemanWunsch """ cmp = NeedlemanWunsch() def test_needleman_wunsch_dist_abs(self): """Test abydos.distance.NeedlemanWunsch.dist_abs.""" self.assertEqual(needleman_wunsch('', ''), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual(needleman_wunsch('GATTACA', 'GCATGCU', 1, _sim_nw), 0) self.assertEqual( needleman_wunsch('AGACTAGTTAC', 'CGAGACGT', 5, _sim_wikipedia), 16 ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) self.assertEqual( needleman_wunsch('CGATATCAG', 'TGACGSTGC', 5, _sim_nw), -5 ) self.assertEqual( needleman_wunsch('AGACTAGTTAC', 'TGACGSTGC', 5, _sim_nw), -7 ) self.assertEqual( needleman_wunsch('AGACTAGTTAC', 'CGAGACGT', 5, _sim_nw), -15 ) def test_needleman_wunsch_dist_abs_nialls(self): """Test abydos.distance.NeedlemanWunsch.dist_abs (Nialls set).""" # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=2, skew=2) nw_vals = (5, 0, -2, 3, 1, 1, -2, -2, -1, -3, -3, -5, -3, -7, -7, -19) for i in range(len(NIALL)): self.assertEqual( needleman_wunsch(NIALL[0], NIALL[i], 2, _sim_nw), nw_vals[i] )
class GotohTestCases(unittest.TestCase): """Test Gotoh functions. abydos.distance.Gotoh """ cmp = Gotoh() nw = NeedlemanWunsch() def test_gotoh_dist_abs(self): """Test abydos.distance.Gotoh.dist_abs.""" self.assertEqual(gotoh('', ''), 0) # https://en.wikipedia.org/wiki/Needleman–Wunsch_algorithm self.assertEqual( self.cmp.dist_abs('GATTACA', 'GCATGCU', 1, 1, _sim_nw), 0) self.assertGreaterEqual( self.cmp.dist_abs('GATTACA', 'GCATGCU', 1, 0.5, _sim_nw), self.nw.dist_abs('GATTACA', 'GCATGCU', 1, _sim_nw), ) self.assertEqual( self.cmp.dist_abs('AGACTAGTTAC', 'CGAGACGT', 5, 5, _sim_wikipedia), 16, ) self.assertGreaterEqual( self.cmp.dist_abs('AGACTAGTTAC', 'CGAGACGT', 5, 2, _sim_wikipedia), self.nw.dist_abs('AGACTAGTTAC', 'CGAGACGT', 5, _sim_wikipedia), ) # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=5, skew=5) self.assertEqual( self.cmp.dist_abs('CGATATCAG', 'TGACGSTGC', 5, 5, _sim_nw), -5) self.assertGreaterEqual( self.cmp.dist_abs('CGATATCAG', 'TGACGSTGC', 5, 2, _sim_nw), self.nw.dist_abs('CGATATCAG', 'TGACGSTGC', 5, _sim_nw), ) self.assertEqual(gotoh('AGACTAGTTAC', 'TGACGSTGC', 5, 5, _sim_nw), -7) self.assertGreaterEqual( self.cmp.dist_abs('AGACTAGTTAC', 'TGACGSTGC', 5, 2, _sim_nw), self.nw.dist_abs('AGACTAGTTAC', 'TGACGSTGC', 5, _sim_nw), ) self.assertEqual( self.cmp.dist_abs('AGACTAGTTAC', 'CGAGACGT', 5, 5, _sim_nw), -15) self.assertGreaterEqual( self.cmp.dist_abs('AGACTAGTTAC', 'CGAGACGT', 5, 2, _sim_nw), self.nw.dist_abs('AGACTAGTTAC', 'CGAGACGT', 5, _sim_nw), ) # Test wrapper self.assertEqual( gotoh('AGACTAGTTAC', 'CGAGACGT', 5, 5, _sim_wikipedia), 16) def test_gotoh_dist_abs_nialls(self): """Test abydos.distance.Gotoh.dist_abs (Nialls set).""" # checked against http://ds9a.nl/nwunsch/ (mismatch=1, gap=2, skew=2) nw_vals = (5, 0, -2, 3, 1, 1, -2, -2, -1, -3, -3, -5, -3, -7, -7, -19) for i in range(len(NIALL)): self.assertEqual( self.cmp.dist_abs(NIALL[0], NIALL[i], 2, 2, _sim_nw), nw_vals[i], ) nw_vals2 = (5, 0, -2, 3, 1, 1, -2, -2, -1, -2, -3, -3, -2, -6, -6, -8) for i in range(len(NIALL)): self.assertEqual( self.cmp.dist_abs(NIALL[0], NIALL[i], 2, 1, _sim_nw), nw_vals2[i], ) self.assertGreaterEqual( self.cmp.dist_abs(NIALL[0], NIALL[i], 2, 0.5, _sim_nw), self.nw.dist_abs(NIALL[0], NIALL[i], 2, _sim_nw), )