Exemple #1
0
def prune_tree(treepath, keep, prune_OTTids=[]):
    '''take a newick tree file and remove all the subtrees in prune_OTTids, then keep only those taxa listed as keys in the "keep" dictionary,
    incrementing the value of the key by one each time. If keep is True, keep all of the remaining taxa'''
    with open(treepath, 'r', encoding='UTF-8') as treefile:
        tree = Tree.get_from_stream(treefile,
                                    schema="newick",
                                    preserve_underscores=True,
                                    rooting='default-rooted')
        removed = 0
        for to_omit in prune_OTTids:
            to_remove = (
                tree.find_node(
                    lambda node: True if node.label is not None and re.search(
                        "_ott" + to_omit + "'?$", node.label) else False)
                or tree.find_node_with_taxon(lambda taxon: True if re.search(
                    "_ott" + to_omit + "'?$", taxon.label) else False))
            if to_remove:
                tree.prune_subtree(to_remove, suppress_unifurcations=False)
                removed += 1
            else:
                warn("Could not find subtree _ott{} within tree in {}".format(
                    to_omit, treepath))

        if not keep == True:
            if removed:
                tree.purge_taxon_namespace(
                )  #make sure that taxa in the pruned subtrees are not counted
            tree.leave_only(keep)
        return (tree)
Exemple #2
0
def check_list_against_tree(treepath, checklist):
    '''take a path to a newick tree file and look for any taxa that correspond to keys in the "keep" dictionary,
    incrementing the value of each one found'''
    with open(treepath, 'r', encoding='UTF-8') as treefile:
        check_list_against_taxa(
            Tree.get_from_stream(treefile,
                                 schema="newick",
                                 preserve_underscores=True,
                                 rooting='default-rooted'), checklist)
def get_tree_and_OTT_list(tree_filehandle, sources, verbosity=0):
    """
    Takes a base tree and creates objects for each node and leaf, attaching them as 'data' dictionaries
    to each node in the DendroPy tree. Nodes and leaves with an OTT id also have pointers to their data 
    dicts stored in an OTT-keyed dict, so that mappings to other databases (ncbi id, etc etc) can be created.
    
    We can easily have duplicate leaf names, so for the entire procedure we ignore the Dendropy concept
    of a taxon list and simply use labels.
    Returns the Dendropy tree and the OTT dict.
    """
    #these variables are all pointers into the same data
    ordered_leaves=[]
    ordered_nodes=[]
    indexed_by_ott={}
    
    try:
        tree = Tree.get_from_stream(tree_filehandle, schema="newick", preserve_underscores=True, suppress_leaf_node_taxa=True)
    except:
        sys.exit("Problem reading tree from " + treefile.name)
    info("-> read tree from " + tree_filehandle.name)
    
    ott_node = re.compile(r"(.*) ott(\d+)(@\d*)?$") #matches the OTT number
    mrca_ott_node = re.compile(r"(.*) (mrcaott\d+ott\d+)(@\d*)?$") #matches a node with an "mrca" node number (no unique OTT)
    for i, node in enumerate(tree.preorder_node_iter()):
        node.data = {'parent':node.parent_node or None}
        if node.label:
            node.label = node.label.replace("_"," ")
            m = ott_node.search(node.label)
            if m is not None:
                if m.group(3):
                    warn("Node has an @ sign at the end ({}), meaning it has probably not been substituted by an OpenTree equivalent. You may want to provide an alternative subtree from this node downwards, as otherwise it will probably be deleted from the main tree.".format(node.label))
                node.label = m.group(1)
                node.data['ott'] = int(m.group(2))
                indexed_by_ott[node.data['ott']] = node.data
                node.data['sources']={k:None for k in sources}
            else:
                m = mrca_ott_node.search(node.label)
                if m is not None:
                    if m.group(3):
                        warn("Node has an @ sign at the end ({}), meaning it has probably not been substituted by an OpenTree equivalent. You may want to provide an alternative subtree from this node downwards, as otherwise it will probably be deleted from the main tree.".format(node.label))
                    node.label = m.group(1)
                    #this is an 'mrca' node, so we want to save sources but *not* save the ott number in node.data
                    indexed_by_ott[m.group(2)] = node.data
                    node.data['sources']={k:None for k in sources}
                elif node.is_leaf():
                    warn("Leaf without an OTT id: '{}'. This will not be associated with any other data".format(node.label))
            #finally, put underscores at the start or the end of the new label back
            #as these denote "fake" names that are hidden and only used for mapping
            #we could keep them as spaces, but leading/trailing underscores are easier to see by eye
            if node.label[0]==" ":
                node.label = "_" + node.label[1:]
            if node.label[-1]==" ":
                node.label = node.label[:-1] + "_"

    info("-> extracted {} otts from among {} leaves and nodes".format(len(indexed_by_ott), i))
    return tree, indexed_by_ott
Exemple #4
0
def evaluate(ref, file_name):

    # To store the data during the process, we create two temporary files.
    tmp1 = tempfile.mkstemp()
    tmp2 = tempfile.mkstemp()

    # Use the commands of fastprot and fnj.
    # The output of the FastPhylo programs is in file 'tmp2'.
    os.system("fastprot -m -o " + tmp1[1] + " " + file_name)
    os.system("fnj -O newick -m FNJ -o " + tmp2[1] + " " + tmp1[1])

    #Use Dendropy to compare the trees.
    in_tree = Tree.get_from_stream(os.fdopen(tmp2[0]),
                                   schema='newick',
                                   taxon_namespace=tns)
    ref_tree = Tree.get_from_path(ref, schema='newick', taxon_namespace=tns)
    sym_diff = treecompare.symmetric_difference(ref_tree, in_tree)

    return sym_diff
