Skip to content

Commit

Permalink
Use alt funcs for reading and writing objects (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored May 21, 2024
1 parent b330cf9 commit a71e33f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 55 deletions.
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ python_requires = >=3.8
# For more information, check out https://semver.org/.
install_requires =
importlib-metadata; python_version<"3.8"
dolomite-base>=0.2.0
dolomite-matrix>=0.1.0
dolomite-ranges>=0.1.0
summarizedexperiment>=0.4.2,<0.5.0
dolomite-base>=0.2.3
dolomite-matrix>=0.2.1
dolomite-ranges>=0.1.3
summarizedexperiment>=0.4.4


[options.packages.find]
Expand Down
26 changes: 10 additions & 16 deletions src/dolomite_se/read_ranged_summarized_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from dolomite_base.read_object import read_object_registry
from summarizedexperiment import RangedSummarizedExperiment

from .utils import read_common_se_props

read_object_registry[
"ranged_summarized_experiment"
] = "dolomite_se.read_ranged_summarized_experiment"
read_object_registry["ranged_summarized_experiment"] = (
"dolomite_se.read_ranged_summarized_experiment"
)


def read_ranged_summarized_experiment(
Expand Down Expand Up @@ -36,23 +34,19 @@ def read_ranged_summarized_experiment(
:py:class:`~summarizedexperiment.RangedSummarizedExperiment.RangedSummarizedExperiment`
with file-backed arrays in the assays.
"""

_row_data, _column_data, _assays = read_common_se_props(path)
metadata["type"] = "summarized_experiment"
se = dl.alt_read_object(path, metadata=metadata, **kwargs)

rse = RangedSummarizedExperiment(
assays=_assays,
row_data=_row_data,
column_data=_column_data,
assays=se.get_assays(),
row_data=se.get_row_data(),
column_data=se.get_column_data(),
metadata=se.get_metadata(),
)

_meta_path = os.path.join(path, "other_data")
if os.path.exists(_meta_path):
_meta = dl.read_object(_meta_path)
rse = rse.set_metadata(_meta.as_dict())

_ranges_path = os.path.join(path, "row_ranges")
if os.path.exists(_ranges_path):
_ranges = dl.read_object(_ranges_path)
_ranges = dl.alt_read_object(_ranges_path, **kwargs)
rse = rse.set_row_ranges(_ranges)

return rse
2 changes: 1 addition & 1 deletion src/dolomite_se/read_summarized_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def read_summarized_experiment(

_meta_path = os.path.join(path, "other_data")
if os.path.exists(_meta_path):
_meta = dl.read_object(_meta_path)
_meta = dl.alt_read_object(_meta_path, **kwargs)
se = se.set_metadata(_meta.as_dict())

return se
34 changes: 18 additions & 16 deletions src/dolomite_se/save_ranged_summarized_experiment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

import dolomite_base as dl
from summarizedexperiment import RangedSummarizedExperiment
from summarizedexperiment import RangedSummarizedExperiment, SummarizedExperiment

from .utils import save_common_se_props
# from .utils import save_common_se_props


@dl.save_object.register
Expand Down Expand Up @@ -40,30 +40,32 @@ def save_ranged_summarized_experiment(
Returns:
``x`` is saved to path.
"""
os.mkdir(path)

if data_frame_args is None:
data_frame_args = {}

if assay_args is None:
assay_args = {}

_se_meta = f"{list(x.shape)}"

with open(os.path.join(path, "OBJECT"), "w", encoding="utf-8") as handle:
handle.write(
'{ "type": "ranged_summarized_experiment", "ranged_summarized_experiment": { "version": "1.0" },'
+ '"summarized_experiment": {"version": "1.0", "dimensions": '
+ _se_meta
+ " } }"
)

save_common_se_props(
x, path, data_frame_args=data_frame_args, assay_args=assay_args
# convert to SE
_se = SummarizedExperiment(
assays=x.get_assays(),
row_data=x.get_row_data(),
column_data=x.get_column_data(),
metadata=x.get_metadata(),
)
dl.alt_save_object(
_se, path, data_frame_args=data_frame_args, assay_args=assay_args, **kwargs
)

# save row_ranges
_ranges = x.get_row_ranges()
if _ranges is not None:
dl.save_object(_ranges, path=os.path.join(path, "row_ranges"))
dl.alt_save_object(_ranges, path=os.path.join(path, "row_ranges"), **kwargs)

# Modify OBJECT
_info = dl.read_object_file(path)
_info["ranged_summarized_experiment"] = {"version": "1.0"}
dl.save_object_file(path, "ranged_summarized_experiment", _info)

return
10 changes: 1 addition & 9 deletions src/dolomite_se/save_summarized_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,8 @@ def save_summarized_experiment(
if assay_args is None:
assay_args = {}

_se_meta = f"{list(x.shape)}"
with open(os.path.join(path, "OBJECT"), "w", encoding="utf-8") as handle:
handle.write(
'{ "type": "summarized_experiment", "summarized_experiment": { "version": "1.0", "dimensions": '
+ _se_meta
+ " } }"
)

save_common_se_props(
x, path, data_frame_args=data_frame_args, assay_args=assay_args
x, path, data_frame_args=data_frame_args, assay_args=assay_args, **kwargs
)

return
26 changes: 17 additions & 9 deletions src/dolomite_se/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dolomite_base as dl


def save_common_se_props(x, path, data_frame_args, assay_args):
def save_common_se_props(x, path, data_frame_args, assay_args, **kwargs):
"""Save common :py:class:`~summarizedexperiment.SummarizedExperiment.SummarizedExperiment`
properties to the specified path.
Expand All @@ -25,6 +25,10 @@ def save_common_se_props(x, path, data_frame_args, assay_args):
Further arguments to pass to the ``save_object`` method for the
assays.
"""
# save OBJECT
_info = {"summarized_experiment": {"version": "1.0", "dimensions": list(x.shape)}}
dl.save_object_file(path, "summarized_experiment", _info)

# save assays
_assay_names = x.get_assay_names()
if len(_assay_names) > 0:
Expand All @@ -37,7 +41,9 @@ def save_common_se_props(x, path, data_frame_args, assay_args):
for _aidx, _aname in enumerate(_assay_names):
_assay_save_path = os.path.join(_assays_path, str(_aidx))
try:
dl.save_object(x.assays[_aname], path=_assay_save_path, **assay_args)
dl.alt_save_object(
x.assays[_aname], path=_assay_save_path, **assay_args
)
except Exception as ex:
raise RuntimeError(
"failed to stage assay '"
Expand All @@ -51,21 +57,23 @@ def save_common_se_props(x, path, data_frame_args, assay_args):
# save row data
_rdata = x.get_row_data()
if _rdata is not None and (_rdata.row_names is not None or _rdata.shape[1] > 0):
dl.save_object(_rdata, path=os.path.join(path, "row_data"), **data_frame_args)
dl.alt_save_object(
_rdata, path=os.path.join(path, "row_data"), **data_frame_args
)

# save column data
_cdata = x.get_column_data()
if _cdata is not None and (_cdata.row_names is not None or _cdata.shape[1] > 0):
dl.save_object(
dl.alt_save_object(
_cdata, path=os.path.join(path, "column_data"), **data_frame_args
)

_meta = x.get_metadata()
if _meta is not None and len(_meta) > 0:
dl.save_object(_meta, path=os.path.join(path, "other_data"))
dl.alt_save_object(_meta, path=os.path.join(path, "other_data"), **kwargs)


def read_common_se_props(path):
def read_common_se_props(path, **kwargs):
"""Read shared properties from a directory containing
:py:class:`~summarizedexperiment.SummarizedExperiment.SummarizedExperiment` or
its derivatives.
Expand All @@ -80,12 +88,12 @@ def read_common_se_props(path):
_row_data = None
_rdata_path = os.path.join(path, "row_data")
if os.path.exists(_rdata_path):
_row_data = dl.read_object(_rdata_path)
_row_data = dl.alt_read_object(_rdata_path, **kwargs)

_column_data = None
_cdata_path = os.path.join(path, "column_data")
if os.path.exists(_cdata_path):
_column_data = dl.read_object(_cdata_path)
_column_data = dl.alt_read_object(_cdata_path, **kwargs)

_assays = {}
_assays_path = os.path.join(path, "assays")
Expand All @@ -97,7 +105,7 @@ def read_common_se_props(path):
_assay_read_path = os.path.join(_assays_path, str(_aidx))

try:
_assays[_aname] = dl.read_object(_assay_read_path)
_assays[_aname] = dl.alt_read_object(_assay_read_path, **kwargs)
except Exception as ex:
raise RuntimeError(
f"failed to load assay '{_aname}' from '{path}'; " + str(ex)
Expand Down

0 comments on commit a71e33f

Please sign in to comment.