Skip to content

Commit

Permalink
parse sce's
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Oct 24, 2024
1 parent 7c2cae7 commit 95d495f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/rds2py/PyRdsReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _handle_r_special_cases(
# Special handling for R integer containing NA
if size != 2:
if any(data == self.R_MIN):
return [None if x == self.R_MIN else x for x in data]
return np.array([np.nan if x == self.R_MIN else x for x in data])

# Special handling for R integer sequences
if (
Expand Down
2 changes: 1 addition & 1 deletion src/rds2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
from .read_granges import parse_genomic_ranges, parse_granges_list
from .read_rle import parse_rle
from .read_se import parse_summarized_experiment, parse_ranged_summarized_experiment
from .read_sce import parse_single_cell_experiment
from .read_sce import parse_single_cell_experiment, parse_alts_summarized_experiment_by_column
1 change: 1 addition & 0 deletions src/rds2py/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"RangedSummarizedExperiment": "rds2py.parse_ranged_summarized_experiment",
# single-cell experiment
"SingleCellExperiment": "rds2py.parse_single_cell_experiment",
"SummarizedExperimentByColumn": "rds2py.parse_alts_summarized_experiment_by_column",
}


Expand Down
48 changes: 27 additions & 21 deletions src/rds2py/read_sce.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
__license__ = "MIT"


def parse_alts_summarized_experiment_by_column(robject: dict):
_cls = get_class(robject)

if _cls not in ["SummarizedExperimentByColumn"]:
raise RuntimeError(
f"`robject` does not contain a 'SummarizedExperimentByColumn' object, contains `{_cls}`."
)

objs = {}

for key, val in robject["attributes"].items():
objs[key] = _dispatcher(val)

return objs


def parse_single_cell_experiment(robject: dict):
"""Parse an R object as :py:class:`~singlecellexperiment.SingleCellExperiment.SingleCellExperiment`.
Expand All @@ -32,7 +48,7 @@ def parse_single_cell_experiment(robject: dict):

# check red. dims, alternative expts
robj_reduced_dims = None
robj_altExps = None
robj_alt_exps = None
col_attrs = list(
_dispatcher(
robject["attributes"]["int_colData"]["attributes"]["listData"][
Expand All @@ -47,28 +63,18 @@ def parse_single_cell_experiment(robject: dict):
"data"
][idx]

if idx_col == "reducedDims" and idx_value["data"] is not None:
if idx_col == "reducedDims" and idx_value.get("data", None) is not None:
robj_reduced_dims = _dispatcher(idx_value)

if idx_col == "altExps":
alt_names = idx_value["attributes"]["listData"]["attributes"]["names"][
"data"
]
robj_altExps = {}
for idx_alt_names in range(len(alt_names)):
altn = alt_names[idx_alt_names]

alt_key = list(
idx_value["attributes"]["listData"]["data"][idx_alt_names][
"attributes"
].keys()
)[0]

robj_altExps[altn] = _dispatcher(
idx_value["attributes"]["listData"]["data"][idx_alt_names][
"attributes"
][alt_key]
)
alt_names = list(
_dispatcher(idx_value["attributes"]["listData"]["attributes"]["names"])
)
robj_alt_exps = {}
for idx, altn in enumerate(alt_names):
robj_alt_exps[altn] = _dispatcher(
idx_value["attributes"]["listData"]["data"][idx]
)["se"]

# ignore colpairs for now, does anyone even use this ?
# if col == "colPairs":
Expand All @@ -78,6 +84,6 @@ def parse_single_cell_experiment(robject: dict):
row_data=_rse.row_data,
column_data=_rse.column_data,
row_ranges=_rse.row_ranges,
alternative_experiments=robj_altExps,
alternative_experiments=robj_alt_exps,
reduced_dims=robj_reduced_dims,
)

0 comments on commit 95d495f

Please sign in to comment.