def merge_Recomtrees(recomtrees, recomnodes): clonaltree = Tree.get_from_string(clonal_tree, schema='newick') set_index(clonaltree) # if one one node is the subset of the other nodes conflict = common_nodes(clonaltree, recomnodes) if (len(conflict) > 0) and (conflict[0][0] == conflict[0][1]): print("Same nodes") conflict = [] if len(conflict) == 0: print("NOOOOOO conflict!") desc = [] equ = np.zeros((len(recomtrees), 4)) for treeid in range(len(recomtrees)): rtree = Tree.get_from_string(recomtrees[treeid], schema='newick') set_index(rtree) equ[treeid, 0] = recomnodes[treeid] equ[treeid, 1:3] = give_equivalent_node(rtree) equ[treeid, 3] = treeid s_equ = equ[equ[:, 2].argsort()[::-1]] s_equ = give_older_recom(s_equ) # print(s_equ) maintree = Tree.get_from_string(recomtrees[int(s_equ[0][3])], schema='newick') set_index(maintree) for i in range(1, len(s_equ)): temptree = Tree.get_from_string(recomtrees[int(s_equ[i][3])], schema='newick') set_index(temptree) node_maintree = int(s_equ[i][0]) if node_maintree >= tips_number: node_maintree = new_mrca(clonaltree, maintree, node_maintree, int(s_equ[i - 1][0])) prunenode = maintree.find_node_with_label(str(node_maintree)) node = temptree.find_node_with_label(str(int(s_equ[i][1]))) parent = prunenode.parent_node attached_node, attached_id, ancestor = find_attached_node( maintree, node, prunenode) regraft_recomnodes(maintree, attached_node, attached_id, prunenode, node, ancestor) return maintree.as_string(schema="newick") elif len(conflict) > 0: print("CONFLICT!! ") for i in range(len(conflict)): if conflict[i][0] < tips_number: print("one node is taxa!") else: print("two nodes are internal!")
def calculate_robinson_foulds(self, species_tree, gene_tree, weighted): """ Calculates the Robinson Foulds distances for weighted and unweighted trees. Input: species_tree -- newick file or newick string containing the species tree gene_tree -- newick file or newick string containing the tree to be compared to the species tree weighted -- boolean parameter for whether the files have weights Returns: The weighted and/or unweighted Robinson Foulds distance of the species tree and input tree. """ # taxon names tns = dendropy.TaxonNamespace() # Create dendropy tree from species tree input file if os.path.isfile(species_tree): species_tree = Tree.get_from_path(species_tree, 'newick', taxon_namespace=tns) # Create dendropy tree from species tree input newick string else: species_tree = Tree.get_from_string(species_tree, 'newick', taxon_namespace=tns) # Create dendropy tree from gene tree input file if os.path.isfile(gene_tree): gene_tree = Tree.get_from_path(gene_tree, 'newick', taxon_namespace=tns) # Create dendropy tree from gene tree input newick string else: gene_tree = Tree.get_from_string(gene_tree, 'newick', taxon_namespace=tns) # both weighted and unweighted foulds distance if weighted: return treecompare.weighted_robinson_foulds_distance(species_tree, gene_tree), \ treecompare.unweighted_robinson_foulds_distance(species_tree, gene_tree) # only unweighted foulds distance else: return treecompare.unweighted_robinson_foulds_distance( species_tree, gene_tree)
def remove_internal_labels(strtree): tree = Tree.get_from_string(strtree, schema='newick') for node in tree.postorder_node_iter(): if not node.label is None: if (int(node.label) >= tips_number): node.label = None return tree.as_string(schema="newick")
def readTreeFromString(self, treeString): ''' input: string containing newick tree return Tree object ''' myTree= Tree() myTree= Tree.get_from_string( treeString, 'newick', annotations_as_nhx=True, extract_comment_metadata=True , suppress_annotations=False) return myTree
def getBalancedTreeByHeight(self, size, th): clades = int(math.log(size,2)) unit = (th*1.0) / clades self.taxCtr = 0; self.bal_newick = '' self.buildBalancedString(1,clades,unit) self.bal_newick += ';' #print(self.bal_newick) tree = Tree.get_from_string(self.bal_newick,"newick") #print(tree) tree.deroot() return tree
def getChainedTreeByHeight(self, size, th): unit = (th*1.0) / size #print('size: ' + str(size) + ' height ' + str(th) + ' unit len ' + str(unit)) newick = '' for i in range(size-1): newick += '(' newick += self.fullTaxonSet[0] + ':' + str(round(unit*2,3)) + ',' + self.fullTaxonSet[1] + ':' + str(round(unit*2,3)) + ')' for i in range(2,size): newick += str(round(unit,3)) + ',' + self.fullTaxonSet[i] + ':' + str(round(unit*(i+1),3)) + ')' newick += ';' tree = Tree.get_from_string(newick,"newick") #print(tree) tree.deroot() return tree
def WrapFastTreeRooted(self, sTree): ''' WrapFastTreeRooted() function is a wrapper over the fasttree tool. input: inputFile: sequences in seqboot output format (Phylip), seqType : d (dna); p (protein) n : Number of datasets, output: Bifucated and rooted tree in newkick format to file ''' cmd= self.Where('FastTree') infile= os.path.join(self.work_dir, self.outFile) if cmd != None: print "--> Generating newick trees using fasttree begins..." try: fastreeCmdLine = FastTreeCommandline(cmd=cmd, input=infile, n=self.n, boot=100, nosupport=True, nome= True, seed= self.seedNum, quiet= True) child = subprocess.Popen(str(fastreeCmdLine),stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=(sys.platform!="win32")) j=1 with open(os.path.join(self.work_dir, str(self.coreId)+'batch.unrooted.ini'), 'w') as bwf: bwf.write(os.path.abspath(sTree) + '\n') for i in child.stdout: outNewickFile= os.path.join(self.work_dir, str(j)+ "_ftree_coreId_" + str(self.coreId) +".newick") bwf.write(outNewickFile+ '\n') ftreeOutFile= outNewickFile with open(ftreeOutFile, 'w') as wf: tree = Tree.get_from_string(i, 'newick') tree.resolve_polytomies(update_splits=False) st=tree.as_string('newick') #remove the extra strings st='\"'+str(st)+'\"' st=st.translate(None,'\'') st=st.translate(None,'\"') wf.write(st) j += 1 except IOError, e: print ("Class: Wrapper, Function: WrapFastTreeRooted(): %s " % e) print "--> Tree generation done..."
def group_sequences(tree_file, cutoff, excluded): """Loads a tree to find sequences which can be merged.""" with open(tree_file) as handle: tree = Tree.get_from_string(handle.read(), schema = 'newick') groups = [] done = set() if excluded is not None: groups.append(set(excluded)) done = set(excluded) for t in tree.level_order_node_iter(): if t.edge.length >= cutoff: lens = [x.edge.length >= cutoff for x in t.level_order_iter() if not x.is_leaf()] if all(lens) or not any(lens) or t.is_leaf(): leafs = set([str(x.taxon) for x in t.leaf_nodes()]) groups.append(leafs-done) done |= leafs groups = [x for x in groups if x] return groups
if node.is_internal(): node_labels.append(node.index) node_weight.append(node.edge_length / tree.length()) if recom_num == 0: recom_num = 1 ideal_gap = int(alignment_len / recom_num) my_trees = [] nodes = [] starts = [] ends = [] recomlens = [] for recom_id in range(int(recom_num)): starting_falg = False tree = Tree.get_from_string(clonal_tree, schema='newick') set_index(tree) while not starting_falg: random_tip = np.random.choice(node_labels, 1, node_weight) start_pos = random.randint(ideal_gap * recom_id, ideal_gap * (recom_id + 1)) r_len = random.randint(threshold_len, recom_len + threshold_len) if (start_pos + r_len <= alignment_len): nodes.append(random_tip) starts.append(start_pos) recomlens.append(r_len) ends.append(start_pos + r_len) recom_node = tree.find_node_with_label(str(int(random_tip))) recom_tree = ex_recom_maker(tree, recom_node, nu_ex) # make external recombination my_trees.append(recom_tree)
print f dirname, basename = path.split(f) input_name = basename.rpartition('.')[0] input_core = input_name.rpartition('_')[0] seqs = read_slr(open(f)) utils.check_dir(path.join(args.outdir, args.clade, basename[:2])) out_fasta = path.abspath(path.join(args.outdir, args.clade, basename[:2], input_core+'.fa')) SeqIO.write(seqs, open(out_fasta, 'w'), 'fasta') with open(path.join(dirname, input_name+'.nwk')) as fl: _ = fl.readline() tree_string = fl.readline().rstrip() tree = Tree.get_from_string(tree_string, 'newick') #tree = Tree.get_from_path(path.join(dirname, input_name+'.nwk'), 'newick', preserve_underscores=True) # rewrite the numbered tree back to a tree with ids. # easy fasta_file = open(f, 'r') fasta = fasta_file.readlines() aln_ids = {} for index,line in enumerate(fasta,start=1): if index%2 == 0: aln_ids[(index)/2] = line.strip("\n") #print line #print (index)/2 for node in tree: if node.is_leaf():
def merge_trees_new(recomtrees, recomnodes): equ = np.zeros((len(recomtrees), 4)) for treeid in range(len(recomtrees)): rtree = Tree.get_from_string(recomtrees[treeid], schema='newick') set_index(rtree) equ[treeid, 0] = recomnodes[treeid] equ[treeid, 1:3] = give_equivalent_node(rtree) equ[treeid, 3] = treeid # print(equ) s_equ = equ[equ[:, 2].argsort()[::-1]] # s_equ = sorted(equ, key=itemgetter(1), reverse=False) print(s_equ) clonaltree = Tree.get_from_string(clonal_tree, schema='newick') set_index(clonaltree) print(clonaltree.as_ascii_plot()) print(clonaltree.as_string(schema="newick")) for node in clonaltree.postorder_node_iter(): print(node.index) for i in range(len(recomtrees)): # print(clonaltree.as_ascii_plot()) # print(clonaltree.as_string(schema="newick")) temptree = Tree.get_from_string(recomtrees[int(s_equ[i][3])], schema='newick') set_index(temptree) # print(temptree.as_ascii_plot()) # print(temptree.as_string(schema="newick")) prunenode = clonaltree.find_node_with_label(str(int(s_equ[i][0]))) print("prunenode:") print(prunenode) print(prunenode.edge_length) node = temptree.find_node_with_label(str(int(s_equ[i][1]))) print("node:") print(node) print(node.edge_length) parent = prunenode.parent_node # changing in topology to make recombination tree: # if ((node.edge_length) > parent.edge_length + prunenode.edge_length) and ((node.edge_length) < clonaltree.max_distance_from_root()): if ((node.edge_length) < clonaltree.max_distance_from_root()): # print(" *********** Stage Two ***********") ancestor = [] for id, tmp_node in enumerate(prunenode.ancestor_iter()): ancestor.append(tmp_node) # print(id ,"::::::",tmp_node.index) if node.edge_length < tmp_node.distance_from_tip(): attached_node = tmp_node attached_id = id # print(attached_node.index) break relocated_nodes = ancestor[ attached_id - 1] # relocated node is the adjacent node of recombinant node # parent.remove_child(prunenode) # the original recombinant node was removed to reinsert in the other side clonaltree.prune_subtree(prunenode) # print(clonaltree.as_ascii_plot()) # print(clonaltree.as_string(schema="newick")) attached_node.remove_child( relocated_nodes ) # relocated node was removed to reinsert in to right side newborn = dendropy.datamodel.treemodel.Node( ) # newborn in the new mrca of recombinant node and its sister # newborn.edge_length = attached_node.distance_from_tip() - node.edge_length # print("-----------------------------------:",node.distance_from_tip()) # newborn.add_child(node) newborn.add_child(prunenode) prunenode.edge_length = node.edge_length newborn.edge_length = clonaltree.max_distance_from_root() - ( node.edge_length + node.distance_from_tip()) relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length newborn.add_child(relocated_nodes) attached_node.add_child(newborn) # print(clonaltree.as_ascii_plot()) # print(clonaltree.as_string(schema="newick")) # changeing in topology when recombiantion is larger than tree.max_distance_from_root() if node.edge_length > clonaltree.max_distance_from_root(): # print(" *********** Stage Three ***********") new_tree = dendropy.Tree(taxon_namespace=taxa) if node.is_leaf(): print("is leaf") tree2 = clonaltree.extract_tree_without_taxa_labels( prunenode.label) other_nodes = tree2.seed_node new_tree.seed_node.add_child(other_nodes) new_tree.seed_node.add_child(node) other_nodes.edge_length = node.edge_length - clonaltree.max_distance_from_root( ) clonaltree = new_tree print(clonaltree.as_ascii_plot()) print(clonaltree.as_string(schema="newick")) else: print("is internal") # clonaltree.prune_subtree(node) clonaltree.prune_subtree(prunenode) other_nodes = clonaltree.seed_node new_tree.seed_node.add_child(other_nodes) new_tree.seed_node.add_child(node) other_nodes.edge_length = node.edge_length + node.distance_from_tip( ) - other_nodes.distance_from_tip() clonaltree = new_tree for node in clonaltree.postorder_node_iter(): print(node.index) print(clonaltree.as_ascii_plot()) print(clonaltree.as_string(schema="newick")) return clonaltree.as_string(schema="newick")
def full_merge_trees(recomtrees, recomnodes): desc = [] equ = np.zeros((len(recomtrees), 4)) for treeid in range(len(recomtrees)): rtree = Tree.get_from_string(recomtrees[treeid], schema='newick') set_index(rtree) equ[treeid, 0] = recomnodes[treeid] equ[treeid, 1:3] = give_equivalent_node(rtree) equ[treeid, 3] = treeid s_equ = equ[equ[:, 2].argsort()[::-1]] print(s_equ) s_equ = give_older_recom(s_equ) # print(s_equ) clonaltree = Tree.get_from_string(clonal_tree, schema='newick') set_index(clonaltree) maintree = Tree.get_from_string(recomtrees[int(s_equ[0][3])], schema='newick') set_index(maintree) # print(maintree.as_ascii_plot()) # print(maintree.as_string(schema="newick")) for i in range(1, len(s_equ)): # print(maintree.as_ascii_plot()) # print(maintree.as_string(schema="newick")) temptree = Tree.get_from_string(recomtrees[int(s_equ[i][3])], schema='newick') set_index(temptree) print(temptree.as_ascii_plot()) print(temptree.as_string(schema="newick")) node_maintree = int(s_equ[i][0]) print(node_maintree) print(s_equ[i - 1][0]) if node_maintree >= tips_number: node_maintree = new_mrca(clonaltree, maintree, node_maintree, int(s_equ[i - 1][0])) print("node_maintree") print(node_maintree) prunenode = maintree.find_node_with_label(str(node_maintree)) print("prunenode:") print(prunenode) print(prunenode.edge_length) node = temptree.find_node_with_label(str(int(s_equ[i][1]))) print("node:") print(node) print(node.edge_length) parent = prunenode.parent_node print("parent:") print(parent) if ((node.edge_length) < maintree.max_distance_from_root()): # print(" *********** Stage Two ***********") ancestor = [] for id, tmp_node in enumerate(prunenode.ancestor_iter()): ancestor.append(tmp_node) # print(id ,"::::::",tmp_node.index) if node.edge_length < tmp_node.distance_from_tip(): attached_node = tmp_node attached_id = id print("attached_node") print(attached_node) break if (node_maintree < tips_number): relocated_nodes = ancestor[ attached_id - 1] # relocated node is the adjacent node of recombinant node print("relocated_nodes:") print(relocated_nodes) maintree.prune_subtree( prunenode ) # the original recombinant node was removed to reinsert in the other side attached_node.remove_child( relocated_nodes ) # relocated node was removed to reinsert in to right side newborn = dendropy.datamodel.treemodel.Node( ) # newborn in the new mrca of recombinant node and its sister newborn.add_child(prunenode) prunenode.edge_length = node.edge_length if attached_node.edge_length is None: attached_node.edge_length = 0 newborn.edge_length = maintree.max_distance_from_root() - ( node.edge_length + attached_node.edge_length) relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length newborn.add_child(relocated_nodes) attached_node.add_child(newborn) else: relocated_nodes = ancestor[ attached_id - 1] # relocated node is the adjacent node of recombinant node print("relocated_nodes:") print(relocated_nodes) maintree.prune_subtree( prunenode ) # the original recombinant node was removed to reinsert in the other side attached_node.remove_child( relocated_nodes ) # relocated node was removed to reinsert in to right side newborn = dendropy.datamodel.treemodel.Node( ) # newborn in the new mrca of recombinant node and its sister newborn.add_child(prunenode) prunenode.edge_length = node.edge_length newborn.edge_length = maintree.max_distance_from_root() - ( node.edge_length + node.distance_from_tip()) relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length newborn.add_child(relocated_nodes) attached_node.add_child(newborn) print(maintree.as_ascii_plot()) print(maintree.as_string(schema="newick")) return maintree.as_string(schema="newick")
def read_from_string(tree_str, schema="newick", taxon_set=None): taxon = taxon_set if taxon is None: taxon = TaxonSet() return PhylogeneticTree(Tree.get_from_string(tree_str, schema=schema, taxon_set=taxon))
def appendTo(nwk, label, tlist): tre = Tree.get_from_string(nwk, 'newick') tre.label = label tlist.append(tre)
def resolve_conflict(clonaltree, recomtrees, recomnodes): conflict = common_nodes(clonaltree, recomnodes) print(recomnodes) print(conflict) if len(conflict) > 0: for i in range(len(conflict)): # print(conflict[i][0], conflict[i][1]) node1 = clonaltree.find_node_with_label(str(conflict[i][0])) node2 = clonaltree.find_node_with_label(str(conflict[i][1])) if node1.parent_node == node2: id_main = recomnodes.index(conflict[i][1]) id_temp = recomnodes.index(conflict[i][0]) maintree = Tree.get_from_string(recomtrees[id_main], schema='newick') set_index(maintree) temptree = Tree.get_from_string(recomtrees[id_temp], schema='newick') set_index(temptree) if int(node1.index) < tips_number: # print("node is taxa") prunenode = maintree.find_node_with_label(str(node1.index)) recomnode = temptree.find_node_with_label(str(node1.index)) else: # print("node is internal") res = set() desc = give_descendents(clonaltree, node1.index, res) prune_index = my_mrca(maintree, list(desc)) prunenode = maintree.find_node_with_label(str(prune_index)) recom_index = my_mrca(temptree, list(desc)) recomnode = temptree.find_node_with_label(str(recom_index)) # print(maintree.as_ascii_plot()) # print(maintree.as_string(schema="newick")) # print("prunenode:") # print(prunenode) # print(prunenode.edge_length) # print("recomnode:") # print(recomnode) # print(recomnode.edge_length) parent = prunenode.parent_node # print("parent") # print(parent) if ((recomnode.edge_length) > parent.distance_from_tip()) and ((recomnode.edge_length) < maintree.max_distance_from_root()): maintree.prune_subtree(prunenode) # the original recombinant node was removed to reinsert in the other side res = set() desc = give_descendents(clonaltree, node2.index, res) d = list(desc) random_node = np.random.choice(taxon_list,replace=False) while int(random_node) in d: random_node = np.random.choice(taxon_list,replace=False) candidatenode = maintree.find_node_with_label(str(random_node)) attached_node, attached_id, ancestor = find_attached_node(maintree, recomnode, candidatenode) relocated_nodes = ancestor[attached_id - 1] # relocated node is the adjacent node of recombinant node attached_node.remove_child(relocated_nodes) # relocated node was removed to reinsert in to right side newborn = dendropy.datamodel.treemodel.Node() # newborn in the new mrca of recombinant node and its sister newborn.add_child(recomnode) if attached_node.edge_length is None: attached_node.edge_length = 0 newborn.edge_length = maintree.max_distance_from_root() - (recomnode.edge_length + attached_node.edge_length) relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length newborn.add_child(relocated_nodes) attached_node.add_child(newborn) # print(maintree.as_string(schema="newick")) # print(maintree.as_ascii_plot()) return maintree.as_string(schema="newick")
def resolve_conflict(clonaltree, recomtrees, recomnodes): conflict = common_nodes(clonaltree, recomnodes) print(recomnodes) if len(conflict) > 0: for i in range(len(conflict)): print(conflict[i][0], conflict[i][1]) node1 = clonaltree.find_node_with_label(str(conflict[i][0])) node2 = clonaltree.find_node_with_label(str(conflict[i][1])) if node1.parent_node == node2: id_main = recomnodes.index(conflict[i][1]) id_temp = recomnodes.index(conflict[i][0]) maintree = Tree.get_from_string(recomtrees[id_main], schema='newick') set_index(maintree) temptree = Tree.get_from_string(recomtrees[id_temp], schema='newick') set_index(temptree) if int(node1.index) < tips_number: print("node is taxa") prunenode = node1 recomnode = temptree.find_node_with_label(str(node1.index)) else: print("node is internal") res = set() desc = give_descendents(clonaltree, node1.index, res) prune_index = my_mrca(maintree, list(desc)) prunenode = maintree.find_node_with_label(str(prune_index)) recom_index = my_mrca(temptree, list(desc)) recomnode = temptree.find_node_with_label(str(recom_index)) print(maintree.as_ascii_plot()) print(maintree.as_string(schema="newick")) print("prunenode:") print(prunenode) print(prunenode.edge_length) print("recomnode:") print(recomnode) print(recomnode.edge_length) parent = prunenode.parent_node print("parent") print(parent) if ((recomnode.edge_length) > parent.distance_from_tip()) and ( (recomnode.edge_length) < tree.max_distance_from_root()): ancestor = [] for id, tmp_node in enumerate(prunenode.ancestor_iter()): ancestor.append(tmp_node) # print(id ,"::::::",tmp_node.index) if recomnode.edge_length < tmp_node.distance_from_tip( ): attached_node = tmp_node attached_id = id # print(attached_node.index) break relocated_nodes = ancestor[ attached_id - 1] # relocated node is the adjacent node of recombinant node parent.remove_child( node ) # the original recombinant node was removed to reinsert in the other side attached_node.remove_child( relocated_nodes ) # relocated node was removed to reinsert in to right side newborn = dendropy.datamodel.treemodel.Node( ) # newborn in the new mrca of recombinant node and its sister # newborn.edge_length = attached_node.distance_from_tip() - recom_length # node.edge_length = recom_length newborn.add_child(node) relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length newborn.add_child(relocated_nodes) attached_node.add_child(newborn) print(maintree.as_string(schema="newick")) # if ((recomnode.edge_length) < maintree.max_distance_from_root()): # print(" *********** Stage Two ***********") # ancestor = [] # attached_id = 0 # for id, tmp_node in enumerate(prunenode.ancestor_iter()): # # print(temp_node) # ancestor.append(tmp_node) # if recomnode.edge_length < tmp_node.distance_from_tip(): # attached_node = tmp_node # attached_id = id # break # if attached_id == 0 : # parent.remove_child(prunenode) # else: # if (int(recomnode.index) < tips_number): # print("step child") # relocated_nodes = ancestor[attached_id-1] # relocated node is the adjacent node of recombinant node # maintree.prune_subtree(prunenode) # the original recombinant node was removed to reinsert in the other side # attached_node.remove_child(relocated_nodes) # relocated node was removed to reinsert in to right side # newborn = dendropy.datamodel.treemodel.Node() # newborn in the new mrca of recombinant node and its sister # newborn.add_child(prunenode) # prunenode.edge_length = recomnode.edge_length # if attached_node.edge_length is None: # attached_node.edge_length = 0 # newborn.edge_length = maintree.max_distance_from_root() - (recomnode.edge_length + attached_node.edge_length) # relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length # newborn.add_child(relocated_nodes) # attached_node.add_child(newborn) # else: # print("step internal") # relocated_nodes = ancestor[attached_id-1] # relocated node is the adjacent node of recombinant node # if attached_node == maintree.seed_node: # print("root") # maintree.prune_subtree(relocated_nodes) # new_tree = dendropy.Tree(taxon_namespace=taxa) # other_nodes = maintree.seed_node # new_tree.seed_node.add_child(other_nodes) # newborn = dendropy.datamodel.treemodel.Node() # newborn.add_child(recomnode) # newborn.add_child(relocated_nodes) # newborn.edge_length = (relocated_nodes.edge_length +relocated_nodes.distance_from_tip()) - (recomnode.edge_length + recomnode.distance_from_tip()) # relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length # new_tree.seed_node.add_child(newborn) # new_tree.prune_subtree(prunenode) # elif not attached_node == maintree.seed_node: # print("not root") # maintree.prune_subtree(prunenode) # the original recombinant node was removed to reinsert in the other side # attached_node.remove_child(relocated_nodes) # relocated node was removed to reinsert in to right side # newborn = dendropy.datamodel.treemodel.Node() # newborn in the new mrca of recombinant node and its sister # newborn.add_child(recomnode) # newborn.edge_length = maintree.max_distance_from_root() - (recomnode.edge_length + recomnode.distance_from_tip()) # relocated_nodes.edge_length = relocated_nodes.edge_length - newborn.edge_length # newborn.add_child(relocated_nodes) # attached_node.add_child(newborn) print(maintree.as_ascii_plot()) print(maintree.as_string(schema="newick"))
def topology_counter(self, rooted=False, outgroup=None): """ Counts the number of times that each topology appears as outputted by running RAxML. Output: topologies_to_counts --- a dictionary mapping topologies to the number of times they appear """ # Initialize a dictionary mapping newick strings to unique topologies unique_topologies_to_newicks = {} # taxon names tns = dendropy.TaxonNamespace() # Create a set of unique topologies unique_topologies = set([]) # Get the topology files from the "Topologies" folder input_directory = "Topologies" # Initialize topology_count to a defaultdict topologies_to_counts = defaultdict(int) # Iterate over each file in the given directory for filename in os.listdir(input_directory): # Create a boolean flag for determining the uniqueness of tree new_tree_is_unique = True # If file is the file with the best tree newick string if os.path.splitext(filename)[0] == "Topology_bestTree": input_file = os.path.join(input_directory, filename) new_tree = Tree.get_from_path(input_file, 'newick', taxon_namespace=tns) if rooted: outgroup_node = new_tree.find_node_with_taxon_label( outgroup) new_tree.to_outgroup_position(outgroup_node, update_bipartitions=False) # Iterate over each topology in unique_topologies for unique_topology in unique_topologies: # Create a tree for each of the unique topologies calculate RF distance compared to new_tree unique_tree = Tree.get_from_string(unique_topology, 'newick', taxon_namespace=tns) rf_distance = treecompare.unweighted_robinson_foulds_distance( unique_tree, new_tree) # If the RF distance is 0 then the new tree is the same as one of the unique topologies if rf_distance == 0: topologies_to_counts[unique_topology] += 1 new_tree_is_unique = False new_tree = new_tree.as_string("newick").replace( "\n", "") unique_topologies_to_newicks[unique_topology].add( new_tree) break # If the new tree is a unique tree add it to the set of unique topologies if new_tree_is_unique: new_tree = new_tree.as_string("newick").replace("\n", "") unique_topologies.add(new_tree) topologies_to_counts[new_tree] += 1 unique_topologies_to_newicks[new_tree] = set([new_tree]) return topologies_to_counts, unique_topologies_to_newicks
def tree(): return Tree.get_from_string('(((A, B), (C, D)), ((E, F), (G, H)));', schema='newick')
return 1 elif c == "G": return 2 elif c == "T": return 3 # ======================================================================================================= dna = 'CGCC' tips = 4 #tips number br_length = 0.1 p = np.zeros((4, 4)) # tree = Tree.get_from_path('/home/nehleh/0_Research/PhD/Data/tree.tre', 'newick') (3,4,(1,2)5)6; tree = Tree.get_from_string('((1,2)5,3,4)6;', 'newick') # internal_idx = len(tree.taxon_namespace) # print(internal_idx) partial = np.zeros((4, 2 * tips)) for i in range(4): for j in range(4): p[i][j] = 0.25 - 0.25 * math.exp(-4 * br_length / 3) p[i][i] = 0.25 + 0.75 * math.exp(-4 * br_length / 3) # pprint.pprint(p) for node in tree.postorder_node_iter(): node.index = -1