|
| 1 | +"""Test that get_caf function works correctly""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from deepdiff import DeepDiff |
| 5 | +from ga4gh.core.models import iriReference |
| 6 | +from ga4gh.va_spec.base import CohortAlleleFrequencyStudyResult |
| 7 | + |
| 8 | +from anyvlm.anyvar.python_client import PythonAnyVarClient |
| 9 | +from anyvlm.anyvlm import AnyVLM |
| 10 | +from anyvlm.functions.get_caf import get_caf |
| 11 | +from anyvlm.storage.postgres import PostgresObjectStore |
| 12 | + |
| 13 | +POS = 2781760 |
| 14 | +REFGET_AC = "SQ.8_liLu1aycC0tPQPFmUaGXJLDs5SbPZ5" |
| 15 | +GA4GH_SEQ_ID = f"ga4gh:{REFGET_AC}" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def alleles_to_add(alleles: dict): |
| 20 | + """Create test fixture for alleles whose sequence reference matches REFGET_AC""" |
| 21 | + return [ |
| 22 | + value["variation"] |
| 23 | + for value in alleles.values() |
| 24 | + if value["location"]["sequenceReference"]["refgetAccession"] == REFGET_AC |
| 25 | + ] |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def alleles_in_range(alleles_to_add): |
| 30 | + """Create test fixture for alleles overlapping POS""" |
| 31 | + return [ |
| 32 | + variation |
| 33 | + for variation in alleles_to_add |
| 34 | + if variation["location"]["start"] <= POS and variation["location"]["end"] >= POS |
| 35 | + ] |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def populated_postgres_storage( |
| 40 | + postgres_storage: PostgresObjectStore, |
| 41 | + alleles_to_add: list[dict], |
| 42 | + caf_iri: CohortAlleleFrequencyStudyResult, |
| 43 | +): |
| 44 | + for variation in alleles_to_add: |
| 45 | + caf_copy = caf_iri.model_copy(deep=True) |
| 46 | + caf_copy.focusAllele = iriReference(root=variation["id"]) |
| 47 | + postgres_storage.add_allele_frequency(caf_copy) |
| 48 | + return postgres_storage |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def anyvlm_populated_client(populated_postgres_storage): |
| 53 | + """Define test fixture for anyvlm""" |
| 54 | + return AnyVLM(populated_postgres_storage) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def expected_cafs(caf_iri, alleles_in_range): |
| 59 | + cafs = [] |
| 60 | + for variation in alleles_in_range: |
| 61 | + new_caf = caf_iri.model_copy(deep=True) |
| 62 | + new_caf.focusAllele = variation |
| 63 | + cafs.append(new_caf) |
| 64 | + return cafs |
| 65 | + |
| 66 | + |
| 67 | +def test_get_caf_results_returned( |
| 68 | + anyvar_populated_python_client: PythonAnyVarClient, |
| 69 | + anyvlm_populated_client: AnyVLM, |
| 70 | + expected_cafs: list[CohortAlleleFrequencyStudyResult], |
| 71 | +): |
| 72 | + """Test get_caf when results are expected""" |
| 73 | + cafs = get_caf( |
| 74 | + anyvar_populated_python_client, |
| 75 | + anyvlm_populated_client, |
| 76 | + GA4GH_SEQ_ID, |
| 77 | + POS, |
| 78 | + POS, |
| 79 | + ) |
| 80 | + diff = DeepDiff( |
| 81 | + [caf.model_dump(exclude_none=True) for caf in cafs], |
| 82 | + [caf.model_dump(exclude_none=True) for caf in expected_cafs], |
| 83 | + ignore_order=True, |
| 84 | + ) |
| 85 | + assert diff == {} |
| 86 | + |
| 87 | + |
| 88 | +def test_get_caf_no_results( |
| 89 | + anyvar_populated_python_client: PythonAnyVarClient, |
| 90 | + anyvlm_populated_client: AnyVLM, |
| 91 | +): |
| 92 | + """Test get_caf when no results are expected""" |
| 93 | + cafs = get_caf( |
| 94 | + anyvar_populated_python_client, |
| 95 | + anyvlm_populated_client, |
| 96 | + "GRCh45.p1:Y", |
| 97 | + POS, |
| 98 | + POS, |
| 99 | + ) |
| 100 | + assert cafs == [] |
0 commit comments