add_qc_metrics: Quality Control for Spatial Data

View source: R/add_qc_metrics.R

add_qc_metricsR Documentation

Quality Control for Spatial Data

Description

This function identify spots in a SpatialExperiment-class (SPE) with outlier quality control values: low sum_umi or sum_gene, or high expr_chrM_ratio, utilizing scuttle::isOutlier. Also identifies in-tissue edge spots and distance to the edge for each spot.

Usage

add_qc_metrics(spe, overwrite = FALSE)

Arguments

spe

a SpatialExperiment object that has sum_umi, sum_gene, expr_chrM_ratio, and in_tissue variables in the colData(spe). Note that these are automatically created when you build your spe object with spatialLIBD::read10xVisiumWrapper().

overwrite

a logical(1) specifying whether to overwrite the 7 colData(spe) columns that this function creates. If set to FALSE and any of them are present, the function will return an error.

Details

The initial version of this function lives at https://github.com/LieberInstitute/Visium_SPG_AD/blob/master/code/07_spot_qc/01_qc_metrics_and_segmentation.R.

Value

A SpatialExperiment with added quality control information added to the colData().

scran_low_lib_size

shows spots that have a low library size.

scran_low_n_features

spots with a low number of expressed genes.

scran_high_Mito_percent

spots with a high percent of mitochondrial gene expression.

scran_discard

spots belonging to either scran_low_lib_size, scran_low_n_feature, or scran_high_Mito_percent.

edge_spot

spots that are automatically detected as the edge spots of the in_tissue section.

edge_distance

closest distance in number of spots to either the vertical or horizontal edge.

scran_low_lib_size_edge

spots that have a low library size and are an edge spot.

Author(s)

Louise A. Huuki-Myers

Examples

## Obtain the necessary data
spe_pre_qc <- fetch_data("spatialDLPFC_Visium_example_subset")

## For now, we fake out tissue spots in example data
spe_qc <- spe_pre_qc
spe_qc$in_tissue[spe_qc$array_col < 10] <- FALSE

## adds QC metrics to colData of the spe
spe_qc <- add_qc_metrics(spe_qc, overwrite = TRUE)
vars <- colnames(colData(spe_qc))
vars[grep("^(scran|edge)", vars)]

## visualize edge spots
vis_clus(spe_qc, sampleid = "Br6432_ant", clustervar = "edge_spot")

## specify your own colors
vis_clus(
    spe_qc,
    sampleid = "Br6432_ant",
    clustervar = "edge_spot",
    colors = c(
        "TRUE" = "lightgreen",
        "FALSE" = "pink",
        "NA" = "red"
    )
)
vis_gene(spe_qc, sampleid = "Br6432_ant", geneid = "edge_distance", minCount = -1)

## Visualize scran QC flags

## Check the spots with low library size as detected by scran::isOutlier()
vis_clus(spe_qc, sample_id = "Br6432_ant", clustervar = "scran_low_lib_size")

## Violin plot of library size with low library size highlighted in a
## different color.
scater::plotColData(spe_qc[, spe_qc$in_tissue], x = "sample_id", y = "sum_umi", colour_by = "scran_low_lib_size")

## Check any spots that scran::isOutlier() flagged
vis_clus(spe_qc, sampleid = "Br6432_ant", clustervar = "scran_discard")

## Low library spots that are on the edge of the tissue
vis_clus(spe_qc, sampleid = "Br6432_ant", clustervar = "scran_low_lib_size_edge")

## Use `low_library_size` (or other variables) and `edge_distance` as you
## please.
spe_qc$our_low_lib_edge <- spe_qc$scran_low_lib_size & spe_qc$edge_distance < 5

vis_clus(spe_qc, sample_id = "Br6432_ant", clustervar = "our_low_lib_edge")

## Clean up
rm(spe_qc, spe_pre_qc, vars)


LieberInstitute/spatialLIBD documentation built on July 17, 2024, 5:05 a.m.