def modexec(tmp_session): if tmp_session == None: return func_node = Tree(name='EC2 Listing') client = tmp_session.client('ec2') ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']] this_name = "" ami_name = "" for region in ec2_regions: ec2 = tmp_session.resource(service_name='ec2', region_name=region) instances = ec2.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) for instance in instances: this_name = "" ami_name = "" inst_handler = ec2.Instance(instance.id) tags = inst_handler.tags or [] names = [tag.get('Value') for tag in tags if tag.get('Key') == 'Name'] if names: ami_name = names[0] else: ami_name = '---' this_name = instance.id + '(' + ami_name + ')' func_node.add_child(name=this_name) return func_node
def build_tree(link, stop=1, rec=0): """ Builds link tree by traversing through children nodes. Args: link (LinkNode): root node of tree stop (int): depth of tree rec (int): level of recursion Returns: tree (ete3.Tree): Built tree. """ tree = Tree(name=link.name) if rec == stop: return tree else: rec += 1 for child in link.links: try: node = LinkNode(child) except Exception as error: print(f"Failed to create LinkNode for link: {child}.") print(f"Error: {error}") continue if node.links: tree.add_child(build_tree(node, stop, rec)) else: tree.add_child(Tree(name=node.name)) return tree
def tree_to_png(self, filepath: str): nodes = list() for i in range(self.leafs.shape[0]): thisNode = Tree() thisNode.add_face( faces.BarChartFace( self.leafs[i].detach().cpu().numpy(), labels=self.action_labels, min_value=0.0, max_value=self.leafs[i].max().detach().cpu().numpy() + 1e-7, ), 0) nodes.append(thisNode) for d in range(self.depth-1): for node_i in range(self.nodes_beta[d].shape[0]): thisNode = Tree() thisNode.add_child(nodes.pop(1)) thisNode.add_child(nodes.pop(0)) beta = F.softmax(self.nodes_beta[d][node_i].squeeze(), 0 ).detach().cpu().numpy() phi = self.nodes_phi[d][node_i].squeeze().detach().cpu().item() thisNode.add_face( faces.BarChartFace( beta, labels=self.labels, min_value=0.0, max_value=1.0 ), 0) thisNode.add_face( faces.TextFace('phi: {0:.3f}'.format(phi)), 0) nodes.append(thisNode) if filepath is not None: nodes[0].render(filepath) return nodes[0]
def spn_to_ete(spn, context=None, unroll=False, symbols=_symbols): assert spn is not None tree = Tree() tree.id = spn.id tree.node_type = type(spn) tree.name = symbols.get(tree.node_type, spn.name) queue = [] if not isinstance(spn, Leaf): for i, child in enumerate(spn.children): if unroll: if child in queue: return "-> " + spn.id else: queue.append(child) c = spn_to_ete(child, context=context, unroll=unroll) if isinstance(spn, Sum): c.support = spn.weights[i] tree.add_child(c) else: feature_names = None if context is not None: feature_names = context.feature_names try: tree.name = spn_to_str_equation(spn, feature_names=feature_names) except: if feature_names is None: feature_names = [] tree.name += "(%s)" % ",".join(feature_names) return tree
def rootTree(tree): print("On root cet arbre:") print(tree) leaves = tree.get_leaves() ## On initialise le résultat à la racine longestPath = Path(tree, tree, 0) print("ce qui m'interesse") print(tree.get_distance("PCDHA1_Souris", "OR2J3_Humain")) for leaf in tree.iter_leaves(): print("Leaf:", leaf) print("Leaf.up:", leaf._get_up()) start = leaf end, dist = start.get_farthest_node() pathLength = tree.get_distance(start.name, end.name) if pathLength > longestPath.get_length(): longestPath = Path(start, end, pathLength) print("LONGEST PATH:") print(longestPath.get_start().name) print(longestPath.get_end().name) print(longestPath.get_length()) rightSubtree, leftSubtree = findMidpoint(longestPath, tree) if rightSubtree is leftSubtree: return tree.set_outgroup(rightSubtree) print(rightSubtree) print(leftSubtree) temp = Tree() temp.add_child(rightSubtree) temp.add_child(leftSubtree) return temp
def createPseudonodes(node): if node.is_leaf(): return node for child in node.get_children(): createPseudonodes(child) if len(node.get_children()) > 2: dDominantTaxon2Children = {} for child in node.get_children(): sDominantTaxon = 'prokaryota' if 'eukaryota' in child.taxonomy and child.taxonomy[ 'eukaryota'] >= 0.5: sDominantTaxon = 'eukaryota' if sDominantTaxon not in dDominantTaxon2Children: dDominantTaxon2Children[sDominantTaxon] = [] dDominantTaxon2Children[sDominantTaxon].append(child) if len(dDominantTaxon2Children) > 1: for (sDominantTaxon, lDominantTaxonChildren) in dDominantTaxon2Children.items(): if len(lDominantTaxonChildren) > 1: newChild = Tree() newChild.dist = min( map(lambda x: x.dist, node.get_children())) / 2. for child in lDominantTaxonChildren: child.dist -= newChild.dist newChild.add_child(child) node.remove_child(child) node.add_child(newChild) return node
def generate_newick(): in1 = Tree() in2 = in1.add_child() oc = in2.add_child(name='Oryza coarctata') in3 = in2.add_child() in4 = in3.add_child() in5 = in3.add_child() in6 = in4.add_child() in7 = in4.add_child() in8 = in6.add_child() in9 = in6.add_child() in10 = in8.add_child() ogl = in8.add_child(name='Oryza glaberrima') os = in10.add_child(name='Oryza sativa') in11 = in10.add_child() on = in11.add_child(name='Oryza nivara') orr = in11.add_child(name='Oryza rufipogon') op = in9.add_child(name='Oryza punctata') om = in9.add_child(name='Oryza minuta') oo = in7.add_child(name='Oryza officinalis') oa = in7.add_child(name='Oryza alta') oau = in5.add_child(name='Oryza australiensis') in13 = in1.add_child() ob = in13.add_child(name='Oryza brachyantha') orri = in13.add_child(name='Oryza ridleyi') print(in1.write())
def to_tree_node(self): t = Tree(f"{self.function_type};", format=1) for child in self.children: t.add_child(child.to_tree_node()) tf = TextFace(f"{self.function_type}") tf.rotation = -90 t.add_face(tf, column=1, position="branch-top") return t
def build_conv_topo(annotated_tree, vnodes): tconv = annotated_tree.copy(method="deepcopy") for n in tconv.iter_leaves(): n.add_features(L=1) for n in tconv.traverse(): n.add_features(COPY=0) # get the most recent ancestral node of all the convergent clades l_convergent_clades = tconv.search_nodes(T=True) common_anc_conv=tconv.get_common_ancestor(l_convergent_clades) # duplicate it at its same location (branch lenght = 0). we get # a duplicated subtree with subtrees A and B (A == B) dist_dup = common_anc_conv.dist if not common_anc_conv.is_root(): dup_point = common_anc_conv.add_sister(name="dup_point",dist=0.000001) dup_point_root = False else: dup_point = Tree() dup_point_root = True dup_point.dist=0.000001 dup_point.add_features(ND=0,T=False, C=False, Cz=False) common_anc_conv.detach() common_anc_conv_copy = common_anc_conv.copy(method="deepcopy") # tag duplicated nodes: for n in common_anc_conv_copy.traverse(): n.COPY=1 if n.ND not in vnodes and not n.is_root(): n.dist=0.000001 # pruned A from all branches not leading to any convergent clade l_leaves_to_keep_A = common_anc_conv.search_nodes(COPY=0, C=False, L=1) #logger.debug("A: %s",l_leaves_to_keep_A) common_anc_conv.prune(l_leaves_to_keep_A, preserve_branch_length=True) # pruned B from all branches not leading to any non-convergent clade l_leaves_to_keep_B = common_anc_conv_copy.search_nodes(COPY=1, C=True, L=1) #logger.debug("B : %s", l_leaves_to_keep_B) common_anc_conv_copy.prune(l_leaves_to_keep_B, preserve_branch_length=True) dup_point.add_child(common_anc_conv_copy) dup_point.add_child(common_anc_conv) tconv = dup_point.get_tree_root() nodeId = 0 for node in tconv.traverse("postorder"): node.ND = nodeId nodeId += 1 return tconv
def to_tree_node(self): t = Tree("IF;", format=1) t.add_child(self.children[1].to_tree_node()) t.add_child(self.children[0].to_tree_node()) t.add_child(self.children[2].to_tree_node()) tf = TextFace("IF") tf.rotation = -90 t.add_face(tf, column=1, position="branch-top") return t
def joinSubtree(maxTreeX, maxTreeY, maxNodeY, maxSimilarity, fastaFiles, connectMethod): file1Tree = Tree('trees_rooted/RAxML_nodeLabelledRootedTree.' + maxTreeX, format=8) file2Tree = Tree('trees_rooted/RAxML_nodeLabelledRootedTree.' + maxTreeY, format=8) # print("MOST RECENT JOIN: " + maxTreeX + " and " + maxTreeY) # print joinedFastaName = maxTreeX.split('.')[0] + "-" + maxTreeY # print(file2Tree.write(format=8)) # print(maxNodeY) if connectMethod == 0: target = file2Tree.search_nodes(name=maxNodeY) if target[0].is_root(): name = randomString(5) root = Tree(name + ";") root.add_child(file1Tree) root.add_child(file2Tree) ofile = open('trees_unrooted/RAxML_bestTree.R_' + joinedFastaName, "w") ofile.write(root.write(format=1)) ofile.close() else: parent = target[0].up target[0].detach() new = parent.add_child(name=randomString(5)) new.add_child(file1Tree) new.add_child(target[0]) ofile = open('trees_unrooted/RAxML_bestTree.R_' + joinedFastaName, "w") ofile.write(file2Tree.write(format=1)) ofile.close() # if connectMethod == 1: # name =randomString(5) # root = Tree(name + ";") # root.add_child(file1Tree) # root.add_child(file2Tree) # ofile = open('trees_unrooted/RAxML_bestTree.R_' + joinedFastaName, "w") # print(root.write(format=1)) # ofile.write(root.write(format = 1)) # ofile.close() fastaFiles.remove(maxTreeX) fastaFiles.remove(maxTreeY) fastaFiles.append(joinedFastaName) joinFasta(maxTreeX, maxTreeY) return fastaFiles
def random_tree(nodes, mean_log_distance=0, std_log_distance=1): working_nodes = [Tree(name=n) for n in nodes] internal_node_count = 0 while len(working_nodes) > 1: left = working_nodes.pop(0) right = working_nodes.pop(0) new = Tree(name='node%d' % internal_node_count) d1, d2 = np.exp(mean_log_distance + std_log_distance * np.random.randn(2)) new.add_child(left, dist=d1) new.add_child(right, dist=d2) internal_node_count += 1 working_nodes.append(new) return working_nodes[0]
def uneven_tree(nodes, l1=1, l2=2): """ Arrange nodes in a systematically uneven tree. """ working_nodes = [Tree(name=n) for n in nodes] internal_node_count = 0 while len(working_nodes) > 1: left = working_nodes.pop(0) right = working_nodes.pop(0) new = Tree(name='node%d' % internal_node_count) new.add_child(left, dist=l1) new.add_child(right, dist=l2) internal_node_count += 1 working_nodes.append(new) return working_nodes[0]
def attach_new_node(distance_matrix, node_a, node_b, index_a, index_b): new_node = Tree(name=node_a.name + "," + node_b.name) #### branch length estimation n = distance_matrix.shape[0] b_a_u = 0.5 * ( distance_matrix[index_a][index_b] + (1.0 / (n - 2)) * (sum(distance_matrix[index_a]) - sum(distance_matrix[index_b]))) b_b_u = distance_matrix[index_a][index_b] - b_a_u #### Concatenate the subtrees to the new node as the new parent new_node.add_child(node_a) new_node.add_child(node_b) #### Assign the estiamted branch lengths to the nodes node_a.dist = b_a_u node_b.dist = b_b_u return new_node
def tree(glottocodes, gl_repos): label_pattern = re.compile("'[^\[]+\[([a-z0-9]{4}[0-9]{4})[^']*'") def rename(n): n.name = label_pattern.match(n.name).groups()[0] n.length = 1 glottocodes = set(glottocodes) glottocodes_in_global_tree = set() languoids = {} families = [] for lang in Glottolog(gl_repos).languoids(): if not lang.lineage: # a top-level node if not lang.category.startswith('Pseudo '): families.append(lang) languoids[lang.id] = lang glob = Tree() glob.name = 'glottolog_global' for family in families: node = family.newick_node(nodes=languoids) node.visit(rename) langs_in_tree = set(n.name for n in node.walk()) langs_selected = glottocodes.intersection(langs_in_tree) if not langs_selected: continue tree = Tree("({0});".format(node.newick), format=3) tree.name = 'glottolog_{0}'.format(family.id) if family.level.name == 'family': tree.prune([n for n in langs_selected]) glottocodes_in_global_tree = glottocodes_in_global_tree.union( set(n.name for n in tree.traverse())) else: glottocodes_in_global_tree = glottocodes_in_global_tree.union( langs_in_tree) glob.add_child(tree) # global nodes = glottocodes_in_global_tree.intersection(glottocodes) glob.prune([n for n in nodes]) return glob.write(format=9), nodes
def keep_sis_genes_together(duplicated_sp_subtree, outgr, sister_outgroup_genes, outgroup_subtree, node_max='node_max'): """ Keeps genes of all outgroup species together when modifying a gene tree, so that the new tree remains species tree consistent for these species that branch between the outgroup and duplicated species. Args: duplicated_sp_subtree (ete3.Tree) : Synteny-corrected subtree outgr (str) : name of the non-duplicated outgroup gene sister_outgroup_genes (list of str) : genes that are grouped with the outgroup gene in the original tree and in related species outgroup_subtree (ete3.Tree) : subtree with only outgroup and related genes node_max (str, optional) : internal node name in the outgroup subtree where to paste the duplicated species subtree. If empty a new tree combining both is created. Returns: ete3.Tree : a new tree where the outgroup gene in the synteny-corrected is replaced by the subtree of all outgroup genes """ outgr_subtree_leaves = [i.name for i in outgroup_subtree.get_leaves() if i.name in\ sister_outgroup_genes] leaves_to_prune = [ i.name for i in duplicated_sp_subtree.get_leaves() if i.name != outgr ] if node_max: outgroup_subtree.prune(outgr_subtree_leaves + [node_max]) node = outgroup_subtree.search_nodes(name=node_max)[0] node.name = '' duplicated_sp_subtree.prune(leaves_to_prune) node.add_child(duplicated_sp_subtree.copy()) duplicated_sp_subtree = outgroup_subtree.copy() else: outgroup_subtree.prune(outgr_subtree_leaves) duplicated_sp_subtree.prune(leaves_to_prune) new = Tree() new.add_child(outgroup_subtree) new.add_child(duplicated_sp_subtree.copy()) duplicated_sp_subtree = new return duplicated_sp_subtree
def displayClusterTree(node_dict, cluster_info, count_leaves, genome_id_to_name, leaves_taxonomy, show_unvalid_branches=False, color={}, node_styles={}): #cluster info is a dict with information on the cluster # with inject into the constructETEtree fct: the shared_taxonomy of the cluster, the genome ids, valid branches t = Tree() root_name = cluster_info["shared_taxonomy"].split(';')[-1] root = t.add_child(name=str(count_leaves[root_name]) + ':' + color['node'] + root_name + color['end']) children = node_dict[root_name] # print('children node', {c for c in children if not c.isdigit() }) # print('valid branches',set(cluster_info['valid_branches'].split('|') )) # print('unvalid branch', cluster_info['unvalid_branches'] ) cluster_info['unvalid_branches'] = set({ c for c in children if not c.isdigit() }) - set(cluster_info['valid_branches'].split('|')) if show_unvalid_branches: cluster_info['unvalid_branches'] = set() constructETEtree(node_dict, count_leaves, genome_id_to_name, color, root, children, cluster_info, node_styles) print(t.get_ascii(show_internal=True)) for k in cluster_info: print(k, cluster_info[k]) return t
def tree_vis(hier_name): # build an tree t = Tree() file_object2 = open(hier_name, 'r') lines = file_object2.readlines() if (len(lines) < 5): return 0 file_object1 = open(hier_name, 'r') try: count = 0 while True: line = file_object1.readline() if line: L = line.split() if L[2] != "null": L[1] = L[1] + " " + L[2] if count < 2: L[1] = t.add_child(name=L[1], dist=1000, support=1000) else: node = t.search_nodes(name=L[0])[0] L[1] = node.add_child(name=L[1], dist=1000, support=1000) count = count + 1 else: break finally: file_object1.close() return t print(t)
def getTree(sentence): nlp = spacy.load("en_core_web_sm") doc = nlp(sentence) for phrase in list(doc.noun_chunks): phrase.merge(phrase.root.tag_, phrase.root.lemma_, phrase.root.ent_type_) dep_tree = Tree() ''' for token in doc: print(token.text + " || " + token.dep_ + " || " + token.head.text) print("---------------------------------------------") ''' for token in doc: if token.dep_ == "ROOT": root = dep_tree.add_child(name=token) root.add_features(dep=token.dep_) generate_tree(root, doc) print (dep_tree.get_ascii(attributes=["name","dep"])) # f = open("text.txt","w") # f.write(dep_tree.get_ascii(attributes=["name","dep"])) # f.write(dep_tree.get_ascii(attributes=["name"])) # f.close() # print("\n") #print(dep_tree.write(format=8)) #print(dep_tree.get_ascii(attributes=["name","dep"])) # print("\n") return dep_tree
def extend(t, P, Q): n = len(t) s = canonical_newick_string(t.write(format=9)) # 1: add the leaf to an external arc # P[m+1] = factor(((1-a)/(m-a))*P[m]) leaves = [node for node in t] for node in leaves: x = node.add_child(name=node.name) y = node.add_child(name=n + 1) z = t.copy() # print "case 1", s, z.write(format=9) Q[canonical_newick_string(z.write(format=9))] = factor( ((1 - a) / (n - a)) * P[s]) x.detach() y.detach() # 2: add the leaf to an internal arc # P[m+1] = factor((g/(m-a))*P[m]) internal = [node for node in t.iter_descendants() if not node.is_leaf()] for node in internal: node.add_features(name="internal") x = t.copy("deepcopy") node.del_feature("name") y = x.search_nodes(name="internal")[0] parent = y.up old = y.detach() new = Tree() new.add_child(old) new.add_child(name=n + 1) parent.add_child(new) # print "case 2", s, x.write(format=9) Q[canonical_newick_string(x.write(format=9))] = factor( (g / (n - a)) * P[s]) # 3: add the leaf to a new root # P[m+1] = factor(g/(m-a)*P[m]) x = t.copy() y = Tree() y.add_child(x) y.add_child(name=n + 1) # print "case 3", s, y.write(format=9) Q[canonical_newick_string(y.write(format=9))] = factor(g / (n - a) * P[s]) # 4: add the leaf to the root # P[m+1] = factor(((len(t.children)-1)*a-g)/(m-a)*P[m]) if not t.is_leaf(): x = t.copy() x.add_child(name=n + 1) # print "case 4r", s, x.write(format=9) Q[canonical_newick_string(x.write(format=9))] = factor( ((len(t.children) - 1) * a - g) / (n - a) * P[s]) # 4: add the leaf to an internal node # P[m+1] = factor(((len(node.children)-1)*a-g)/(m-a)*P[m]) for node in t.iter_descendants(): if not node.is_leaf(): y = node.add_child(name=n + 1) # print "case 4i", s, t.write(format=9) Q[canonical_newick_string(t.write(format=9))] = factor( ((len(node.children) - 2) * a - g) / (n - a) * P[s]) y.detach()
def generate(taxa): if len(taxa) == 3: return [Tree('(' + ','.join(taxa) + ');')] else: res = [] sister = Tree('(' + taxa[-1] + ');') for tree in generate(taxa[:-1]): for node in tree.traverse('preorder'): if not node.is_root(): node.up.add_child(sister) node.detach() sister.add_child(node) res.append(copy.deepcopy(tree)) node.detach() sister.up.add_child(node) sister.detach() return res
def write_resolved_tree(orthog_tree, outgr_gene_name, out): """ Writes solution trees for orthogroup with only 2 genes. Args: orthogroup tree (ete3.Treeode) : Node with the 2 descendants of the orthogroup. outgr_gene_name (str): full outgroup gene name (with species tag). outfile (str): filename to write the tree. """ new_tree = Tree() new_tree.add_child(orthog_tree) new_tree.add_child(name=outgr_gene_name) new_tree.prune([i for i in new_tree.get_leaves()]) new_tree.write(outfile=out, format=1)
def makeNewDistanceMatrix(n, seqStringList, distanceMatrix, i, j, dictPos, dictTree): newMatrix = [] rows = n columns = rows for row in range(rows + 1): rowScore = [] for column in range(columns + 1): if row == 0 and column == 0: rowScore.append("~") elif row == 0: rowScore.append(seqStringList[column - 1]) #On spécifie la valeur du noeud dans la nouvelle matrix (oldVal,newVal) if seqStringList[column - 1] in dictPos: dictPos[seqStringList[column - 1]] = (dictPos[seqStringList[column - 1]][0], column) else: # On doit créer un nouvel entrée pour le merge dictPos[seqStringList[column - 1]] = (column, column) t = Tree() t.add_child(dictTree[distanceMatrix[i][0]]) t.add_child(dictTree[distanceMatrix[0][j]]) t.add_features(name=seqStringList[column - 1], dist=0) dictTree[seqStringList[column - 1]] = t #On doit inactiver les anciennes valeurs elif column == 0: rowScore.append(seqStringList[row - 1]) elif row != i and column != i and row != column: rowScore.append(distanceMatrix[dictPos[seqStringList[ row - 1]][0]][dictPos[seqStringList[column - 1]][0]]) else: rowScore.append(0) newMatrix.append(rowScore) for row in range(rows + 1): # On met à jour les anciens indices dictPos[seqStringList[row - 1]] = (dictPos[seqStringList[row - 1]][1], dictPos[seqStringList[row - 1]][1]) return newMatrix, dictPos, dictTree
def even_tree(nodes, edge_length=1.0): """ Arrange the nodes in a roughly uniform tree, for testing purposes. Arguments: nodes - list of strings giving the names of the leaves. edge_length - length of each edge in the tree (default 1.0) """ working_nodes = [Tree(name=n) for n in nodes] internal_node_count = 0 while len(working_nodes) > 1: left = working_nodes.pop(0) right = working_nodes.pop(0) new = Tree(name='node%d' % internal_node_count) new.add_child(left, dist=1) new.add_child(right, dist=1) internal_node_count += 1 working_nodes.append(new) return working_nodes[0]
class Pool: def __init__(self, strains, hinit): self.strains = strains self.tree = Tree() #initialise empty tree object self.d_var = {} #dictionary to reference each genome in global tree self.d_var["S{0}".format(0)] = self.tree.add_child( name="0", dist=self.strains[0].t_birth) #add root node to tree self.d_var["S{0}".format(0)].add_features( t_birth=self.strains[0].t_birth, hospital=hinit) #add attributes to the root node def tree(self): return(self.tree) def __repr__(self): #set how to display complete output return self.__str__() def __str__(self): #set how to display readable output to user return 'Pool(strains = '+str(self.strains)+')' def add_new_strain(self, mut_rate, index, t, hosp, k, len_seq): num_muts = np.random.poisson(mut_rate*(t - self.strains[index].t_birth)) parent_strain = self.strains[index] if num_muts > 0: if k + num_muts <= len_seq: parent_strain.num_child += 1 patient_strain = Strain( parent_strain.parent + [parent_strain.num_child], 0, num_muts, t, parent_strain.gen_seq + []) #add empty list so stored in memory as new object for i in range(k,k+num_muts): #mutate num_mutations from parent strain patient_strain.gen_seq[i] = 1 self.strains.append(patient_strain) self.d_var["S{0}".format(len(self.strains)-1)] = self.d_var["S{0}".format(index)].add_child( name=".".join(str(i) for i in patient_strain.parent), dist=t - self.strains[index].t_birth) self.d_var["S{0}".format(len(self.strains)-1)].add_features( hospital=hosp, t_birth=t) #add new genome to global tree with atributes self.d_var["S{0}".format(index)] = self.d_var["S{0}".format(index)].add_child( name=self.d_var["S{0}".format(index)].name, dist=0) #duplicate partent genome to ensure bifurcating tree in output self.d_var["S{0}".format(index)].add_features(hospital=hosp,t_birth=t) return len(self.strains)-1 else: return -1 #stop run elif num_muts == 0: #no mutation event so genome observed is identical to parent self.d_var["S{0}".format(index)] = self.d_var["S{0}".format(index)].add_child( name=self.d_var["S{0}".format(index)].name, dist=t - self.strains[index].t_birth) #duplicate partent strain self.d_var["S{0}".format(index)].add_features(hospital=hosp,t_birth=t) return index
def modexec(tmp_session): if tmp_session == None: return func_node = Tree(name='Lambda Listing') client = tmp_session.client('ec2') regions = [ region['RegionName'] for region in client.describe_regions()['Regions'] ] for region in regions: ll = tmp_session.client('lambda', region_name=region) result = ll.list_functions() for function in result['Functions']: func_node.add_child(name=function['FunctionName']) return func_node
def merge_trees(str_arc_tree: str, str_bac_tree: str): str_arc_tree = gtdb_format_names(str_arc_tree) dendro_tree_arc = dendropy.Tree.get_from_string(str_arc_tree, schema='newick', rooting='force-rooted', preserve_underscores=True) max_length = 0 for edge in dendro_tree_arc.postorder_edge_iter(): if edge.length: if edge.length > max_length: max_length = edge.length str_bac_tree = gtdb_format_names(str_bac_tree) dendro_tree_bac = dendropy.Tree.get_from_string(str_bac_tree, schema='newick', rooting='force-rooted', preserve_underscores=True) for edge in dendro_tree_bac.postorder_edge_iter(): if edge.length: if edge.length > max_length: max_length = edge.length ete_tree_arc = Tree(dendro_tree_arc.as_string(schema="newick", suppress_rooting=True), format=1, quoted_node_names=True) ete_tree_bac = Tree(dendro_tree_bac.as_string(schema="newick", suppress_rooting=True), format=1, quoted_node_names=True) ete_tree = Tree(name='root') ete_tree.add_child(ete_tree_arc.get_tree_root(), dist=max_length) ete_tree.add_child(ete_tree_bac.get_tree_root(), dist=max_length) return ete_tree
def subtree(clone): '''Helper function to generate the subtree for each subclone Recursively called to include all subclones situated under given clone''' # calculate branch distance as difference between clone and parent birthdays distance = clone.birthday - clone.parent.birthday s = Tree(name=clone.ID, dist=distance) # set clone as root of subtree if log == True: size = 10*np.log10(clone.get_family_size()) else: size = clone.get_family_size() s.add_features(weight=size, rgb_color=clone.rgb_color) # create copy of subclones list and filter (this avoids the original subclones list to be filtered) sub_filtered = clone.subclones[:] if det_lim > 0: sub_filtered = list(filter(lambda subclone: subclone.get_family_size() >= det_lim, sub_filtered)) for sub in sub_filtered: st = subtree(sub) # call subtree function recursively for each subclone s.add_child(st) return s
def hard_tree_to_png(self, filepath: str): nodes = list() leaf_tmpl = '{}: y_{}' for i in range(self.leafs.shape[0]): thisNode = Tree() if self.continuous: thisNode.add_face( faces.BarChartFace( self.leafs[i].detach().cpu().numpy(), min_value=0.0, max_value=self.leafs[i].max().detach().cpu().numpy() + 1e-7, labels=self.action_labels ), 0) else: max_leaf_idx = np.argmax(self.leafs[i].detach().cpu().numpy()) thisNode.add_face(faces.TextFace( leaf_tmpl.format( self.action_labels[max_leaf_idx], max_leaf_idx)), 0) nodes.append(thisNode) node_tmpl = '{}: x_{} >= {}' for d in range(self.depth-1): for node_i in range(self.nodes_beta[d].shape[0]): thisNode = Tree() thisNode.add_child(nodes.pop(1)) thisNode.add_child(nodes.pop(0)) beta = F.softmax(self.nodes_beta[d][node_i].squeeze(), 0 ).detach().cpu().numpy() phi = self.nodes_phi[d][node_i].squeeze().detach().cpu().item() max_beta_idx = np.argmax(beta) thisNode.add_face(faces.TextFace(node_tmpl.format( self.labels[max_beta_idx], max_beta_idx, phi)), 0) nodes.append(thisNode) if filepath is not None: nodes[0].render(filepath,) return nodes[0]
def merge_trees_and_write(trees, outgr, outfile, keep_br=False): """ Merges two subtrees independently resolved into a single tree and adds the outgroup gene. Writes the result to file. Args: trees (list of ete3.Tree): Tree(s) to merge outgr (str): Outgroup gene name outfile (str): Output filename """ merged_tree = Tree() for tree in trees: merged_tree.add_child(tree) #merge the two and place outgroup correctly merged_final = Tree() merged_final.add_child(merged_tree) merged_final.add_child(name=outgr) merged_final.prune([i for i in merged_final.get_leaves()]) if keep_br: merged_final.write(outfile=outfile) else: merged_final.write(outfile=outfile, format=9)
def ConvertListofClustersToTree(cluster_list): """ FIXME: recurse this function""" t = Tree() for i, node in enumerate(cluster_list): print "Cluster %d"%i if (len(node)) >= 1: print "We have a node of size : %d" % len(node) child = t.add_child(name="Cluster %d"%i) for subnode in node: if len(subnode.child) >= 1: print "Adding subnode of size : %d"%len(subnode.child) for subsubnode in subnode.child: print "Adding subsubnode of %s"%subsubnode.child child.add_child(name=subsubnode.child, dist=subsubnode.distance) return t
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]
from ete3 import Tree # Creates an empty tree and populates it with some new # nodes t = Tree() A = t.add_child(name="A") B = t.add_child(name="B") C = A.add_child(name="C") D = A.add_child(name="D") print t # /-C # /--------| #---------| \-D # | # \-B print 'is "t" the root?', t.is_root() # True print 'is "A" a terminal node?', A.is_leaf() # False print 'is "B" a terminal node?', B.is_leaf() # True print 'B.get_tree_root() is "t"?', B.get_tree_root() is t # True print 'Number of leaves in tree:', len(t) # returns number of leaves under node (3) print 'is C in tree?', C in t # Returns true print "All leaf names in tree:", [node.name for node in t]
def final_tree(sortedDM): tree_list = [] parse_states = [chunk for chunk in sortedDM\ if chunk[0].typename == "parse_state" and\ chunk[0].daughter1 != None] words = set(str(chunk[0].form) for chunk in sortedDM\ if chunk[0].typename == "word") nodes = [chunk for chunk in parse_states if chunk[0].node_cat == "S"] while nodes: current_chunk = nodes.pop(0) current_node = str(current_chunk[0].node_cat) + " " +\ str(current_chunk[1]) current_tree = Tree(name=current_node) if current_chunk[0].daughter2 != None: child_categs = [current_chunk[0].daughter1,\ current_chunk[0].daughter2] else: child_categs = [current_chunk[0].daughter1] children = [] for cat in child_categs: if cat == 'NP': chunkFromCat = [chunk for chunk in parse_states\ if chunk[0].node_cat == cat and\ chunk[0].mother ==\ current_chunk[0].node_cat] if chunkFromCat: children += chunkFromCat current_child = str(chunkFromCat[-1][0].node_cat)\ + " " + str(chunkFromCat[-1][1]) current_tree.add_child(name=current_child) elif cat == 'ProperN': chunkFromCat = [chunk for chunk in parse_states if\ chunk[0].node_cat == cat and\ chunk[0].daughter1 ==\ current_chunk[0].lex_head] if chunkFromCat: children += chunkFromCat current_child = str(chunkFromCat[-1][0].node_cat)\ + " " + str(chunkFromCat[-1][1]) current_tree.add_child(name=current_child) elif cat in words: last_act_time = [chunk[1][-1] for chunk in dm.items()\ if chunk[0].typename == "word"\ and str(chunk[0].form) == cat] current_child = cat + " " + str(last_act_time[0]) current_tree.add_child(name=current_child) else: chunkFromCat = [chunk for chunk in parse_states\ if chunk[0].node_cat == cat] if chunkFromCat: children += chunkFromCat current_child = str(chunkFromCat[-1][0].node_cat)\ + " " + str(chunkFromCat[-1][1]) current_tree.add_child(name=current_child) tree_list.append(current_tree) nodes += children final_tree = tree_list[0] tree_list.remove(final_tree) while tree_list: leaves = final_tree.get_leaves() for leaf in leaves: subtree_list = [tree for tree in tree_list\ if tree.name == leaf.name] if subtree_list: subtree = subtree_list[0] tree_list.remove(subtree) leaf.add_sister(subtree) leaf.detach() return final_tree
from ete3 import Tree t = Tree() # Creates an empty tree A = t.add_child(name="A") # Adds a new child to the current tree root # and returns it B = t.add_child(name="B") # Adds a second child to the current tree # root and returns it C = A.add_child(name="C") # Adds a new child to one of the branches D = C.add_sister(name="D") # Adds a second child to same branch as # before, but using a sister as the starting # point R = A.add_child(name="R") # Adds a third child to the # branch. Multifurcations are supported # Next, I add 6 random leaves to the R branch names_library is an # optional argument. If no names are provided, they will be generated # randomly. R.populate(6, names_library=["r1","r2","r3","r4","r5","r6"]) # Prints the tree topology print t # /-C # | # |--D # | # /--------| /-r4 # | | /--------| # | | /--------| \-r3 # | | | | # | | | \-r5 # | \--------| # ---------| | /-r6 # | | /--------| # | \--------| \-r2
from ete3 import Tree import numpy as np t = Tree(name = "Lisa") t.add_child(name = "Alex") t.add_child(name = "Dilly") t = Tree(name = "GATTACA") def uniform_killing(pop, proportion): return filter(lambda y: np.random.rand() < proportion, pop) # filter keeps values less than proportion pop = [t.name]*10 for i in xrange(0,5): print(uniform_killing(pop,.9))
data = open(read_path+filename+'.txt').read().replace(',',' ').replace('\n',' ') x = data.split() ParentChild = np.array(x).astype(str) y = len(ParentChild)/5 ParentChild1 = np.reshape(ParentChild, (y,5)) firsttwo = ParentChild1[:,0:2] #chops off first line which encodes parameters of simulation and third column which is not yet used parents = [] children = [] for row in range(0, len(firsttwo)): for column in range(0,2): firsttwo[row,column] = 'r'+firsttwo[row,column] t = Tree() # Creates an empty tree r1 = t.add_child(name="r1") lookup = {"r1": r1} prune_list = ['r1'] for pair in sorted(firsttwo, key=sort_pairs): parentname = pair[0] childname = pair[1] if childname not in lookup: if parentname in lookup: newchild = lookup[parentname].add_child(name = childname) lookup.update({childname: newchild}) if parentname not in parents: prune_list.append(lookup[parentname]) parents.append(parentname) #make list of unique terminal nodes (no children of children) children.append(newchild) else: