Spatially Variable Genes - Answer Key

Author

Noor Sohail

Published

April 10, 2026

Exercise 1

  1. Pick a different cell type and run FindSpatiallyVariableFeatures(). Name the population you chose and print the top 6 genes in the results.
# Vector of unique cell types
cell_types <- seurat_rctd$first_type %>%
  unique() %>% sort()

for (ct in cell_types) {
  # Subset to cell type P5CRC bins
  crc_sub <- subset(seurat_rctd,
                subset = (orig.ident == "P5CRC") &
                        (first_type == ct))
  # Re-calculate HVGs on subset data
  crc_sub <- FindVariableFeatures(crc_sub)
  # Scale log-normalized counts
  crc_sub <- ScaleData(crc_sub)

  # Run Moran's I on the top 50 highly variable genes
  crc_sub <- FindSpatiallyVariableFeatures(crc_sub,
                                          selection.method = "moransi",
                                          verbose = TRUE,
                                          features = VariableFeatures(crc_sub)[1:50],
                                          image = "P5CRC.008um")


  # Get top spatially variable features
  top_svg <- SpatiallyVariableFeatures(crc_sub, 
                                        method = "moransi")
  top_svg <- top_svg[1:6]


  p <- SpatialFeaturePlot(crc_sub,
                   features = top_svg,
                   pt.size.factor = 15,
                   image.alpha = 0.1, 
                   ncol = 3)

  print(ct)
  print(top_svg)
  print(p)
}
[1] "B cells"
[1] "MS4A1"  "IGHG1"  "CXCL13" "IGHM"   "IGLC1"  "C3"    

[1] "Endothelial"
[1] "JCHAIN"   "IGKC"     "IGHA1"    "SERPINE1" "HBA2"     "IGHG1"   

[1] "Fibroblast"
[1] "MMP3"   "AGT"    "IGHA1"  "COL1A1" "F3"     "COL3A1"

[1] "Intestinal Epithelial"
[1] "SLC26A3" "OLFM4"   "FABP1"   "GUCA2A"  "CEACAM7" "KRT20"  

[1] "Myeloid"
[1] "CXCL8"  "G0S2"   "IL1B"   "S100A9" "MMP12"  "IL1RN" 

[1] "Neuronal"
[1] "VIP"   "CALB2" "SCGN"  "UCHL1" "SCG2"  "IGHA1"

[1] "Smooth Muscle"
[1] "IGHM"    "IGHA1"   "IGKC"    "JCHAIN"  "SOSTDC1" "IGHG1"  

[1] "T cells"
[1] "JCHAIN" "IGHA1"  "GZMB"   "S100A9" "IGLC1"  "IGKC"  

[1] "Tumor"
[1] "PIGR"  "REG3A" "OLFM4" "PI3"   "MUC17" "PPBP" 

[1] "unassigned"
[1] "CXCL8" "HBA2"  "HBB"   "DES"   "IL1B"  "PTGS2"


Back to Lesson >>

Back to Schedule

Reuse

CC-BY-4.0