bio-spatial-transcriptomics-spatial-statistics
Compute spatial statistics for spatial transcriptomics data using Squidpy. Calculate Moran's I, Geary's C, spatial autocorrelation, co-occurrence analysis, and neighborhood enrichment. Use when computing spatial autocorrelation or co-occurrence statistics.
Unsigned, install at your own risk
UnverifiedThis skill has no cryptographic signature attached. We can't verify the contents match what the publisher intended.
Install this skill
No account required. Pick your tool, or use the command that detects it for you.
Run in your terminal. It detects your AI tool.
npx @skills-hub-ai/cli install openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-statistics-bio-spatial-transcripManual setup by platform
Claude Code
~/.claude/skills/openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-statistics-bio-spatial-transcrip/SKILL.md
Instructions
Spatial Statistics
Compute spatial statistics and identify spatially variable features.
Required Imports
import squidpy as sq
import scanpy as sc
import pandas as pd
import numpy as np
Compute Spatial Autocorrelation (Moran's I)
# Requires spatial neighbors
sq.gr.spatial_neighbors(adata, coord_type='generic', n_neighs=6)
# Compute Moran's I for all genes (can be slow)
sq.gr.spatial_autocorr(adata, mode='moran')
# Or for specific genes
sq.gr.spatial_autocorr(adata, mode='moran', genes=['GENE1', 'GENE2', 'GENE3'])
# Results stored in adata.uns['moranI']
moran_results = adata.uns['moranI']
print(moran_results.head(20))
Interpret Moran's I
# Moran's I ranges from -1 to 1
# I > 0: positive spatial autocorrelation (similar values cluster)
# I = 0: random spatial distribution
# I < 0: negative spatial autocorrelation (dissimilar values cluster)
# Get significantly spatially variable genes
svg = moran_results[moran_results['pval_norm'] < 0.05].sort_values('I', ascending=False)
print(f'Found {len(svg)} spatially variable genes (p < 0.05)')
print('\nTop 10 spatially variable genes:')
print(svg.head(10)[['I', 'pval_norm']])
Compute Geary's C
# Alternative spatial autocorrelation measure
sq.gr.spatial_autocorr(adata, mode='geary')
# Results in adata.uns['gearyC']
geary_results = adata.uns['gearyC']
# C < 1: positive spatial autocorrelation
# C = 1: random
# C > 1: negative spatial autocorrelation
Co-occurrence Analysis
# Analyze co-localization of cell types/clusters
# First, ensure you have cluster labels
sc.pp.neighbors(adata)
sc.tl.leiden(adata)
# Compute co-occurrence
sq.gr.co_occurrence(adata, cluster_key='leiden')
# Results in adata.uns['leiden_co_occurrence']
# Visualize co-occurrence
sq.pl.co_occurrence(adata, cluster_key='leiden')
Interpret Co-occurrence
co_occ = adata.uns['leiden_co_occurrence']
occ_matrix = co_occ['occ'] # Occurrence matrix
interval = co_occ['interval'] # Distance intervals
# occ_matrix[i, j, k] = occurrence of cluster j around cluster i at distance interval k
print(f'Occurrence matrix shape: {occ_matrix.shape}')
print(f'Distance intervals: {interval}')
Neighborhood Enrichment
# Test if clusters are enriched in each other's neighborhoods
sq.gr.nhood_enrichment(adata, cluster_key='leiden')
# Results in adata.uns['leiden_nhood_enrichment']
# zscore > 0: clusters co-localize more than expected
# zscore < 0: clusters avoid each other
# Visualize
sq.pl.nhood_enrichment(adata, cluster_key='leiden')
Extract Enrichment Z-scores
enrichment = adata.uns['leiden_nhood_enrichment']
zscore = enrichment['zscore']
clusters = adata.obs['leiden'].cat.categories
# Convert to DataFrame
zscore_df = pd.DataFrame(zscore, index=clusters, columns=clusters)
print('Neighborhood enrichment z-scores:')
print(zscore_df)
Ripley's Statistics
# Ripley's K/L function for point pattern analysis (single-cell resolution data)
sq.gr.ripley(adata, cluster_key='leiden', mode='L')
# Results in adata.uns['leiden_ripley']
sq.pl.ripley(adata, cluster_key='leiden')
Centrality Scores
# Compute centrality of each cell type
sq.gr.centrality_scores(adata, cluster_key='leiden')
# Results in adata.uns['leiden_centrality_scores']
centrality = adata.uns['leiden_centrality_scores']
print(centrality)
Interaction Matrix
# Build interaction matrix between clusters
sq.gr.interaction_matrix(adata, cluster_key='leiden')
# Results in adata.uns['leiden_interactions']
interactions = adata.uns['leiden_interactions']
print(interactions)
Custom Spatial Statistic
from scipy.stats import pearsonr
def spatial_correlation(adata, gene1, gene2):
'''Compute spatial correlation between two genes'''
expr1 = adata[:, gene1].X.toarray().flatten()
expr2 = adata[:, gene2].X.toarray().flatten()
r, p = pearsonr(expr1, expr2)
return r, p
r, p = spatial_correlation(adata, 'GENE1', 'GENE2')
print(f'Spatial correlation: r={r:.3f}, p={p:.2e}')
Local Moran's I (LISA)
from esda.moran import Moran_Local
from libpysal.weights import KNN
# Build weights matrix
coords = adata.obsm['spatial']
w = KNN.from_array(coords, k=6)
w.transform = 'r'
# Compute local Moran's I for a gene
gene_expr = adata[:, 'GENE1'].X.toarray().flatten()
lisa = Moran_Local(gene_expr, w)
# Add to adata
adata.obs['GENE1_lisa'] = lisa.Is
adata.obs['GENE1_lisa_q'] = lisa.q # Quadrant (HH, HL, LH, LL)
Batch Spatial Statistics
# Compute Moran's I for top variable genes only
hvg = adata.var_names[adata.var['highly_variable']][:500]
sq.gr.spatial_autocorr(adata, mode='moran', genes=hvg)
results = adata.uns['moranI']
significant = results[results['pval_norm'] < 0.01]
print(f'{len(significant)} genes with significant spatial autocorrelation')
Related Skills
- spatial-neighbors - Build spatial graphs (prerequisite)
- spatial-domains - Identify spatial domains
- spatial-visualization - Visualize spatial statistics
Security
Reviews (0)
Related skills
Browse all →More from OpenClaw Medical
View source →More Build skills
Browse category →Frequently asked questions about bio-spatial-transcriptomics-spatial-statistics
What does the bio-spatial-transcriptomics-spatial-statistics skill do?
Compute spatial statistics for spatial transcriptomics data using Squidpy. Calculate Moran's I, Geary's C, spatial autocorrelation, co-occurrence analysis, and neighborhood enrichment. Use when computing spatial autocorrelation or co-occurrence statistics. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the bio-spatial-transcriptomics-spatial-statistics skill?
Run `npx @skills-hub-ai/cli install openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-statistics-bio-spatial-transcrip` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-statistics-bio-spatial-transcrip/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does bio-spatial-transcriptomics-spatial-statistics work with?
bio-spatial-transcriptomics-spatial-statistics runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the bio-spatial-transcriptomics-spatial-statistics skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use bio-spatial-transcriptomics-spatial-statistics after installing it?
In Claude Code, type `/openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-statistics-bio-spatial-transcrip` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the bio-spatial-transcriptomics-spatial-statistics skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.