Skip to content

Commit 3fb2107

Browse files
authored
set_assay accepts either an index or assay name (#94)
1 parent d64911d commit 3fb2107

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Changelog
22

3-
## Version 0.5.4
3+
## Version 0.5.4 - 0.5.5
44

55
- Coercions from SE to RSE and vice-versa.
66
- Adapting changes from genomic ranges's search and overlap methods.
7+
- Update `set_assay` to accept either an assay name or an index position of the assay to replace.
78

89
## Version 0.5.1 - 0.5.3
910

src/summarizedexperiment/BaseSE.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,16 @@ def assay(self, assay: Union[int, str]) -> Any:
960960
"""Alias for :py:attr:`~assay`. For backwards compatibility"""
961961
return self.get_assay(assay)
962962

963-
def set_assay(self, name: str, assay: Any, in_place: bool = False) -> "BaseSE":
963+
def set_assay(self, name: Union[str, int], assay: Any, in_place: bool = False) -> "BaseSE":
964964
"""Add or replace :py:attr:`~summarizedexperiment.BaseSE.BaseSE.assays`'s.
965965
966966
Args:
967967
name:
968968
New or existing assay name.
969969
970+
Alternatively, may provide an index position of the assay
971+
to replace.
972+
970973
assay:
971974
A 2-dimensional matrix represented as either
972975
:py:class:`~numpy.ndarray` or :py:class:`~scipy.sparse.spmatrix`.
@@ -991,7 +994,19 @@ def set_assay(self, name: str, assay: Any, in_place: bool = False) -> "BaseSE":
991994
output = self._define_output(in_place)
992995
if in_place is False:
993996
output._assays = output._assays.copy()
994-
output._assays[name] = assay
997+
998+
if isinstance(name, int):
999+
if name > len(output._assays):
1000+
raise IndexError("'name' is greather than the number of assays.")
1001+
1002+
if name < 0:
1003+
raise ValueError("'name' cannot be less than 0.")
1004+
1005+
output._assays[output.get_assay_names()[name]] = assay
1006+
elif isinstance(name, str):
1007+
output._assays[name] = assay
1008+
else:
1009+
raise ValueError("'name' must be either a string or an index value.")
9951010
return output
9961011

9971012
##########################

tests/test_SE_methods.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ def test_SE_assay_getters_and_setters():
128128
assert tse.get_assay("new_counts") is not None
129129
assert new_tse.get_assay("new_counts") is not None
130130

131+
mod_tse = tse.set_assay(0, assay=np.random.rand(nrows, ncols), in_place=False)
132+
assert mod_tse.get_assay_names() == tse.get_assay_names()
133+
assert tse.get_assay("new_counts") is not None
134+
assert mod_tse.get_assay("new_counts") is not None
135+
136+
with pytest.raises(Exception):
137+
tse.set_assay(4, assay=np.random.rand(nrows, ncols), in_place=False)
138+
139+
with pytest.raises(Exception):
140+
tse.set_assay(-1, assay=np.random.rand(nrows, ncols), in_place=False)
141+
131142
def test_SE_to_rse():
132143
tse = SummarizedExperiment(
133144
assays={"counts": counts}, row_data=row_data, column_data=col_data

0 commit comments

Comments
 (0)