Utilities

Helper functions and network utilities used internally by SpaceTravLR and available for advanced users building custom workflows.

Spatial Utilities

SpaceTravLR.tools.utils.gaussian_kernel_2d(origin, xy_array, radius, eps=0.001)[source]

Compute a 2D Gaussian kernel weights for a given origin on a given grid.

Args:

origin (np.ndarray): The center point for the Gaussian kernel. xy_array (np.ndarray): Array of points to compute weights for. radius (float): The radius of the Gaussian kernel. eps (float): A small epsilon value to prevent division by zero.

Returns:

np.ndarray: Array of Gaussian weights.

SpaceTravLR.tools.utils.knn_distance_matrix(data, metric=None, k=40, mode='connectivity', n_jobs=4)[source]

Calculate a nearest neighbour distance matrix

Notice that k is meant as the actual number of neighbors NOT INCLUDING itself To achieve that we call kneighbors_graph with X = None

SpaceTravLR.tools.utils.connectivity_to_weights(mknn, axis=1)[source]
SpaceTravLR.tools.utils.convolve_by_sparse_weights(data, w)[source]
SpaceTravLR.tools.utils.is_mouse_data(adata)[source]

Determine if an AnnData object contains mouse or human data based on gene names.

This function examines gene names to determine if the data is from mouse (capitalized first letter only) or human (all caps gene symbols). It samples a subset of genes to make the determination.

Parameters:

adata (AnnData) – The annotated data matrix to check

Returns:

True if the data appears to be from mouse, False if it appears to be from human

Return type:

bool

SpaceTravLR.tools.utils.clean_up_adata(adata, fields_to_keep)[source]
SpaceTravLR.models.spatial_map.xyc2spatial_fast(xyc, m, n)[source]

Converts spatial coordinates (x, y) and cluster labels (c) to a spatial distance map with grid sizes m x n. Each channels encodes the distance map for a unique cluster.

Return (n_samples, n_clusters, m, n)

Ligand-Receptor Utilities

SpaceTravLR.models.parallel_estimators.received_ligands(xy, ligands_df, lr_info, scale_factor=1)[source]

Compute the amount of ligand received on the surface of each cell based on location.

Args:

xy (np.ndarray): Array of spatial coordinates (x, y). ligands_df (pd.DataFrame): ligand gene expression values.

Returns:

pd.DataFrame: DataFrame of received ligands for each cell.

SpaceTravLR.models.parallel_estimators.init_received_ligands(adata, radius, cell_threshes, contact_distance=50, scale_factor=1, layer='imputed_count')[source]
SpaceTravLR.models.parallel_estimators.create_spatial_features(x, y, celltypes, obs_index, radius=200)[source]
SpaceTravLR.models.parallel_estimators.get_filtered_df(counts_df, cell_thresholds=None, genes=None, min_expression=1e-09)[source]

Get filtered expression of ligands/ receptors based on celltype/ thresholds

Network & GRN Utilities

SpaceTravLR.tools.network.get_cellchat_db(species)[source]
SpaceTravLR.tools.network.expand_paired_interactions(df)[source]

Beta / Coefficient Utilities

SpaceTravLR.beta.compute_all_derivatives(tf_vals, lr_betas, lr_ligs, lr_recs, tfl_betas, tfl_ligs, tfl_regs)[source]