本文整理汇总了Python中tree.Node.li方法的典型用法代码示例。如果您正苦于以下问题:Python Node.li方法的具体用法?Python Node.li怎么用?Python Node.li使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Node
的用法示例。
在下文中一共展示了Node.li方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import li [as 别名]
def parse(data, ttable=None, treename=None):
"""
Parse a newick string.
Args:
data: Any file-like object that can be coerced into shlex, or
a string (converted to StringIO)
ttable (dict): Mapping of node labels in the newick string
to other values.
Returns:
Node: The root node.
"""
from tree import Node
if type(data) in types.StringTypes:
data = StringIO(data)
start_pos = data.tell()
tokens = Tokenizer(data)
node = None; root = None
lp=0; rp=0; rooted=1
previous = None
ni = 0 # node id counter (preorder) - zero-based indexing
li = 0 # leaf index counter
ii = 0 # internal node index counter
pi = 0 # postorder sequence
while 1:
token = tokens.get_token()
#print token,
if token == ';' or token == tokens.eof:
assert lp == rp, \
"unbalanced parentheses in tree description: (%s, %s)" \
% (lp, rp)
break
# internal node
elif token == '(':
lp = lp+1
newnode = Node()
newnode.ni = ni; ni += 1
newnode.isleaf = False
newnode.ii = ii; ii += 1
newnode.treename = treename
if node:
if node.children: newnode.left = node.children[-1].right+1
else: newnode.left = node.left+1
node.add_child(newnode)
else:
newnode.left = 1; newnode.right = 2
newnode.right = newnode.left+1
node = newnode
elif token == ')':
rp = rp+1
node = node.parent
node.pi = pi; pi += 1
if node.children:
node.right = node.children[-1].right + 1
elif token == ',':
node = node.parent
if node.children:
node.right = node.children[-1].right + 1
# branch length
elif token == ':':
token = tokens.get_token()
if token == '[':
node.length_comment = tokens.parse_embedded_comment()
token = tokens.get_token()
if not (token == ''):
try: brlen = float(token)
except ValueError:
raise ValueError, ("invalid literal for branch length, "
"'%s'" % token)
else:
raise 'NewickError', \
'unexpected end-of-file (expecting branch length)'
node.length = brlen
# comment
elif token == '[':
node.comment = tokens.parse_embedded_comment()
if node.comment[0] == '&':
# metadata
meta = META.findall(node.comment[1:])
if meta:
node.meta = {}
for k, v in meta:
v = eval(v.replace('{','(').replace('}',')'))
node.meta[k] = v
# leaf node or internal node label
else:
if previous != ')': # leaf node
#.........这里部分代码省略.........