Skip to content

Commit

Permalink
Use alt functions to save and read objects. (#2)
Browse files Browse the repository at this point in the history
Update documentation and bump versions for dependencies
  • Loading branch information
jkanche authored May 21, 2024
1 parent 3e1cadf commit 3960958
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ 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-se>=0.1.1
singlecellexperiment>=0.4.3,<0.5.0
dolomite-base>=0.2.4
dolomite-se>=0.1.3
singlecellexperiment>=0.4.5


[options.packages.find]
Expand Down
34 changes: 13 additions & 21 deletions src/dolomite_sce/read_single_cell_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

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"
] = "dolomite_sce.read_single_cell_experiment"
read_object_registry["single_cell_experiment"] = (
"dolomite_sce.read_single_cell_experiment"
)


def read_single_cell_experiment(
Expand All @@ -29,37 +28,30 @@ def read_single_cell_experiment(
Metadata for the object.
kwargs:
Further arguments, ignored.
Further arguments.
Returns:
A
:py:class:`~singlecellexperiment.SingleCellExperiment.SingleCellExperiment`
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.read_object(_meta_path)
sce = sce.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)
sce = sce.set_row_ranges(_ranges)

_rdim_path = os.path.join(path, "reduced_dimensions")
if os.path.exists(_rdim_path):
_rdims = {}
Expand All @@ -71,7 +63,7 @@ def read_single_cell_experiment(
_rdim_read_path = os.path.join(_rdim_path, str(_aidx))

try:
_rdims[_aname] = dl.read_object(_rdim_read_path)
_rdims[_aname] = dl.alt_read_object(_rdim_read_path, **kwargs)
except Exception as ex:
raise RuntimeError(
f"failed to load reduced dimension '{_aname}' from '{path}'; "
Expand All @@ -91,7 +83,7 @@ def read_single_cell_experiment(
_alt_read_path = os.path.join(_alt_path, str(_aidx))

try:
_alts[_aname] = dl.read_object(_alt_read_path)
_alts[_aname] = dl.alt_read_object(_alt_read_path, **kwargs)
except Exception as ex:
raise RuntimeError(
f"failed to load alternative experiment '{_aname}' from '{path}'; "
Expand Down
57 changes: 26 additions & 31 deletions src/dolomite_sce/save_single_cell_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import os

import dolomite_base as dl
from dolomite_se import save_common_se_props
from singlecellexperiment import SingleCellExperiment
from summarizedexperiment import RangedSummarizedExperiment


@dl.save_object.register
Expand Down Expand Up @@ -46,13 +46,11 @@ def save_single_cell_experiment(
alternative experiments.
kwargs:
Further arguments, ignored.
Further arguments.
Returns:
``x`` is saved to path.
"""
os.mkdir(path)

if data_frame_args is None:
data_frame_args = {}

Expand All @@ -65,34 +63,28 @@ def save_single_cell_experiment(
if alt_expts_args is None:
alt_expts_args = {}

_se_meta = f"{list(x.shape)}"
## Convert to RSE
_rse = RangedSummarizedExperiment(
assays=x.get_assays(),
row_data=x.get_row_data(),
column_data=x.get_column_data(),
row_ranges=x.get_row_ranges(),
row_names=x.get_row_names(),
column_names=x.get_column_names(),
metadata=x.get_metadata(),
)
dl.alt_save_object(
_rse, path, data_frame_args=data_frame_args, assay_args=assay_args, **kwargs
)

_sce_meta = '"single_cell_experiment": { "version": "1.0" }'
# Modify OBJECT
_info = dl.read_object_file(path)
_info["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
+ " } }"
_info["single_cell_experiment"]["main_experiment_name"] = str(
x.get_main_experiment_name()
)

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

_ranges = x.get_row_ranges()
if _ranges is not None:
dl.save_object(_ranges, path=os.path.join(path, "row_ranges"))
dl.save_object_file(path, "single_cell_experiment", _info)

# save rdims
_rdim_names = x.get_reduced_dim_names()
Expand All @@ -106,7 +98,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 '"
Expand All @@ -129,10 +123,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(
Expand Down

0 comments on commit 3960958

Please sign in to comment.