Skip to content

Commit

Permalink
feat!: add support for transforming civic diagnostic evidence items (#…
Browse files Browse the repository at this point in the history
…414)

close #241

* MetaKB now supports `VariantDiagnosticStudyStatement`s in addition to
`VariantTherapeuticResponseStudyStatement` and
`VariantPrognosticStudyStatement`
  • Loading branch information
korikuzma authored Dec 18, 2024
1 parent 4ac6439 commit 61d2958
Show file tree
Hide file tree
Showing 8 changed files with 606 additions and 665 deletions.
32 changes: 18 additions & 14 deletions src/metakb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from ga4gh.core.entity_models import Coding, Document, Extension, Method
from ga4gh.va_spec.profiles.var_study_stmt import (
VariantDiagnosticStudyStatement,
VariantPrognosticStudyStatement,
VariantTherapeuticResponseStudyStatement,
)
Expand Down Expand Up @@ -69,6 +70,14 @@ class TherapeuticProcedureType(str, Enum):
SUBSTITUTES = "TherapeuticSubstituteGroup"


# Statement types to corresponding class mapping
STMT_TYPE_TO_CLASS = {
"VariantDiagnosticStudyStatement": VariantDiagnosticStudyStatement,
"VariantPrognosticStudyStatement": VariantPrognosticStudyStatement,
"VariantTherapeuticResponseStudyStatement": VariantTherapeuticResponseStudyStatement,
}


def _deserialize_field(node: dict, field_name: str) -> None | dict:
"""Deserialize JSON blob property.
Expand Down Expand Up @@ -493,8 +502,8 @@ def _get_nested_stmts(self, statement_nodes: list[Node]) -> list[dict]:

def _get_nested_stmt(self, stmt_node: Node) -> dict:
"""Get information related to a statement
Only VariantTherapeuticResponseStudyStatement and VariantPrognosticStudyStatement
are supported at the moment
Only VariantTherapeuticResponseStudyStatement, VariantPrognosticStudyStatement,
and VariantDiagnosticStudyStatement are supported at the moment
:param stmt_node: Neo4j graph node for statement
:return: Nested statement
Expand All @@ -503,15 +512,14 @@ def _get_nested_stmt(self, stmt_node: Node) -> dict:
if study_stmt_type not in {
"VariantTherapeuticResponseStudyStatement",
"VariantPrognosticStudyStatement",
"VariantDiagnosticStudyStatement",
}:
return {}

if study_stmt_type == "VariantPrognosticStudyStatement":
study_stmt_cls = VariantPrognosticStudyStatement
condition_key = "objectCondition"
else:
study_stmt_cls = VariantTherapeuticResponseStudyStatement
if study_stmt_type == "VariantTherapeuticResponseStudyStatement":
condition_key = "conditionQualifier"
else:
condition_key = "objectCondition"

