## ----style, echo = FALSE, results = 'asis'------------------------------------
BiocStyle::markdown()

## ----'install_dev', eval = FALSE----------------------------------------------
#  if (!require("devtools")) install.packages("devtools")
#  remotes::install_github("MicTott/SpotSweeper")

## ----'install', eval = FALSE--------------------------------------------------
#  if (!requireNamespace("BiocManager", quietly = TRUE)) {
#      install.packages("BiocManager")
#  }
#  
#  BiocManager::install("SpotSweeper")

## ----example_spe--------------------------------------------------------------
library(SpotSweeper)

# load  Maynard et al DLPFC daatset
spe <- STexampleData::Visium_humanDLPFC()

# show column data before SpotSweeper
colnames(colData(spe))

# drop out-of-tissue spots
spe <- spe[, spe$in_tissue == 1]

## ----example_scuttle----------------------------------------------------------
# change from gene id to gene names
rownames(spe) <- rowData(spe)$gene_name

# identifying the mitochondrial transcripts
is.mito <- rownames(spe)[grepl("^MT-", rownames(spe))]

# calculating QC metrics for each spot using scuttle
spe <- scuttle::addPerCellQCMetrics(spe, subsets = list(Mito = is.mito))
colnames(colData(spe))

## ----example_local_outliers---------------------------------------------------
# library size
spe <- localOutliers(spe,
    metric = "sum",
    direction = "lower",
    log = TRUE
)

# unique genes
spe <- localOutliers(spe,
    metric = "detected",
    direction = "lower",
    log = TRUE
)

# mitochondrial percent
spe <- localOutliers(spe,
    metric = "subsets_Mito_percent",
    direction = "higher",
    log = FALSE
)

## ----example_combine_local_outliers-------------------------------------------
# combine all outliers into "local_outliers" column
spe$local_outliers <- as.logical(spe$sum_outliers) |
    as.logical(spe$detected_outliers) |
    as.logical(spe$subsets_Mito_percent_outliers)

## ----local_outlier_plot, fig.width=3, fig.height=3, dpi=100-------------------
library(escheR)


# all local outliers
plotQC(spe, metric = "sum_log", outliers = "local_outliers", point_size = 1.1, 
       stroke = 0.75) +
      ggtitle("All Local Outliers")


## ----example_artifactRemoval--------------------------------------------------
# load in DLPFC sample with hangnail artifact
data(DLPFC_artifact)
spe <- DLPFC_artifact

# inspect colData before artifact detection
colnames(colData(spe))

## ----artifact_QC_plots, fig.width=4, fig.height=4, dpi=100--------------------

plotQC(spe,
    metric = "expr_chrM_ratio",
    outliers = NULL, point_size = 1.1
) +
    ggtitle("Mitochondrial Percent")


## ----artifact_plot------------------------------------------------------------
# find artifacts using SpotSweeper
spe <- findArtifacts(spe,
    mito_percent = "expr_chrM_ratio",
    mito_sum = "expr_chrM",
    n_rings = 5,
    name = "artifact"
)

# check that "artifact" is now in colData
colnames(colData(spe))

## ----artifact_visualization, fig.width=4, fig.height=4, dpi=100---------------
plotQC(spe,
    metric = "expr_chrM_ratio",
    outliers = "artifact", point_size = 1.1
) +
    ggtitle("Hangnail artifact")

## -----------------------------------------------------------------------------
utils::sessionInfo()