grassp.io.write_msnset#
- write_msnset(data, path, *, layer=None, layers=None, nan_to_unknown=True, obs_columns=None, var_columns=None, obsm_keys=None, varm_keys=None, check_normalized=True, write_script=False, overwrite=False)[source]#
Export a grassp
AnnDataas a pRoloc-ready artifact.Writes a self-describing h5ad that the companion R package turns into a real
MSnSet:remotes::install_github("czbiohub-sf/grassp", subdir = "r/grasspio") library(grasspio) x <- grassp_as_msnset("experiment.h5ad")
Everything crosses by default –
.obs,.var, every.obsmand.varmentry, every layer, and the whole of.uns– because the expected next step is to bring the object back withread_msnset(). The exceptions are.obsp/.varp, whicheSethas no slot for, and whatever you exclude yourself; both are listed inuns["msnset_dropped"]. Column names and dtypes are written out as they are; the one edit isnan_to_unknown, which swaps grassp’sNaNfor the"unknown"string pRoloc requires.Note that no column is nominated as the marker column. pRoloc’s
fcolis a per-call argument, so anMSnSetcan carry as many marker columns as it likes –markers,markers.orig,pd.markers– and each function is pointed at whichever it needs, exactly as in AnnData. You choose in R, at the call.- Parameters:
- data
AnnData Proteins in
.obs, fractions in.var(grassp’s orientation, which already matchesMSnSet’s features-by-fractionsexprs).- path
str|Path Destination
.h5ad.- layer
str|None(default:None) Which matrix becomes
exprs(), the one pRoloc’s functions operate on.Noneuses.X. If you name a layer here,.Xitself is not carried, and that is recorded inuns["msnset_dropped"].- layers
Optional[Sequence[str]] (default:None) Additional layers to carry across as extra
assayDataelements.None(default) sends all of them; pass[]to send none. AnMSnSet’sassayDatais a Biobase environment holding any number of equal-dimension matrices, so they survive intact and – importantly – are subset together withexprsbymarkerMSnSet,filterNAand ordinary[indexing. In R they are reachable withassayDataElementNames(x)andassayDataElement(x, "pvals").- nan_to_unknown
bool(default:True) Replace
NaNwith"unknown"in every text column of.obs– string, object and Categorical alike; numeric and boolean columns are left alone. pRoloc encodes unlabelled features with that sentinel and needs it:markerMSnSetandunknownMSnSetfail outright onNA, and a classifier’s training set is chosen withfData(object)[, fcol] != "unknown". Applying it to every text column rather than to a nominated one is what lets an object carry several marker sets at once.- obs_columns
Optional[Sequence[str]] (default:None) Restrict what crosses over.
Noneexports everything.- var_columns
Optional[Sequence[str]] (default:None) Restrict what crosses over.
Noneexports everything.- obsm_keys
Optional[Sequence[str]] (default:None) .obsm/.varmentries to export as matrix-valuedfData/pDatacolumns.None(default) sends all of them; pass[]to send none.pDatais the sameAnnotatedDataFrameclass asfData, so the two use one mechanism, and R subsets each correctly along its own axis. Column names are taken fromuns["obsm_colnames"]/uns["varm_colnames"], elseuns[f"{key}_categories"], else the categories of the companion label column, elseV1..Vn– so a probability matrix gets its compartment names even when theunskey was never written, which is how portal datasets are curated.- varm_keys
Optional[Sequence[str]] (default:None) .obsm/.varmentries to export as matrix-valuedfData/pDatacolumns.None(default) sends all of them; pass[]to send none.pDatais the sameAnnotatedDataFrameclass asfData, so the two use one mechanism, and R subsets each correctly along its own axis. Column names are taken fromuns["obsm_colnames"]/uns["varm_colnames"], elseuns[f"{key}_categories"], else the categories of the companion label column, elseV1..Vn– so a probability matrix gets its compartment names even when theunskey was never written, which is how portal datasets are curated.- check_normalized
bool(default:True) Warn when profiles do not sum to 1 per protein. pRoloc’s distance-based methods and its plots assume sum-normalised profiles; see
grassp.pp.normalize_total().- write_script
bool(default:False) Also write
<stem>_run_proloc.Rnext to the artifact: the install line, thegrassp_as_msnsetcall, a worked SVM/TAGM/phenoDisco sequence, and thegrassp_write_msnsetcall to send results back.- overwrite
bool(default:False) Overwrite an existing file rather than raising.
- data
- Return type:
- Returns:
The path actually written.
- Raises:
ValueError – If
.obs_namesare not unique or contain blanks (R rownames cannot), iflayeris missing, or if the destination exists andoverwriteisFalse.
Notes
The contract owns the
unskeys ingrassp.io._msnset.RESERVED_UNS_KEYS; they are regenerated on every export rather than copied fromdata.uns, so an artifact always describes itself rather than whatever it was last read from.Examples
>>> gr.io.write_msnset(adata, "experiment.h5ad", write_script=True) PosixPath('experiment.h5ad')