Skip to content

Commit

Permalink
chore: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Apr 13, 2024
1 parent 7891226 commit ffff4f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/metakb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from os import environ
from pathlib import Path
from timeit import default_timer as timer
from typing import Optional
from typing import Callable, Optional
from zipfile import ZipFile

import asyncclick as click
Expand Down Expand Up @@ -228,7 +228,7 @@ def _load_normalizers_db() -> None:

def _update_normalizer_db(
name: str,
update_normalizer_db_fn: callable,
update_normalizer_db_fn: Callable,
) -> None:
"""Update Normalizer DynamoDB database.
Expand Down
5 changes: 2 additions & 3 deletions src/metakb/harvesters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def harvest(self) -> bool:
any manner, but must be retrievable by :method:`iterate_records`.
:return: `True` if operation was successful, `False` otherwise.
:rtype: bool
"""
raise NotImplementedError

Expand All @@ -26,8 +25,8 @@ def create_json(
) -> bool:
"""Create composite and individual JSON for harvested data.
:param Dict items: item types keyed to Lists of values
:param Optional[str] filename: custom filename for composite document
:param items: item types keyed to Lists of values
:param filename: custom filename for composite document
:return: `True` if JSON creation was successful. `False` otherwise.
"""
composite_dict = {}
Expand Down
18 changes: 8 additions & 10 deletions src/metakb/harvesters/civic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""A module for the CIViC harvester."""
import logging
from pathlib import Path
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

from civicpy import LOCAL_CACHE_PATH
from civicpy import civic as civicpy
Expand All @@ -18,7 +17,7 @@ def __init__(
self,
update_cache: bool = False,
update_from_remote: bool = True,
local_cache_path: Optional[Path] = LOCAL_CACHE_PATH,
local_cache_path: Union[str, bool] = LOCAL_CACHE_PATH,
) -> None:
"""Initialize CivicHarvester class.
Expand Down Expand Up @@ -48,7 +47,6 @@ def harvest(self, filename: Optional[str] = None) -> bool:
:param filename: File name for composite json
:return: `True` if operation was successful, `False` otherwise.
:rtype: bool
"""
try:
self.evidence = self.harvest_evidence()
Expand Down Expand Up @@ -79,47 +77,47 @@ def harvest(self, filename: Optional[str] = None) -> bool:
logger.info("CIViC Harvester was successful.")
return True

def harvest_evidence(self) -> List[Dict]:
def harvest_evidence(self) -> List[Union[None, Dict, List]]:
"""Harvest all CIViC evidence item records.
:return: A list of all CIViC evidence item records represented as dicts
"""
evidence_items = civicpy.get_all_evidence()
return [self._dictify(e) for e in evidence_items]

def harvest_genes(self) -> List[Dict]:
def harvest_genes(self) -> List[Union[None, Dict, List]]:
"""Harvest all CIViC gene records.
:return: A list of all CIViC gene records represented as dicts
"""
genes = civicpy.get_all_genes()
return [self._dictify(g) for g in genes]

def harvest_variants(self) -> List[Dict]:
def harvest_variants(self) -> List[Union[None, Dict, List]]:
"""Harvest all CIViC variant records.
:return: A list of all CIViC variant records represented as dicts
"""
variants = civicpy.get_all_variants()
return [self._dictify(v) for v in variants]

def harvest_molecular_profiles(self) -> List[Dict]:
def harvest_molecular_profiles(self) -> List[Union[None, Dict, List]]:
"""Harvest all CIViC Molecular Profile records
:return: A list of all CIViC molecular profile records represented as dicts
"""
molecular_profiles = civicpy.get_all_molecular_profiles()
return [self._dictify(mp) for mp in molecular_profiles]

def harvest_assertions(self) -> List[Dict]:
def harvest_assertions(self) -> List[Union[None, Dict, List]]:
"""Harvest all CIViC assertion records.
:return: A list of all CIViC assertion records represented as dicts
"""
assertions = civicpy.get_all_assertions()
return [self._dictify(a) for a in assertions]

def _dictify(self, obj: any) -> Dict:
def _dictify(self, obj: Any) -> Union[None, Dict, List]: # noqa: ANN401
"""Recursively convert object to dictionary
:param obj: Object to convert to dict
Expand Down
37 changes: 13 additions & 24 deletions src/metakb/harvesters/moa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A module for the Molecular Oncology Almanac harvester"""
import logging
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Tuple

import requests
import requests_cache
Expand All @@ -17,9 +17,8 @@ def harvest(self, filename: Optional[str] = None) -> bool:
"""Retrieve and store sources, variants, and assertions
records from MOAlmanac in composite and individual JSON files.
:param Optional[str] filename: File name for composite json
:param filename: File name for composite json
:return: True if successfully retrieved, False otherwise
:rtype: bool
"""
try:
assertion_resp = self._get_all_assertions()
Expand Down Expand Up @@ -62,9 +61,8 @@ def _harvest_genes() -> List[Dict]:
def _harvest_sources(self, assertion_resp: List[Dict]) -> List[Dict]:
"""Harvest all MOA sources
:param List[Dict] assertion_resp: A list of MOA assertion records
:param assertion_resp: A list of MOA assertion records
:return: A list of sources
:rtype: list
"""
sources = []

Expand All @@ -76,11 +74,10 @@ def _harvest_sources(self, assertion_resp: List[Dict]) -> List[Dict]:

return sources

def harvest_variants(self) -> List[Dict]:
def harvest_variants(self) -> Tuple[List[Dict], List[Dict]]:
"""Harvest all MOA variants
:return: A list of variants
:rtype: list
"""
variants_list = self._get_all_variants()
variants = []
Expand All @@ -96,10 +93,9 @@ def harvest_assertions(
) -> List[Dict]:
"""Harvest all MOA assertions
:param List[Dict] assertion_resp: A list of MOA assertion records
:param List[Dict] variants_list: A list of MOA variant records
:param assertion_resp: A list of MOA assertion records
:param variants_list: A list of MOA variant records
:return: A list of assertions
:rtype: list
"""
assertions = []
for assertion in assertion_resp:
Expand Down Expand Up @@ -129,9 +125,8 @@ def _get_all_variants(self) -> List[Dict]:
def _source_item(self, source: Dict) -> Dict:
"""Harvest an individual MOA source of evidence
:param Dict source: source record of each assertion record
:param source: source record of each assertion record
:return: a dictionary containing MOA source of evidence data
:rtype: dict
"""
return {
"id": source["source_id"],
Expand All @@ -146,9 +141,8 @@ def _source_item(self, source: Dict) -> Dict:
def _harvest_variant(self, variant: Dict) -> Dict:
"""Harvest an individual MOA variant record.
:param Dict variant: A MOA variant record
:param variant: A MOA variant record
:return: A dictionary containing MOA variant data
:rtype: dict
"""
variant_record = {"id": variant["feature_id"]}

Expand All @@ -160,10 +154,9 @@ def _harvest_variant(self, variant: Dict) -> Dict:
def _harvest_assertion(self, assertion: Dict, variants_list: List[Dict]) -> Dict:
"""Harvest an individual MOA assertion record
:param Dict assertion: a MOA assertion record
:param List[Dict] variants_list: a list of MOA variant records
:param assertion: a MOA assertion record
:param variants_list: a list of MOA variant records
:return: A dictionary containing MOA assertion data
:rtype: dict
"""
assertion_record = {
"id": assertion["assertion_id"],
Expand Down Expand Up @@ -197,12 +190,9 @@ def _harvest_assertion(self, assertion: Dict, variants_list: List[Dict]) -> Dict
def _get_therapy(self, resistance: bool, sensitivity: bool) -> Optional[str]:
"""Get therapy response data.
:param bool resistance: `True` if Therapy Resistance.
`False` if not Therapy Resistance
:param bool sensitivity: `True` if Therapy Sensitivity.
`False` if not Therapy Sensitivity
:param resistance: `True` if Therapy Resistance. `False` if not Therapy Resistance
:param sensitivity: `True` if Therapy Sensitivity. `False` if not Therapy Sensitivity
:return: whether the therapy response is resistance or sensitivity
:rtype: str
"""
if resistance:
return "resistance"
Expand All @@ -213,9 +203,8 @@ def _get_therapy(self, resistance: bool, sensitivity: bool) -> Optional[str]:
def _get_feature(self, v: Dict) -> Dict:
"""Get feature name from the harvested variants
:param Dict v: harvested MOA variant
:param v: harvested MOA variant
:return: feature name same format as displayed in moalmanac.org
:rtype: dict
"""
feature_type = v["feature_type"]
if feature_type == "rearrangement":
Expand Down

0 comments on commit ffff4f8

Please sign in to comment.