Before using code patterns, verify installed versions match. If versions differ:
Python: pip show <package> then help(module.function) to check signatures
R: packageVersion('<pkg>') then ?function_name to verify parameters
CLI: <tool> --version then <tool> --help to confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Trajectory Inference
Monocle3 (R)
Goal: Infer developmental trajectories and pseudotime ordering using Monocle3's principal graph approach.
Approach: Learn a principal graph through the data manifold, order cells along the graph from a root state, and extract pseudotime values.
"Find the developmental trajectory in my data" → Construct a tree-like graph through the cell embedding, assign pseudotime from a root population, and identify branch points.
Goal: Infer smooth lineage trajectories and pseudotime using Slingshot's minimum spanning tree and principal curves.
Approach: Build a minimum spanning tree through cluster centroids to define lineage structure, then fit smooth principal curves for per-lineage pseudotime.
library(slingshot)
library(SingleCellExperiment)
# From Seurat object
sce <- as.SingleCellExperiment(seurat_obj)
reducedDims(sce)$UMAP <- Embeddings(seurat_obj, 'umap')
# Run slingshot
sce <- slingshot(sce, clusterLabels = 'seurat_clusters', reducedDim = 'UMAP')
# Get pseudotime for each lineage
pseudotime_mat <- slingPseudotime(sce)
# Get lineage curves
curves <- slingCurves(sce)
# Plot trajectories
plot(reducedDims(sce)$UMAP, col = sce$seurat_clusters, pch = 16)
lines(SlingshotDataSet(sce), lwd = 2)