Skip to content

Commit

Permalink
Fix for initializing empty GenomicRangesList (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Dec 22, 2023
1 parent 7137b5b commit 2a14ff2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/genomicranges/GenomicRangesList.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def _validate_ranges(ranges, num_ranges):
"`ranges` must be either a `GenomicRanges` or a list of `GenomicRanges`."
)

if isinstance(ranges, list) and len(ranges) != num_ranges:
if isinstance(ranges, list) and sum([len(x) for x in ranges]) != num_ranges:
raise ValueError(
"Length of 'ranges' does not match the number of genomic elements.",
f"Need to be {num_ranges}, provided {len(ranges)}.",
)
elif isinstance(ranges, GenomicRanges) and num_ranges != 1:
elif isinstance(ranges, GenomicRanges) and len(ranges) != num_ranges:
raise ValueError(
"Length of 'ranges' does not match the number of genomic elements.",
f"Need to be {num_ranges}, provided {len(ranges)}.",
Expand Down Expand Up @@ -175,7 +175,7 @@ def __init__(
self._metadata = {} if metadata is None else metadata

if validate is True:
_validate_ranges(self._ranges, len(self._range_lengths))
_validate_ranges(self._ranges, sum(self._range_lengths))
_validate_optional_attrs(self._mcols, self._names, len(self._range_lengths))

def _define_output(self, in_place: bool = False) -> "GenomicRangesList":
Expand Down Expand Up @@ -360,7 +360,7 @@ def set_ranges(
or as a reference to the (in-place-modified) original.
"""

_validate_ranges(ranges, len(self))
_validate_ranges(ranges, sum(self._range_lengths))
output = self._define_output(in_place)
output._ranges = ranges
return output
Expand Down Expand Up @@ -622,6 +622,10 @@ def is_empty(self) -> bool:

return False

##############################
######>> accessors <<#########
##############################

# TODO: convert some of these properties to a factorized array

@property
Expand Down Expand Up @@ -694,6 +698,10 @@ def is_circular(self) -> Dict[str, List[int]]:
"""
return self._generic_accessor("is_circular")

###################################
######>> pandas interop <<#########
###################################

def to_pandas(self) -> "pandas.DataFrame":
"""Coerce object to a :py:class:`pandas.DataFrame`.
Expand Down Expand Up @@ -724,8 +732,9 @@ def to_pandas(self) -> "pandas.DataFrame":

return all_concat

def add_element(self, key, value, element_metadata):
raise NotImplementedError("Adding new elements is not yet implemented!")
############################
######>> slicers <<#########
############################

def __getitem__(
self, args: Union[str, int, tuple, list, slice]
Expand Down Expand Up @@ -807,6 +816,10 @@ def __getitem__(

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

##########################
######>> empty <<#########
##########################

@classmethod
def empty(cls, n: int):
"""Create an empty ``n``-length `GenomicRangesList` object.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_grl_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ def test_create_grl():
def test_create_grl_should_fail():
with pytest.raises(Exception):
GenomicRangesList(ranges=[a, 2])


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

0 comments on commit 2a14ff2

Please sign in to comment.