Skip to content

Commit

Permalink
calculate depth and information content for CLASS node type
Browse files Browse the repository at this point in the history
  • Loading branch information
valearna committed Sep 12, 2019
1 parent 487e492 commit c4e3482
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 3 additions & 5 deletions genedescriptions/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ontobio.io.gafparser import GafParser
from genedescriptions.commons import Gene, DataType, Module
from genedescriptions.config_parser import GenedescConfigParser, ConfigModuleProperty
from genedescriptions.ontology_tools import set_all_depths_in_subgraph
from genedescriptions.ontology_tools import set_all_depths


class ExpressionClusterType(Enum):
Expand Down Expand Up @@ -156,8 +156,7 @@ def set_ontology(self, ontology_type: DataType, ontology: Ontology, config: Gene
terms_replacement_regex = config.get_module_property(module=module, prop=ConfigModuleProperty.RENAME_TERMS)
if terms_replacement_regex:
self.rename_ontology_terms(ontology=new_ontology, terms_replacement_regex=terms_replacement_regex)
for root_id in new_ontology.get_roots():
set_all_depths_in_subgraph(ontology=new_ontology, root_id=root_id, relations=None)
set_all_depths(ontology=new_ontology, relations=None)
if slim_cache_path:
slim_url = config.get_module_property(module=module, prop=ConfigModuleProperty.SLIM_URL)
self.load_slim(module=module, slim_url=slim_url, slim_cache_path=slim_cache_path)
Expand Down Expand Up @@ -209,8 +208,7 @@ def load_ontology_from_file(self, ontology_type: DataType, ontology_url: str, on
terms_replacement_regex = config.get_module_property(module=module, prop=ConfigModuleProperty.RENAME_TERMS)
if terms_replacement_regex:
self.rename_ontology_terms(ontology=new_ontology, terms_replacement_regex=terms_replacement_regex)
for root_id in new_ontology.get_roots():
set_all_depths_in_subgraph(ontology=new_ontology, root_id=root_id, relations=None)
set_all_depths(ontology=new_ontology, relations=None)
slim_url = config.get_module_property(module=module, prop=ConfigModuleProperty.SLIM_URL)
self.load_slim(module=module, slim_url=slim_url, slim_cache_path=slim_cache_path)

Expand Down
18 changes: 13 additions & 5 deletions genedescriptions/ontology_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
logger = logging.getLogger(__name__)


def set_all_depths(ontology: Ontology, relations: List[str] = None, comparison_func=max):
for root_id in ontology.get_roots():
if ontology.node_type(root_id) == "CLASS":
set_all_depths_in_subgraph(ontology=ontology, root_id=root_id, relations=relations,
comparison_func=comparison_func)


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)
Expand All @@ -34,17 +41,18 @@ def set_all_depths_in_subgraph(ontology: Ontology, root_id: str, relations: List
def set_all_information_content_values(ontology: Ontology, relations: List[str] = None):
roots = ontology.get_roots(relations=relations)
for root_id in roots:
if "num_subsumers" not in ontology.node(root_id):
if "num_subsumers" not in ontology.node(root_id) and 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):
if "num_leaves" not in ontology.node(root_id) and 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):
if "depth" not in ontology.node(root_id) and ontology.node_type(root_id) == "CLASS":
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)
if 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)


def get_all_paths_to_root(node_id: str, ontology: Ontology, min_distance_from_root: int = 0,
Expand Down

0 comments on commit c4e3482

Please sign in to comment.