|
| 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 to add""" |
| 21 | + alleles_ = [] |
| 22 | + for value in alleles.values(): |
| 23 | + variation = value["variation"] |
| 24 | + if variation["location"]["sequenceReference"]["refgetAccession"] == REFGET_AC: |
| 25 | + alleles_.append(variation) |
| 26 | + return alleles_ |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def alleles_in_range(alleles_to_add): |
| 31 | + """Create test fixture for alleles in range""" |
| 32 | + alleles = [] |
| 33 | + for variation in alleles_to_add: |
| 34 | + start = variation["location"]["start"] |
| 35 | + end = variation["location"]["end"] |
| 36 | + if start <= POS and end >= POS: |
| 37 | + alleles.append(variation) |
| 38 | + return alleles |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def populated_postgres_storage( |
| 43 | + postgres_storage: PostgresObjectStore, |
| 44 | + alleles_to_add: list[dict], |
| 45 | + caf_iri: CohortAlleleFrequencyStudyResult, |
| 46 | +): |
| 47 | + for variation in alleles_to_add: |
| 48 | + caf_copy = caf_iri.model_copy(deep=True) |
| 49 | + caf_copy.focusAllele = iriReference(root=variation["id"]) |
| 50 | + postgres_storage.add_allele_frequency(caf_copy) |
| 51 | + return postgres_storage |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def anyvlm_populated_client(populated_postgres_storage): |
| 56 | + """Define test fixture for anyvlm""" |
| 57 | + return AnyVLM(populated_postgres_storage) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def expected_cafs(caf_iri, alleles_in_range): |
| 62 | + cafs = [] |
| 63 | + for variation in alleles_in_range: |
| 64 | + new_caf = caf_iri.model_copy(deep=True) |
| 65 | + new_caf.focusAllele = variation |
| 66 | + cafs.append(new_caf) |
| 67 | + return cafs |
| 68 | + |
| 69 | + |
| 70 | +def test_get_caf_results_returned( |
| 71 | + anyvar_populated_python_client: PythonAnyVarClient, |
| 72 | + anyvlm_populated_client: AnyVLM, |
| 73 | + expected_cafs: list[CohortAlleleFrequencyStudyResult], |
| 74 | +): |
| 75 | + """Test get_caf when results are expected""" |
| 76 | + cafs = get_caf( |
| 77 | + anyvar_populated_python_client, |
| 78 | + anyvlm_populated_client, |
| 79 | + GA4GH_SEQ_ID, |
| 80 | + POS, |
| 81 | + POS, |
| 82 | + ) |
| 83 | + diff = DeepDiff( |
| 84 | + [caf.model_dump(exclude_none=True) for caf in cafs], |
| 85 | + [caf.model_dump(exclude_none=True) for caf in expected_cafs], |
| 86 | + ignore_order=True, |
| 87 | + ) |
| 88 | + assert diff == {} |
| 89 | + |
| 90 | + |
| 91 | +def test_get_caf_no_results( |
| 92 | + anyvar_populated_python_client: PythonAnyVarClient, |
| 93 | + anyvlm_populated_client: AnyVLM, |
| 94 | +): |
| 95 | + """Test get_caf when no results are expected""" |
| 96 | + cafs = get_caf( |
| 97 | + anyvar_populated_python_client, |
| 98 | + anyvlm_populated_client, |
| 99 | + "GRCh45.p1:Y", |
| 100 | + POS, |
| 101 | + POS, |
| 102 | + ) |
| 103 | + assert cafs == [] |
0 commit comments