Skip to content

Commit

Permalink
KBDEV-1236
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieulemieux committed Aug 12, 2024
1 parent 509494a commit 25471f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
30 changes: 29 additions & 1 deletion pori_python/graphkb/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions tests/test_graphkb/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
)

Expand All @@ -284,26 +287,31 @@ 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"},
)

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"}
)

Expand All @@ -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"}
)

Expand All @@ -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:
Expand Down

0 comments on commit 25471f1

Please sign in to comment.