def reroot_tree(tstr): """Input: a tree path to a Newick tree. Output: a re-rooted version of the tree, based on the outgroup defined in configuration.py""" t = Tree() t.read_from_string(tstr.__str__(), "newick") og = ap.params["outgroup"] og = re.sub("\[", "", og) og = re.sub("\]", "", og) og = re.sub("\"", "", og) ogs = og.split(",") mrca = t.mrca(taxon_labels=ogs) t.reroot_at_edge(mrca.edge, update_splits=False) ret = t.as_string("newick") ret = re.sub("\[\&\R\] ", "", ret) ret = ret.strip() return ret
def reroot_newick(con, newick): """Provide a newick string, this method will re-root the tree based on the 'outgroup' setting.""" cur = con.cursor() dendrotree = Tree() dendrotree.read_from_string(newick, "newick") sql = "select shortname from Taxa where id in (select taxonid from GroupsTaxa where groupid in (select id from TaxaGroups where name='outgroup'))" cur.execute(sql) rrr = cur.fetchall() outgroup_labels = [] for iii in rrr: label = re.sub("_", " ", iii[0]) outgroup_labels.append( label.__str__() ) mrca = dendrotree.mrca(taxon_labels=outgroup_labels) if mrca.edge.tail_node != None and mrca.edge.head_node != None: dendrotree.reroot_at_edge(mrca.edge, update_splits=True) newick = dendrotree.as_string("newick") return newick