|
| 1 | +import logging |
| 2 | +import random |
| 3 | +import time |
| 4 | + |
| 5 | +from ontoma.interface import OnToma |
| 6 | +from pandarallel import pandarallel |
| 7 | +from pyspark.sql.functions import col, when |
| 8 | + |
| 9 | +ONTOMA_MAX_ATTEMPTS = 3 |
| 10 | +pandarallel.initialize() |
| 11 | + |
| 12 | + |
| 13 | +def _simple_retry(func, **kwargs): |
| 14 | + """Simple retry handling for functions. Cannot be a decorator, so that the functions could still be pickled.""" |
| 15 | + for attempt in range(1, ONTOMA_MAX_ATTEMPTS + 1): |
| 16 | + try: |
| 17 | + return func(**kwargs) |
| 18 | + except: |
| 19 | + # If this is not the last attempt, wait until the next one. |
| 20 | + if attempt != ONTOMA_MAX_ATTEMPTS: |
| 21 | + time.sleep(5 + 10 * random.random()) |
| 22 | + logging.error(f'OnToma lookup failed for {kwargs!r}') |
| 23 | + return [] |
| 24 | + |
| 25 | + |
| 26 | +def _ontoma_udf(row, ontoma_instance): |
| 27 | + """Try to map first by disease name (because that branch of OnToma is more stable), then by disease ID.""" |
| 28 | + disease_name = row['diseaseFromSource'] |
| 29 | + disease_id = row['diseaseFromSourceId'].replace('_', ':') if row['diseaseFromSourceId'] else None |
| 30 | + mappings = [] |
| 31 | + if disease_name: |
| 32 | + mappings = _simple_retry(ontoma_instance.find_term, query=disease_name, code=False) |
| 33 | + if not mappings and disease_id and ':' in disease_id: |
| 34 | + mappings = _simple_retry(ontoma_instance.find_term, query=disease_id, code=True) |
| 35 | + return [m.id_ot_schema for m in mappings] |
| 36 | + |
| 37 | + |
| 38 | +def add_efo_mapping(evidence_strings, spark_instance, ontoma_cache_dir=None): |
| 39 | + """Given evidence strings with diseaseFromSource and diseaseFromSourceId fields, try to populate EFO mapping |
| 40 | + field diseaseFromSourceMappedId. In case there are multiple matches, the evidence strings will be exploded |
| 41 | + accordingly. |
| 42 | +
|
| 43 | + Currently, both source columns (diseaseFromSource and diseaseFromSourceId) need to be present in the original |
| 44 | + schema, although they do not have to be populated for all rows.""" |
| 45 | + logging.info('Collect all distinct (disease name, disease ID) pairs.') |
| 46 | + disease_info_to_map = ( |
| 47 | + evidence_strings |
| 48 | + .select('diseaseFromSource', 'diseaseFromSourceId') |
| 49 | + .distinct() |
| 50 | + .toPandas() |
| 51 | + ) |
| 52 | + |
| 53 | + logging.info('Initialise OnToma instance') |
| 54 | + ontoma_instance = OnToma(cache_dir=ontoma_cache_dir) |
| 55 | + |
| 56 | + logging.info('Map disease information to EFO.') |
| 57 | + disease_info_to_map['diseaseFromSourceMappedId'] = disease_info_to_map.parallel_apply( |
| 58 | + _ontoma_udf, args=(ontoma_instance,), axis=1 |
| 59 | + ) |
| 60 | + disease_info_to_map = disease_info_to_map.explode('diseaseFromSourceMappedId') |
| 61 | + |
| 62 | + logging.info('Join the resulting information into the evidence strings.') |
| 63 | + disease_info_df = ( |
| 64 | + spark_instance |
| 65 | + .createDataFrame(disease_info_to_map.astype(str)) |
| 66 | + .withColumn( |
| 67 | + 'diseaseFromSourceMappedId', |
| 68 | + when(col('diseaseFromSourceMappedId') != 'nan', col('diseaseFromSourceMappedId')) |
| 69 | + ) |
| 70 | + ) |
| 71 | + return evidence_strings.join( |
| 72 | + disease_info_df, |
| 73 | + on=['diseaseFromSource', 'diseaseFromSourceId'], |
| 74 | + how='left' |
| 75 | + ) |
0 commit comments