to_seurat#
- SingleCell.to_seurat(seurat_object_name, /, *, QC_column='passed_QC', assay='RNA', layer='counts', empty_X=False, v3=False)[source]#
Convert this SingleCell dataset to a Seurat object in the R workspace of the ryp Python-R bridge.
Make sure to remove cells failing QC with sc.filter_obs(QC_column) first, or specify subset=True in qc(). Alternatively, to include cells failing QC in the Seurat object, set QC_column to None.
When converting to Seurat, to match the requirements of Seurat objects, the ‘X_’ prefix (often used by Scanpy) will be removed from each key of obsm where it is present (e.g. ‘X_umap’ will become ‘umap’). Seurat will also add ‘orig.ident’, ‘nCount_RNA’ and ‘nFeature_RNA’ as gene-level metadata by default; you can disable the calculation of the latter two columns with:
from ryp import r r('options(Seurat.object.assay.calcn = FALSE)')
varm and DataFrame keys of obsm will not be converted.
- Parameters:
seurat_object_name: str
the name of the R variable to assign the Seurat object to
QC_column: str | None
if not None, raise an error if this column is present in obs and not all cells pass QC
assay: str
the name to use for the active assay
layer: str
the name of the layer within the active assay to save X to; must be ‘counts’ or ‘data’
empty_X: bool
if True, allow converting SingleCell datasets with missing counts (i.e. where self.X is None), by using an empty, dummy matrix with no non-zero entries as the Seurat object’s count matrix
v3: bool
if True, save to Seurat’s version 3 format instead of the more current version 5 format. When v3=True, the Seurat object’s assay is created with
CreateAssayObject()instead ofCreateAssay5Object().
- Return type:
None
Note
Seurat objects do not support count matrices with more than 2,147,483,647 (INT32_MAX) non-zero elements. If your SingleCell dataset is larger than this, it cannot be converted!
Note
Seurat requires the counts to be float64 and the indices of the count matrix to be int32. To avoid copying data when converting, consider converting the counts to float64 with sc.X = sc.X.astype(np.float64, copy=False) and the indices with sc.X.shrink_indices() (an in-place operation).
Note
In the special case where the counts are float64 or the indices of the count matrix are int32, the counts or indices will not be copied during the conversion to save memory. This means modifying the Seurat object’s count matrix will also modify the original SingleCell dataset. If this behavior is undesirable, explicitly copy the dataset before converting with sc.copy().to_seurat().