Skip to content

Commit

Permalink
fix: remove unncessary add_all in OverlapDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval committed Nov 8, 2024
1 parent 08151be commit b6b34a8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
6 changes: 2 additions & 4 deletions bedspec/_bedspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ def __init_subclass__(cls) -> None:
return super().__init_subclass__()

@final
@property
def length(self) -> int:
def __len__(self) -> int:
"""The length of this record."""
return 1

Expand Down Expand Up @@ -132,8 +131,7 @@ def __post_init__(self) -> None:
raise ValueError("start must be greater than 0 and less than end!")

@final
@property
def length(self) -> int:
def __len__(self) -> int:
"""The length of this record."""
return self.end - self.start

Expand Down
20 changes: 8 additions & 12 deletions bedspec/overlap/_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,22 @@ def __init__(self, features: Iterable[ReferenceSpanType] | None = None) -> None:
self._refname_to_tree: dict[Refname, cr.cgranges] = defaultdict(cr.cgranges) # type: ignore[attr-defined,name-defined] # pyright: ignore[reportUnknownArgumentType, reportUnknownMemberType]
self._refname_to_is_indexed: dict[Refname, bool] = defaultdict(lambda: False)
if features is not None:
self.add_all(features)
self.add(*features)

@override
def __iter__(self) -> Iterator[ReferenceSpanType]:
"""Iterate over the features in the overlap detector."""
return chain(*self._refname_to_features.values())

def add(self, feature: ReferenceSpanType) -> None:
def add(self, *features: ReferenceSpanType) -> None:
"""Add a feature to this overlap detector."""
refname: Refname = feature.refname
feature_idx: int = len(self._refname_to_features[refname])

self._refname_to_features[refname].append(feature)
self._refname_to_tree[refname].add(refname, feature.start, feature.end, feature_idx) # pyright: ignore[reportUnknownMemberType]
self._refname_to_is_indexed[refname] = False # mark that this tree needs re-indexing

def add_all(self, features: Iterable[ReferenceSpanType]) -> None:
"""Adds one or more features to this overlap detector."""
for feature in features:
self.add(feature)
refname: Refname = feature.refname
feature_idx: int = len(self._refname_to_features[refname])

self._refname_to_features[refname].append(feature)
self._refname_to_tree[refname].add(refname, feature.start, feature.end, feature_idx) # pyright: ignore[reportUnknownMemberType]
self._refname_to_is_indexed[refname] = False # mark that this tree needs re-indexing

def overlapping(self, feature: ReferenceSpan) -> Iterator[ReferenceSpanType]:
"""Yields all the overlapping features for a given query feature."""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_bedspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_point_bed_types_have_a_territory() -> None:

def test_point_bed_types_are_length_1() -> None:
"""Test that a point BED has a length of 1."""
assert Bed2(refname="chr1", start=1).length == 1
assert len(Bed2(refname="chr1", start=1)) == 1


def test_simple_bed_types_have_a_territory() -> None:
Expand All @@ -169,9 +169,9 @@ def test_simple_bed_types_have_a_territory() -> None:

def test_simple_bed_types_have_length() -> None:
"""Test that a simple BED has the right length."""
assert Bed3(refname="chr1", start=1, end=2).length == 1
assert Bed3(refname="chr1", start=1, end=3).length == 2
assert Bed3(refname="chr1", start=1, end=4).length == 3
assert len(Bed3(refname="chr1", start=1, end=2)) == 1
assert len(Bed3(refname="chr1", start=1, end=3)) == 2
assert len(Bed3(refname="chr1", start=1, end=4)) == 3


def test_simple_bed_validates_start_and_end() -> None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def test_we_can_add_all_features_to_the_overlap_detector() -> None:
bed1 = Bed3(refname="chr1", start=1, end=2)
bed2 = Bed4(refname="chr2", start=4, end=5, name="Clint Valentine")
detector: OverlapDetector[Bed3 | Bed4] = OverlapDetector()
detector.add_all([bed1, bed2])
beds: list[Bed3 | Bed4] = [bed1, bed2]
detector.add(*beds)
assert list(detector) == [bed1, bed2]


Expand Down

0 comments on commit b6b34a8

Please sign in to comment.