def test_write(self): self.nex = NexusReader() self.nex.read_string(""" #NEXUS begin data; Dimensions ntax=2 nchar=2; Format datatype=standard gap=- symbols="01"; Matrix Harry 1- Simon 0? ; End; """) expected_patterns = [ '^begin data;$', '^\s+dimensions ntax=2 nchar=2;$', '^\s+format datatype=standard gap=- symbols="01";$', "^matrix$", "^Harry\s+1-", "^Simon\s+0\?$", '^\s+;$', '^end;$', ] written = self.nex.write() for expected in expected_patterns: assert re.search(expected, written, re.MULTILINE), \ 'Expected "%s"' % expected
class Test_DataHandler_NoMissingInSymbols(unittest.TestCase): """ Regression: Test that the missing or gap symbols are NOT in the SYMBOLS format string """ def test_write(self): self.nex = NexusReader() self.nex.read_string(""" #NEXUS begin data; Dimensions ntax=2 nchar=2; Format datatype=standard gap=- symbols="01"; Matrix Harry 1- Simon 0? ; End; """) expected_patterns = [ '^begin data;$', '^\s+dimensions ntax=2 nchar=2;$', '^\s+format datatype=standard gap=- symbols="01";$', "^matrix$", "^Harry\s+1-", "^Simon\s+0\?$", '^\s+;$', '^end;$', ] written = self.nex.write() for expected in expected_patterns: assert re.search(expected, written, re.MULTILINE), \ 'Expected "%s"' % expected
def test_write_produces_end(self): nex = NexusReader() nex.read_string(""" begin assumptions; A = 1; end; """) assert "end;" in nex.write()
def test_read_string(self): handle = open(os.path.join(EXAMPLE_DIR, 'example.nex')) data = handle.read() handle.close() nex = NexusReader() nex.read_string(data) assert 'data' in nex.blocks assert 'Simon' in nex.blocks['data'].matrix
def test_run_detranslate(self): nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example-translated.trees')) assert not nex.trees._been_detranslated nex = run_detranslate(nex) # should NOW be the same as tree 0 in example.trees other_tree_file = NexusReader( os.path.join(EXAMPLE_DIR, 'example.trees')) assert other_tree_file.trees[0] == nex.trees[0]
def test_read_file(nex, examples): """Test the Core functionality of NexusReader""" assert 'data' in nex.blocks assert 'Simon' in nex.blocks['data'].matrix with warnings.catch_warnings(record=True) as w: n = NexusReader() n.read_file(examples / 'example.nex') assert len(w) == 1
def test_write_to_file(self): tmp = NamedTemporaryFile(delete=False, suffix=".nex") tmp.close() nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex')) nex.write_to_file(tmp.name) assert os.path.isfile(tmp.name) n2 = NexusReader(tmp.name) assert n2.data.matrix == nex.data.matrix assert sorted(n2.data.taxa) == sorted(nex.data.taxa) os.unlink(tmp.name) # cleanup
def test_error_on_incorrect_dimensions(): with pytest.raises(NexusFormatException): NexusReader.from_string(""" #NEXUS begin taxa; dimensions ntax=2; taxlabels A B C; end; """)
def test_generic_readwrite(self): expected = [ "begin sets;", " A = 1;", " B = 2;", "end;", ] nex = NexusReader() nex.read_string("\n".join(expected)) for line in nex.sets.write().split("\n"): e = expected.pop(0).strip() assert line.strip() == e
def test_error_on_duplicate_taxa(): with pytest.raises(NexusFormatException): NexusReader.from_string(""" #NEXUS begin trees; translate 0 Tom, 1 Simon, 2 Tom; tree tree = (0,1,2) end; """)
def test_treelabel(self): nex = NexusReader() nex.read_string(""" #NEXUS begin trees; translate 0 Tom, 1 Simon, 2 Fred; tree TREEONE = (0,1,2); end; """) assert len(nex.trees.trees) == 1 assert nex.trees.trees == ['tree TREEONE = (0,1,2);']
def test_labelled_unrooted(self): nex = NexusReader() nex.read_string(""" #NEXUS begin trees; translate 0 Tom, 1 Simon, 2 Fred; tree unrooted [U] = (0,1,2); end; """) assert len(nex.trees.trees) == 1 assert nex.trees.trees == ['tree unrooted [U] = (0,1,2);']
def test_BEASTAscertainmentChecker_fail_with_non_empty(): # fail with two nex = NexusReader.from_string(""" #NEXUS Begin data; charstatelabels 1 CHAR_A_ascertainement, 2 CHAR_A, 3 CHAR_B_ascertainement, 4 CHAR_B ; Dimensions ntax=3 nchar=4; Format datatype=standard symbols="01" gap=-; Matrix A 0101 B 0101 C 10?1 ; begin assumptions; charset A = 1-2; charset B = 3-4; end; """) c = BEASTAscertainmentChecker(nex) assert len(c.errors) == 1 # should ONLY be one
class Test_TreeHandler_Regression_Mesquite(unittest.TestCase): """Regression: Test that we can parse MESQUITE taxa blocks""" def setUp(self): self.nex = NexusReader( os.path.join(REGRESSION_DIR, 'mesquite_formatted_branches.trees') ) def test_attributes(self): assert len(self.nex.trees.attributes) == 2 assert self.nex.trees.attributes[0] == \ """Title 'Trees from "temp.trees"';""" assert self.nex.trees.attributes[1] == \ """LINK Taxa = Untitled_Block_of_Taxa;""" def test_found_trees(self): assert self.nex.trees.ntrees == 1 def test_found_taxa(self): assert len(self.nex.trees.taxa) == 3 assert 'A' in self.nex.trees.taxa assert 'B' in self.nex.trees.taxa assert 'C' in self.nex.trees.taxa def test_was_translated(self): assert self.nex.trees.was_translated def test_translation(self): assert self.nex.trees.translators['1'] == 'A' assert self.nex.trees.translators['2'] == 'B' assert self.nex.trees.translators['3'] == 'C' def test_write(self): written = self.nex.write() assert """Title 'Trees from "temp.trees"';""" in written assert """LINK Taxa = Untitled_Block_of_Taxa;""" in written
def test_TreeHandler_MrBayes(regression): """ Test reading of treefile generated by a 2003-era MrBayes which, for some reason, have a tab between "tree\t<name>=(...)" """ nex = NexusReader(regression / 'mrbayes.trees') assert len(nex.trees.trees) == 1
def test_write_produces_end(): nex = NexusReader.from_string(""" begin assumptions; A = 1; end; """) assert "end;" in nex.write()
def setUp(self): self.nex = NexusReader() self.nex.read_string(""" #NEXUS Begin data; TITLE Untitled_Block_of_Taxa; LINK Taxa = Untitled_Block_of_Taxa; Dimensions ntax=2 nchar=2; Format datatype=standard gap=- symbols="01"; Matrix Harry 00 Simon 01 ; End; """)
def test_run_deltree(self): nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees')) new_nex = run_deltree('2', nex, do_print=False) assert len(new_nex.trees.trees) == 2 assert new_nex.trees.ntrees == 2 assert new_nex.trees[0].startswith('tree tree.0.1065.603220') assert new_nex.trees[1].startswith('tree tree.20000.883.396049')
def test_TreeHandler_Taxon_with_asterisk(regression): """ Test reading of treefile that contains a taxon with an asterisk in it. """ nex = NexusReader(regression / 'tree_with_asterisk_in_taxa.trees') assert len(nex.trees.trees) == 1 assert len(nex.trees.taxa) == 38 assert list(nex.trees.taxa)[35 - 1] == "*R35" # zero indexed, so 35-1
def test_TreeHandler_TranslateBlockMismatch(regression): """ Test that a warning is generated when a treefile has an incorrectly sized translate block. """ with pytest.raises(TranslateTableException): nex = NexusReader(regression / 'tree_translate_mismatch.trees') nex.trees.detranslate()
def test_generic_readwrite(self): expected = """Begin data; Dimensions ntax=4 nchar=2; Format datatype=standard symbols="01" gap=-; Matrix Harry 00 Simon 01 Betty 10 Louise 11 ; """.split("\n") nex = NexusReader() nex.handlers['data'] = GenericHandler nex.read_file(os.path.join(EXAMPLE_DIR, 'example.nex')) for line in nex.data.write().split("\n"): e = expected.pop(0).strip() assert line.strip() == e
def test_write_to_file(nex, tmpdir): tmp = pathlib.Path(str(tmpdir.join('f.nex'))) nex.write_to_file(tmp) assert tmp.is_file() n2 = NexusReader(tmp) assert n2.data.matrix == nex.data.matrix assert sorted(n2.data.taxa) == sorted(nex.data.taxa)
def test_ok_starting_with_zero(self): nex = NexusReader() nex.read_string(""" #NEXUS begin trees; translate 0 Tom, 1 Simon, 2 Fred; tree tree = (0,1,2) end; """) assert len(nex.trees.translators) == 3 assert '0' in nex.trees.translators assert '1' in nex.trees.translators assert '2' in nex.trees.translators
def test_detranslate(self): assert not self.nex.trees._been_detranslated self.nex.trees.detranslate() # should NOW be the same as tree 0 in example.trees other_tree_file = NexusReader( os.path.join(EXAMPLE_DIR, 'example.trees') ) assert other_tree_file.trees[0] == self.nex.trees[0]
def test_ok_starting_with_one(self): nex = NexusReader() nex.read_string(""" #NEXUS begin trees; translate 1 Tom, 2 Simon, 3 Fred; tree tree = (1,2,3) end; """) assert len(nex.trees.translators) == 3 assert '1' in nex.trees.translators assert '2' in nex.trees.translators assert '3' in nex.trees.translators
def test_read_gzip_file(nex_string, tmpdir): # first, MAKE a gzip file with gzip.open(str(tmpdir.join('f.gz')), 'wb') as h: h.write(nex_string.encode('utf8')) # test it's ok nex = NexusReader(str(tmpdir.join('f.gz'))) assert 'data' in nex.blocks assert 'Simon' in nex.blocks['data'].matrix
def test_regression(self): nex = NexusReader( os.path.join(REGRESSION_DIR, 'white_space_in_matrix.nex') ) assert nex.blocks['data'].nchar == 2 assert nex.blocks['data'].matrix['Harry'] == ['0', '0'] assert nex.blocks['data'].matrix['Simon'] == ['0', '1'] assert nex.blocks['data'].matrix['Betty'] == ['1', '0'] assert nex.blocks['data'].matrix['Louise'] == ['1', '1']
def test_run_resample_1(self): # shouldn't resample anything.. nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees')) new_nex = run_resample('1', nex, do_print=False) assert len(new_nex.trees.trees) == 3 assert new_nex.trees.ntrees == 3 assert new_nex.trees[0].startswith('tree tree.0.1065.603220') assert new_nex.trees[1].startswith('tree tree.10000.874.808756') assert new_nex.trees[2].startswith('tree tree.20000.883.396049')
class Test_DataHandler_Regression_Mesquite(unittest.TestCase): """Regression: Test that we can parse MESQUITE data blocks""" def setUp(self): self.nex = NexusReader() self.nex.read_string(""" #NEXUS Begin data; TITLE Untitled_Block_of_Taxa; LINK Taxa = Untitled_Block_of_Taxa; Dimensions ntax=2 nchar=2; Format datatype=standard gap=- symbols="01"; Matrix Harry 00 Simon 01 ; End; """) def test_attributes(self): assert len(self.nex.data.attributes) == 2 assert self.nex.data.attributes[0] == \ """TITLE Untitled_Block_of_Taxa;""" assert self.nex.data.attributes[1] == \ """LINK Taxa = Untitled_Block_of_Taxa;""" def test_write(self): expected_patterns = [ '^begin data;$', '^\s+TITLE Untitled_Block_of_Taxa;$', '^\s+LINK Taxa = Untitled_Block_of_Taxa;$', '^\s+dimensions ntax=2 nchar=2;$', '^\s+format datatype=standard gap=- symbols="01";$', "^matrix$", "^Harry\s+00", "^Simon\s+01$", '^\s+;$', '^end;$', ] written = self.nex.write() for expected in expected_patterns: assert re.search(expected, written, re.MULTILINE), \ 'Expected "%s"' % expected
def test_error_on_duplicate_block(): with warnings.catch_warnings(record=True): with pytest.raises(NexusFormatException): NexusReader.from_string(""" #NEXUS Begin data; Dimensions ntax=5 nchar=1; Format datatype=standard symbols="01" gap=-; Matrix Harry 1 ; Begin data; Dimensions ntax=5 nchar=1; Format datatype=standard symbols="01" gap=-; Matrix Harry 1 """)
def test_error_on_incorrect_dimensions(self): with self.assertRaises(NexusFormatException): NexusReader().read_string(""" #NEXUS begin taxa; dimensions ntax=2; taxlabels A B C; end; """)