本文整理汇总了Python中nexus.NexusReader.write方法的典型用法代码示例。如果您正苦于以下问题:Python NexusReader.write方法的具体用法?Python NexusReader.write怎么用?Python NexusReader.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nexus.NexusReader
的用法示例。
在下文中一共展示了NexusReader.write方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test_TreeHandler_Regression_Mesquite
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
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
示例2: Test_TaxaHandler_Regression_Mesquite
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
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
示例3: Test_DataHandler_Regression_Mesquite
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
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
示例4: len
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
sys.exit("No trees found in found %s!" % nexusname)
if options.quiet is False:
print "%d trees found with %d translated taxa" % (nexus.trees.ntrees, len(nexus.trees.translators))
# Delete trees
if options.deltree:
nexus = run_deltree(options.deltree, nexus, options.quiet)
# Resample trees
if options.resample:
nexus = run_resample(options.resample, nexus, options.quiet)
# Randomly sample trees
if options.random:
nexus = run_random(options.random, nexus, options.quiet)
# remove comments
if options.removecomments:
nexus = run_removecomments(nexus, options.quiet)
# detranslate
if options.detranslate:
nexus = run_detranslate(nexus, options.quiet)
if newnexus is not None:
nexus.write_to_file(newnexus)
if options.quiet is False:
print "New nexus with %d trees written to %s" % (nexus.trees.ntrees, newnexus)
else:
print nexus.write()
示例5: OptionParser
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
__doc__ = """deinterleave - python-nexus tools v%(version)s
Converts an interleaved nexus to a simple nexus.
""" % {'version': VERSION,}
if __name__ == '__main__':
#set up command-line options
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog fudge.nex output.nex")
options, args = parser.parse_args()
try:
nexusname = args[0]
except IndexError:
print(__doc__)
print("Author: %s\n" % __author__)
parser.print_help()
sys.exit()
try:
newnexus = args[1]
except IndexError:
newnexus = None
nexus = NexusReader(nexusname)
if newnexus is not None:
nexus.write_to_file(newnexus)
print("New nexus written to %s" % newnexus)
else:
print(nexus.write())
示例6: Test_Manipulation_Data
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
class Test_Manipulation_Data(unittest.TestCase):
"""Test the manipulation of data in the NexusReader"""
def setUp(self):
self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))
def test_add_taxa(self):
assert self.nex.data.ntaxa == 4
self.nex.data.add_taxon('Elvis', ['1', '2'])
assert self.nex.data.ntaxa == 5
assert self.nex.data.matrix['Elvis'] == ['1', '2']
assert 'Elvis' in self.nex.data.taxa
assert 'Elvis' in self.nex.data.matrix
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=5 nchar=2;$',
'^\s+format datatype=standard symbols="01" gap=-;$',
'^matrix$',
'^Simon\s+01$',
'^Louise\s+11$',
'^Betty\s+10$',
'^Harry\s+00$',
'^Elvis\s+12$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected
def test_delete_taxa(self):
assert self.nex.data.ntaxa == 4
self.nex.data.del_taxon('Simon')
assert self.nex.data.ntaxa == 3
assert 'Simon' not in self.nex.data.taxa
assert 'Simon' not in self.nex.data.matrix
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=3 nchar=2;$',
'^\s+format datatype=standard symbols="01" gap=-;$',
'^matrix$',
'^Louise\s+11$',
'^Betty\s+10$',
'^Harry\s+00$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected
# should NOT be here
assert re.search('^Simon\s+01$', written, re.MULTILINE) == None, \
'Expected Taxon "Simon" to be Deleted'
def test_add_character(self):
pass
def test_delete_character(self):
pass
def test_edit_charlabels(self):
pass
# TreeHandler
# self.translators = {}
# self.attributes = []
# self.taxa = []
# self.trees = []
# ntrees
示例7: Test_Manipulation_Data
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
class Test_Manipulation_Data(unittest.TestCase):
"""Test the manipulation of data in the NexusReader"""
def setUp(self):
self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))
def test_add_taxa(self):
assert self.nex.data.ntaxa == 4
self.nex.data.add_taxon('Elvis', ['1', '2'])
assert self.nex.data.ntaxa == 5
assert self.nex.data.matrix['Elvis'] == ['1', '2']
assert 'Elvis' in self.nex.data.taxa
assert 'Elvis' in self.nex.data.matrix
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=5 nchar=2;$',
'^\s+format datatype=standard gap=- symbols="012";$',
'^matrix$',
'^Simon\s+01$',
'^Louise\s+11$',
'^Betty\s+10$',
'^Harry\s+00$',
'^Elvis\s+12$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), \
'Expected "%s"' % expected
def test_delete_taxa(self):
assert self.nex.data.ntaxa == 4
self.nex.data.del_taxon('Simon')
assert self.nex.data.ntaxa == 3
assert 'Simon' not in self.nex.data.taxa
assert 'Simon' not in self.nex.data.matrix
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=3 nchar=2;$',
'^\s+format datatype=standard gap=- symbols="01";$',
'^matrix$',
'^Louise\s+11$',
'^Betty\s+10$',
'^Harry\s+00$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), \
'Expected "%s"' % expected
# should NOT be here
assert re.search('^Simon\s+01$', written, re.MULTILINE) is None, \
'Expected Taxon "Simon" to be Deleted'
def test_add_character(self):
assert self.nex.data.nchar == 2
for taxon in self.nex.data.matrix:
self.nex.data.matrix[taxon].append('9')
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=4 nchar=3;$',
'^\s+format datatype=standard gap=- symbols="019";$',
'^matrix$',
'^Simon\s+019$',
'^Louise\s+119$',
'^Betty\s+109$',
'^Harry\s+009$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), \
'Expected "%s"' % expected
def test_delete_character(self):
assert self.nex.data.nchar == 2
for taxon in self.nex.data.matrix:
self.nex.data.matrix[taxon].pop()
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=4 nchar=1;$',
'^\s+format datatype=standard gap=- symbols="01";$',
'^matrix$',
'^Simon\s+0$',
'^Louise\s+1$',
'^Betty\s+1$',
'^Harry\s+0$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), \
'Expected "%s"' % expected
示例8: Test_DataHandler_SimpleNexusFormat
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
class Test_DataHandler_SimpleNexusFormat(unittest.TestCase):
expected = {
'Harry': ['0', '0'],
'Simon': ['0', '1'],
'Betty': ['1', '0'],
'Louise': ['1', '1'],
}
def setUp(self):
self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))
def test_block_find(self):
assert 'data' in self.nex.blocks
assert hasattr(self.nex, 'data')
assert self.nex.data == self.nex.data
def test_raw(self):
assert self.nex.data.block == [
'Begin data;',
'Dimensions ntax=4 nchar=2;',
'Format datatype=standard symbols="01" gap=-;',
'Matrix',
'Harry 00',
'Simon 01',
'Betty 10',
'Louise 11',
';'
]
def test_format_string(self):
# did we get the expected tokens in the format string?
expected = {'datatype': 'standard', 'gap': '-', 'symbols': '01'}
for k, v in expected.items():
assert self.nex.data.format[k] == v, \
"%s should equal %s and not %s" % (k, v, self.nex.data.format[k])
# did we get the right number of tokens?
assert len(self.nex.data.format) == len(expected)
def test_taxa(self):
# did we get the right taxa in the matrix?
for taxon in self.expected:
assert taxon in self.nex.data.matrix
# did we get the right number of taxa in the matrix?
assert self.nex.data.ntaxa == len(self.expected) == len(self.nex.data.taxa)
def test_characters(self):
# did we parse the characters properly?
assert self.nex.data.nchar == 2
for taxon, expected in self.expected.items():
assert self.nex.data.matrix[taxon] == expected
def test_iterable(self):
for taxon, block in self.nex.data:
assert block == self.expected[taxon]
def test_parse_format_line(self):
d = DataHandler()
f = d.parse_format_line('Format datatype=standard symbols="01" gap=-;')
assert f['datatype'] == 'standard', "Expected 'standard', but got '%s'" % f['datatype']
assert f['symbols'] == '01', "Expected '01', but got '%s'" % f['symbols']
assert f['gap'] == '-', "Expected 'gap', but got '%s'" % f['gap']
f = d.parse_format_line('FORMAT datatype=RNA missing=? gap=- symbols="ACGU" labels interleave;')
assert f['datatype'] == 'rna', "Expected 'rna', but got '%s'" % f['datatype']
assert f['missing'] == '?', "Expected '?', but got '%s'" % f['missing']
assert f['gap'] == '-', "Expected '-', but got '%s'" % f['gap']
assert f['symbols'] == 'acgu', "Expected 'acgu', but got '%s'" % f['symbols']
assert f['labels'] == True, "Expected <True>, but got '%s'" % f['labels']
assert f['interleave'] == True, "Expected <True>, but got '%s'" % f['interleave']
def test_write(self):
expected_patterns = [
'^begin data;$',
'^\s+dimensions ntax=4 nchar=2;$',
'^\s+format datatype=standard symbols="01" gap=-;$',
'^matrix$',
'^Simon\s+01$',
'^Louise\s+11$',
'^Betty\s+10$',
'^Harry\s+00$',
'^\s+;$',
'^end;$',
]
written = self.nex.write()
for expected in expected_patterns:
assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected
def test__load_characters(self):
for site, data in self.nex.data.characters.items():
for taxon, value in data.items():
assert value == self.expected[taxon][site]
def test_get_site(self):
for i in (0, 1):
site_data = self.nex.data.characters[i]
for taxon, value in site_data.items():
assert self.expected[taxon][i] == value
def test_incorrect_dimensions_warnings_ntaxa(self):
nex = NexusReader()
with warnings.catch_warnings(record=True) as w:
#.........这里部分代码省略.........
示例9: test_write
# 需要导入模块: from nexus import NexusReader [as 别名]
# 或者: from nexus.NexusReader import write [as 别名]
def test_write(self):
nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees'))
text = open(os.path.join(EXAMPLE_DIR, 'example.trees')).read()
assert text == nex.write()