anndata_fcs#
Attributes#
Exceptions#
Warning emitted when information cannot be converted between AnnData and FCS without loss. |
Classes#
Structure of adata.uns["fcs"]. |
Functions#
Package Contents#
- exception anndata_fcs.FCSConversionWarning#
Bases:
UserWarningWarning emitted when information cannot be converted between AnnData and FCS without loss.
- class anndata_fcs.FCSMetadata#
Bases:
TypedDictStructure of adata.uns[“fcs”].
Fields#
- schema_version
Version of the anndata-fcs layout.
- version
FCS version of the source file (e.g. “3.1”).
- text
Global TEXT keywords as parsed by FlowIO (lower case, without “$”). Per-channel keywords are stored in var columns ($PnS, $PnR, …). Structural keywords (offsets, $DATATYPE, $PnB, …) are not stored, since they are regenerated when writing.
- analysis
Keywords of the ANALYSIS segment. Not written back, since FlowIO cannot write ANALYSIS segments.
- filename
Name of the source file, if known. Optional.
- anndata_fcs.read_fcs(file, *, preprocess=False, **flowio_kwargs)#
Read an FCS file into an AnnData object.
Xholds the event data as stored in the file. All metadata of the FCS file is kept:Channel names (
$PnN) becomevar_names. Duplicate names are made unique by appending-1,-2, …Per-channel keywords are stored in
varcolumns named after the keyword:$PnSholds$P1S,$P2S, …,$PnRholds$P1R,$P2R, … and so on for every keyword of the file (e.g.$PnDISPLAY). Values are raw strings, missing for channels without the keyword.Global keywords are stored in
uns["fcs"]["text"]with the names parsed by FlowIO (lower case, without$). Values are raw strings. Keywords describing the binary layout (offsets,$DATATYPE,$PnB, …) are not stored, since they are regenerated when writing.The ANALYSIS segment and the FCS version are stored in
uns["fcs"].If the file was written by
write_fcs(),obs,obs_namesand additionalvarcolumns are restored and the synthetic event index channel is removed from the event data. Ifobswas not stored,obs_namesare taken from the event index channel, i.e. the positions of the events in the written AnnData object.
Note
FlowIO closes file handles after reading them.
- Parameters:
file (str | os.PathLike[str] | IO[bytes] | flowio.FlowData) – Path, binary file handle or FlowData instance. A FlowData instance is not modified and does not share memory with the result.
preprocess (bool) – If
True, additionally store the event data with gain ($PnG), log ($PnE) and time ($TIMESTEP) scaling applied inlayers["preprocessed"].Xand the keywords are not modified. The layer is derived fromXand the keywords, sowrite_fcs()does not write it.**flowio_kwargs (Any) – Passed to
flowio.FlowData(e.g.ignore_offset_error=True).
- Returns:
AnnData object with events as observations and channels as variables.
- Return type:
AnnData
- Raises:
TypeError – If FlowIO arguments are given with a FlowData instance.
ValueError – If the file has no event data.
- anndata_fcs.write_fcs(adata, file, *, index_channel=DEFAULT_INDEX_CHANNEL, store_obs=True)#
Write an AnnData object to an FCS file.
The FCS file holds the following information:
Xas event data (32-bit float, the only data type supported by FlowIO) withvar_namesas$PnN. Channels stored on log scale ($PnE) are converted to linear scale.varcolumns$PnS,$PnR, … as per-channel keywords (as strings, missing values are not written). They follow their channel if channels are removed, reordered or renamed. E.g. set marker labels withadata.var["$PnS"] = adata.var["gene_ids"].uns["fcs"]["text"]as global keywords.$SPILLOVERand$SPILLare restricted to the written channels.$ORIGINALITYis set toDataModifiedand$LAST_MODIFIEDto the current time, since the file is a derived copy of the data.obs(includingobs_names) and othervarcolumns as JSON encoded keywords in theANNDATA_FCS_*namespace, which are restored byread_fcs().
Only
Xis written. To writeraw(e.g. counts of a scanpy object), usewrite_fcs(adata.raw.to_adata(), ...).Information that cannot be stored (
raw,layersexceptlayers["preprocessed"],obsm,obsp,varm,varp,unsentries other thanuns["fcs"], unsupported column dtypes, keywords that cannot be written, float precision beyond float32) triggers aFCSConversionWarning. To raise an error instead, turn the warning into an exception:with warnings.catch_warnings(): warnings.simplefilter("error", FCSConversionWarning) write_fcs(adata, "out.fcs")
All checks run before the file is opened, so a failed conversion does not modify an existing file.
- Parameters:
adata (anndata.AnnData) – AnnData object to write. It is not modified.
file (str | os.PathLike[str] | IO[bytes]) – Path or binary file handle.
index_channel (str | None) – Name of a synthetic channel holding the position of each event in
adata. It allows mapping events of exported gates back to observations. UseNoneto omit it.store_obs (bool) – If
True, storeobsandobs_namesin the FCS file.
- Raises:
TypeError – If the event data is not numeric.
ValueError – If channel names or keywords are invalid, or the metadata exceeds the size of the FCS TEXT segment.
- Return type:
None