本文整理汇总了Python中skbio.TreeNode.traverse方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.traverse方法的具体用法?Python TreeNode.traverse怎么用?Python TreeNode.traverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skbio.TreeNode
的用法示例。
在下文中一共展示了TreeNode.traverse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_consensus_tree
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import traverse [as 别名]
def make_consensus_tree(cons_split, check_for_rank=True, tips=None):
"""Returns a mapping by rank for names to their parent names and counts"""
god_node = TreeNode(name=None)
god_node.Rank = None
base = list(cons_split)[0]
cur_node = god_node
# create a base path in the tree
for rank, name in enumerate(base):
new_node = TreeNode(name=name)
new_node.Rank = rank
cur_node.append(new_node)
cur_node = new_node
# setup the initial childlookup structure so taht we don't have to
# always iterate over .children
for n in god_node.traverse(include_self=True):
if n.is_tip():
n.ChildLookup = {}
continue
n.ChildLookup = {n.children[0].name: n.children[0]}
# for every consensus string, start at the "god" node
for idx, con in enumerate(cons_split):
cur_node = god_node
# for each name, see if we've seen it, if not, add that puppy on
for rank, name in enumerate(con):
if name in cur_node.ChildLookup:
cur_node = cur_node.ChildLookup[name]
else:
new_node = TreeNode(name=name)
new_node.Rank = rank
new_node.ChildLookup = {}
cur_node.append(new_node)
cur_node.ChildLookup[name] = new_node
cur_node = new_node
if tips is not None:
cur_node.append(TreeNode(name=tips[idx]))
# build an assist lookup dict
lookup = {}
for node in god_node.traverse():
if node.name is None:
continue
if check_for_rank and '__' in node.name and \
node.name.split('__')[1] == '':
continue
lookup[node.name] = node
return god_node, lookup
示例2: make_d3_phylogram
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import traverse [as 别名]
def make_d3_phylogram(output_dir: str, tree: skbio.TreeNode, otu_metadata: qiime.Metadata) -> None:
mapping_df = otu_metadata.to_dataframe()
# ERROR CHECK INPUTS
if isinstance(mapping_df, pd.DataFrame):
leaves = set([l for l in tree.traverse() if l.is_tip()])
mapping_otus = set(mapping_df.index)
if leaves > mapping_otus:
print("\n*** NOTE *** ")
print("Not all leaves were found in the OTU mapping file; as a consequence, these leaves cannot be styled.\n")
# CONSTRUCT BODY TAG
dat_tree = '"dat/tree.tre"'
div = '"#phylogram"'
dat_mapping = '{"mapping_dat" : %s}' %mapping_df.reset_index().to_json(orient="records") # store the mapping file in the options obj
if isinstance(mapping_df, pd.DataFrame):
body = "\t<body onload='init(%s, %s, %s);'>" %(dat_tree, div, dat_mapping)
else:
body = "\t<body onload='init(%s, %s);'>" %(dat_tree, div)
index = template.replace('REPLACE',body) # html to write to index.html
# WRITE ALL OUR FILES
# index.html, tree.tre, mapping.txt (optional)
dat_dir = os.path.join(output_dir,'dat')
if not os.path.exists(dat_dir):
os.makedirs(dat_dir)
with open(os.path.join(output_dir,'index.html'), 'w') as fout:
fout.write(index)
tree_out = os.path.join(dat_dir,'tree.tre')
with open(tree_out, 'w') as fout:
fout.write("d3.jsonp.readNewick('%s');" %str(tree).strip()) # add callback around Newick string for use in cross-origin setting