params = {
condition_key: None,
Expand Down Expand Up @@ -559,7 +567,7 @@ def _get_nested_stmt(self, stmt_node: Node) -> dict:
else:
logger.warning("relation type not supported: %s", rel_type)

return study_stmt_cls(**params).model_dump()
return STMT_TYPE_TO_CLASS[study_stmt_type](**params).model_dump()

@staticmethod
def _get_vicc_normalizer_extension(node: dict) -> ViccNormalizerDataExtension:
Expand Down Expand Up @@ -917,10 +925,6 @@ async def batch_search_statements(
statement_nodes = [r[0] for r in result]
response.statement_ids = [n["id"] for n in statement_nodes]
stmts = self._get_nested_stmts(statement_nodes)
response.statements = [
VariantTherapeuticResponseStudyStatement(**s)
if s["type"] == "VariantTherapeuticResponseStudyStatement"
else VariantPrognosticStudyStatement(**s)
for s in stmts
]

response.statements = [STMT_TYPE_TO_CLASS[s["type"]](**s) for s in stmts]
return response
9 changes: 7 additions & 2 deletions src/metakb/schemas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Literal

from ga4gh.va_spec.profiles.var_study_stmt import (
VariantDiagnosticStudyStatement,
VariantPrognosticStudyStatement,
VariantTherapeuticResponseStudyStatement,
)
Expand Down Expand Up @@ -48,7 +49,9 @@ class SearchStatementsService(BaseModel):
warnings: list[StrictStr] = []
statement_ids: list[StrictStr] = []
statements: list[
VariantTherapeuticResponseStudyStatement | VariantPrognosticStudyStatement
VariantTherapeuticResponseStudyStatement
| VariantPrognosticStudyStatement
| VariantDiagnosticStudyStatement
] = []
service_meta_: ServiceMeta

Expand All @@ -73,6 +76,8 @@ class BatchSearchStatementsService(BaseModel):
warnings: list[StrictStr] = []
statement_ids: list[StrictStr] = []
statements: list[
VariantTherapeuticResponseStudyStatement | VariantPrognosticStudyStatement
VariantTherapeuticResponseStudyStatement
| VariantPrognosticStudyStatement
| VariantDiagnosticStudyStatement
] = []
service_meta_: ServiceMeta
5 changes: 4 additions & 1 deletion src/metakb/transformers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from ga4gh.core.entity_models import Coding, Document, Extension, Method
from ga4gh.va_spec.profiles.var_study_stmt import (
VariantDiagnosticStudyStatement,
VariantPrognosticStudyStatement,
VariantTherapeuticResponseStudyStatement,
)
Expand Down Expand Up @@ -111,7 +112,9 @@ class TransformedData(BaseModel):
"""Define model for transformed data"""

statements: list[
VariantTherapeuticResponseStudyStatement | VariantPrognosticStudyStatement
VariantTherapeuticResponseStudyStatement
| VariantPrognosticStudyStatement
| VariantDiagnosticStudyStatement
] = []
categorical_variants: list[CategoricalVariant] = []
variations: list[Allele] = []
Expand Down
9 changes: 8 additions & 1 deletion src/metakb/transformers/civic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
)
from ga4gh.va_spec.profiles.var_study_stmt import (
AlleleOriginQualifier,
DiagnosticPredicate,
PrognosticPredicate,
TherapeuticResponsePredicate,
VariantDiagnosticStudyStatement,
VariantPrognosticStudyStatement,
VariantTherapeuticResponseStudyStatement,
)
Expand Down Expand Up @@ -91,6 +93,8 @@
"RESISTANCE": TherapeuticResponsePredicate.RESISTANCE,
"POOR_OUTCOME": PrognosticPredicate.WORSE_OUTCOME,
"BETTER_OUTCOME": PrognosticPredicate.BETTER_OUTCOME,
"POSITIVE": DiagnosticPredicate.INCLUSIVE,
"NEGATIVE": DiagnosticPredicate.EXCLUSIVE,
}


Expand Down Expand Up @@ -121,6 +125,7 @@ class _CivicEvidenceType(str, Enum):

PREDICTIVE = "PREDICTIVE"
PROGNOSTIC = "PROGNOSTIC"
DIAGNOSTIC = "DIAGNOSTIC"


class _VariationCache(BaseModel):
Expand Down Expand Up @@ -359,8 +364,10 @@ def _add_variant_study_stmt(
if evidence_type == _CivicEvidenceType.PREDICTIVE:
params["objectTherapeutic"] = civic_therapeutic
statement = VariantTherapeuticResponseStudyStatement(**params)
else:
elif evidence_type == _CivicEvidenceType.PROGNOSTIC:
statement = VariantPrognosticStudyStatement(**params)
else:
statement = VariantDiagnosticStudyStatement(**params)

self.processed_data.statements.append(statement)

Expand Down
Loading

0 comments on commit 61d2958

Please sign in to comment.