diff --git a/setup.cfg b/setup.cfg index ac3ab7f..9ecf041 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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] diff --git a/src/dolomite_se/read_ranged_summarized_experiment.py b/src/dolomite_se/read_ranged_summarized_experiment.py index 290b1cd..8211105 100644 --- a/src/dolomite_se/read_ranged_summarized_experiment.py +++ b/src/dolomite_se/read_ranged_summarized_experiment.py @@ -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( @@ -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 diff --git a/src/dolomite_se/read_summarized_experiment.py b/src/dolomite_se/read_summarized_experiment.py index 084d857..bb8af4b 100644 --- a/src/dolomite_se/read_summarized_experiment.py +++ b/src/dolomite_se/read_summarized_experiment.py @@ -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 diff --git a/src/dolomite_se/save_ranged_summarized_experiment.py b/src/dolomite_se/save_ranged_summarized_experiment.py index d1e1697..8b23c96 100644 --- a/src/dolomite_se/save_ranged_summarized_experiment.py +++ b/src/dolomite_se/save_ranged_summarized_experiment.py @@ -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 @@ -40,7 +40,6 @@ def save_ranged_summarized_experiment( Returns: ``x`` is saved to path. """ - os.mkdir(path) if data_frame_args is None: data_frame_args = {} @@ -48,22 +47,25 @@ def save_ranged_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": "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 diff --git a/src/dolomite_se/save_summarized_experiment.py b/src/dolomite_se/save_summarized_experiment.py index 5f060be..c04b440 100644 --- a/src/dolomite_se/save_summarized_experiment.py +++ b/src/dolomite_se/save_summarized_experiment.py @@ -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 diff --git a/src/dolomite_se/utils.py b/src/dolomite_se/utils.py index db63296..644f817 100644 --- a/src/dolomite_se/utils.py +++ b/src/dolomite_se/utils.py @@ -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. @@ -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: @@ -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 '" @@ -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. @@ -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") @@ -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)