bio-spatial-transcriptomics-spatial-neighbors
Build spatial neighbor graphs for spatial transcriptomics data using Squidpy. Compute k-nearest neighbors, Delaunay triangulation, and radius-based connectivity for downstream spatial analyses. Use when building spatial neighborhood graphs.
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-neighbors-bio-spatial-transcriptManual setup by platform
Claude Code
~/.claude/skills/openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-neighbors-bio-spatial-transcript/SKILL.md
Instructions
Spatial Neighbor Graphs
Build spatial neighbor graphs for connectivity-based analyses.
Required Imports
import squidpy as sq
import scanpy as sc
import numpy as np
Build K-Nearest Neighbors Graph
# Build spatial KNN graph
sq.gr.spatial_neighbors(adata, n_neighs=6, coord_type='generic')
# Check the graph
print(f"Connectivities shape: {adata.obsp['spatial_connectivities'].shape}")
print(f"Distances shape: {adata.obsp['spatial_distances'].shape}")
Build Delaunay Triangulation Graph
# Delaunay triangulation (natural neighbors)
sq.gr.spatial_neighbors(adata, delaunay=True, coord_type='generic')
Radius-Based Neighbors
# Connect all spots within a radius
sq.gr.spatial_neighbors(adata, radius=100, coord_type='generic')
For Visium Data (Grid Structure)
# For Visium hexagonal grid, use n_rings
sq.gr.spatial_neighbors(adata, n_rings=1, coord_type='grid') # 6 immediate neighbors
sq.gr.spatial_neighbors(adata, n_rings=2, coord_type='grid') # Extended neighborhood
Access Neighbor Information
# Get connectivities as sparse matrix
conn = adata.obsp['spatial_connectivities']
print(f'Edges in graph: {conn.nnz}')
print(f'Mean neighbors per spot: {conn.nnz / adata.n_obs:.1f}')
# Get distances
dist = adata.obsp['spatial_distances']
nonzero_dist = dist.data[dist.data > 0]
print(f'Mean neighbor distance: {nonzero_dist.mean():.1f}')
Get Neighbors for a Specific Spot
from scipy.sparse import csr_matrix
spot_idx = 0
conn = adata.obsp['spatial_connectivities']
# Get neighbor indices
neighbor_indices = conn[spot_idx].nonzero()[1]
print(f'Spot {spot_idx} has {len(neighbor_indices)} neighbors: {neighbor_indices}')
# Get distances to neighbors
dist = adata.obsp['spatial_distances']
neighbor_distances = dist[spot_idx, neighbor_indices].toarray().flatten()
print(f'Distances: {neighbor_distances}')
Build Expression-Based Neighbors
# Standard expression-based neighbors (for comparison)
sc.pp.neighbors(adata, n_neighbors=15, n_pcs=30)
# Now adata has both:
# - adata.obsp['spatial_connectivities'] (spatial)
# - adata.obsp['connectivities'] (expression)
Combine Spatial and Expression Neighbors
# Build both graphs
sq.gr.spatial_neighbors(adata, n_neighs=6, coord_type='generic')
sc.pp.neighbors(adata, n_neighbors=15, n_pcs=30)
# Weighted combination (manual)
alpha = 0.5 # Weight for spatial vs expression
spatial_conn = adata.obsp['spatial_connectivities']
expr_conn = adata.obsp['connectivities']
# Normalize and combine
from sklearn.preprocessing import normalize
spatial_norm = normalize(spatial_conn, norm='l1', axis=1)
expr_norm = normalize(expr_conn, norm='l1', axis=1)
combined = alpha * spatial_norm + (1 - alpha) * expr_norm
adata.obsp['combined_connectivities'] = combined
Visualize Neighbor Graph
import matplotlib.pyplot as plt
# Get coordinates
coords = adata.obsm['spatial']
conn = adata.obsp['spatial_connectivities']
fig, ax = plt.subplots(figsize=(10, 10))
# Draw edges
rows, cols = conn.nonzero()
for i, j in zip(rows, cols):
if i < j: # Avoid drawing twice
ax.plot([coords[i, 0], coords[j, 0]], [coords[i, 1], coords[j, 1]], 'k-', alpha=0.1, linewidth=0.5)
# Draw nodes
ax.scatter(coords[:, 0], coords[:, 1], s=10, c='blue', alpha=0.5)
ax.set_aspect('equal')
plt.title('Spatial neighbor graph')
Compute Graph Statistics
import networkx as nx
from scipy.sparse import csr_matrix
conn = adata.obsp['spatial_connectivities']
G = nx.from_scipy_sparse_array(conn)
print(f'Nodes: {G.number_of_nodes()}')
print(f'Edges: {G.number_of_edges()}')
print(f'Average degree: {2 * G.number_of_edges() / G.number_of_nodes():.2f}')
print(f'Connected components: {nx.number_connected_components(G)}')
Store Multiple Neighbor Graphs
# Store different neighborhood sizes
for n_neighs in [4, 6, 10]:
sq.gr.spatial_neighbors(adata, n_neighs=n_neighs, coord_type='generic')
adata.obsp[f'spatial_conn_{n_neighs}'] = adata.obsp['spatial_connectivities'].copy()
adata.obsp[f'spatial_dist_{n_neighs}'] = adata.obsp['spatial_distances'].copy()
Related Skills
- spatial-statistics - Use neighbor graph for spatial statistics
- spatial-domains - Identify domains using spatial graph
- single-cell/clustering - Non-spatial neighbor graphs
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-neighbors
What does the bio-spatial-transcriptomics-spatial-neighbors skill do?
Build spatial neighbor graphs for spatial transcriptomics data using Squidpy. Compute k-nearest neighbors, Delaunay triangulation, and radius-based connectivity for downstream spatial analyses. Use when building spatial neighborhood graphs. 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-neighbors skill?
Run `npx @skills-hub-ai/cli install openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-neighbors-bio-spatial-transcript` 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-neighbors-bio-spatial-transcript/ 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-neighbors work with?
bio-spatial-transcriptomics-spatial-neighbors 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-neighbors 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-neighbors after installing it?
In Claude Code, type `/openclaw-medical-spatial-transcriptomics-analysis-bioskills-spatial-neighbors-bio-spatial-transcript` (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-neighbors 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.