def _data_to_object(self, data, schema=None): """Attaches a DendroPy tree object""" schemas = [ "newick", "nexus" ] # If schemas is not given, try each schema until one works. if schema is None: for s in schemas: # Try different schemas until a tree works try: tree = DendropyTree.get(data=data, schema=s) except: pass else: tree = DendropyTree.get(data=data, schema=schema) # Check that a tree was made. try: self._Tree._DendroPy = tree self._Tree._construct() except NameError: raise Exception("""Tree data doesn't seem to be in a format that DendroPy can read.""") return self._Tree
def _data_to_object(self, data, schema=None): """Attaches a DendroPy tree object""" schemas = ["newick", "nexus"] # If schemas is not given, try each schema until one works. if schema is None: for s in schemas: # Try different schemas until a tree works try: tree = DendropyTree.get(data=data, schema=s) except: pass else: tree = DendropyTree.get(data=data, schema=schema) # Check that a tree was made. try: self._Tree._DendroPy = tree self._Tree._construct() except NameError: raise Exception( """Tree data doesn't seem to be in a format that DendroPy can read.""" ) return self._Tree
def add_outgroup(tree, relative_additional_height): desired_height = ( 1 + relative_additional_height) * tree.seed_node.distance_from_tip() outgroup = Node(taxon=Taxon("outgroup"), edge_length=desired_height) tns = deepcopy(tree.taxon_namespace) tns.add_taxon(outgroup.taxon) new_root = Node() new_root.add_child(outgroup) new_root.add_child(tree.seed_node) new_tree = Tree(taxon_namespace=tns) new_tree.seed_node = new_root # Despite my best efforts, I was getting taxon namespace errors. So we round trip # from Newick. ¯\_(ツ)_/¯ new_newick = str(new_tree) + ";" return Tree.get(data=new_newick, schema="newick")
def nexus(self, data): Tree = DendropyTree.get(data=data, schema="nexus") return self._data_to_object(Tree)