step.stmodel

Overview

Classes

stModel

stModel is the main class for spatial transcriptomics data.

Classes

class stModel(adata: anndata.AnnData | None = None, file_path: str | None = None, n_top_genes: int | None = 2000, geneset_to_use: Sequence[str] | None = None, batch_key: str | None = None, layer_key: str | None = None, coord_keys: Tuple[str, str] = ('array_row', 'array_col'), log_transformed=False, module_dim=30, decoder_input_dim=None, hidden_dim=64, n_modules=32, model_checkpoint=None, edge_clip=2, logarithm_first=False, variational=True, n_glayers=4, hvg_method='seurat_v3', filtered=False, dispersion='gene', device=None, **kwargs)

stModel is the main class for spatial transcriptomics data.

adata

Annotated data object containing the gene expression data.

dataset

StDataset object.

_functional

stSmoother object.

Initialize the stModel object.

Parameters:
  • adata (Optional[AnnData]) – Annotated data matrix with shape (n_obs, n_vars). If provided, it will be used to create the StDataset.

  • file_path (Optional[str]) – Path to the adata file. If provided, it will be used to create the StDataset.

  • n_top_genes (Optional[int]) – Number of top genes to select. Default is 2000.

  • geneset_to_use (Optional[Sequence[str]]) – List of genes to use. Default is None.

  • batch_key (Optional[str]) – Key for batch information in adata.obs. Default is None.

  • layer_key (Optional[str]) – Key for layer information in adata.layers. Default is None.

  • coord_keys (Tuple[str, str]) – Keys for spatial coordinates in adata.obsm. Default is (‘array_row’, ‘array_col’).

  • log_transformed (bool) – Whether the gene expression values are log-transformed. Default is False.

  • module_dim (int) – Dimensionality of the modules. Default is 30.

  • decoder_input_dim (Optional[int]) – Dimensionality of the decoder input. If None, it will be set to module_dim.

  • hidden_dim (int) – Dimensionality of the hidden layers. Default is 64.

  • n_modules (int) – Number of modules. Default is 32.

  • model_checkpoint (Any) – Model checkpoint to load. Default is None.

  • edge_clip (int) – Clip the adj edges to edge_clip. Default is 2.

  • logarithm_first (bool) – Whether to apply logarithm transformation before hvgs. Default is False.

  • variational (bool) – Whether to use variational inference. Default is False.

  • n_glayers (int) – Number of graph convolutional layers. Default is 4.

  • hvg_method (str) – Method to select highly variable genes. Default is “seurat_v3”.

  • filtered (bool) – Whether the gene expression data (cells) are filtered. Default is False.

  • **kwargs (Any) – Additional keyword arguments.

Raises:

AssertionError – If neither adata nor file_path is provided.

Overview

Methods

cluster(adata, n_clusters, use_rep, key_added, method, seed)

Cluster the embedding of spatial transcriptomics.

sub_cluster(adata, n_clusters, use_rep, pre_key, key_added)

Sub-cluster the clusters of spatial transcriptomics.

summarize_domain(cell_type_names, adata, domain_key, average, obsm_key, figsize, show, save)

Summarize the domain of spatial transcriptomics.

summarize_single_domain(cell_type_names, adata, domain_key, obsm_key, figsize, show, save)

abc -

save(path)

-

load(path, adata, filepath, config_name, model_name)

class Load the model and the data.

spatial_plot(slide, with_images, **kwargs)

Wrapper for plotting spatial feature plot with self-contained data.

Members

cluster(adata: anndata.AnnData | None = None, n_clusters=3, use_rep='X_smoothed', key_added='domain', method='kmeans', seed=None)

Cluster the embedding of spatial transcriptomics.

Parameters:
  • adata (Optional[AnnData]) – Annotated data matrix with shape (n_obs, n_vars). If provided, it will be used to perform clustering, and the result will be added to adata.obs; otherwise, it will use the adata in the stModel object.

  • n_clusters (int) – Number of clusters. Default is 3.

  • use_rep (Optional[str]) – Key for the representation to use. If None, it will use the default representation.

  • key_added (str) – Key to add to adata.obs. Default is “domain”.

  • method (str) – Clustering method. Default is “kmeans”.

  • seed (Optional[int]) – Random seed. Default is None.

sub_cluster(adata: anndata.AnnData | None, n_clusters=3, use_rep='X_smoothed', pre_key='domain', key_added='sub_domain')

Sub-cluster the clusters of spatial transcriptomics.

Parameters:
  • adata (Optional[AnnData]) – Annotated data matrix with shape (n_obs, n_vars). If provided, it will be used to perform sub-clustering, and the result will be added to adata.obs; otherwise, it will use the adata in the stModel object.

  • n_clusters (int) – Number of clusters. Default is 3.

  • use_rep (Optional[str]) – Key for the representation to use. If None, it will use the default representation.

  • pre_key (str) – Key for the pre-clustered clusters in adata.obs. Default is “domain”.

  • key_added (str) – Key to add to adata.obs. Default is “sub_domain”.

summarize_domain(cell_type_names, adata: anndata.AnnData | None = None, domain_key='domain', average=True, obsm_key='deconv', figsize=(15, 5), show=True, save=False)

Summarize the domain of spatial transcriptomics.

Parameters:
  • cell_type_names (Sequence[str]) – List of cell type names.

  • adata (Optional[AnnData]) – Annotated data matrix with shape (n_obs, n_vars). If provided, it will be used to perform sub-clustering, and the result will be added to adata.obs; otherwise, it will use the adata in the stModel object.

  • domain_key (str) – Key for the domain information in adata.obs. Default is “domain”.

  • average (bool) – Whether to average the domain information. Default is True.

  • obsm_key (str) – Key for the deconvolution information in adata.obsm. Default is “deconv”.

  • figsize (Tuple[int, int]) – Figure size. Default is (15, 5).

  • show (bool) – Whether to show the plot. Default is True.

  • save (bool) – Whether to save the plot. Default is False.

abstract summarize_single_domain(cell_type_names, adata: anndata.AnnData | None = None, domain_key='domain', obsm_key='deconv', figsize=(15, 5), show=True, save=True)
save(path: str | pathlib.Path = '.')
classmethod load(path: str, adata: anndata.AnnData | None = None, filepath: str | None = None, config_name: str = 'config.json', model_name: str = 'model.pth')

Load the model and the data.

Parameters:
  • path (str) – The path to load the model and the dataset.

  • adata (Optional[AnnData]) – Annotated data object containing the gene expression data.

  • filepath (Optional[str]) – Path to a file containing the gene expression data.

  • config_name (str) – The name of the config file.

  • model_name (str) – The name of the model file.

Returns:

The scModel object.

Return type:

scModel

spatial_plot(slide: str | int | None = None, with_images: bool = True, **kwargs)

Wrapper for plotting spatial feature plot with self-contained data.

Parameters:
  • slide (str | int | None) – Slide name or index. Default is 0.

  • with_images (bool) – Whether to plot based on images which uses scanpy.pl.spatial. Default is True.

  • **kwargs (Any) – Additional keyword arguments for scanpy.pl.spatial or scanpy,pl.embedding

Returns:

Figure.

Return type:

matplotlib.figure.Figure