scviva.external.Tangram

scviva.external.Tangram#

class scviva.external.Tangram(sc_adata, constrained=False, target_count=None, **model_kwargs)[source]#

Torch reimplementation of Tangram [Biancalani et al., 2021].

Maps single-cell RNA-seq data to spatial data. Original implementation: broadinstitute/Tangram.

Currently the “cells” and “constrained” modes are implemented.

Parameters:
  • mdata – MuData object that has been registered via setup_mudata().

  • constrained (bool) – Whether to use the constrained version of Tangram instead of cells mode.

  • target_count (int | None) – The number of cells to be filtered. Necessary when constrained is True.

  • **model_kwargs – Keyword args for TangramMapper

Examples

>>> from scvi.external import Tangram
>>> ad_sc = anndata.read_h5ad(path_to_sc_anndata)
>>> ad_sp = anndata.read_h5ad(path_to_sp_anndata)
>>> markers = pd.read_csv(path_to_markers, index_col=0)  # genes to use for mapping
>>> mdata = mudata.MuData(
        {
            "sp_full": ad_sp,
            "sc_full": ad_sc,
            "sp": ad_sp[:, markers].copy(),
            "sc": ad_sc[:, markers].copy()
        }
    )
>>> modalities = {"density_prior_key": "sp", "sc_layer": "sc", "sp_layer": "sp"}
>>> Tangram.setup_mudata(
        mdata, density_prior_key="rna_count_based_density", modalities=modalities
    )
>>> tangram = Tangram(sc_adata)
>>> tangram.train()
>>> ad_sc.obsm["tangram_mapper"] = tangram.get_mapper_matrix()
>>> ad_sp.obsm["tangram_cts"] = tangram.project_cell_annotations(
        ad_sc, ad_sp, ad_sc.obsm["tangram_mapper"], ad_sc.obs["labels"]
    )
>>> projected_ad_sp = tangram.project_genes(ad_sc, ad_sp, ad_sc.obsm["tangram_mapper"])

Notes

See further usage examples in the following tutorials: 1. /tutorials/notebooks/spatial/tangram_scvi_tools

__init__(sc_adata, constrained=False, target_count=None, **model_kwargs)[source]#

Methods

__init__(sc_adata[, constrained, target_count])

convert_legacy_save(dir_path, output_dir_path)

Converts a legacy saved model (<v0.15.0) to the updated save format.

data_registry(registry_key)

Returns the object in AnnData associated with the key in the data registry.

deregister_manager([adata])

Deregisters the AnnDataManager instance associated with adata.

differential_abundance(*args, **kwargs)

Not implemented for this model class.

from_spatialdata(sdata[, table_key, region])

Convenience constructor from a SpatialData object.

get_anndata_manager(adata[, required])

Retrieves the AnnDataManager for a given AnnData object.

get_from_registry(adata, registry_key)

Returns the object in AnnData associated with the key in the data registry.

get_latent_representation([adata, indices, ...])

Return latent representation with optional RAPIDS acceleration.

get_mapper_matrix()

Return the mapping matrix.

get_normalized_expression(*args, **kwargs)

Not implemented for this model class.

get_setup_arg(setup_arg)

Returns the string provided to setup of a specific setup_arg.

get_state_registry(registry_key)

Returns the state registry for the AnnDataField registered with this instance.

get_var_names([legacy_mudata_format])

Variable names of input data.

load(dir_path[, adata, accelerator, device, ...])

Instantiate a model from the saved output.

load_registry(dir_path[, prefix])

Return the full registry saved with the model.

plot_spatial_embedding([adata, basis, color])

Plot latent embedding overlaid on tissue spatial coordinates.

project_cell_annotations(adata_sc, adata_sp, ...)

Project cell annotations to spatial data.

project_genes(adata_sc, adata_sp, mapper)

Project gene expression to spatial data.

register_manager(adata_manager)

Registers an AnnDataManager instance with this model class.

save(dir_path[, prefix, overwrite, ...])

Save the state of the model.

setup_anndata()

Not implemented, use setup_mudata.

setup_mudata(mdata[, density_prior_key, ...])

Sets up the AnnData object for this model.

setup_spatialdata(sdata[, table_key, region])

Register fields from a SpatialData object.

to_device(device)

Move the model to the device.

train([max_epochs, accelerator, devices, ...])

Train the model.

transfer_fields(adata, **kwargs)

Transfer fields from a model to an AnnData object.

update_setup_method_args(setup_method_args)

Update setup method args.

view_anndata_setup([adata, ...])

Print summary of the setup for the initial AnnData or a given AnnData object.

view_registry([hide_state_registries])

Prints summary of the registry.

view_setup_args(dir_path[, prefix])

Print args used to setup a saved model.

view_setup_method_args()

Prints setup kwargs used to produce a given registry.

Attributes

adata

Data attached to model instance.

adata_manager

Manager instance associated with self.adata.

device

The current device that the module's params are on.

get_normalized_function_name

What the get normalized functions name is

history

Returns computed metrics during training.

is_trained

Whether the model has been trained.

registry

Data attached to model instance.

run_id

Returns the run id of the model.

run_name

Returns the run name of the model.

summary_string

Summary string of the model.

test_indices

Observations that are in test set.

train_indices

Observations that are in train set.

validation_indices

Observations that are in validation set.