本文整理汇总了Python中ete3.Tree.name方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.name方法的具体用法?Python Tree.name怎么用?Python Tree.name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ete3.Tree
的用法示例。
在下文中一共展示了Tree.name方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parseTree
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def parseTree(root):
tree = Tree()
tree.name = root['Name']
tree.add_face(TextFace(root['Split'], fgcolor="red"), column=0, position="branch-bottom")
if root['Children']:
for child in root['Children']:
tree.children.append(parseTree(child))
return tree
示例2: load_ncbi_tree_from_dump
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def load_ncbi_tree_from_dump(tar):
from ete3 import Tree
# Download: ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz
parent2child = {}
name2node = {}
node2taxname = {}
synonyms = set()
name2rank = {}
node2common = {}
print("Loading node names...")
for line in tar.extractfile("names.dmp"):
line = str(line.decode())
fields = list(map(str.strip, line.split("|")))
nodename = fields[0]
name_type = fields[3].lower()
taxname = fields[1]
if name_type == "scientific name":
node2taxname[nodename] = taxname
if name_type == "genbank common name":
node2common[nodename] = taxname
elif name_type in set(["synonym", "equivalent name", "genbank equivalent name",
"anamorph", "genbank synonym", "genbank anamorph", "teleomorph"]):
synonyms.add( (nodename, taxname) )
print(len(node2taxname), "names loaded.")
print(len(synonyms), "synonyms loaded.")
print("Loading nodes...")
for line in tar.extractfile("nodes.dmp"):
line = str(line.decode())
fields = line.split("|")
nodename = fields[0].strip()
parentname = fields[1].strip()
n = Tree()
n.name = nodename
n.taxname = node2taxname[nodename]
if nodename in node2common:
n.common_name = node2common[nodename]
n.rank = fields[2].strip()
parent2child[nodename] = parentname
name2node[nodename] = n
print(len(name2node), "nodes loaded.")
print("Linking nodes...")
for node in name2node:
if node == "1":
t = name2node[node]
else:
parent = parent2child[node]
parent_node = name2node[parent]
parent_node.add_child(name2node[node])
print("Tree is loaded.")
return t, synonyms
示例3: neighbor_based_method
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def neighbor_based_method(M, names, closest_neighbors_fn, new_dist_fn, parent_dist_fn):
def search_nodes(trees ,name):
for tree in trees:
if tree.name == name:
return tree
trees = []
while True:
taxa1, taxa2 = closest_neighbors_fn(M)
if taxa1 > taxa2:
tmp = taxa1
taxa1 = taxa2
taxa1 = tmp
#define a new parent for the join
t = Tree()
#search for the children in trees and add them
A = search_nodes(trees, names[taxa1])
if A == None:
A = t.add_child(name = names[taxa1])
else:
t.add_child(A)
trees.remove(A)
B = search_nodes(trees, names[taxa2])
if B == None:
B = t.add_child(name = names[taxa2])
else:
t.add_child(B)
trees.remove(B)
#delete old taxa names and update the new name
new_names = [names[taxa1] + names[taxa2]]
del names[taxa2]
del names[taxa1]
[new_names.append(name) for name in names]
names = new_names
#create the distance between children and parent
A.dist, B.dist = parent_dist_fn(M, taxa1, taxa2)
#name the parent
t.name = names[0]
#add the new subtree
trees.append(t)
if len(M) <= 2:
break
M = update_matrix(M, taxa1, taxa2, new_dist_fn)
return trees[0]
示例4: creation_by_words
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def creation_by_words(self, words):
"""
Creation of a tree based on separate words in the word list
:type words: list
"""
# Creates an empty tree
tree = Tree()
tree.name = ""
# Make sure there are no duplicates
words = set(words)
# Populate tree
for word in words:
# If no similar words exist, add it to the base of tree
target = tree
if self.is_reversed:
words = list(reversed(split(r'[\s-]+|:[\\/]{2}', word)))
else:
words = split(r'[\s-]+|:[\\/]{2}', word)
# Find relatives in the tree
root = ''
pos = 0
for pos in xrange(len(words), -1, -1):
root = ' '.join(words[:pos])
if root in self.name2node:
target = self.name2node[root]
break
# Add new nodes as necessary
fullname = root
for wd in words[pos:]:
fullname = (fullname + ' ' + wd).strip()
new_node = TreeNode(name=wd.strip(), dist=target.dist + 1)
target.add_child(new_node)
self.name2node[fullname] = new_node
target = new_node
return tree
示例5: getTheTrees
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def getTheTrees():
##DOWNLOAD taxdump and store in taxo folder
##DOWNLOAD TAXREF BY HAND! and put it in taxo/
class Trans:
def __init__(self):
self.common_name_FR = []
print "Getting french translations..."
TRANS = {} ##translations in french
with open("taxo/TAXREFv11.txt") as f:
for line in f:
sciname = line.split("\t")[14]
comnameFR = line.split("\t")[19]
if (TRANS.has_key(sciname)==False and line.split("\t")[19]!=''):
TRANS[sciname] = Trans()
if (line.split("\t")[19]!=''):
TRANS[sciname].common_name_FR.append(comnameFR)
#get translation of ranks
print "\nGetting rank names in french..."
RANKS = {}
with open("ranks.txt") as f:
for line in f:
rank_en = line.split("\t")[0]
rank_fr = line.split("\t")[1].rstrip() ##to remove \n
RANKS[rank_en] = rank_fr
class Taxid:
def __init__(self):
self.sci_name = ""
self.authority = ""
self.synonym = ""
# self.common_name = ""
self.common_name = []
# self.common_name_FR = ""
self.common_name_FR = []
cpt = 0
cptfr = 0
ATTR = {} ##here we will list attribute of each species per taxid
print "Reading NCBI taxonomy..."
with open("taxo/names.dmp") as f:
for line in f:
taxid = line.split("|")[0].replace("\t","")
tid_val = line.split("|")[1].replace("\t","")
tid_type = line.split("|")[3].replace("\t","")
if (ATTR.has_key(taxid)==False):
ATTR[taxid] = Taxid()
if (tid_type=="scientific name"):
ATTR[taxid].sci_name = tid_val
#and get translation in french (if any)
if TRANS.has_key(tid_val):
ATTR[taxid].common_name_FR = TRANS[tid_val].common_name_FR
cptfr += 1
if (tid_type=="authority"):
if (ATTR[taxid].authority!=""):
ATTR[taxid].authority = ATTR[taxid].authority + ", " + tid_val
else:
ATTR[taxid].authority = tid_val
if (tid_type=="synonym"):
if (ATTR[taxid].synonym!=""):
ATTR[taxid].synonym = ATTR[taxid].synonym + ", " + tid_val
else:
ATTR[taxid].synonym = tid_val
if (tid_type=="common name"):
cpt +=1
ATTR[taxid].common_name.append(tid_val)
# if (ATTR[taxid].common_name!=""):
# ATTR[taxid].common_name = ATTR[taxid].common_name + ", " + tid_val
# else:
# ATTR[taxid].common_name = tid_val
T = {}
###New gettrees
from ete3 import Tree
filepath = 'taxo/nodes.dmp'
print "Building the NCBI taxonomy tree..."
with open(filepath) as fp:
first_line = fp.readline() ## remove the 1 | 1 edge
for line in fp:
dad = line.split("|")[1].replace("\t","")
son = line.split("|")[0].replace("\t","")
rank = line.split("|")[2].replace("\t","")
if (T.has_key(dad)==False):
T[dad] = Tree()
T[dad].name = dad
# T[dad].rank = rank
# T[dad].rank_FR = RANKS[rank]
T[dad].taxid = dad
T[dad].sci_name = ATTR[dad].sci_name
T[dad].common_name = ATTR[dad].common_name
T[dad].synonym = ATTR[dad].synonym
T[dad].authority = ATTR[dad].authority
T[dad].common_name_FR = ATTR[dad].common_name_FR
if (T.has_key(son)==False):
#.........这里部分代码省略.........
示例6: get_tree_object_in_newick
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def get_tree_object_in_newick(tree, id_to_sample_dict=None):
"""Take a tree object, and create a newick formatted representation of it"""
new_tree = Tree()
new_tree.dist = 0
new_tree.name = "root"
node_id = 0
node_id_to_node_in_old_tree = {node_id: tree}
node_id_to_node_in_new_tree = {node_id: new_tree}
node_ids_to_visit_in_old_tree = [node_id]
while node_ids_to_visit_in_old_tree:
node_id_in_old_tree = node_ids_to_visit_in_old_tree.pop()
node_in_old_tree = node_id_to_node_in_old_tree[node_id_in_old_tree]
cl_dist = node_in_old_tree.dist / 2.0
for ch_node_in_old_tree in [node_in_old_tree.left, node_in_old_tree.right]:
if ch_node_in_old_tree:
ch_for_new_tree = Tree()
ch_for_new_tree.dist = cl_dist
node_id += 1
node_id_to_node_in_new_tree[node_id] = ch_for_new_tree
if ch_node_in_old_tree.is_leaf():
if id_to_sample_dict:
ch_for_new_tree.name = id_to_sample_dict[ch_node_in_old_tree.id]
else:
ch_for_new_tree.name = ch_node_in_old_tree.id
else:
# we used to export our trees with internal node labels so we could
# do various interface operations more easily:
#
# ch_for_new_tree.name = 'Int' + str(ch_node_in_old_tree.id)
#
# but our new interface design does not require such addditions to
# dendrograms. Although here we add 0 branch support for our
# dendrograms since we wish to use a standard format to export these
# data as a tree.
ch_for_new_tree.support = 0.0
node_id_to_node_in_new_tree[node_id_in_old_tree].add_child(ch_for_new_tree)
node_id_to_node_in_old_tree[node_id] = ch_node_in_old_tree
node_ids_to_visit_in_old_tree.append(node_id)
for node in new_tree.traverse("preorder"):
if node.is_leaf():
continue
has_child_with_dist_or_int = False
for child in node.get_children():
if not child.is_leaf() or child.dist > 0:
has_child_with_dist_or_int = True
break
if has_child_with_dist_or_int:
continue
# swap childs alphabetically
node.children = sorted(node.get_children(), key=lambda x:x.name, reverse=True)
return new_tree.write(format=2)
示例7: satisfiable
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
def satisfiable(stmt_set):
# create a Tree node for the current set
t = Tree()
t.name = str(stmt_set)
if stmt_set == []:
# if the set is empty, the starting set was Satisfiable, OPEN BRANCH
# construct an extra "child" tree that is just an "O" to represent an open branch
ob = Tree()
ob.name = "O"
t.children.append(ob)
# return True and the final node
return True, t
if False in stmt_set:
# if there is a False in the set, then something was unsatisfiable, CLOSE BRANCH
# construct an extra "child" tree that is just an "X" to represent an closed branch
cb = Tree()
cb.name = "X"
t.children.append(cb)
# return False and the final node
return False, t
# determine the next atom to split on
nxt_atm = findNextSplit(stmt_set)
# get Statements of this atom and its negation
nxt_l = parse.parse(nxt_atm)
nxt_r = parse.parse("~(%s)" % nxt_atm)
# create two new statement sets for the left and right branches
l = [] # for the statements reduced on the "positive" atom
r = [] # for the statements reduced on the "negative" atom
for stmt in stmt_set:
# reduce the statement on the "positive" atom
stmt_l = stmt.reduce(nxt_l)
if stmt_l is not True:
# don't add it to the new list if the recution was a True
l.append(stmt_l)
# reduce the statement on the "negative" atom
stmt_r = stmt.reduce(nxt_r)
if stmt_r is not True:
# don't add it to the new list if the recution was a True
r.append(stmt_r)
# get the satisfiablity of the left and right branches, plus the tree representations of both
ret_l, tree_l = satisfiable(l)
ret_r, tree_r = satisfiable(r)
# add an extra marker to the tree to represent which literal the resulting branch was reduced on
tree_l.add_face(TextFace(str(nxt_l), fgcolor="red"), column=0, position="branch-bottom")
tree_r.add_face(TextFace(str(nxt_r), fgcolor="red"), column=0, position="branch-bottom")
# add the left and right child branches to the current node
t.children.append(tree_l)
t.children.append(tree_r)
# return ether the left or right branch and the current tree
return ret_l or ret_r, t
示例8: open
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import name [as 别名]
T = {}
###New gettrees
from ete3 import Tree
filepath = 'taxo/nodes.dmp'
print "Building the NCBI taxonomy tree..."
with open(filepath) as fp:
first_line = fp.readline() ## remove the 1 | 1 edge
for line in fp:
dad = line.split("|")[1].replace("\t","")
son = line.split("|")[0].replace("\t","")
rank = line.split("|")[2].replace("\t","")
if (T.has_key(dad)==False):
T[dad] = Tree()
T[dad].name = dad
T[dad].rank = rank
T[dad].rank_FR = RANKS[rank]
T[dad].taxid = dad
T[dad].sci_name = ATTR[dad].sci_name
T[dad].common_name = ATTR[dad].common_name
T[dad].synonym = ATTR[dad].synonym
T[dad].authority = ATTR[dad].authority
T[dad].common_name_FR = ATTR[dad].common_name_FR
if (T.has_key(son)==False):
T[son] = Tree()
T[son].name = son
T[son].rank = rank
T[son].rank_FR = RANKS[rank]
T[son].taxid = son
T[son].sci_name = ATTR[son].sci_name