diff --git a/pori_python/graphkb/match.py b/pori_python/graphkb/match.py index c646f49..6172a63 100644 --- a/pori_python/graphkb/match.py +++ b/pori_python/graphkb/match.py @@ -275,7 +275,26 @@ def positions_overlap( return start is None or pos == start +def equivalent_types(conn: GraphKBConnection, type1: str, type2: str): + + # Convert rid to displayName if needed + if looks_like_rid(type1): + type1 = conn.get_records_by_id([type1])[0]['displayName'] + if looks_like_rid(type2): + type2 = conn.get_records_by_id([type2])[0]['displayName'] + + # Get terms set + terms1 = get_terms_set(conn, [type1]) + terms2 = get_terms_set(conn, [type2]) + + if len(terms1.intersection(terms2)) == 0: + return False + + return True + + def compare_positional_variants( + conn: GraphKBConnection, variant: Union[PositionalVariant, ParsedVariant], reference_variant: Union[PositionalVariant, ParsedVariant], generic: bool = True, @@ -377,6 +396,11 @@ def compare_positional_variants( return False elif len(variant["refSeq"]) != len(reference_variant["refSeq"]): # type: ignore return False + + # Equivalent types + if variant.get('type') and reference_variant.get('type'): + if not equivalent_types(conn, variant["type"], reference_variant["type"]): + return False return True @@ -598,10 +622,14 @@ def match_positional_variant( ): # TODO: Check if variant and reference_variant should be interchanged if compare_positional_variants( - variant=parsed, reference_variant=cast(PositionalVariant, row), generic=True + conn, + variant=parsed, + reference_variant=cast(PositionalVariant, row), + generic=True, ): filtered_similarAndGeneric.append(row) if compare_positional_variants( + conn, variant=parsed, reference_variant=cast(PositionalVariant, row), generic=False, # Similar variants only diff --git a/tests/test_graphkb/test_match.py b/tests/test_graphkb/test_match.py index 175c7b1..7c18f22 100644 --- a/tests/test_graphkb/test_match.py +++ b/tests/test_graphkb/test_match.py @@ -269,13 +269,16 @@ def test_known_increased_expression(self, conn): class TestComparePositionalVariants: def test_nonspecific_altseq(self): assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}}, {"break1Start": {"pos": 1}} ) # null matches anything assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "untemplatedSeq": "T"}, {"break1Start": {"pos": 1}} ) assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}}, {"break1Start": {"pos": 1}, "untemplatedSeq": "T"} ) @@ -284,16 +287,19 @@ def test_nonspecific_altseq(self): def test_ambiguous_altseq(self, seq1, seq2): # ambiguous AA matches anything the same length assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "untemplatedSeq": seq1}, {"break1Start": {"pos": 1}, "untemplatedSeq": seq2}, ) def test_altseq_length_mismatch(self): assert not match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "untemplatedSeq": "??"}, {"break1Start": {"pos": 1}, "untemplatedSeq": "T"}, ) assert not match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "untemplatedSeq": "?"}, {"break1Start": {"pos": 1}, "untemplatedSeq": "TT"}, ) @@ -301,9 +307,11 @@ def test_altseq_length_mismatch(self): def test_nonspecific_refseq(self): # null matches anything assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "refSeq": "T"}, {"break1Start": {"pos": 1}} ) assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}}, {"break1Start": {"pos": 1}, "refSeq": "T"} ) @@ -312,36 +320,43 @@ def test_nonspecific_refseq(self): def test_ambiguous_refseq(self, seq1, seq2): # ambiguous AA matches anything the same length assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "refSeq": seq1}, {"break1Start": {"pos": 1}, "refSeq": seq2} ) def test_refseq_length_mismatch(self): assert not match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "refSeq": "??"}, {"break1Start": {"pos": 1}, "refSeq": "T"} ) assert not match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "refSeq": "?"}, {"break1Start": {"pos": 1}, "refSeq": "TT"} ) def test_diff_altseq(self): assert not match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "untemplatedSeq": "M"}, {"break1Start": {"pos": 1}, "untemplatedSeq": "R"}, ) def test_same_altseq_matches(self): assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "untemplatedSeq": "R"}, {"break1Start": {"pos": 1}, "untemplatedSeq": "R"}, ) def test_diff_refseq(self): assert not match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "refSeq": "M"}, {"break1Start": {"pos": 1}, "refSeq": "R"} ) def test_same_refseq_matches(self): assert match.compare_positional_variants( + conn, {"break1Start": {"pos": 1}, "refSeq": "R"}, {"break1Start": {"pos": 1}, "refSeq": "R"} ) @@ -364,8 +379,8 @@ def test_range_vs_sub(self): "refSeq": "G", "untemplatedSeq": "VV", } - assert not match.compare_positional_variants(sub, range_variant) - assert not match.compare_positional_variants(range_variant, sub) + assert not match.compare_positional_variants(conn, sub, range_variant) + assert not match.compare_positional_variants(conn, range_variant, sub) class TestMatchPositionalVariant: