Пример #1
0
def set_all_depths_in_subgraph(ontology: Ontology,
                               root_id: str,
                               relations: List[str] = None,
                               comparison_func=max,
                               current_depth: int = 0):
    """calculate and set max_depth and min_depth (maximum and minimum distances from root terms in the ontology)
    recursively for all terms in a branch of the ontology

    Args:
        ontology (Ontology): the ontology
        root_id (str): the ID of the root term of the branch to process
        relations (List[str]): list of relations to consider
        comparison_func: a comparison function to calculate the depth when multiple paths exist between the node and
            the root. max calculates the length of the longest path, min the one of the shortest
        current_depth (int): the current depth in the ontology
    """
    if "depth" not in ontology.node(root_id):
        ontology.node(root_id)["depth"] = current_depth
    else:
        ontology.node(root_id)["depth"] = comparison_func(
            ontology.node(root_id)["depth"], current_depth)
    for child_id in ontology.children(node=root_id, relations=relations):
        set_all_depths_in_subgraph(ontology=ontology,
                                   root_id=child_id,
                                   relations=relations,
                                   comparison_func=comparison_func,
                                   current_depth=current_depth + 1)
Пример #2
0
def _set_information_content_in_subgraph(ontology: Ontology,
                                         root_id: str,
                                         maxleaves: int,
                                         relations: List[str] = None):
    node = ontology.node(root_id)
    node["IC"] = -math.log(
        (float(node["num_leaves"]) / node["num_subsumers"] + 1) /
        (maxleaves + 1))
    for child_id in ontology.children(node=root_id, relations=relations):
        _set_information_content_in_subgraph(ontology=ontology,
                                             root_id=child_id,
                                             maxleaves=maxleaves,
                                             relations=relations)
Пример #3
0
def _set_num_leaves_in_subgraph(ontology: Ontology,
                                root_id: str,
                                relations: List[str] = None):
    num_leaves = 0
    for child_id in ontology.children(node=root_id):
        if "num_leaves" not in ontology.node(child_id):
            _set_num_leaves_in_subgraph(ontology=ontology,
                                        root_id=child_id,
                                        relations=relations)
        if ontology.node(child_id)["num_leaves"] == 0:
            num_leaves += 1
        else:
            num_leaves += ontology.node(child_id)["num_leaves"]
    ontology.node(root_id)["num_leaves"] = num_leaves
def _set_tot_annots_in_subgraph(ontology: Ontology,
                                root_id: str,
                                relations: List[str] = None):
    if "tot_annot_genes" not in ontology.node(root_id):
        children = set(ontology.children(root_id, relations=relations))
        children.discard(root_id)
        children = list(children)
        ontology.node(root_id)["tot_annot_genes"] = ontology.node(
            root_id)["rel_annot_genes"] | set([
                annot_gene for child_id in children
                for annot_gene in _set_tot_annots_in_subgraph(
                    ontology, child_id)
            ])
    return ontology.node(root_id)["tot_annot_genes"]
def _set_num_subsumers_in_subgraph(ontology: Ontology,
                                   root_id: str,
                                   relations: List[str] = None):
    if "num_subsumers" not in ontology.node(root_id):
        parents = set(ontology.parents(root_id))
        parents.discard(root_id)
        parents = list(parents)
        if not parents or all(
            ["set_subsumers" in ontology.node(parent) for parent in parents]):
            subsumers = {subsumer for parent in parents for subsumer in ontology.node(parent)["set_subsumers"]} | \
                        {root_id}
            ontology.node(root_id)["num_subsumers"] = len(subsumers)
            ontology.node(root_id)["set_subsumers"] = subsumers
            for child_id in ontology.children(node=root_id):
                _set_num_subsumers_in_subgraph(ontology, child_id, relations)
Пример #6
0
def _set_num_subsumers_in_subgraph(ontology: Ontology,
                                   root_id: str,
                                   relations: List[str] = None):
    parents = ontology.parents(root_id)
    if len(parents) == 1:
        ontology.node(root_id)["num_subsumers"] = ontology.node(
            parents[0])["num_subsumers"] + 1
    else:
        ontology.node(root_id)["num_subsumers"] = len(
            ontology.ancestors(node=root_id,
                               relations=relations,
                               reflexive=True))
    for child_id in ontology.children(node=root_id, relations=relations):
        _set_num_subsumers_in_subgraph(ontology=ontology,
                                       root_id=child_id,
                                       relations=relations)
def _set_num_leaves_in_subgraph(ontology: Ontology,
                                root_id: str,
                                relations: List[str] = None):
    if "set_leaves" in ontology.node(root_id):
        return ontology.node(root_id)["set_leaves"]
    children = set(ontology.children(node=root_id))
    children.discard(root_id)
    children = list(children)
    if not children:
        leaves = {root_id}
        num_leaves = 0
    else:
        leaves = {
            leaf
            for child_id in children for leaf in _set_num_leaves_in_subgraph(
                ontology=ontology, root_id=child_id, relations=relations)
        }
        num_leaves = len(leaves)
    ontology.node(root_id)["num_leaves"] = num_leaves
    ontology.node(root_id)["set_leaves"] = leaves
    return leaves
def _set_information_content_in_subgraph(ontology: Ontology,
                                         root_id: str,
                                         maxleaves: int,
                                         relations: List[str] = None):
    node = ontology.node(root_id)
    if str(root_id) == root_id and "ARTIFICIAL_NODE:" in root_id:
        node["IC"] = 0
    else:
        if "num_leaves" in node and "num_subsumers" in node:
            node["IC"] = -math.log(
                (float(node["num_leaves"]) / node["num_subsumers"] + 1) /
                (maxleaves + 1))
        else:
            logger.warning("Disconnected node: " + root_id)
            node["IC"] = 0
    children = set(ontology.children(node=root_id, relations=relations))
    children.discard(root_id)
    children = list(children)
    for child_id in children:
        _set_information_content_in_subgraph(ontology=ontology,
                                             root_id=child_id,
                                             maxleaves=maxleaves,
                                             relations=relations)