From 6dc81fc0d1bdea66c44737700a8c9df4011d0ea4 Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Mon, 27 Jan 2025 16:22:27 -0800 Subject: [PATCH] recycle indices vector. fix slicing with empty arrays --- src/genomicranges/GenomicRangesList.py | 9 ++------- tests/test_grl_methods.py | 5 +++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/genomicranges/GenomicRangesList.py b/src/genomicranges/GenomicRangesList.py index b464d45..d459bef 100644 --- a/src/genomicranges/GenomicRangesList.py +++ b/src/genomicranges/GenomicRangesList.py @@ -847,12 +847,6 @@ def __getitem__(self, args: Union[str, int, tuple, list, slice]) -> Union[Genomi else: idx, _ = ut.normalize_subscript(args, len(self), self._names) - if ut.is_list_of_type(idx, bool): - if len(idx) != len(self): - raise ValueError("`indices` is a boolean vector, length should match the size of the data.") - - idx = [i for i in range(len(idx)) if idx[i] is True] - new_ranges = ut.subset_sequence(self._ranges, idx) new_range_lengths = ut.subset_sequence(self._range_lengths, idx) @@ -864,7 +858,8 @@ def __getitem__(self, args: Union[str, int, tuple, list, slice]) -> Union[Genomi if self.mcols is not None: new_mcols = ut.subset(self.mcols, idx) - return GenomicRangesList(new_ranges, new_range_lengths, new_names, new_mcols, self._metadata) + current_class_const = type(self) + return current_class_const(new_ranges, new_range_lengths, new_names, new_mcols, self._metadata) ####################################### ######>> class initializers <<######### diff --git a/tests/test_grl_methods.py b/tests/test_grl_methods.py index bae6b5b..8a4c88b 100644 --- a/tests/test_grl_methods.py +++ b/tests/test_grl_methods.py @@ -62,8 +62,9 @@ def test_slice_by_bool(): assert isinstance(sgrl, GenomicRangesList) assert len(sgrl) == 1 - with pytest.raises(Exception): - grl[[False]] + empty = grl[[False]] + assert isinstance(empty, GenomicRangesList) + assert len(empty) == 0 def test_is_empty_True():