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
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_write_produces_end(self): nex = NexusReader() nex.read_string(""" begin assumptions; A = 1; end; """) assert "end;" in nex.write()
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
class Test_TaxaHandler_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_taxa_block.nex') ) def test_taxa_block(self): for taxon in ['A', 'B', 'C']: assert taxon in self.nex.taxa # did we get the right number of taxa in the matrix? assert self.nex.taxa.ntaxa == len(self.nex.taxa.taxa) == 3 def test_taxa_block_attributes(self): assert 'taxa' in self.nex.blocks assert len(self.nex.taxa.attributes) == 2 assert 'TITLE Untitled_Block_of_Taxa;' in self.nex.taxa.attributes assert 'LINK Taxa = Untitled_Block_of_Taxa;' in \ self.nex.taxa.attributes def test_write(self): expected_patterns = [ '^begin taxa;$', '^\s+TITLE Untitled_Block_of_Taxa;$', '^\s+dimensions ntax=3;$', '^\s+taxlabels$', "^\s+\[1\] A$", "^\s+\[2\] B$", "^\s+\[3\] C$", '^;$', '^end;$', ] written = self.nex.write() for expected in expected_patterns: assert re.search(expected, written, re.MULTILINE), \ 'Expected "%s"' % expected
def test_write(examples): nex = NexusReader(examples / 'example.trees') assert nex.write().startswith('#NEXUS') assert examples.joinpath('example.trees').read_text( encoding='utf8') == nex.write()
def test_roundtrip(examples): nex = NexusReader(examples.joinpath('example2.nex')) _ = NexusReader.from_string(nex.write())
def test_write(self): nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees')) with open(os.path.join(EXAMPLE_DIR, 'example.trees')) as handle: text = handle.read() assert text == nex.write()