Skip to content

Commit

Permalink
Sanitize masked arrays for SeqInfo (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Jan 22, 2024
1 parent cc72493 commit 4a8f942
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/genomicranges/SeqInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from warnings import warn

import biocutils as ut
import numpy

from .utils import _sanitize_vec

__author__ = "jkanche"
__copyright__ = "jkanche"
Expand All @@ -19,7 +20,7 @@ def _validate_seqnames(seqnames):


def _validate_seqlengths(seqlengths, num_seqs):
if not ut.is_list_of_type(seqlengths, (int, numpy.ndarray), ignore_none=True):
if not ut.is_list_of_type(seqlengths, int, ignore_none=True):
raise ValueError("'seqlengths' should be a list of integers.")

if num_seqs != len(seqlengths):
Expand All @@ -31,7 +32,7 @@ def _validate_seqlengths(seqlengths, num_seqs):


def _validate_is_circular(is_circular, num_seqs):
if not ut.is_list_of_type(is_circular, (bool, numpy.ndarray), ignore_none=True):
if not ut.is_list_of_type(is_circular, bool, ignore_none=True):
raise ValueError("'is_circular' should be a list of booleans.")

if num_seqs != len(is_circular):
Expand Down Expand Up @@ -175,6 +176,8 @@ def _flatten_incoming(self, values, expected) -> List:
output.append(None)
return output

values = _sanitize_vec(values)

if isinstance(values, list):
return values

Expand Down
8 changes: 8 additions & 0 deletions src/genomicranges/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def sanitize_strand_vector(
)


def _sanitize_vec(x: Sequence):
if isinstance(x, np.ma.MaskedArray):
x.filled(fill_value=None)
return x.tolist()

return list(x)


def _sanitize_strand_search_ops(query_strand, subject_strand):
query_strand = REV_STRAND_MAP[query_strand]
subject_strand = REV_STRAND_MAP[subject_strand]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_SeqInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ def test_create_seqInfo_numpy():
assert str(ex.value).find("list of strings") >= 0


def test_create_seqInfo_numpy_masked():
si = SeqInfo(
["chrA", "chrB", "chrC"],
np.ma.MaskedArray([10, None, 2200], mask=[0, 1, 0]),
np.array([None, True, False]),
["hg19", "hg38", None],
)

assert isinstance(si, SeqInfo)


def test_create_empty():
si = SeqInfo.empty()

Expand Down

0 comments on commit 4a8f942

Please sign in to comment.