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_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_write_produces_end(self): nex = NexusReader() nex.read_string(""" begin assumptions; A = 1; end; """) assert "end;" in nex.write()
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_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_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_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_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
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