-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refactor ancestry mapping to include distance from descendant node + implement functions to support curated list term mapping #96
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b0765b
feat: refactor ancestry mapping to include distance from descendant n…
nayib-jose-gloria 66759a5
Merge branch 'main' into nayib/curated-list-mapper-api
nayib-jose-gloria be8b80e
Merge branch 'main' into nayib/curated-list-mapper-api
nayib-jose-gloria 41f44c9
update schemas to reflect that IDs can have more than 7 numbers (but …
nayib-jose-gloria 6a9f394
AUTO: update ontologies
invalid-email-address bcab919
fix lca logic (can be multiple in a DAG)
nayib-jose-gloria 79d5ec1
add tests + add get_term_ancestors_with_distances
nayib-jose-gloria f72e26d
lint
nayib-jose-gloria dc985ed
Merge branch 'main' into nayib/curated-list-mapper-api
nayib-jose-gloria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,8 +41,8 @@ def _parse_ontology_name(self, term_id: str) -> str: | |
|
||
def get_term_ancestors(self, term_id: str, include_self: bool = False) -> List[str]: | ||
""" | ||
Get the ancestor ontology terms for a given term. If include_self is True, the term itself will be included as an | ||
ancestor. | ||
Get the ancestor ontology terms for a given term. If include_self is True, the term itself will be included as | ||
an ancestor. | ||
|
||
Example: get_term_ancestors("CL:0000005") -> ["CL:0000000", ...] | ||
|
||
|
@@ -51,13 +51,13 @@ def get_term_ancestors(self, term_id: str, include_self: bool = False) -> List[s | |
:return: flattened List[str] of ancestor terms | ||
""" | ||
ontology_name = self._parse_ontology_name(term_id) | ||
ancestors: List[str] = self.cxg_schema.ontology(ontology_name)[term_id]["ancestors"] | ||
ancestors = list(self.cxg_schema.ontology(ontology_name)[term_id]["ancestors"].keys()) | ||
return ancestors + [term_id] if include_self else ancestors | ||
|
||
def get_term_list_ancestors(self, term_ids: str, include_self: bool = False) -> Dict[str, List[str]]: | ||
def get_term_list_ancestors(self, term_ids: List[str], include_self: bool = False) -> Dict[str, List[str]]: | ||
""" | ||
Get the ancestor ontology terms for each term in a list. If include_self is True, the term itself will be included | ||
as an ancestor. | ||
Get the ancestor ontology terms for each term in a list. If include_self is True, the term itself will be | ||
included as an ancestor. | ||
|
||
Example: get_term_list_ancestors(["CL:0000003", "CL:0000005"], include_self=True) -> { | ||
"CL:0000003": ["CL:0000003"], | ||
|
@@ -71,10 +71,102 @@ def get_term_list_ancestors(self, term_ids: str, include_self: bool = False) -> | |
""" | ||
return {term_id: self.get_term_ancestors(term_id, include_self) for term_id in term_ids} | ||
|
||
def map_high_level_terms( | ||
self, term_ids: List[str], high_level_terms: List[str], include_self: bool = True | ||
) -> Dict[str, List[str]]: | ||
""" | ||
Given a list of ontology term IDs and a list of high_level_terms to map them to, returns a dictionary with | ||
format | ||
|
||
{"CL:0000003": ["CL:0000000", ...], "CL:0000005": ["CL:0000000", ...]} | ||
|
||
Where each term_id is mapped to a List[str] of high-level terms that it is a descendant of | ||
|
||
:param term_ids: list of str ontology terms to map high level terms for | ||
:param high_level_terms: list of str ontology terms that can be mapped to descendant term_ids | ||
:param include_self: bool to map a term_id to itself if it is in high_level_terms | ||
:return: Dictionary mapping str term IDs to their respective List[str] of ancestor terms from the input list. | ||
Each key maps to empty list if there are no ancestors among the provided input. | ||
""" | ||
ancestors = self.get_term_list_ancestors(term_ids, include_self) | ||
for term_id in term_ids: | ||
ancestors[term_id] = [ | ||
high_level_term for high_level_term in ancestors[term_id] if high_level_term in high_level_terms | ||
] | ||
return ancestors | ||
|
||
def get_distance_between_terms(self, ontology: Ontology, term_id_1: str, term_id_2: str) -> int: | ||
""" | ||
Get the distance between two ontology terms. The distance is defined as the number of edges between the | ||
two terms. Terms must be from the same ontology. Returns -1 if terms are disjoint. | ||
|
||
:param ontology: Ontology enum of the ontology to find distance for | ||
:param term_id_1: str ontology term to find distance for | ||
:param term_id_2: str ontology term to find distance for | ||
:return: int distance between the two terms, measured in number of edges between their shortest path. | ||
""" | ||
lcas = self.get_lowest_common_ancestors(ontology, term_id_1, term_id_2) | ||
if not lcas: | ||
return -1 | ||
return int( | ||
self.cxg_schema.ontology(ontology.name)[term_id_1]["ancestors"][lcas[0]] | ||
+ self.cxg_schema.ontology(ontology.name)[term_id_2]["ancestors"][lcas[0]] | ||
) | ||
|
||
def get_lowest_common_ancestors(self, ontology: Ontology, term_id_1: str, term_id_2: str) -> List[str]: | ||
""" | ||
Get the lowest common ancestors between two ontology terms that is from the given ontology. | ||
Terms must be from the same ontology. Ontologies are DAGs, so there may be multiple lowest common ancestors. | ||
|
||
:param ontology: Ontology enum of the ontology to find distance for | ||
:param term_id_1: str ontology term to find LCA for | ||
:param term_id_2: str ontology term to find LCA for | ||
:return: str term ID of the lowest common ancestor term | ||
""" | ||
# include path to term itself | ||
ancestors_1 = self.cxg_schema.ontology(ontology.name)[term_id_1]["ancestors"] + {term_id_1: 0} | ||
ancestors_2 = self.cxg_schema.ontology(ontology.name)[term_id_2]["ancestors"] + {term_id_2: 0} | ||
common_ancestors = set(ancestors_1.keys()) & set(ancestors_2.keys()) | ||
min_sum_distances = min(common_ancestors, key=lambda x: ancestors_1[x] + ancestors_2[x]) | ||
return [term for term in common_ancestors if ancestors_1[term] + ancestors_2[term] == min_sum_distances] | ||
|
||
def map_highest_level_term( | ||
self, term_ids: List[str], high_level_terms: List[str], include_self: bool = True | ||
) -> Dict[str, Union[str, None]]: | ||
""" | ||
Given a list of ontology term IDs and a list of high_level_terms to map them to, returns a dictionary with | ||
format | ||
|
||
{"CL:0000003": "CL:0000000", "CL:0000005": "CL:0000000"} | ||
|
||
Where each term_id is mapped to the highest level term that it is a descendant of, from the list provided. Maps | ||
to None if term_id does not map to any high level terms among the provided input. | ||
|
||
:param term_ids: list of str ontology terms to map high level terms for | ||
:param high_level_terms: list of str ontology terms that can be mapped to descendant term_ids | ||
:param include_self: bool to map a term_id to itself if it is in high_level_terms | ||
:return: Dictionary mapping str term IDs to their respective List[str] of ancestor terms from the input list. | ||
Each key maps to empty list if there are no ancestors among the provided input. | ||
""" | ||
high_level_term_map = self.map_high_level_terms(term_ids, high_level_terms, include_self) | ||
highest_level_term_map = dict() | ||
for term_id in term_ids: | ||
ontology = self._parse_ontology_name(term_id) | ||
# map term_id to the high_level_term with the longest distance from term_id | ||
highest_level_term_map[term_id] = ( | ||
max( | ||
high_level_term_map[term_id], | ||
key=lambda x: self.cxg_schema.ontology(ontology)[term_id]["ancestors"][x], | ||
) | ||
if high_level_term_map[term_id] | ||
else None | ||
) | ||
return highest_level_term_map | ||
|
||
def get_terms_descendants(self, term_ids: List[str], include_self: bool = False) -> Dict[str, List[str]]: | ||
""" | ||
Get the descendant ontology terms for each term in a list. If include_self is True, the term itself will be included | ||
as a descendant. | ||
Get the descendant ontology terms for each term in a list. If include_self is True, the term itself will be | ||
included as a descendant. | ||
|
||
Example: get_terms_descendants(["CL:0000003", "CL:0000005"], include_self=True) -> { | ||
"CL:0000003": ["CL:0000003", "CL:0000004", ...], | ||
|
@@ -83,8 +175,8 @@ def get_terms_descendants(self, term_ids: List[str], include_self: bool = False) | |
|
||
:param term_ids: list of str ontology terms to find descendants for | ||
:param include_self: boolean flag to include the term itself as an descendant | ||
:return: Dictionary mapping str term IDs to their respective flattened List[str] of descendant terms. Maps to empty | ||
list if there are no descendants. | ||
:return: Dictionary mapping str term IDs to their respective flattened List[str] of descendant terms. Maps to | ||
empty list if there are no descendants. | ||
""" | ||
descendants_dict = dict() | ||
ontology_names = set() | ||
|
@@ -96,7 +188,8 @@ def get_terms_descendants(self, term_ids: List[str], include_self: bool = False) | |
for ontology in ontology_names: | ||
for candidate_descendant, candidate_metadata in self.cxg_schema.ontology(ontology).items(): | ||
for ancestor_id in descendants_dict: | ||
if ancestor_id in candidate_metadata["ancestors"]: | ||
ancestors = candidate_metadata["ancestors"].keys() | ||
if ancestor_id in ancestors: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: You don't need to make this change. It should work with the original implementation. |
||
descendants_dict[ancestor_id].append(candidate_descendant) | ||
|
||
return descendants_dict | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The last two lines would be more efficient if you put them in a single for loop rather than looping through the common_ancestors twice. It would mean half as many dictionary lookups.