-
Notifications
You must be signed in to change notification settings - Fork 3
General OnToma module for PySpark parsers + PhenoDigm & PanelApp implementation #94
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e23f1a5
Update execution environments
tskir 9042668
Implement OnToma mapping for PhenoDigm
tskir 6394b03
Added the module for OnToma mapping
tskir 7d5859d
Simplify OnToma instance construction
tskir 69ce776
Fix PySpark dataframe construction issue
tskir 7599e82
Implement EFO mapping for PanelApp
tskir b103ca8
Address review comments
tskir 2707c71
Improve performance of the temporary retry policy
tskir d36380d
Workaround for Pandas null conversion bug
tskir e8b66bc
Update common/ontology.py
tskir 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import logging | ||
import random | ||
import time | ||
|
||
from ontoma.interface import OnToma | ||
from pandarallel import pandarallel | ||
|
||
ONTOMA_MAX_ATTEMPTS = 5 | ||
pandarallel.initialize() | ||
|
||
|
||
def _ontoma_udf(row, ontoma_instance): | ||
disease_name, disease_id = row['diseaseFromSource'], row['diseaseFromSourceId'] | ||
for attempt in range(1, ONTOMA_MAX_ATTEMPTS + 1): | ||
# Try to map first by disease name (because that branch of OnToma is more stable), then by disease ID. | ||
try: | ||
mappings = [] | ||
if disease_name: | ||
mappings = ontoma_instance.find_term(query=disease_name, code=False) | ||
if disease_id and not mappings: | ||
mappings = ontoma_instance.find_term(query=disease_id, code=True) | ||
return [m.id_ot_schema for m in mappings] | ||
except: | ||
# If this is not the last attempt, wait until the next one | ||
if attempt != ONTOMA_MAX_ATTEMPTS: | ||
time.sleep(10 + 30 * random.random()) | ||
logging.error(f'OnToma lookup failed for {disease_name!r} / {disease_id!r}') | ||
return [] | ||
|
||
|
||
def add_efo_mapping(evidence_strings, spark_instance, ontoma_cache_dir=None): | ||
"""Given evidence strings with diseaseFromSource and diseaseFromSourceId fields, try to populate EFO mapping | ||
field diseaseFromSourceMappedId. In case there are multiple matches, the evidence strings will be exploded | ||
accordingly. | ||
|
||
Currently, both source columns (diseaseFromSource and diseaseFromSourceId) need to be present in the original | ||
schema, although they do not have to be populated for all rows.""" | ||
logging.info('Collect all distinct (disease name, disease ID) pairs.') | ||
disease_info_to_map = ( | ||
evidence_strings | ||
.select('diseaseFromSource', 'diseaseFromSourceId') | ||
DSuveges marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.distinct() | ||
.toPandas() | ||
) | ||
|
||
logging.info('Initialise OnToma instance') | ||
ontoma_instance = OnToma(cache_dir=ontoma_cache_dir) | ||
|
||
logging.info('Map disease information to EFO.') | ||
disease_info_to_map['diseaseFromSourceMappedId'] = disease_info_to_map.parallel_apply( | ||
_ontoma_udf, args=(ontoma_instance,), axis=1 | ||
) | ||
disease_info_to_map = disease_info_to_map.explode('diseaseFromSourceMappedId') | ||
|
||
logging.info('Join the resulting information into the evidence strings.') | ||
disease_info_df = spark_instance.createDataFrame(disease_info_to_map.astype(str)) | ||
return evidence_strings.join( | ||
disease_info_df, | ||
on=['diseaseFromSource', 'diseaseFromSourceId'], | ||
how='left' | ||
) |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -15,4 +15,5 @@ dependencies: | |
- snakemake==6.0.0 | ||
- tqdm=4.58.0 | ||
- pip: | ||
- ontoma==0.0.17 | ||
- ontoma==1.0.0 | ||
- pandarallel==1.5.2 |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.