本文整理汇总了Python中cogent.parse.tree.DndParser.indexByAttr方法的典型用法代码示例。如果您正苦于以下问题:Python DndParser.indexByAttr方法的具体用法?Python DndParser.indexByAttr怎么用?Python DndParser.indexByAttr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cogent.parse.tree.DndParser
的用法示例。
在下文中一共展示了DndParser.indexByAttr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RangeTreeTests
# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import indexByAttr [as 别名]
class RangeTreeTests(TestCase):
"""Tests of the RangeTree class."""
def setUp(self):
"""Make some standard objects to test."""
#Notes on sample string:
#
#1. trailing zeros are stripped in conversion to/from float, so result
# is only exactly the same without them.
#
#2. trailing chars (e.g. semicolon) are not recaptured in the output,
# so were deleted from original Newick-format string.
#
#3. whitespace is stripped, but is handy for formatting, so is stripped
# from original string before comparisons.
self.sample_tree_string = """
(
(
xyz:0.28124,
(
def:0.24498,
mno:0.03627)
A:0.1771)
B:0.0487,
abc:0.05925,
(
ghi:0.06914,
jkl:0.13776)
C:0.09853)
"""
self.t = DndParser(self.sample_tree_string, RangeNode)
self.i = self.t.indexByAttr('Name')
self.sample_string_2 = '((((a,b),c),(d,e)),((f,g),h))'
self.t2 = DndParser(self.sample_string_2, RangeNode)
self.i2 = self.t2.indexByAttr('Name')
self.sample_string_3 = '(((a,b),c),(d,e))'
self.t3 = DndParser(self.sample_string_3, RangeNode)
def test_str(self):
"""RangeNode should round-trip Newick string corrrectly."""
r = RangeNode()
self.assertEqual(str(r), '()')
#should work for tree with branch lengths set
t = DndParser(self.sample_tree_string, RangeNode)
expected = self.sample_tree_string.replace('\n', '')
expected = expected.replace(' ', '')
self.assertEqual(str(t), expected)
#self.assertEqual(t.getNewick(with_distances=True), expected)
#should also work for tree w/o branch lengths
t2 = DndParser(self.sample_string_2, RangeNode)
self.assertEqual(str(t2), self.sample_string_2)
def test_traverse(self):
"""RangeTree traverse should visit all nodes in correct order"""
t = self.t
i = self.i
#first, check that lengths are correct
#naked traverse() only does leaves; should be 6.
self.assertEqual(len(list(t.traverse())), 6)
#traverse() with self_before should count all nodes.
self.assertEqual(len(list(t.traverse(self_before=True))), 10)
#traverse() with self_after should have same count as self_before
self.assertEqual(len(list(t.traverse(self_after=True))), 10)
#traverse() with self_before and self_after should visit internal
#nodes multiple times
self.assertEqual(len(list(t.traverse(True,True))), 14)
#now, check that items are in correct order
exp = ['xyz','def','mno','abc','ghi','jkl']
obs = [i.Name for i in t.traverse()]
self.assertEqual(obs, exp)
exp = [None, 'B', 'xyz', 'A', 'def', 'mno', 'abc', 'C', 'ghi', 'jkl']
obs = [i.Name for i in t.traverse(self_before=True)]
self.assertEqual(obs, exp)
exp = ['xyz', 'def', 'mno', 'A', 'B', 'abc', 'ghi', 'jkl', 'C', None]
obs = [i.Name for i in t.traverse(self_after=True)]
self.assertEqual(obs, exp)
exp = [None, 'B', 'xyz', 'A', 'def', 'mno', 'A', 'B', 'abc', 'C', \
'ghi', 'jkl', 'C', None]
obs = [i.Name for i in t.traverse(self_before=True, self_after=True)]
self.assertEqual(obs, exp)
def test_indexByAttr(self):
"""RangeNode indexByAttr should make index using correct attr"""
t = self.t
i = self.i
#check that we got the right number of elements
#all elements unique, so should be same as num nodes
self.assertEqual(len(i), len(list(t.traverse(self_before=True))))
#check that we got everything
i_keys = i.keys()
i_vals = i.values()
for node in t.traverse(self_before=True):
#.........这里部分代码省略.........