Skip to content

Commit

Permalink
Use Names instead of StringList, bump biocutils (#10)
Browse files Browse the repository at this point in the history
also does a better job at specifying dependencies with minor and major versions.
  • Loading branch information
jkanche authored Nov 17, 2023
1 parent 7597e2d commit 6adb1af
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
5 changes: 2 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ python_requires = >=3.8
# For more information, check out https://semver.org/.
install_requires =
importlib-metadata; python_version<"3.8"
biocutils>=0.0.7
biocgenerics
biocframe>=0.4.0
biocutils>=0.1.2,<0.2.0
biocframe>=0.5.2,<0.6.0
numpy


Expand Down
28 changes: 14 additions & 14 deletions src/iranges/IRanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import biocutils as ut
from biocframe import BiocFrame
from biocgenerics import combine_rows, combine_seqs, show_as_cell
from biocutils import Names, combine_rows, combine_sequences, show_as_cell
from numpy import array, clip, int32, ndarray, printoptions, where, zeros

from .interval import calc_gap_and_overlap, create_np_interval_vector
Expand Down Expand Up @@ -136,7 +136,7 @@ def _sanitize_names(self, names):
if names is None:
return None
elif not isinstance(names, list):
names = list(names)
names = Names(names)

return names

Expand Down Expand Up @@ -308,7 +308,7 @@ def end(self) -> ndarray:
"""
return self.get_end()

def get_names(self) -> Union[None, List[str]]:
def get_names(self) -> Optional[Names]:
"""Get all names.
Returns:
Expand Down Expand Up @@ -339,7 +339,7 @@ def set_names(
return output

@property
def names(self) -> Union[None, List[str]]:
def names(self) -> Optional[Names]:
"""Get all names.
Returns:
Expand Down Expand Up @@ -1417,8 +1417,8 @@ def union(self, other: "IRanges") -> "IRanges":
if not isinstance(other, IRanges):
raise TypeError("'other' is not an IRanges object.")

all_starts = combine_seqs(self.start, other.start)
all_widths = combine_seqs(self.width, other.width)
all_starts = combine_sequences(self.start, other.start)
all_widths = combine_sequences(self.width, other.width)

output = IRanges(all_starts, all_widths)
output = output.reduce(min_gap_width=0, drop_empty_ranges=True)
Expand All @@ -1440,8 +1440,8 @@ def setdiff(self, other: "IRanges") -> "IRanges":
if not isinstance(other, IRanges):
raise TypeError("'other' is not an IRanges object.")

all_starts = combine_seqs(self.start, other.start)
all_ends = combine_seqs(self.end, other.end)
all_starts = combine_sequences(self.start, other.start)
all_ends = combine_sequences(self.end, other.end)
start = min(all_starts)
end = max(all_ends)

Expand All @@ -1467,8 +1467,8 @@ def intersect(self, other: "IRanges") -> "IRanges":
if not isinstance(other, IRanges):
raise TypeError("'other' is not an IRanges object.")

all_starts = combine_seqs(self.start, other.start)
all_ends = combine_seqs(self.end, other.end)
all_starts = combine_sequences(self.start, other.start)
all_ends = combine_sequences(self.end, other.end)
start = min(all_starts)
end = max(all_ends)

Expand Down Expand Up @@ -1814,7 +1814,7 @@ def follow(
self._delete_ncls_index()
return hits

def distance(self, query: "IRanges") -> List[int]:
def distance(self, query: "IRanges") -> List[Optional[int]]:
"""Calculate the pair-wise distance with intervals in query.
Args:
Expand Down Expand Up @@ -1847,7 +1847,7 @@ def distance(self, query: "IRanges") -> List[int]:
return all_distances


@combine_seqs.register
@combine_sequences.register
def _combine_IRanges(*x: IRanges) -> IRanges:
has_names = False
for y in x:
Expand All @@ -1865,8 +1865,8 @@ def _combine_IRanges(*x: IRanges) -> IRanges:
all_names += [""] * len(y)

return IRanges(
start=combine_seqs(*[y._start for y in x]),
width=combine_seqs(*[y._width for y in x]),
start=combine_sequences(*[y._start for y in x]),
width=combine_sequences(*[y._width for y in x]),
names=all_names,
mcols=combine_rows(*[y._mcols for y in x]),
metadata=x[0]._metadata,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_IRanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest
from biocframe import BiocFrame
from biocgenerics import combine_seqs
from biocutils import combine_sequences
from iranges import IRanges

__author__ = "Aaron Lun"
Expand Down Expand Up @@ -171,22 +171,22 @@ def test_IRanges_combine():

x = IRanges(starts, widths)
y = IRanges(starts2, widths2)
comb = combine_seqs(x, y)
comb = combine_sequences(x, y)
assert (comb.get_start() == np.array([1, 2, 3, 4, 10, 20, 30, 40])).all()
assert (comb.get_width() == np.array([4, 5, 6, 7, 50, 60, 70, 80])).all()
assert comb.get_names() is None

x = IRanges(starts, widths, mcols=BiocFrame({"foo": ["a", "b", "c", "d"]}))
y = IRanges(starts2, widths2, mcols=BiocFrame({"foo": ["A", "B", "C", "D"]}))
comb = combine_seqs(x, y)
comb = combine_sequences(x, y)
assert comb.get_mcols().column("foo") == ["a", "b", "c", "d", "A", "B", "C", "D"]

x = IRanges(starts, widths, names=["a", "b", "c", "d"])
y = IRanges(starts2, widths2, names=["A", "B", "C", "D"])
comb = combine_seqs(x, y)
comb = combine_sequences(x, y)
assert comb.get_names() == ["a", "b", "c", "d", "A", "B", "C", "D"]

x = IRanges(starts, widths)
y = IRanges(starts2, widths2, names=["A", "B", "C", "D"])
comb = combine_seqs(x, y)
comb = combine_sequences(x, y)
assert comb.get_names() == ["", "", "", "", "A", "B", "C", "D"]
8 changes: 4 additions & 4 deletions tests/test_interrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_reduce():

assert all(np.equal(reduced.start, [-6, -2, 6, 9]))
assert all(np.equal(reduced.width, [3, 7, 0, 6]))
assert reduced.mcols.colnames == ["revmap"]
assert reduced.mcols.colnames.as_list() == ["revmap"]


def test_reduce_drop_ranges():
Expand All @@ -38,7 +38,7 @@ def test_reduce_drop_ranges():
reduced = x.reduce(drop_empty_ranges=True)
assert all(np.equal(reduced.start, [-6, -2, 9]))
assert all(np.equal(reduced.width, [3, 7, 6]))
assert reduced.mcols.colnames == []
assert reduced.mcols.colnames.as_list() == []


def test_reduce_drop_ranges_and_revmap():
Expand All @@ -49,7 +49,7 @@ def test_reduce_drop_ranges_and_revmap():
reduced = x.reduce(drop_empty_ranges=True, with_reverse_map=True)
assert all(np.equal(reduced.start, [-6, -2, 9]))
assert all(np.equal(reduced.width, [3, 7, 6]))
assert reduced.mcols.colnames == ["revmap"]
assert reduced.mcols.colnames.as_list() == ["revmap"]


def test_gap():
Expand Down Expand Up @@ -82,4 +82,4 @@ def test_disjoin_with_revmap():
dj = x.disjoin(with_reverse_map=True)
assert all(np.equal(dj.start, [-6, -4, -2, 0, 1, 3, 9, 10, 13]))
assert all(np.equal(dj.width, [2, 1, 2, 1, 2, 2, 1, 3, 2]))
assert dj.mcols.colnames == ["revmap"]
assert dj.mcols.colnames.as_list() == ["revmap"]

0 comments on commit 6adb1af

Please sign in to comment.