本文整理汇总了Python中dendropy.Tree.get_from_stream方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.get_from_stream方法的具体用法?Python Tree.get_from_stream怎么用?Python Tree.get_from_stream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dendropy.Tree
的用法示例。
在下文中一共展示了Tree.get_from_stream方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_tree_and_OTT_list
# 需要导入模块: from dendropy import Tree [as 别名]
# 或者: from dendropy.Tree import get_from_stream [as 别名]
def get_tree_and_OTT_list(tree_filehandle, sources, verbosity=0):
"""
Takes a base tree and creates objects for each node and leaf, attaching them as 'data' dictionaries
to each node in the DendroPy tree. Nodes and leaves with an OTT id also have pointers to their data
dicts stored in an OTT-keyed dict, so that mappings to other databases (ncbi id, etc etc) can be created.
We can easily have duplicate leaf names, so for the entire procedure we ignore the Dendropy concept
of a taxon list and simply use labels.
Returns the Dendropy tree and the OTT dict.
"""
#these variables are all pointers into the same data
ordered_leaves=[]
ordered_nodes=[]
indexed_by_ott={}
try:
tree = Tree.get_from_stream(tree_filehandle, schema="newick", preserve_underscores=True, suppress_leaf_node_taxa=True)
except:
sys.exit("Problem reading tree from " + treefile.name)
info("-> read tree from " + tree_filehandle.name)
ott_node = re.compile(r"(.*) ott(\d+)(@\d*)?$") #matches the OTT number
mrca_ott_node = re.compile(r"(.*) (mrcaott\d+ott\d+)(@\d*)?$") #matches a node with an "mrca" node number (no unique OTT)
for i, node in enumerate(tree.preorder_node_iter()):
node.data = {'parent':node.parent_node or None}
if node.label:
node.label = node.label.replace("_"," ")
m = ott_node.search(node.label)
if m is not None:
if m.group(3):
warn("Node has an @ sign at the end ({}), meaning it has probably not been substituted by an OpenTree equivalent. You may want to provide an alternative subtree from this node downwards, as otherwise it will probably be deleted from the main tree.".format(node.label))
node.label = m.group(1)
node.data['ott'] = int(m.group(2))
indexed_by_ott[node.data['ott']] = node.data
node.data['sources']={k:None for k in sources}
else:
m = mrca_ott_node.search(node.label)
if m is not None:
if m.group(3):
warn("Node has an @ sign at the end ({}), meaning it has probably not been substituted by an OpenTree equivalent. You may want to provide an alternative subtree from this node downwards, as otherwise it will probably be deleted from the main tree.".format(node.label))
node.label = m.group(1)
#this is an 'mrca' node, so we want to save sources but *not* save the ott number in node.data
indexed_by_ott[m.group(2)] = node.data
node.data['sources']={k:None for k in sources}
elif node.is_leaf():
warn("Leaf without an OTT id: '{}'. This will not be associated with any other data".format(node.label))
#finally, put underscores at the start or the end of the new label back
#as these denote "fake" names that are hidden and only used for mapping
#we could keep them as spaces, but leading/trailing underscores are easier to see by eye
if node.label[0]==" ":
node.label = "_" + node.label[1:]
if node.label[-1]==" ":
node.label = node.label[:-1] + "_"
info("-> extracted {} otts from among {} leaves and nodes".format(len(indexed_by_ott), i))
return tree, indexed_by_ott
示例2: prune_tree
# 需要导入模块: from dendropy import Tree [as 别名]
# 或者: from dendropy.Tree import get_from_stream [as 别名]
def prune_tree(treepath, keep, prune_OTTids=[]):
'''take a newick tree file and remove all the subtrees in prune_OTTids, then keep only those taxa listed as keys in the "keep" dictionary,
incrementing the value of the key by one each time. If keep is True, keep all of the remaining taxa'''
with open(treepath, 'r', encoding='UTF-8') as treefile:
tree = Tree.get_from_stream(treefile, schema="newick", preserve_underscores=True, rooting='default-rooted')
removed = 0
for to_omit in prune_OTTids:
to_remove = (tree.find_node(lambda node: True if node.label is not None and re.search("_ott" + to_omit + "'?$", node.label) else False) or
tree.find_node_with_taxon(lambda taxon: True if re.search("_ott" + to_omit + "'?$", taxon.label) else False))
if to_remove:
tree.prune_subtree(to_remove, suppress_unifurcations=False)
removed += 1
else:
warn("Could not find subtree _ott{} within tree in {}".format(to_omit,treepath))
if not keep == True:
if removed:
tree.purge_taxon_namespace() #make sure that taxa in the pruned subtrees are not counted
tree.leave_only(keep)
return(tree)
示例3: open
# 需要导入模块: from dendropy import Tree [as 别名]
# 或者: from dendropy.Tree import get_from_stream [as 别名]
for chldNode in node.child_nodes():
if chldNode.is_leaf():
return False
return True
# read class name and sci name
clsList = []
sciList = []
fCls = open("classes.csv", "rb")
csvCls = csv.reader(fCls)
for row in csvCls:
clsList.append(row[2])
sciList.append(row[4])
# read tree
tree = Tree.get_from_stream(open("CUB11.tre"), "nexus")
# generate grouping system
nodeList = [tree.seed_node]
grpSys = []
grpSys.append(nodeList)
GRP_SYS_NUM = 8
for g in xrange(1, GRP_SYS_NUM):
nodeList = []
for node in grpSys[g - 1]:
if IsSplitNode(node):
for chldNode in node.child_nodes():
nodeList.append(chldNode)
else:
nodeList.append(node)
grpSys.append(nodeList)
示例4: check_list_against_tree
# 需要导入模块: from dendropy import Tree [as 别名]
# 或者: from dendropy.Tree import get_from_stream [as 别名]
def check_list_against_tree(treepath, checklist):
'''take a path to a newick tree file and look for any taxa that correspond to keys in the "keep" dictionary,
incrementing the value of each one found'''
with open(treepath, 'r', encoding='UTF-8') as treefile:
check_list_against_taxa(Tree.get_from_stream(treefile, schema="newick", preserve_underscores=True, rooting='default-rooted'), checklist)