class DirectionsMatrixTest(unittest.TestCase): def setUp(self): self.solver = NeedlemanWunschSolver( CellCostComputer(same_cost=5, diff_cost=-5, gap_penalty=-2)) @parameterized.expand([[[[[], [], []], [[], [Direction.DIAGONAL], [Direction.LEFT, Direction.DIAGONAL]], [[], [Direction.UP], [Direction.LEFT, Direction.UP]]], "AD", "AA"], [[[[], [], [], []], [[], [Direction.DIAGONAL], [Direction.LEFT, Direction.DIAGONAL], [Direction.LEFT]], [[], [Direction.UP], [Direction.LEFT, Direction.UP], [Direction.LEFT, Direction.UP]], [[], [Direction.DIAGONAL, Direction.UP], [Direction.DIAGONAL], [Direction.LEFT]]], "ADA", "AAS"], [[[[]]], "", ""], [[[[]], [[]], [[]]], "AA", ""], [[[[], [], []]], "", "AA"]]) def test_directions_matrix(self, expected_result, a_seq, b_seq): _, result = self.solver.compute_cost_direction_matrices(a_seq=a_seq, b_seq=b_seq) self.assertListEqual(result, expected_result)
class AlignmentPathsTests(unittest.TestCase): def setUp(self): self.solver = NeedlemanWunschSolver( CellCostComputer(same_cost=5, diff_cost=-5, gap_penalty=-2)) @parameterized.expand( [[0, {("", "")}, "", ""], [5, {("A", "A")}, "A", "A"], [3, {("AA", "A-"), ("AA", "-A")}, "AA", "A"], [1, {("A-S", "AA-"), ("-AS", "AA-"), ("AS-", "A-A")}, "AS", "AA"], [ -3, {("AAA-D", "--AS-"), ("A-AAD", "AS---"), ("AA-AD", "A-S--"), ("AAA-D", "A--S-"), ("AA-AD", "-AS--"), ("AAAD-", "A---S"), ("AAAD-", "--A-S"), ("AAAD-", "-A--S"), ("AAA-D", "-A-S-")}, "AAAD", "AS" ], [-8, {("----", "AAAD")}, "", "AAAD"], [ -10, {("AAAD-", "----S"), ("AA-AD", "--S--"), ("AAA-D", "---S-"), ("-AAAD", "S----"), ("A-AAD", "-S---")}, "AAAD", "S" ], [6, {("ABCD", "A--D")}, "ABCD", "AD"]]) def test(self, expected_score, expected_alignments, seq_a, seq_b): score, alignment = self.solver.generate_alignments(a_seq=seq_a, b_seq=seq_b) self.assertSetEqual(alignment, expected_alignments) self.assertEqual(score, expected_score) def test_max_paths_if_there_are_more_possible(self): self.__check_max_paths(2) def test_max_paths_if_there_are_less_possible(self): self.__check_max_paths(100) def test_max_paths_is_0_then_no_alignment(self): self.__check_max_paths(0) def __check_max_paths(self, max_paths): _, alignment = self.solver.generate_alignments("AADD", "S", max_paths) self.assertTrue(len(alignment) <= max_paths)
class GeneralNeedlemanWunschTests(unittest.TestCase): gap_penalty = -2 same_cost = 5 diff_cost = -5 def setUp(self): self.solver = NeedlemanWunschSolver( CellCostComputer(gap_penalty=self.gap_penalty, same_cost=self.same_cost, diff_cost=self.diff_cost)) def test_generate_initial_cost_matrix(self): a_seq_len = 2 b_seq_len = 3 result_matrix = self.solver.generate_initial_cost_matrix( a_seq_len=a_seq_len, b_seq_len=b_seq_len) gp = self.gap_penalty expected_matrix = [[0, gp, 2 * gp, 3 * gp], [gp, 0, 0, 0], [2 * gp, 0, 0, 0]] self.assertListEqual(result_matrix, expected_matrix) def test_generate_initial_directions_matrix(self): a_seq_len = 2 b_seq_len = 3 result_matrix = self.solver.generate_initial_directions_matrix( a_seq_len=a_seq_len, b_seq_len=b_seq_len) expected_matrix = [[[] for _ in range(b_seq_len + 1)]] * (a_seq_len + 1) self.assertListEqual(result_matrix, expected_matrix)
class CostMatrixTests(unittest.TestCase): def setUp(self): self.solver = NeedlemanWunschSolver( CellCostComputer(same_cost=5, diff_cost=-5, gap_penalty=-2)) @parameterized.expand([[[[0, -2, -4, -6], [-2, 5, 3, 1], [-4, 3, 10, 8], [-6, 1, 8, 15], [-8, -1, 6, 13]], "acca", "acc"], [ [[0]], "", "", ], [[[0, -2, -4, -6]], "", "ala"], [[[0], [-2], [-4], [-6]], "ala", ""], [[[0, -2, -4, -6, -8], [-2, -4, 3, 1, -1], [-4, 3, 1, -1, 6], [-6, 1, 8, 6, 4], [-8, -1, 6, 13, 11], [-10, -3, 4, 11, 9]], "alaaa", "laal"], [[[0, -2, -4], [-2, 5, 3], [-4, 3, 10], [-6, 1, 8], [-8, -1, 6]], "adad", "ad"]]) def test_cost_matrix(self, expected_matrix, a_seq, b_seq): result, _ = self.solver.compute_cost_direction_matrices(a_seq=a_seq, b_seq=b_seq) self.assertListEqual(result, expected_matrix)
def setUp(self): self.solver = NeedlemanWunschSolver( CellCostComputer(same_cost=5, diff_cost=-5, gap_penalty=-2))
def setUp(self): self.solver = NeedlemanWunschSolver( CellCostComputer(gap_penalty=self.gap_penalty, same_cost=self.same_cost, diff_cost=self.diff_cost))
def get_alignments(config: Dict[str, int], seq_a: str, seq_b: str): solver = NeedlemanWunschSolver( CellCostComputer(same_cost=config["SAME"], diff_cost=config["DIFF"], gap_penalty=config["GAP_PENALTY"])) return solver.generate_alignments(seq_a, seq_b, config["MAX_NUMBER_PATHS"])