def set_all_information_content_values(ontology: Ontology, relations: List[str] = None): logger.info("calculating information content for all terms in ontology") roots = ontology.get_roots(relations=relations) for root_id in roots: if "num_subsumers" not in ontology.node(root_id): _set_num_subsumers_in_subgraph(ontology=ontology, root_id=root_id, relations=relations) for root_id in roots: if "num_leaves" not in ontology.node(root_id): _set_num_leaves_in_subgraph(ontology=ontology, root_id=root_id, relations=relations) for root_id in roots: if "depth" not in ontology.node(root_id): set_all_depths_in_subgraph(ontology=ontology, root_id=root_id, relations=relations) for root_id in roots: _set_information_content_in_subgraph( ontology=ontology, root_id=root_id, maxleaves=ontology.node(root_id)["num_leaves"], relations=relations)
def set_ic_ontology_struct(ontology: Ontology, relations: List[str] = None): logger.info( "Setting information content values based on ontology structure") roots = ontology.get_roots(relations=relations) for root_id in roots: if "num_subsumers" not in ontology.node(root_id) and ( "type" not in ontology.node(root_id) or ontology.node_type(root_id) == "CLASS"): _set_num_subsumers_in_subgraph(ontology=ontology, root_id=root_id, relations=relations) for root_id in roots: if "num_leaves" not in ontology.node(root_id) and ( "type" not in ontology.node(root_id) or ontology.node_type(root_id) == "CLASS"): _set_num_leaves_in_subgraph(ontology=ontology, root_id=root_id, relations=relations) for root_id in roots: if "depth" not in ontology.node(root_id) and ( "type" not in ontology.node(root_id) or ontology.node_type(root_id) == "CLASS"): set_all_depths_in_subgraph(ontology=ontology, root_id=root_id, relations=relations) for root_id in roots: if "type" not in ontology.node(root_id) or ontology.node_type( root_id) == "CLASS": _set_information_content_in_subgraph( ontology=ontology, root_id=root_id, maxleaves=ontology.node(root_id)["num_leaves"], relations=relations) logger.info("Finished setting information content values")
def set_ic_annot_freq(ontology: Ontology, annotations: AssociationSet): logger.info( "Setting information content values based on annotation frequency") for node_id in ontology.nodes(): node_prop = ontology.node(node_id) if "rel_annot_genes" in node_prop: del node_prop["rel_annot_genes"] if "tot_annot_genes" in node_prop: del node_prop["tot_annot_genes"] if "IC" in node_prop: del node_prop["IC"] for root_id in ontology.get_roots(): if "depth" not in ontology.node(root_id) and ( "type" not in ontology.node(root_id) or ontology.node_type(root_id) == "CLASS"): set_all_depths_in_subgraph(ontology=ontology, root_id=root_id) node_gene_map = defaultdict(set) for subj, obj in annotations.associations_by_subj_obj.keys(): node_gene_map[obj].add(subj) for node_id in ontology.nodes(): node_pr = ontology.node(node_id) node_pr["rel_annot_genes"] = node_gene_map[node_id] for root_id in ontology.get_roots(): _set_tot_annots_in_subgraph(ontology, root_id) for node_prop in ontology.nodes().values(): if "tot_annot_genes" not in node_prop: node_prop["tot_annot_genes"] = set() tot_annots = len( set([ gene for set_genes in node_gene_map.values() for gene in set_genes ])) min_annots = min([ len(node["tot_annot_genes"]) for node in ontology.nodes().values() if "tot_annot_genes" in node and len(node["tot_annot_genes"]) > 0 ]) if not min_annots: min_annots = 1 for node_prop in ontology.nodes().values(): node_prop["IC"] = -math.log(len(node_prop["tot_annot_genes"]) / tot_annots) if \ len(node_prop["tot_annot_genes"]) > 0 else -math.log(min_annots / (tot_annots + 1)) logger.info("Finished setting information content values")
def set_all_depths(ontology: Ontology, relations: List[str] = None, comparison_func=max): for root_id in ontology.get_roots(): if "type" not in ontology.node(root_id) or ontology.node_type( root_id) == "CLASS": set_all_depths_in_subgraph(ontology=ontology, root_id=root_id, relations=relations, comparison_func=comparison_func) for node_id, node_content in ontology.nodes().items(): if "depth" not in node_content: node_content["depth"] = 0