diff --git a/pori_python/ipr/ipr.py b/pori_python/ipr/ipr.py index 2eee502..b5c0ccf 100644 --- a/pori_python/ipr/ipr.py +++ b/pori_python/ipr/ipr.py @@ -269,7 +269,9 @@ def select_expression_plots( def create_key_alterations( - kb_matches: List[Hashabledict], all_variants: Sequence[IprVariant] + kb_matches: List[Hashabledict], + all_variants: Sequence[IprVariant], + included_kb_matches: List[KbVariantMatch], ) -> Tuple[List[Dict], Dict]: """Create the list of significant variants matched by the KB. @@ -284,7 +286,12 @@ def create_key_alterations( } counts: Dict[str, Set] = {v: set() for v in type_mapping.values()} skipped_variant_types = [] + + included_kbvariant_ids = list(set([item['kbVariantId'] for item in included_kb_matches])) + for kb_match in kb_matches: + if kb_match['kbVariantId'] not in included_kbvariant_ids: + continue variant_type = kb_match["variantType"] variant_key = kb_match["variant"] if kb_match["category"] == "unknown": diff --git a/pori_python/ipr/main.py b/pori_python/ipr/main.py index f74e851..76fd768 100644 --- a/pori_python/ipr/main.py +++ b/pori_python/ipr/main.py @@ -512,8 +512,9 @@ def ipr_report( ) # KEY ALTERATIONS - # must do after pruning of kbMatches for kb_matched_sections - key_alterations, variant_counts = create_key_alterations(gkb_matches, all_variants) + key_alterations, variant_counts = create_key_alterations( + gkb_matches, all_variants, kb_matched_sections['kbMatches'] + ) # OUTPUT CONTENT # thread safe deep-copy the original content diff --git a/tests/test_ipr/test_ipr.py b/tests/test_ipr/test_ipr.py index b67b1c2..9994cf4 100644 --- a/tests/test_ipr/test_ipr.py +++ b/tests/test_ipr/test_ipr.py @@ -11,6 +11,7 @@ get_kb_statement_matched_conditions, get_kb_variants, get_kb_matches_sections, + create_key_alterations, ) from pori_python.types import Statement @@ -484,9 +485,9 @@ def test_germline_kb_matches(self): "kbContextId": "#135:8764", "kbRelevanceId": "#147:32", "kbStatementId": "#155:13511", - "requiredKbMatches": ["#159:5426", "#161:938"], + "requiredKbMatches": ["#159:54261", "#161:9381"], "kbVariant": "BRCA1 mutation", - "kbVariantId": "#161:938", + "kbVariantId": "#161:9381", "matchedCancer": False, "reference": "MOAlmanac FDA-56", "relevance": "therapy", @@ -495,6 +496,13 @@ def test_germline_kb_matches(self): }, ] +ALL_VARIANTS = [ + {"variant": "var1", "key": '1', "variantType": 'mut'}, + {"variant": "var2", "key": '2', "variantType": 'mut'}, + {"variant": "var3", "key": '3', "variantType": 'mut'}, + {"variant": "var4", "key": '4', "variantType": 'mut'}, +] + BASIC_GKB_MATCH = { "approvedTherapy": False, "category": "test", @@ -830,3 +838,21 @@ def test_partial_matches_included(self): kbcs = get_kb_statement_matched_conditions(gkb_matches, allow_partial_matches=True) assert len(stmts) == 2 # X and Y assert len(kbcs) == 2 + + def test_create_key_alterations_includes_only_pruned_kbmatches(self): + gkb_matches = create_gkb_matches(GKB_MATCHES) + + sections1 = get_kb_matches_sections(gkb_matches, allow_partial_matches=False) + key_alts1, counts1 = create_key_alterations( + gkb_matches, ALL_VARIANTS, sections1['kbMatches'] + ) + + sections2 = get_kb_matches_sections(gkb_matches, allow_partial_matches=True) + key_alts2, counts2 = create_key_alterations( + gkb_matches, ALL_VARIANTS, sections2['kbMatches'] + ) + + # check partial-match-only variants are not included in key alterations when + # partial matches is false + assert len(key_alts1) == 3 + assert len(key_alts2) == 4