--- title: "Getting started with STBench" output: rmarkdown::html_vignette: toc: true toc_depth: 3 vignette: > %\VignetteIndexEntry{Getting started with STBench} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include=FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 8, fig.height = 5, message = FALSE, warning = FALSE ) library(STBench) ``` # Introduction STBench is an R package for comparing normalisation methods in spatial transcriptomics. # Load data ```{r load-data} data(spe_example) data(norm_example) dim(spe_example) names(norm_example) ``` # Quality control ## Calculate and visualise QC metrics We first calculate spot-level metrics, including library size, detected genes and mitochondrial fraction, together with gene-level expression summaries. We inspect their distributions before filtering. ```{r qc-initial} spe <- add_qcs( spe_example, assay_name = "counts", mito_pattern = "^mt-|^MT-" ) p_qc <- plot_qc( spe, bins = 45, show_density = TRUE, theme_name = "classic" ) p_qc$plot ``` ## Explore filtering thresholds We examine the relationship between possible cutoffs and the proportions of spots and genes removed, without performing any filtering. We use illustrative rather than dataset-specific thresholds. ```{r qc-cutoffs} p_cutoffs <- plot_qc_cutoffs( spe, library_cutoff = 6.5, gene_cutoff = 0.2, n_points = 100, theme_name = "classic" ) p_cutoffs$plot ``` ## Apply filtering and reassess QC We remove spots and genes below the selected thresholds. Choose cutoffs appropriate to the dataset. ```{r qc-filter} spe_filtered <- filter_qc( spe, library_cutoff = 2.5, gene_cutoff = 0.2, recalculate = TRUE, assay_name = "counts", mito_pattern = "^mt-|^MT-" ) plot_qc(spe_filtered, theme_name = "classic")$plot ``` # Apply normalisation methods The `None` method provides a log1p-transformed baseline. Method-specific arguments are supplied through `params`. We execute normalisation on the small dataset; for subsequent comparisons, we use the saved `norm_example` for a consistent reference. ```{r normalisation} norm_recomputed <- normalize_spe( spe_example, methods = c("None", "SpaNorm"), params = list( spanorm = list( sample_p = 0.15, df_tps = 6, adj_method = "logpac", seed = 123 ) ) ) ``` ```{r normalisation-precomputed} norm <- norm_example lapply(norm, dim) ``` # Compare normalised library sizes We compare summed normalised expression across spots. The comparison uses the normalisation methods in the example dataset. ```{r library-size} p_ls <- plot_library_size_comparison( norm, methods = names(norm), assay_name = "logcounts", point_size = 0.5, theme_name = "classic" ) p_ls$plot ``` # UMAP visualisation We run PCA and UMAP independently for each normalisation method and colour by the available cell-type annotation. ```{r umap-calculate} umap_data <- get_umap_data( norm, methods = names(norm), prop_spots = 1, seed = 123, assay_name = "logcounts", n_pcs = 10, n_neighbors = 15 ) ``` ```{r umap-cell-types} p_umap <- plot_umap_comparison( umap_data, colour_by = "cell_type", point_size = 1, alpha = 0.6, ncol = 2, theme_name = "classic" ) p_umap$plot ``` # Session information ```{r session-info} sessionInfo() ```