From 10b4840ca5408655bc5d80684b4be821de652ba4 Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Mon, 20 May 2024 16:34:55 -0700 Subject: [PATCH] use alt funcs and object readers and writers --- setup.cfg | 2 +- .../read_single_cell_experiment.py | 22 ++++------ .../save_single_cell_experiment.py | 40 ++++++++----------- 3 files changed, 24 insertions(+), 40 deletions(-) diff --git a/setup.cfg b/setup.cfg index 182de57..f1fdf7e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,7 +49,7 @@ 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-base>=0.2.3 dolomite-se>=0.1.1 singlecellexperiment>=0.4.3,<0.5.0 diff --git a/src/dolomite_sce/read_single_cell_experiment.py b/src/dolomite_sce/read_single_cell_experiment.py index e8ca41b..f6b648b 100644 --- a/src/dolomite_sce/read_single_cell_experiment.py +++ b/src/dolomite_sce/read_single_cell_experiment.py @@ -3,7 +3,6 @@ import dolomite_base as dl from dolomite_base.read_object import read_object_registry -from dolomite_se import read_common_se_props from singlecellexperiment import SingleCellExperiment read_object_registry["single_cell_experiment"] = ( @@ -37,29 +36,22 @@ def read_single_cell_experiment( with file-backed arrays in the assays. """ - _row_data, _column_data, _assays = read_common_se_props(path) + metadata["type"] = "range_summarized_experiment" + rse = dl.alt_read_object(path, metadata=metadata, **kwargs) _main_expt_name = None if "main_experiment_name" in metadata["single_cell_experiment"]: _main_expt_name = metadata["single_cell_experiment"]["main_experiment_name"] sce = SingleCellExperiment( - assays=_assays, - row_data=_row_data, - column_data=_column_data, + assays=rse.get_assays(), + row_data=rse.get_row_data(), + column_data=rse.get_column_data(), + row_ranges=rse.get_row_ranges(), + metadata=rse.get_metadata(), main_experiment_name=_main_expt_name, ) - _meta_path = os.path.join(path, "other_data") - if os.path.exists(_meta_path): - _meta = dl.alt_read_object(_meta_path, **kwargs) - sce = sce.set_metadata(_meta.as_dict()) - - _ranges_path = os.path.join(path, "row_ranges") - if os.path.exists(_ranges_path): - _ranges = dl.alt_read_object(_ranges_path, **kwargs) - sce = sce.set_row_ranges(_ranges) - _rdim_path = os.path.join(path, "reduced_dimensions") if os.path.exists(_rdim_path): _rdims = {} diff --git a/src/dolomite_sce/save_single_cell_experiment.py b/src/dolomite_sce/save_single_cell_experiment.py index 4ce0f35..0ca0fd9 100644 --- a/src/dolomite_sce/save_single_cell_experiment.py +++ b/src/dolomite_sce/save_single_cell_experiment.py @@ -65,34 +65,23 @@ def save_single_cell_experiment( if alt_expts_args is None: alt_expts_args = {} - _se_meta = f"{list(x.shape)}" - - _sce_meta = '"single_cell_experiment": { "version": "1.0" }' - if x.get_main_experiment_name() is not None: - _sce_meta = ( - '"single_cell_experiment": { "version": "1.0", "main_experiment_name": "' - + str(x.get_main_experiment_name()) - + '" }' - ) - - with open(os.path.join(path, "OBJECT"), "w", encoding="utf-8") as handle: - handle.write( - '{ "type": "single_cell_experiment", ' - + _sce_meta - + ", " - + '"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 ) + # Modify OBJECT + _info = dl.read_object_file(path) + _info["single_cell_experiment"] = {"version": "1.0"} + if x.get_main_experiment_name() is not None: + _info["single_cell_experiment"]["main_experiment_name"] = str( + x.get_main_experiment_name() + ) + dl.save_object_file(path, "single_cell_experiment", _info) + + # 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) # save rdims _rdim_names = x.get_reduced_dim_names() @@ -106,7 +95,9 @@ def save_single_cell_experiment( for _aidx, _aname in enumerate(_rdim_names): _rdim_save_path = os.path.join(_rdim_path, str(_aidx)) try: - dl.save_object(x.reduced_dim(_aname), path=_rdim_save_path, **rdim_args) + dl.alt_save_object( + x.reduced_dim(_aname), path=_rdim_save_path, **rdim_args, **kwargs + ) except Exception as ex: raise RuntimeError( "failed to stage reduced dimension '" @@ -129,10 +120,11 @@ def save_single_cell_experiment( for _aidx, _aname in enumerate(_alt_names): _alt_save_path = os.path.join(_alt_path, str(_aidx)) try: - dl.save_object( + dl.alt_save_object( x.alternative_experiment(_aname), path=_alt_save_path, **alt_expts_args, + **kwargs, ) except Exception as ex: raise RuntimeError(