## ---- eval=FALSE-------------------------------------------------------------- # if (!requireNamespace("BiocManager", quietly = TRUE)) { # install.packages("BiocManager") # } # BiocManager::install("nnSVG") ## ---- message=FALSE----------------------------------------------------------- library(SpatialExperiment) library(STexampleData) library(scran) library(nnSVG) library(ggplot2) ## ----------------------------------------------------------------------------- # load example dataset from STexampleData package spe <- Visium_humanDLPFC() dim(spe) ## ----------------------------------------------------------------------------- # preprocessing steps # keep only spots over tissue spe <- spe[, colData(spe)$in_tissue == 1] dim(spe) # skip spot-level quality control, since this has been performed previously # on this dataset # filter low-expressed and mitochondrial genes # using default filtering parameters spe <- filter_genes(spe) # calculate log-transformed normalized counts using scran package # (alternatively could calculate deviance residuals using scry package) set.seed(123) qclus <- quickCluster(spe) spe <- computeSumFactors(spe, cluster = qclus) spe <- logNormCounts(spe) assayNames(spe) ## ----------------------------------------------------------------------------- # select small set of random genes and several known SVGs for # faster runtime in this example set.seed(123) ix_random <- sample(seq_len(nrow(spe)), 10) known_genes <- c("MOBP", "PCP4", "SNAP25", "HBB", "IGKC", "NPY") ix_known <- which(rowData(spe)$gene_name %in% known_genes) ix <- c(ix_known, ix_random) spe <- spe[ix, ] dim(spe) ## ----------------------------------------------------------------------------- # run nnSVG # set seed for reproducibility set.seed(123) # using a single thread in this example spe <- nnSVG(spe) # show results rowData(spe) ## ----------------------------------------------------------------------------- # number of significant SVGs table(rowData(spe)$padj <= 0.05) ## ----------------------------------------------------------------------------- # show results for top n SVGs rowData(spe)[order(rowData(spe)$rank)[1:10], ] ## ---- fig.width=5, fig.height=5----------------------------------------------- # plot spatial expression of top-ranked SVG ix <- which(rowData(spe)$rank == 1) ix_name <- rowData(spe)$gene_name[ix] ix_name df <- as.data.frame( cbind(spatialCoords(spe), expr = counts(spe)[ix, ])) ggplot(df, aes(x = pxl_col_in_fullres, y = pxl_row_in_fullres, color = expr)) + geom_point(size = 0.8) + coord_fixed() + scale_y_reverse() + scale_color_gradient(low = "gray90", high = "blue", name = "counts") + ggtitle(ix_name) + theme_bw() + theme(panel.grid = element_blank(), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank()) ## ----------------------------------------------------------------------------- sessionInfo()