Skip to content

Commit

Permalink
subset an empty gr/grl
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Dec 28, 2023
1 parent e683fbc commit 81b382a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
49 changes: 7 additions & 42 deletions src/genomicranges/GenomicRangesList.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,7 @@ def __getitem__(
If ``args`` is not a supported slice argument.
Returns:
The genomic element as a ``GenomicRanges`` object
or a new ``GenomicRangesList`` of the slice.
A new ``GenomicRangesList`` of the slice.
"""
if isinstance(args, int):
return self._ranges[args]
Expand All @@ -772,50 +771,16 @@ def __getitem__(
_idx = self.names.map(args)
return self._ranges[_idx]
else:
new_ranges = None
new_range_lengths = None
new_names = None
new_mcols = None
new_metadata = self.metadata

if isinstance(args, tuple):
# TODO: should figure out what to do with the second dimension later.
if len(args) >= 1:
args = args[0]

if isinstance(args, list):
if ut.is_list_of_type(args, bool):
if len(args) != len(self):
raise ValueError(
"`indices` is a boolean vector, length should match the size of the data."
)

args = [i for i in range(len(args)) if args[i] is True]

new_ranges = [self.ranges[i] for i in args]
new_range_lengths = [self._range_lengths[i] for i in args]
if self.names is not None:
new_names = [self.names[i] for i in args]

if self.mcols is not None:
new_mcols = self.mcols[args, :]
elif isinstance(args, slice):
new_ranges = self.ranges[args]
new_range_lengths = self._range_lengths[args]
if self.names is not None:
new_names = self.names[args]

if self.mcols is not None:
new_mcols = self.mcols[args, :]
else:
raise TypeError("Arguments to slice is not a list of supported types.")
idx, _ = ut.normalize_subscript(args, len(self), self._names)

return GenomicRangesList(
new_ranges, new_range_lengths, new_names, new_mcols, new_metadata
self._ranges[idx],
self._range_lengths[idx],
self._names[idx],
self._mcols[idx, :],
self._metadata,
)

raise TypeError("Arguments to slice is not supported.")

##########################
######>> empty <<#########
##########################
Expand Down
8 changes: 8 additions & 0 deletions tests/test_gr_methods_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def test_slices():
assert len(subset_gr) == 3
assert subset_gr.seqnames == ["chr1", "chr3", "chr3"]

def test_gr_empty_subset():
gre = GenomicRanges.empty()

assert gre is not None
assert isinstance(gre, GenomicRanges)
assert len(gre) == 0

subset = gre[0:10]

def test_export():
df = gr.to_pandas()
Expand Down
11 changes: 10 additions & 1 deletion tests/test_grl_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_is_empty_slice():

assert grl.is_empty() == False

sgrl = grl[0:1, :]
sgrl = grl[0:1]
assert sgrl is not None
assert isinstance(sgrl, GenomicRangesList)
assert len(sgrl) == 1
Expand Down Expand Up @@ -95,3 +95,12 @@ def test_combine():
cgrl = combine_sequences(grla, grlb)

assert len(cgrl) == 3


def test_empty_grl_slice():
grl = GenomicRangesList.empty(n=100)
assert isinstance(grl, GenomicRangesList)

subset = grl[0:10]
assert isinstance(subset, GenomicRangesList)
assert len(subset) == 10

0 comments on commit 81b382a

Please sign in to comment.