11"""Craft a VlmResponse object from a list of CohortAlleleFrequencyStudyResults"""
22
3+ from ga4gh .core .models import MappableConcept
34from ga4gh .va_spec .base .core import CohortAlleleFrequencyStudyResult
45
56from anyvlm .schemas .vlm import (
910 ResultSet ,
1011 VlmResponse ,
1112)
13+ from anyvlm .utils .types import Zygosity
14+
15+
16+ def _extract_zygosity (caf_study_result : CohortAlleleFrequencyStudyResult ) -> Zygosity :
17+ """Extracts a zygosity from a CohortAlleleFrequencyStudyResult.
18+ #TODO: This is my best guess at how we'll want to represent zygosity in our CAF results. We may need
19+ to update this function during or after Issue #16 based on how we choose to implement our CAF data pull.
20+
21+ :param caf_study_result: The CohortAlleleFrequencyStudyResult whose zygosity we wish to determine.
22+ :return: The `Zygosity` of the study result.
23+ """
24+ cohort_characteristics : list [MappableConcept ] | None = (
25+ caf_study_result .cohort .characteristics
26+ )
27+ if not (cohort_characteristics ):
28+ error_message : str = "Each CohortAlleleFrequencyStudyResult's 'cohort' field must have 'characteristics' set"
29+ raise ValueError (error_message )
30+
31+ zygosity : str | None = next (
32+ (
33+ concept .name
34+ for concept in cohort_characteristics
35+ if getattr (concept , "conceptType" , None ) == "Zygosity"
36+ ),
37+ None ,
38+ )
39+ if not zygosity :
40+ error_message : str = "'CohortAlleleFrequencyStudyResult.cohort.characteristics' must contain a 'Zygosity' entry"
41+ raise ValueError (error_message )
42+
43+ try :
44+ return Zygosity (zygosity )
45+ except ValueError as e :
46+ error_message : str = f"Invalid zygosity provided: { zygosity } "
47+ raise ValueError (error_message ) from e
1248
1349
1450def build_vlm_response (caf_data : list [CohortAlleleFrequencyStudyResult ]) -> VlmResponse :
@@ -21,15 +57,17 @@ def build_vlm_response(caf_data: list[CohortAlleleFrequencyStudyResult]) -> VlmR
2157 if caf_data :
2258 total : int = sum (
2359 [caf_study_result .focusAlleleCount for caf_study_result in caf_data ]
24- ) # TODO: I'm not sure this is the right field?
60+ ) # TODO: I'm not sure this is the right field? Will need to verify during/after Issue #16.
2561 response_summary = ResponseSummary (exists = True , total = total )
2662
2763 for caf_study_result in caf_data :
2864 result_sets .extend (
2965 [
3066 ResultSet (
3167 exists = True ,
32- id = f"{ HandoverType .id } { caf_study_result .cohort } " , # TODO - not sure that caf_study_result.cohort is the right field
68+ # TODO - HandoverType.id represents the ID of the node from which the dataset was pulled.
69+ # In the future, this ID should be set dynamically.
70+ id = f"{ HandoverType .id } { _extract_zygosity (caf_study_result )} " ,
3371 resultsCount = caf_study_result .focusAlleleCount ,
3472 )
3573 ]
0 commit comments