Skip to content

Commit

Permalink
use alt funcs and object readers and writers
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed May 20, 2024
1 parent 8d6692d commit 10b4840
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 7 additions & 15 deletions src/dolomite_sce/read_single_cell_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = (
Expand Down Expand Up @@ -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 = {}
Expand Down
40 changes: 16 additions & 24 deletions src/dolomite_sce/save_single_cell_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 '"
Expand All @@ -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(
Expand Down

0 comments on commit 10b4840

Please sign in to comment.