def test_char_to_index(self): """should correctly recode a DNA & RNA seqs into indices""" seq = "TCAGRNY?-" expected = [0, 1, 2, 3, -9, -9, -9, -9, -9] indices = seq_to_indices(seq, self.dna_char_indices) assert_equal(indices, expected) seq = "UCAGRNY?-" indices = seq_to_indices(seq, self.rna_char_indices) assert_equal(indices, expected)
def test_python_vs_cython_fill_matrix(self): """python & cython fill_diversity_matrix give same answer""" s1 = seq_to_indices("RACGTACGTACN", self.dna_char_indices) s2 = seq_to_indices("AGTGTACGTACA", self.dna_char_indices) matrix1 = numpy.zeros((4, 4), float) _fill_diversity_matrix(matrix1, s1, s2) matrix2 = numpy.zeros((4, 4), float) pyx_fill_diversity_matrix(matrix2, s1, s2) assert_allclose(matrix1, matrix2)
def test_jc69_from_matrix(self): """compute JC69 from diversity matrix""" s1 = seq_to_indices("ACGTACGTAC", self.dna_char_indices) s2 = seq_to_indices("GTGTACGTAC", self.dna_char_indices) matrix = numpy.zeros((4, 4), float) _fill_diversity_matrix(matrix, s1, s2) total, p, dist, var = _jc69_from_matrix(matrix) self.assertEqual(total, 10.0) self.assertEqual(p, 0.2)
def test_fill_diversity_matrix_some(self): """make correct diversity matrix when not all chars valid""" s1 = seq_to_indices("RACGTACGTACN", self.dna_char_indices) s2 = seq_to_indices("AGTGTACGTACA", self.dna_char_indices) matrix = numpy.zeros((4, 4), float) # small diffs matrix.fill(0) _fill_diversity_matrix(matrix, s1, s2) assert_equal( matrix, numpy.array( [[2, 0, 0, 0], [1, 2, 0, 0], [0, 0, 2, 1], [0, 0, 0, 2]], float), )
def test_fill_diversity_matrix_all(self): """make correct diversity matrix when all chars valid""" s1 = seq_to_indices("ACGTACGTAC", self.dna_char_indices) s2 = seq_to_indices("GTGTACGTAC", self.dna_char_indices) matrix = numpy.zeros((4, 4), float) # self-self should just be an identity matrix _fill_diversity_matrix(matrix, s1, s1) assert_equal(matrix.sum(), len(s1)) assert_equal( matrix, numpy.array( [[2, 0, 0, 0], [0, 3, 0, 0], [0, 0, 3, 0], [0, 0, 0, 2]], float), ) # small diffs matrix.fill(0) _fill_diversity_matrix(matrix, s1, s2) assert_equal( matrix, numpy.array( [[2, 0, 0, 0], [1, 2, 0, 0], [0, 0, 2, 1], [0, 0, 0, 2]], float), )