Skip to content

Commit

Permalink
Compute pair-wise distances (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Jan 24, 2024
1 parent 64bb746 commit ece53c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/genomicranges/GenomicRanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,28 @@ def follow(

return rev_map

def distance(self, query: Union["GenomicRanges", IRanges]) -> np.ndarray:
"""Compute the pair-wise distance with intervals in query.
Args:
query:
Query `GenomicRanges` or `IRanges`.
Returns:
Numpy vector containing distances for each interval in query.
"""
if not isinstance(query, (IRanges, GenomicRanges)):
raise TypeError("'query' is not a `GenomicRanges` or `IRanges` object.")

if len(self) != len(query):
raise ValueError("'query' does not contain the same number of intervals.")

_qranges = query
if isinstance(query, GenomicRanges):
_qranges = query.get_ranges()

return self._ranges.distance(_qranges)

def match(self, query: "GenomicRanges") -> List[List[int]]:
"""Element wise comparison to find exact match ranges.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_gr_methods_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ def test_follow():

assert query_hits is not None
assert query_hits == [[4], [], []]


def test_distance():
query = GenomicRanges(["A", "A", "A"], ranges=IRanges([1, 2, 10], [5, 7, 2]))
subject = GenomicRanges(["A", "A", "A"], ranges=IRanges([6, 5, 13], [5, 6, 3]))

distance = subject.distance(query)

assert distance is not None
assert isinstance(distance, np.ndarray)
assert distance.tolist() == [0, 0, 1]

0 comments on commit ece53c6

Please sign in to comment.