本文整理汇总了Python中ete3.Tree.get_common_ancestor方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.get_common_ancestor方法的具体用法?Python Tree.get_common_ancestor怎么用?Python Tree.get_common_ancestor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ete3.Tree
的用法示例。
在下文中一共展示了Tree.get_common_ancestor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_example_tree
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
def get_example_tree():
# Set dashed blue lines in all leaves
nst1 = NodeStyle()
nst1["bgcolor"] = "LightSteelBlue"
nst2 = NodeStyle()
nst2["bgcolor"] = "Moccasin"
nst3 = NodeStyle()
nst3["bgcolor"] = "DarkSeaGreen"
nst4 = NodeStyle()
nst4["bgcolor"] = "Khaki"
t = Tree("((((a1,a2),a3), ((b1,b2),(b3,b4))), ((c1,c2),c3));")
for n in t.traverse():
n.dist = 0
n1 = t.get_common_ancestor("a1", "a2", "a3")
n1.set_style(nst1)
n2 = t.get_common_ancestor("b1", "b2", "b3", "b4")
n2.set_style(nst2)
n3 = t.get_common_ancestor("c1", "c2", "c3")
n3.set_style(nst3)
n4 = t.get_common_ancestor("b3", "b4")
n4.set_style(nst4)
ts = TreeStyle()
ts.layout_fn = layout
ts.show_leaf_name = False
ts.mode = "c"
ts.root_opening_factor = 1
return t, ts
示例2: smart_reroot
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
def smart_reroot(treefile, outgroupfile, outfile, format=0):
"""
simple function to reroot Newick format tree using ete2
Tree reading format options see here:
http://packages.python.org/ete2/tutorial/tutorial_trees.html#reading-newick-trees
"""
tree = Tree(treefile, format=format)
leaves = [t.name for t in tree.get_leaves()][::-1]
outgroup = []
for o in must_open(outgroupfile):
o = o.strip()
for leaf in leaves:
if leaf[:len(o)] == o:
outgroup.append(leaf)
if outgroup:
break
if not outgroup:
print("Outgroup not found. Tree {0} cannot be rerooted.".format(treefile), file=sys.stderr)
return treefile
try:
tree.set_outgroup(tree.get_common_ancestor(*outgroup))
except ValueError:
assert type(outgroup) == list
outgroup = outgroup[0]
tree.set_outgroup(outgroup)
tree.write(outfile=outfile, format=format)
logging.debug("Rerooted tree printed to {0}".format(outfile))
return outfile
示例3:
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
# | | /-L
# | \--------|
# ---------| \-M
# |
# | /-B
# | /--------|
# | | | /-J
# | | \--------|
# \--------| \-K
# |
# | /-E
# \--------|
# \-D
#
# Each main branch of the tree is independently rooted.
node1 = t.get_common_ancestor("A", "H")
node2 = t.get_common_ancestor("B", "D")
node1.set_outgroup("H")
node2.set_outgroup("E")
print "Tree after rooting each node independently:"
print t
#
# /-F
# |
# /--------| /-L
# | | /--------|
# | | | \-M
# | \--------|
# /--------| | /-A
# | | \--------|
# | | \-C
示例4: Tree
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
tree.render(file_name=sys.argv[1] + "_" + cluster + ".pdf", tree_style=ts, w=width)
big_tree = Tree(sys.argv[1])
mode = sys.argv[2]
metadata = {}
metadata = get_meta_new(metadata, big_tree)
colourDict = get_colours(clusters, big_tree, colours)
# remove dodgy sample
big_tree.search_nodes(name="'EBOV|EMLab-RT|IPDPFHGINSP_GUI_2015_5339||GIN|Conakry|?|MinION_LQ05|2015-04-08'")[0].delete(
preserve_branch_length=True
)
# root the same as the MCC tree
ancestor = big_tree.get_common_ancestor(
"'EBOV|EMLab|EM_079422|KR817187|GIN|Macenta|?||2014-03-27'",
"'EBOV|EMLab|Gueckedou-C05|KJ660348|GIN|Gueckedou|?||2014-03-19'",
)
big_tree.set_outgroup(ancestor)
big_tree.ladderize()
ts = TreeStyle()
ts.show_leaf_name = False
# ts.show_branch_support = True
ts.scale = 100000
if mode == "small":
ts.scale = 750000
# add legend
for each in colourDict.keys():
ts.legend.add_face(CircleFace(radius=size[mode] / 2, color=colourDict[each]), column=0)
ts.legend.add_face(TextFace(each, ftype="Helvetica", fsize=size[mode]), column=1)
示例5: float
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
line = line.rstrip()
(geneFamily, nodeRb, iesColumn, presence) = line.split() # nodes are in rb format
# find what type of speciation event nodeRb corresponds to
if float(presence) > cutoff:
spe = d[geneFamily][nodeRb]
if spe:
homies.setdefault((geneFamily,iesColumn), []).append(spe)
# load species tree
t = Tree(os.path.join(basePath, 'analysis/', 'phyldogT' + asrRun, 'results', 'OutputSpeciesTree_ConsensusNumbered.tree'), format = 1)
# replace species names with speciation events and add 0 to root node label
for l in t.traverse():
if(l.is_leaf()):
l.name = (re.sub(r'.+_.+_(\d+)', r'\1', l.name))
elif(l.is_root()):
if(l.name):
quit(l.name) # is root named?
else:
l.name = '0'
for (geneFamily, iesColumn) in homies:
L = homies[(geneFamily, iesColumn)]
if(len(L) == 1):
# if only one node
print('\t'.join([geneFamily, iesColumn, L[0]]))
else:
ancestor = t.get_common_ancestor(L)
print('\t'.join([geneFamily, iesColumn, ancestor.name]))
示例6: len
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
target_leaf = leaf
else:
leaf.add_features(domain="Other")
#print eukaryote_seqs
#test the various phylogenetic criteria for LGT.
#euk sequence is a singleton nested within a clade of bacteria, and there is only one eukaryote sequence in the tree
if len(eukaryote_seqs) == 1: #this is, I guess, an LGT candidate
print sys.argv[1] + "\tSingleton"
#euk sequence is a singleton nested within a clade of bacteria, and the eukaryotes are not monophyletic in the tree
#print len(eukaryote_seqs)
else:
try:
answer = tree.check_monophyly(values=eukaryote_seqs, target_attr="name")
if answer[0] == True:
ca = tree.get_common_ancestor(eukaryote_seqs)
print sys.argv[1] + "\tEuks monophyletic\t" + str(len(eukaryote_seqs)) + "\t" + str(ca.support)
elif answer[0] == False:
mono_groups = []
target_group = ''
for node in tree.get_monophyletic(values=['Eukaryote'], target_attr="domain"):
if target_leaf in node:
target_group = node
else:
mono_groups.append(node)
size_target_group = len(target_group)
#get distance
shortest_distance = 999999999999999.0
closest_other_group = ''
for subtree in mono_groups:
curr_distance = tree.get_distance(target_group, subtree, topology_only=True)
示例7: build_nj_phylip
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
def build_nj_phylip(alignment, outfile, outgroup, work_dir="."):
"""
build neighbor joining tree of DNA seqs with PHYLIP in EMBOSS
PHYLIP manual
http://evolution.genetics.washington.edu/phylip/doc/
"""
phy_file = op.join(work_dir, "work", "aln.phy")
try:
AlignIO.write(alignment, file(phy_file, "w"), "phylip")
except ValueError:
print("Repeated seq name, possibly due to truncation. NJ tree not built.", file=sys.stderr)
return None
seqboot_out = phy_file.rsplit(".",1)[0] + ".fseqboot"
seqboot_cl = FSeqBootCommandline(FPHYLIP_BIN("fseqboot"), \
sequence=phy_file, outfile=seqboot_out, \
seqtype="d", reps=100, seed=12345)
stdout, stderr = seqboot_cl()
logging.debug("Resampling alignment: %s" % seqboot_cl)
dnadist_out = phy_file.rsplit(".",1)[0] + ".fdnadist"
dnadist_cl = FDNADistCommandline(FPHYLIP_BIN("fdnadist"), \
sequence=seqboot_out, outfile=dnadist_out, method="f")
stdout, stderr = dnadist_cl()
logging.debug\
("Calculating distance for bootstrapped alignments: %s" % dnadist_cl)
neighbor_out = phy_file.rsplit(".",1)[0] + ".njtree"
e = phy_file.rsplit(".",1)[0] + ".fneighbor"
neighbor_cl = FNeighborCommandline(FPHYLIP_BIN("fneighbor"), \
datafile=dnadist_out, outfile=e, outtreefile=neighbor_out)
stdout, stderr = neighbor_cl()
logging.debug("Building Neighbor Joining tree: %s" % neighbor_cl)
consense_out = phy_file.rsplit(".",1)[0] + ".consensustree.nodesupport"
e = phy_file.rsplit(".",1)[0] + ".fconsense"
consense_cl = FConsenseCommandline(FPHYLIP_BIN("fconsense"), \
intreefile=neighbor_out, outfile=e, outtreefile=consense_out)
stdout, stderr = consense_cl()
logging.debug("Building consensus tree: %s" % consense_cl)
# distance without bootstrapping
dnadist_out0 = phy_file.rsplit(".",1)[0] + ".fdnadist0"
dnadist_cl0 = FDNADistCommandline(FPHYLIP_BIN("fdnadist"), \
sequence=phy_file, outfile=dnadist_out0, method="f")
stdout, stderr = dnadist_cl0()
logging.debug\
("Calculating distance for original alignment: %s" % dnadist_cl0)
# infer branch length on consensus tree
consensustree1 = phy_file.rsplit(".",1)[0] + ".consensustree.branchlength"
run_ffitch(distfile=dnadist_out0, outtreefile=consensustree1, \
intreefile=consense_out)
# write final tree
ct_s = Tree(consense_out)
if outgroup:
t1 = consensustree1 + ".rooted"
t2 = smart_reroot(consensustree1, outgroup, t1)
if t2 == t1:
outfile = outfile.replace(".unrooted", "")
ct_b = Tree(t2)
else:
ct_b = Tree(consensustree1)
nodesupport = {}
for node in ct_s.traverse("postorder"):
node_children = tuple(sorted([f.name for f in node]))
if len(node_children) > 1:
nodesupport[node_children] = node.dist/100.
for k,v in nodesupport.items():
ct_b.get_common_ancestor(*k).support = v
print(ct_b)
ct_b.write(format=0, outfile=outfile)
try:
s = op.getsize(outfile)
except OSError:
s = 0
if s:
logging.debug("NJ tree printed to %s" % outfile)
return outfile, phy_file
else:
logging.debug("Something was wrong. NJ tree was not built.")
return None
示例8: Tree
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
import random
from ete3 import Tree
# Creates a normal tree
t = Tree( '((H:0.3,I:0.1):0.5, A:1, (B:0.4,(C:0.5,(J:1.3, (F:1.2, D:0.1):0.5):0.5):0.5):0.5);' )
print t
# Let's locate some nodes using the get common ancestor method
ancestor=t.get_common_ancestor("J", "F", "C")
# the search_nodes method (I take only the first match )
A = t.search_nodes(name="A")[0]
# and using the shorcut to finding nodes by name
C= t&"C"
H= t&"H"
I= t&"I"
# Let's now add some custom features to our nodes. add_features can be
# used to add many features at the same time.
C.add_features(vowel=False, confidence=1.0)
A.add_features(vowel=True, confidence=0.5)
ancestor.add_features(nodetype="internal")
# Or, using the oneliner notation
(t&"H").add_features(vowel=False, confidence=0.2)
# But we can automatize this. (note that i will overwrite the previous
# values)
for leaf in t.traverse():
if leaf.name in "AEIOU":
leaf.add_features(vowel=True, confidence=random.random())
else:
leaf.add_features(vowel=False, confidence=random.random())
# Now we use these information to analyze the tree.
print "This tree has", len(t.search_nodes(vowel=True)), "vowel nodes"
print "Which are", [leaf.name for leaf in t.iter_leaves() if leaf.vowel==True]
# But features may refer to any kind of data, not only simple
示例9: outgroup
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
parser.add_argument(
'--verbose', action='store_true',
help=('Print information about the outgroup (if any) taxa to standard '
'error'))
args = parser.parse_args()
tree = Tree(args.treeFile.read())
if args.outgroupRegex:
from re import compile
regex = compile(args.outgroupRegex)
taxa = [leaf.name for leaf in tree.iter_leaves() if regex.match(leaf.name)]
if taxa:
ca = tree.get_common_ancestor(taxa)
if args.verbose:
print('Taxa for outgroup:', taxa, file=sys.stderr)
print('Common ancestor:', ca.name, file=sys.stderr)
print('Common ancestor is tree:', tree == ca, file=sys.stderr)
if len(taxa) == 1:
tree.set_outgroup(tree & taxa[0])
else:
if ca == tree:
tree.set_outgroup(tree.get_midpoint_outgroup())
else:
tree.set_outgroup(tree.get_common_ancestor(taxa))
print(tree.get_ascii())
示例10:
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
print tree
archaea = [] #make a list of archaea that are in the tree
bacteria = []
#check the domain of each taxon in the tree
for taxon in tree:
print taxon.name + "\t" + id_to_domain[taxon.name]
if id_to_domain[taxon.name] == 'Archaea':
archaea.append(taxon.name)
else:
bacteria.append(taxon.name)
#first, check if archaea are monophyletic in the tree
if tree.check_monophyly(values=archaea, target_attr="name")[0] == True:
#find the branch separating archaea and bacteria, and reroot the tree on that
archaea_ancestor = tree.get_common_ancestor(archaea)
tree.set_outgroup(archaea_ancestor)
elif tree.check_monophyly(values=bacteria, target_attr="name")[0] == True:
bacteria_ancestor = tree.get_common_ancestor(bacteria)
tree.set_outgroup(bacteria_ancestor)
else:
#neither archaea nor bacteria were monophyletic, so print some error and quit
print sys.argv[1] + ": neither A nor B monophyletic."
quit()
outfile_name = sys.argv[1] + "_rerooted"
tree.write(outfile=outfile_name)
示例11: len
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
if len(node) > biggest_other_node:
biggest_other_node = len(node)
tree.set_outgroup(node)
#test the various phylogenetic criteria for LGT.
print "Tree\tResult\tEuksInTree\tSupportEukMonophyly\tEuksInTargetGroup\tDistanceToClosestEukClade\tSupergroupsInTargetGroup"
#euk sequence is a singleton nested within a clade of bacteria, and there is only one eukaryote sequence in the tree
if len(eukaryote_seqs) == 1: #this is, I guess, an LGT candidate
print sys.argv[1] + "\tSingleton\t1\tN/A\tN/A\tN/A\t1"
#euk sequence is a singleton nested within a clade of bacteria, and the eukaryotes are not monophyletic in the tree
#print len(eukaryote_seqs)
else:
try:
answer = tree.check_monophyly(values=eukaryote_seqs, target_attr="name")
if answer[0] == True:
ca = tree.get_common_ancestor(eukaryote_seqs)
target_group_sgs = {}
for leaf in ca:
if leaf.name in group_assignments:
leaf_supergroup = group_assignments[leaf.name]
if leaf_supergroup in euk_supergroups:
target_group_sgs[leaf_supergroup] = 1
else:
print "Warning: a sequence in this tree doesn't have a supergroup assignment: " + str(leaf.name)
num_sgs = len(target_group_sgs.keys())
print sys.argv[1] + "\tEuks monophyletic\t" + str(len(eukaryote_seqs)) + "\t" + str(ca.support) + "\tN/A\tN/A\t" + str(num_sgs)
elif answer[0] == False:
mono_groups = []
target_group = ''
for node in tree.get_monophyletic(values=['Eukaryote'], target_attr="domain"):
for leaf in node:
示例12: ClusterIdentification
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
#.........这里部分代码省略.........
self.dictSharedReads[comb2].append(node)
if not mate in self.dictSharedReads[comb2]:
self.dictSharedReads[comb2].append(mate)
elif not support == "None":
if float(supportInput) <= float(support):
if not comb2 in self.dictSharedReads:
if not comb in self.dictSharedReads:
self.dictSharedReads[comb] = []
if not node in self.dictSharedReads[comb]:
self.dictSharedReads[comb].append(node)
if not mate in self.dictSharedReads[comb]:
self.dictSharedReads[comb].append(mate)
else:
if not node in self.dictSharedReads[comb2]:
self.dictSharedReads[comb2].append(node)
if not mate in self.dictSharedReads[comb2]:
self.dictSharedReads[comb2].append(mate)
##Identifying potential outliers is optional (-oR flag from the command line )
# Based on the retrieve common ancestor function, it identifies outliers as those which contain < 3 intra-variants associated with a given sample
def PhylyOutlierRem(self, n, node1, node2, OutlierFile, idStart, idLen):
PhyloOutliers = {}
ancestorList = []
ancshort = []
node1short = node1[idStart:idLen]
node2short = node2[idStart:idLen]
nodecomb = node1short + "__" + node2short
nodecombRev = node2short + "__" + node2short
if not nodecomb or not nodecombRev in self.monoFinalRes:
if not node1 in self.nodesRemoved:
if not node2 in self.nodesRemoved:
# Collect all common ancestors for each pair of variants
ancestor = self.t.get_common_ancestor(n)
for i in ancestor:
ancestorList.append(i.name)
ancestorShort = i.name[idStart:idLen]
if not ancestorShort in PhyloOutliers:
PhyloOutliers[ancestorShort] = []
PhyloOutliers[ancestorShort].append(1)
# Sum the variants for each sample, if < 3, store variant as outlier
for k, v in PhyloOutliers.iteritems():
vsum = sum(v)
if vsum < 3:
for item in ancestorList:
if item[idStart:idLen] == k:
if not item in self.nodesRemoved:
if node1short in self.SerialNodes:
if not node2short in self.SerialNodes[node1short]:
ancestorList.remove(item)
self.nodesRemoved.append(item)
elif node2short in self.SerialNodes:
if not node1short in self.SerialNodes[node2short]:
ancestorList.remove(item)
self.nodesRemoved.append(item)
else:
ancestorList.remove(item)
self.nodesRemoved.append(item)
for i in self.nodesRemoved:
if not i in self.nodecheck:
try:
item = self.t.search_nodes(name=item)[0]
i.delete()
self.nodecheck.append(i)
except:
示例13:
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
print t
# /-A
# |
# | /-H
#---------|---------|
# | \-F
# |
# | /-B
# \--------|
# | /-E
# \--------|
# \-D
#
# Let's define that the ancestor of E and D as the tree outgroup. Of
# course, the definition of an outgroup will depend on user criteria.
ancestor = t.get_common_ancestor("E","D")
t.set_outgroup(ancestor)
print "Tree rooteda at E and D's ancestor is more basal that the others."
print t
#
# /-B
# /--------|
# | | /-A
# | \--------|
# | | /-H
#---------| \--------|
# | \-F
# |
# | /-E
# \--------|
# \-D
示例14: Tree
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
tree = Tree( '((H:1,I:1):0.5, A:1, (B:1,(C:1,D:1):0.5):0.5);' )
print "this is the original tree:"
print tree
# /-H
# /--------|
# | \-I
# |
#---------|--A
# |
# | /-B
# \--------|
# | /-C
# \--------|
# \-D
# Finds the first common ancestor between B and C.
ancestor = tree.get_common_ancestor("D", "C")
print "The ancestor of C and D is:"
print ancestor
# /-C
#---------|
# \-D
# You can use more than two nodes in the search
ancestor = tree.get_common_ancestor("B", "C", "D")
print "The ancestor of B, C and D is:"
print ancestor
# /-B
#---------|
# | /-C
# \--------|
# \-D
# Finds the first sister branch of the ancestor node. Because
示例15: open
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_common_ancestor [as 别名]
for line in outg:
outgroups.append(line.rstrip())
outg.close()
target_taxa = []
tt = open(sys.argv[2])
for line in tt:
target_taxa.append(line.rstrip())
tt.close()
#now read in a collection of trees, calc branch lengths over sample, summarise and print out
branch_lengths = defaultdict(list) #key = taxa, value = list of brlens
treefile = open(sys.argv[3])
for line in treefile:
curr_tree = Tree(line.rstrip())
root_node = curr_tree.get_common_ancestor(outgroups)
if curr_tree != root_node:
curr_tree.set_outgroup(root_node)
print curr_tree
#bundle = curr_tree.check_monophyly(values=outgroups,target_attr='name')
#print bundle
#if bundle[0] == False:
# continue
#find common ancestor of the target taxa, and use this as the reference node for calculating branch lengths. This might not always be the measure you want!
reference_node = curr_tree.get_common_ancestor(target_taxa)
#if reference_node != curr_tree:
# curr_tree.set_outgroup(reference_node)
#calc distance from root to each branch of interest
for taxon in target_taxa:
dist = curr_tree.get_distance(taxon, reference_node)
branch_lengths[taxon].append(dist)