def prune_tree(treepath, keep, prune_OTTids=[]):
    '''take a newick tree file and remove all the subtrees in prune_OTTids, then keep only those taxa listed as keys in the "keep" dictionary,
    incrementing the value of the key by one each time. If keep is True, keep all of the remaining taxa'''
    with open(treepath, 'r', encoding='UTF-8') as treefile:
        tree = Tree.get_from_stream(treefile, schema="newick", preserve_underscores=True, rooting='default-rooted')
        removed = 0
        for to_omit in prune_OTTids:
            to_remove = (tree.find_node(lambda node: True if node.label is not None and re.search("_ott" + to_omit + "'?$", node.label) else False) or
                        tree.find_node_with_taxon(lambda taxon: True if re.search("_ott" + to_omit + "'?$", taxon.label) else False))
            if to_remove:
                tree.prune_subtree(to_remove, suppress_unifurcations=False)
                removed += 1
            else:
                warn("Could not find subtree _ott{} within tree in {}".format(to_omit,treepath))
    
        if not keep == True:
            if removed:
                tree.purge_taxon_namespace() #make sure that taxa in the pruned subtrees are not counted 
            tree.leave_only(keep)
        return(tree)
Exemple #6
0
        for chldNode in node.child_nodes():
            if chldNode.is_leaf():
                return False
        return True

# read class name and sci name
clsList = []
sciList = []
fCls = open("classes.csv", "rb")
csvCls = csv.reader(fCls)
for row in csvCls:
    clsList.append(row[2])
    sciList.append(row[4])

# read tree
tree = Tree.get_from_stream(open("CUB11.tre"), "nexus")

# generate grouping system
nodeList = [tree.seed_node]
grpSys = []
grpSys.append(nodeList)
GRP_SYS_NUM = 8
for g in xrange(1, GRP_SYS_NUM):
    nodeList = []
    for node in grpSys[g - 1]:
        if IsSplitNode(node):
            for chldNode in node.child_nodes():
                nodeList.append(chldNode)
        else:
            nodeList.append(node)
    grpSys.append(nodeList)
Exemple #7
0
def get_tree_and_OTT_list(tree_filehandle, sources, verbosity=0):
    """
    Takes a base tree and creates objects for each node and leaf, attaching them as 'data' dictionaries
    to each node in the DendroPy tree. Nodes and leaves with an OTT id also have pointers to their data 
    dicts stored in an OTT-keyed dict, so that mappings to other databases (ncbi id, etc etc) can be created.
    
    We can easily have duplicate leaf names, so for the entire procedure we ignore the Dendropy concept
    of a taxon list and simply use labels.
    Returns the Dendropy tree and the OTT dict.
    """
    #these variables are all pointers into the same data
    ordered_leaves = []
    ordered_nodes = []
    indexed_by_ott = {}

    try:
        tree = Tree.get_from_stream(tree_filehandle,
                                    schema="newick",
                                    preserve_underscores=True,
                                    suppress_leaf_node_taxa=True)
    except:
        sys.exit("Problem reading tree from " + treefile.name)
    info("-> read tree from " + tree_filehandle.name)

    ott_node = re.compile(r"(.*) ott(\d+)(@\d*)?$")  #matches the OTT number
    mrca_ott_node = re.compile(
        r"(.*) (mrcaott\d+ott\d+)(@\d*)?$"
    )  #matches a node with an "mrca" node number (no unique OTT)
    for i, node in enumerate(tree.preorder_node_iter()):
        node.data = {'parent': node.parent_node or None}
        if node.label:
            node.label = node.label.replace("_", " ")
            m = ott_node.search(node.label)
            if m is not None:
                if m.group(3):
                    warn(
                        "Node has an @ sign at the end ({}), meaning it has probably not been substituted by an OpenTree equivalent. You may want to provide an alternative subtree from this node downwards, as otherwise it will probably be deleted from the main tree."
                        .format(node.label))
                node.label = m.group(1)
                node.data['ott'] = int(m.group(2))
                indexed_by_ott[node.data['ott']] = node.data
                node.data['sources'] = {k: None for k in sources}
            else:
                m = mrca_ott_node.search(node.label)
                if m is not None:
                    if m.group(3):
                        warn(
                            "Node has an @ sign at the end ({}), meaning it has probably not been substituted by an OpenTree equivalent. You may want to provide an alternative subtree from this node downwards, as otherwise it will probably be deleted from the main tree."
                            .format(node.label))
                    node.label = m.group(1)
                    #this is an 'mrca' node, so we want to save sources but *not* save the ott number in node.data
                    indexed_by_ott[m.group(2)] = node.data
                    node.data['sources'] = {k: None for k in sources}
                elif node.is_leaf():
                    warn(
                        "Leaf without an OTT id: '{}'. This will not be associated with any other data"
                        .format(node.label))
            #finally, put underscores at the start or the end of the new label back
            #as these denote "fake" names that are hidden and only used for mapping
            #we could keep them as spaces, but leading/trailing underscores are easier to see by eye
            if node.label[0] == " ":
                node.label = "_" + node.label[1:]
            if node.label[-1] == " ":
                node.label = node.label[:-1] + "_"

    info("-> extracted {} otts from among {} leaves and nodes".format(
        len(indexed_by_ott), i))
    return tree, indexed_by_ott
def check_list_against_tree(treepath, checklist):
    '''take a path to a newick tree file and look for any taxa that correspond to keys in the "keep" dictionary,
    incrementing the value of each one found'''
    with open(treepath, 'r', encoding='UTF-8') as treefile:
        check_list_against_taxa(Tree.get_from_stream(treefile, schema="newick", preserve_underscores=True, rooting='default-rooted'), checklist)