def test_newick_to_tree_node_convert_underscores(self): fh = StringIO('(_:0.1, _a, _b)__;') tree = _newick_to_tree_node(fh, convert_underscores=False) fh2 = StringIO() _tree_node_to_newick(tree, fh2) self.assertEqual(fh2.getvalue(), "('_':0.1,'_a','_b')'__';\n") fh2.close() fh.close()
def test_newick_to_tree_node_invalid_files(self): for invalid, error_fragments in self.invalid_newicks: fh = StringIO(invalid) with self.assertRaises(NewickFormatError) as cm: _newick_to_tree_node(fh) for frag in error_fragments: self.assertIn(frag, str(cm.exception)) fh.close()
def test_tree_node_to_newick(self): for tree, newicks in self.trees_newick_lists: newick = newicks[0] fh = StringIO() _tree_node_to_newick(tree, fh) self.assertEqual(newick, fh.getvalue()) fh.close()
def test_newick_to_tree_node_valid_files(self): for tree, newicks in self.trees_newick_lists: for newick in newicks: fh = StringIO(newick) read_tree = _newick_to_tree_node(fh) self._assert_equal(tree, read_tree) fh.close()
def test_roundtrip(self): for tree, newicks in self.trees_newick_lists: newick = newicks[0] fh = StringIO(newick) tree = _newick_to_tree_node(fh) fh2 = StringIO() _tree_node_to_newick(tree, fh2) fh2.seek(0) tree2 = _newick_to_tree_node(fh2) self.assertEqual(newick, fh2.getvalue()) self._assert_equal(tree, tree2) fh.close() fh2.close()
def test_roundtrip_read_write(self): for reader_fn, writer_fn, fhs in ( (_lsmat_to_dissimilarity_matrix, _dissimilarity_matrix_to_lsmat, self.dissim_fhs), (_lsmat_to_distance_matrix, _distance_matrix_to_lsmat, self.dist_fhs), ): for fh in fhs: # Read. fh.seek(0) lsmat1 = reader_fn(fh) # Write. out_fh = StringIO() writer_fn(lsmat1, out_fh) out_fh.seek(0) # Read. lsmat2 = reader_fn(out_fh) out_fh.close() self.assertEqual(lsmat1, lsmat2)
def test_write(self): for fn, objs, strs in ((_dissimilarity_matrix_to_lsmat, self.dissim_objs, self.dissim_strs), (_distance_matrix_to_lsmat, self.dist_objs, self.dist_strs)): for obj, str_ in zip(objs, strs): fh = StringIO() fn(obj, fh) obs = fh.getvalue() fh.close() self.assertEqual(obs, str_) # Test writing CSV (TSV is written above). for fn, cls in ((_dissimilarity_matrix_to_lsmat, DissimilarityMatrix), (_distance_matrix_to_lsmat, DistanceMatrix)): obj = cls(self.lsmat_3x3_data, ['a', 'b', 'c']) fh = StringIO() fn(obj, fh, delimiter=',') obs = fh.getvalue() fh.close() self.assertEqual(obs, LSMat_3x3_CSV)
def test_roundtrip_read_write(self): for reader_fn, writer_fn, fhs in ((_lsmat_to_dissimilarity_matrix, _dissimilarity_matrix_to_lsmat, self.dissim_fhs), (_lsmat_to_distance_matrix, _distance_matrix_to_lsmat, self.dist_fhs)): for fh in fhs: # Read. fh.seek(0) lsmat1 = reader_fn(fh) # Write. out_fh = StringIO() writer_fn(lsmat1, out_fh) out_fh.seek(0) # Read. lsmat2 = reader_fn(out_fh) out_fh.close() self.assertEqual(lsmat1, lsmat2)
def test_write(self): for fn, objs, strs in ( (_dissimilarity_matrix_to_lsmat, self.dissim_objs, self.dissim_strs), (_distance_matrix_to_lsmat, self.dist_objs, self.dist_strs), ): for obj, str_ in zip(objs, strs): fh = StringIO() fn(obj, fh) obs = fh.getvalue() fh.close() self.assertEqual(obs, str_) # Test writing CSV (TSV is written above). for fn, cls in ( (_dissimilarity_matrix_to_lsmat, DissimilarityMatrix), (_distance_matrix_to_lsmat, DistanceMatrix), ): obj = cls(self.lsmat_3x3_data, ["a", "b", "c"]) fh = StringIO() fn(obj, fh, delimiter=",") obs = fh.getvalue() fh.close() self.assertEqual(obs, LSMat_3x3_CSV)
def test_newick_sniffer_invalid_files(self): for invalid, _ in self.invalid_newicks: fh = StringIO(invalid) self.assertEqual(_newick_sniffer(fh), (False, {})) fh.close()
def test_newick_sniffer_valid_files(self): for _, newicks in self.trees_newick_lists: for newick in newicks: fh = StringIO(newick) self.assertEqual(_newick_sniffer(fh), (True, {})) fh.close()