anndata_fcs#

Attributes#

Exceptions#

FCSConversionWarning

Warning emitted when information cannot be converted between AnnData and FCS without loss.

Classes#

FCSMetadata

Structure of adata.uns["fcs"].

Functions#

read_fcs(file, *[, preprocess])

Read an FCS file into an AnnData object.

write_fcs(adata, file, *[, index_channel, store_obs])

Write an AnnData object to an FCS file.

Package Contents#

exception anndata_fcs.FCSConversionWarning#

Bases: UserWarning

Warning emitted when information cannot be converted between AnnData and FCS without loss.

class anndata_fcs.FCSMetadata#

Bases: TypedDict

Structure 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.

analysis: dict[str, str]#
filename: NotRequired[str]#
schema_version: int#
text: dict[str, str]#
version: str#
anndata_fcs.__version__: str#
anndata_fcs.read_fcs(file, *, preprocess=False, **flowio_kwargs)#

Read an FCS file into an AnnData object.

X holds the event data as stored in the file. All metadata of the FCS file is kept:

  • Channel names ($PnN) become var_names. Duplicate names are made unique by appending -1, -2, …

  • Per-channel keywords are stored in var columns named after the keyword: $PnS holds $P1S, $P2S, …, $PnR holds $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_names and additional var columns are restored and the synthetic event index channel is removed from the event data. If obs was not stored, obs_names are 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 in layers["preprocessed"]. X and the keywords are not modified. The layer is derived from X and the keywords, so write_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:

  • X as event data (32-bit float, the only data type supported by FlowIO) with var_names as $PnN. Channels stored on log scale ($PnE) are converted to linear scale.

  • var columns $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 with adata.var["$PnS"] = adata.var["gene_ids"].

  • uns["fcs"]["text"] as global keywords. $SPILLOVER and $SPILL are restricted to the written channels. $ORIGINALITY is set to DataModified and $LAST_MODIFIED to the current time, since the file is a derived copy of the data.

  • obs (including obs_names) and other var columns as JSON encoded keywords in the ANNDATA_FCS_* namespace, which are restored by read_fcs().

Only X is written. To write raw (e.g. counts of a scanpy object), use write_fcs(adata.raw.to_adata(), ...).

Information that cannot be stored (raw, layers except layers["preprocessed"], obsm, obsp, varm, varp, uns entries other than uns["fcs"], unsupported column dtypes, keywords that cannot be written, float precision beyond float32) triggers a FCSConversionWarning. 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. Use None to omit it.

  • store_obs (bool) – If True, store obs and obs_names in 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