本文整理汇总了Python中nexus.NexusReader类的典型用法代码示例。如果您正苦于以下问题:Python NexusReader类的具体用法?Python NexusReader怎么用?Python NexusReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NexusReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_find_unique_sites_2
def test_find_unique_sites_2(self):
nexus = NexusReader()
nexus.read_string("""Begin data;
Dimensions ntax=4 nchar=7;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 10000?-
Simon 1100011
Betty 1110000
Louise 1111000
;""")
unique = find_unique_sites(nexus)
# site 1 should NOT be in the uniques (3x1 and 1x0)
# - i.e. are we ignoring sites with ONE absent taxon
assert 1 not in unique
# these should also NOT be in unique
assert 0 not in unique
assert 2 not in unique
assert 4 not in unique # constant
# site 3 is a simple unique site - check we found it
assert 3 in unique
# sites 5 and 6 should also be unique
# - are we handling missing data appropriately?
assert 5 in unique
assert 6 in unique
示例2: setUp
def setUp(self):
self.nex1 = NexusReader()
self.nex1.read_string(
"""Begin data;
Dimensions ntax=2 nchar=1;
Format datatype=standard symbols="12" gap=-;
Matrix
Harry 1
Simon 2
;"""
)
self.nex2 = NexusReader()
self.nex2.read_string(
"""Begin data;
Dimensions ntax=2 nchar=1;
Format datatype=standard symbols="34" gap=-;
Matrix
Harry 3
Simon 4
;"""
)
self.nex3 = NexusReader()
self.nex3.read_string(
"""Begin data;
Dimensions ntax=3 nchar=1;
Format datatype=standard symbols="345" gap=-;
Matrix
Betty 3
Boris 4
Simon 5
;"""
)
示例3: test_read_string
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
示例4: test_write_to_file
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
示例5: test_notimplemented_exception
def test_notimplemented_exception(self):
with self.assertRaises(NotImplementedError):
nex = NexusReader()
nex.read_string(
"""Begin something;
Dimensions ntax=5 nchar=1;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 1
;""")
anonymise(nex)
示例6: test_incorrect_dimensions_warnings_nchar
def test_incorrect_dimensions_warnings_nchar(self):
with warnings.catch_warnings(record=True) as w:
nex = NexusReader()
nex.read_string(
"""Begin data;
Dimensions ntax=1 nchar=5;
Format datatype=standard symbols="01" gap=-;
Matrix
Harry 1
;""")
assert len(w) == 1, 'Expected 1 warning, got %r' % w
assert issubclass(w[-1].category, UserWarning)
assert "Expected" in str(w[-1].message)
assert nex.data.nchar == 1
示例7: test_labelled_unrooted
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);']
示例8: test_treelabel
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);']
示例9: Test_Binarise
class Test_Binarise(unittest.TestCase):
def setUp(self):
self.nex = NexusReader()
self.nex.read_string(
"""Begin data;
Dimensions ntax=3 nchar=2;
Format datatype=standard symbols="01" gap=-;
Charstatelabels
1 char1, 2 char2;
Matrix
Maori 14
Dutch 25
Latin 36
;""")
self.nex = binarise(self.nex)
def test_to_binary(self):
"""Test Nexus -> Binary: Two Character"""
expected = {
'char1_0': {"Maori": '1', "Dutch": "0", "Latin": "0"},
'char1_1': {"Maori": '0', "Dutch": "1", "Latin": "0"},
'char1_2': {"Maori": '0', "Dutch": "0", "Latin": "1"},
'char2_0': {"Maori": '1', "Dutch": "0", "Latin": "0"},
'char2_1': {"Maori": '0', "Dutch": "1", "Latin": "0"},
'char2_2': {"Maori": '0', "Dutch": "0", "Latin": "1"},
}
for char, data in expected.items():
for taxon, exp_value in data.items():
assert self.nex.data[char][taxon] == exp_value
def test_to_binary_nchar(self):
"""Test Nexus -> Binary: Number of Characters"""
assert len(self.nex.characters) == 6
def test_to_binary_symbollist(self):
"""Test Nexus -> Binary: Update Symbol List"""
# check symbol list was updated
assert len(self.nex.symbols) == 2
assert '1' in self.nex.symbols
assert '0' in self.nex.symbols
def test_to_binary_nexus(self):
"""Test Nexus -> Binary: Nexus"""
nexus = self.nex.make_nexus(interleave=False)
assert re.search("Dutch\s+010010", nexus)
assert re.search("Maori\s+100100", nexus)
assert re.search("Latin\s+001001", nexus)
示例10: Test_TreeHandler_Regression_Mesquite
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
示例11: Test_TaxaHandler_Regression_Mesquite
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) == 1
assert 'TITLE 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
示例12: test_generic_readwrite
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
示例13: test_ok_starting_with_zero
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
示例14: test_ok_starting_with_one
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
示例15: Test_TallyBySite
class Test_TallyBySite(unittest.TestCase):
def setUp(self):
self.nex = NexusReader()
self.nex.read_string(
"""Begin data;
Dimensions ntax=3 nchar=6;
Format datatype=standard symbols="12" gap=-;
Matrix
Harry 0111-?
Simon 0011-?
Elvis 0001-?
;"""
)
def test_errorcheck(self):
self.assertRaises(TypeError, tally_by_site, "I am a string")
self.assertRaises(TypeError, tally_by_site, 0)
def test_tally_by_site(self):
tally = tally_by_site(self.nex)
# 000
assert 'Harry' in tally[0]['0']
assert 'Simon' in tally[0]['0']
assert 'Elvis' in tally[0]['0']
# 100
assert 'Harry' in tally[1]['1']
assert 'Simon' in tally[1]['0']
assert 'Elvis' in tally[1]['0']
# 110
assert 'Harry' in tally[2]['1']
assert 'Simon' in tally[2]['1']
assert 'Elvis' in tally[2]['0']
# 111
assert 'Harry' in tally[3]['1']
assert 'Simon' in tally[3]['1']
assert 'Elvis' in tally[3]['1']
# ---
assert 'Harry' in tally[4]['-']
assert 'Simon' in tally[4]['-']
assert 'Elvis' in tally[4]['-']
# ???
assert 'Harry' in tally[5]['?']
assert 'Simon' in tally[5]['?']
assert 'Elvis' in tally[5]['?']