spamosaic.utils
- class spamosaic.utils.Config(dictionary)[source]
Bases:
objectA wrapper class that recursively converts a dictionary to an object with attribute-style access.
- __dict__
Stores the nested configuration structure.
- Type:
dict
- spamosaic.utils.check_batch_empty(modBatch_dict, verbose=True)[source]
Checks if any batch in the modality-batch dictionary is empty and returns indices of modalities per batch.
- Parameters:
modBatch_dict (dict) – Dictionary of modality -> list of AnnData objects (or None).
verbose (bool) – Whether to print out batch composition.
- Returns:
List of lists indicating indices of modalities present in each batch.
- Return type:
list[list[int]]
- Raises:
ValueError – If any batch is completely empty.
- spamosaic.utils.clustering(adata, n_cluster, used_obsm, algo='kmeans', key='tmp_clust')[source]
Performs clustering on an AnnData object using k-means or Mclust.
- Parameters:
adata (AnnData) – Input data.
n_cluster (int) – Number of clusters.
used_obsm (str) – Key in .obsm to use for clustering.
algo (str) – Clustering algorithm, ‘kmeans’ or ‘mclust’.
key (str) – Key name to store clustering results in .obs.
- Returns:
Annotated object with cluster assignments.
- Return type:
AnnData
- spamosaic.utils.dict_map(_dict, _list)[source]
Applies dictionary mapping over a list.
- Parameters:
_dict (dict) – Dictionary to map from.
_list (list) – List of keys.
- Returns:
Mapped values.
- Return type:
list
- spamosaic.utils.flip_axis(ads, axis=0)[source]
Flips the spatial coordinates of AnnData objects along a specified axis.
- Parameters:
ads (list[AnnData]) – List of data objects to modify.
axis (int) – Axis to flip (0 for x, 1 for y).
- spamosaic.utils.get_barc2batch(modBatch_dict)[source]
Creates a mapping from cell barcodes to their batch indices.
- Parameters:
modBatch_dict (dict) – Dictionary of modality -> list of AnnData objects.
- Returns:
Mapping from barcode to batch index.
- Return type:
dict
- spamosaic.utils.get_umap(ad, use_reps=[])[source]
Computes UMAP embeddings for given representation keys and stores them in .obsm.
- Parameters:
ad (AnnData) – Input data.
use_reps (list[str]) – Keys in .obsm to compute UMAP for.
- Returns:
Annotated object with new UMAPs.
- Return type:
AnnData
- spamosaic.utils.load_config(filepath)[source]
Loads a YAML configuration file into a Config object.
- Parameters:
filepath (str) – Path to the YAML configuration file.
- Returns:
Parsed configuration object.
- Return type:
- spamosaic.utils.mclust_R(adata, num_cluster, modelNames='EEE', used_obsm='emb', random_seed=2020)[source]
Performs R’s Mclust via rpy2.
- Parameters:
adata (AnnData) – AnnData object with embedding stored in .obsm.
num_cluster (int) – Desired number of clusters.
modelNames (str) – Covariance structure model in Mclust.
used_obsm (str) – Key in .obsm to use for clustering.
random_seed (int) – Random seed for reproducibility.
- Returns:
Annotated object with added mclust cluster label.
- Return type:
AnnData
- spamosaic.utils.nn_approx(ds1, ds2, norm=True, knn=10, metric='manhattan', n_trees=10, include_distances=False)[source]
Performs approximate nearest neighbor search using Annoy.
- Parameters:
ds1 (np.ndarray) – Query data (n_samples, n_features).
ds2 (np.ndarray) – Reference data (n_samples, n_features).
norm (bool) – Whether to normalize data before search.
knn (int) – Number of nearest neighbors to retrieve.
metric (str) – Distance metric for Annoy (e.g., ‘manhattan’).
n_trees (int) – Number of trees used by Annoy.
include_distances (bool) – Whether to return distances with indices.
- Returns:
Indices of nearest neighbors, optionally with distances.
- Return type:
np.ndarray or tuple
- spamosaic.utils.plot_basis(ad, basis, color, **kwargs)[source]
Wrapper for Scanpy’s sc.pl.embedding with warning suppression.
- Parameters:
ad (AnnData) – Annotated data object.
basis (str) – Key of embedding (e.g., ‘umap’).
color (str) – Variable in .obs to color by.
**kwargs – Additional plotting arguments.
- spamosaic.utils.reorder(ad1, ad2)[source]
Aligns and reorders two AnnData objects to only shared barcodes.
- Parameters:
ad1 (AnnData) – First data object.
ad2 (AnnData) – Second data object.
- Returns:
Reordered and aligned data objects.
- Return type:
Tuple[AnnData, AnnData]
- spamosaic.utils.split_adata_ob(ads, ad_ref, ob='obs', key='emb')[source]
Splits a reference AnnData object’s observations or embeddings into a list of AnnData objects.
- Parameters:
ads (list[AnnData]) – List of target AnnData objects.
ad_ref (AnnData) – Source AnnData object containing merged info.
ob (str) – Whether to split ‘obs’ or ‘obsm’.
key (str) – Key to split in .obs or .obsm.
Functions
Checks if any batch in the modality-batch dictionary is empty and returns indices of modalities per batch. |
|
Performs clustering on an AnnData object using k-means or Mclust. |
|
Applies dictionary mapping over a list. |
|
Flips the spatial coordinates of AnnData objects along a specified axis. |
|
Creates a mapping from cell barcodes to their batch indices. |
|
Computes UMAP embeddings for given representation keys and stores them in .obsm. |
|
Loads a YAML configuration file into a Config object. |
|
Performs R's Mclust via rpy2. |
|
Performs approximate nearest neighbor search using Annoy. |
|
Wrapper for Scanpy's sc.pl.embedding with warning suppression. |
|
Aligns and reorders two AnnData objects to only shared barcodes. |
|
Splits a reference AnnData object's observations or embeddings into a list of AnnData objects. |
Classes
A wrapper class that recursively converts a dictionary to an object with attribute-style access. |