Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues when subsetting empty granges objects #59

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/genomicranges/GenomicRanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,9 @@ def get_subset(self, subset: Union[str, int, bool, Sequence]) -> "GenomicRanges"
Returns:
A new ``GenomicRanges`` object with the ranges of interest.
"""
if len(self) == 0:
return GenomicRanges.empty()

idx, _ = ut.normalize_subscript(subset, len(self), self._names)

current_class_const = type(self)
Expand Down
8 changes: 7 additions & 1 deletion src/genomicranges/GenomicRangesList.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,13 @@ def __str__(self) -> str:
output = (
f"GenomicRangesList with {len(self)} range{'s' if len(self) != 1 else ''}"
)
output += f" and {len(self._mcols)} metadata column{'s' if len(self._mcols) != 1 else ''}\n \n"
output += f" and {len(self._mcols)} metadata column{'s' if len(self._mcols) != 1 else ''}\n"

if isinstance(self._ranges, GenomicRanges) and len(self._ranges) == 0:
output += "--- empty genomic ranges list ---"
return output

output += " \n"

nr = len(self)
added_table = False
Expand Down
4 changes: 4 additions & 0 deletions tests/test_grl_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,7 @@ def test_empty_grl_slice():
subset = grl[0:10]
assert isinstance(subset, GenomicRangesList)
assert len(subset) == 10

subset = grl[[1, 2, 3]]
assert isinstance(subset, GenomicRangesList)
assert len(subset) == 3
Loading