### Install decontX from Bioconductor or GitHub Source: https://context7.com/campbio/decontx/llms.txt Installs the stable version from Bioconductor or the development version from GitHub. Loads the library for use. ```r # Bioconductor (stable) if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("decontX") # GitHub (development) # install.packages("devtools") devtools::install_github("campbio/decontX") library(decontX) ``` -------------------------------- ### Install decontX Development Version from GitHub Source: https://github.com/campbio/decontx/blob/main/README.md Install the latest development version of the decontX package from GitHub using the devtools package. This requires devtools to be installed. ```r # install.packages("devtools") devtools::install_github("campbio/decontX") ``` -------------------------------- ### Install decontX from Bioconductor Source: https://github.com/campbio/decontx/blob/main/README.md Use this R code to install the decontX package from Bioconductor. Ensure BiocManager is installed first. ```r if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("decontX") ``` -------------------------------- ### Install decontX with Vignettes from GitHub Source: https://github.com/campbio/decontx/blob/main/README.md Install the decontX package from GitHub with vignettes included. This requires the devtools package and setting the build_vignettes argument to TRUE. ```r library(devtools) install_github("campbio/decontX", build_vignettes = TRUE) ``` -------------------------------- ### Run decontPro with SingleCellExperiment input Source: https://context7.com/campbio/decontx/llms.txt Demonstrates using decontPro with a SingleCellExperiment object as input. Ensure the SingleCellExperiment object contains a 'counts' assay. ```R library(SingleCellExperiment) sce_adt <- SingleCellExperiment(list(counts = counts)) out_sce <- decontPro(sce_adt, cell_type = cell_type) ``` -------------------------------- ### Run decontPro with ambient counts matrix Source: https://context7.com/campbio/decontx/llms.txt Applies decontPro to a counts matrix, optionally providing an ambient counts matrix for more accurate decontamination. Adjust delta_sd and background_sd for smaller datasets. ```R ambient_counts <- matrix(sample(1:5, 1000, replace = TRUE), ncol = 10) rownames(ambient_counts) <- rownames(counts) out2 <- decontPro( counts, cell_type = cell_type, delta_sd = 2e-4, # slightly larger priors for smaller datasets background_sd = 2e-5, ambient_counts = ambient_counts ) ``` -------------------------------- ### Simulate Contaminated Count Matrix with simulateContamination Source: https://context7.com/campbio/decontx/llms.txt Generates a synthetic scRNA-seq dataset with known contamination levels for benchmarking. Returns observed and native counts, marker genes, and contamination fractions. ```r # Simulate 300 cells, 100 genes, 3 cell populations # delta = c(1, 10) → ~90 % native, ~10 % contamination on average sim <- simulateContamination( C = 300, G = 100, K = 3, NRange = c(500, 1000), beta = 0.1, delta = c(1, 10), numMarkers = 3, seed = 12345 ) # Structure of the returned list names(sim) # [1] "nativeCounts" "observedCounts" "NByC" # "z" "eta" "phi" # "markers" "numMarkers" "contamination" # True contamination per cell (ground truth) summary(sim$contamination) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 0.0000 0.0515 0.0978 0.1038 0.1478 0.3607 # Marker genes per cell type sim$markers # $CellType_1_Markers [1] "Gene_23" "Gene_67" "Gene_11" # $CellType_2_Markers [1] "Gene_5" "Gene_42" "Gene_88" # $CellType_3_Markers [1] "Gene_77" "Gene_14" "Gene_61" ``` -------------------------------- ### Access decontX Vignettes Source: https://github.com/campbio/decontx/blob/main/README.md Load the decontX package and access its vignettes for DecontX and DecontPro using the vignette function. ```r vignette('decontX', package = 'decontX') vignette('decontPro', package = 'decontX') ``` -------------------------------- ### Estimate and Remove Contamination in Protein Data with decontPro Source: https://context7.com/campbio/decontx/llms.txt Use `decontPro` to estimate and remove contamination from CITE-seq ADT count matrices. It employs variational Bayesian inference to decompose protein counts into native expression, ambient contamination, and background. Prior variances for contamination and background can be specified. ```r # --- Minimal example with a simulated count matrix --- set.seed(42) counts <- matrix(sample(1:100, 1000, replace = TRUE), ncol = 10) rownames(counts) <- paste0("ADT_", seq_len(nrow(counts))) # Cell-type indicator (1-based integer vector, same length as ncol(counts)) cell_type <- c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4) out <- decontPro( counts, cell_type = cell_type, delta_sd = 2e-5, # prior variance for ambient contamination background_sd = 2e-6 # prior variance for non-specific background ) ``` -------------------------------- ### Estimate and Remove Ambient RNA Contamination with decontX Source: https://context7.com/campbio/decontx/llms.txt Applies the DecontX EM algorithm to estimate and remove ambient RNA contamination from scRNA-seq data. Accepts SingleCellExperiment or matrix inputs. Results are stored in the object's colData and assays. ```r library(SingleCellExperiment) # --- Using a simulated SCE object --- sim <- simulateContamination(seed = 12345) # Basic run: DecontX auto-clusters cells with UMAP + DBSCAN sce <- decontX(SingleCellExperiment(list(counts = sim$observedCounts))) # Contamination estimates stored in colData head(colData(sce)$decontX_contamination) # [1] 0.1142 0.0823 0.0571 0.2201 0.0389 0.1673 # Decontaminated counts stored as a new assay decon_mat <- decontXcounts(sce) # sparse dgCMatrix round_mat <- round(decontXcounts(sce)) # integer matrix for downstream use # --- Supplying known cluster labels (faster, more accurate) --- sce2 <- decontX(SingleCellExperiment(list(counts = sim$observedCounts)), z = sim$z) # --- Multi-sample / multi-batch experiment --- batch <- rep(c("sample1", "sample2"), each = ncol(sce) / 2) sce3 <- decontX(SingleCellExperiment(list(counts = sim$observedCounts)), batch = batch) # --- Providing raw/empty-droplet matrix for empirical background --- # sce.raw contains empty droplets (e.g., from CellRanger raw output) # sce4 <- decontX(sce, background = sce.raw) # --- Adjusting prior aggressiveness (fix delta, force higher contamination) --- sce5 <- decontX(SingleCellExperiment(list(counts = sim$observedCounts)), delta = c(9, 20), estimateDelta = FALSE) # --- Working with a plain sparse matrix (returns a list, not SCE) --- result <- decontX(sim$observedCounts) str(result, max.level = 1) # List of 5 # $ runParams : list # $ estimates : list # $ decontXcounts: dgCMatrix # $ contamination: Named num [1:300] # $ z : chr [1:300] ``` -------------------------------- ### simulateContamination Source: https://context7.com/campbio/decontx/llms.txt Generates a synthetic scRNA-seq dataset with a known ground-truth contamination level. This function is useful for benchmarking and testing the decontX function. ```APIDOC ## simulateContamination — Simulate a contaminated count matrix ### Description Generates a synthetic scRNA-seq dataset with a known ground-truth contamination level. Returns both the "observed" (contaminated) matrix and the "native" (clean) matrix, along with marker gene lists and the true contamination fraction per cell. Useful for benchmarking and testing decontx. ### Parameters - **C** (integer) - Number of cells. - **G** (integer) - Number of genes. - **K** (integer) - Number of cell populations. - **NRange** (numeric vector) - Range for the total UMI counts per cell. - **beta** (numeric) - Parameter controlling the proportion of contamination. - **delta** (numeric vector) - Parameters for the Dirichlet distribution, influencing contamination levels. - **numMarkers** (integer) - Number of marker genes per cell type. - **seed** (integer) - Random seed for reproducibility. ### Returns A list containing: - **nativeCounts**: The true native count matrix. - **observedCounts**: The simulated observed (contaminated) count matrix. - **NByC**: Total UMI counts per cell. - **z**: Cell type labels. - **eta**: Contamination parameters. - **phi**: Gene-specific contamination parameters. - **markers**: List of marker genes per cell type. - **numMarkers**: Number of marker genes. - **contamination**: True contamination fraction per cell. ### Example ```r # Simulate 300 cells, 100 genes, 3 cell populations sim <- simulateContamination( C = 300, G = 100, K = 3, NRange = c(500, 1000), beta = 0.1, delta = c(1, 10), numMarkers = 3, seed = 12345 ) # Structure of the returned list names(sim) # [1] "nativeCounts" "observedCounts" "NByC" # "z" "eta" "phi" # "markers" "numMarkers" "contamination" # True contamination per cell (ground truth) summary(sim$contamination) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 0.0000 0.0515 0.0978 0.1038 0.1478 0.3607 # Marker genes per cell type sim$markers # $CellType_1_Markers [1] "Gene_23" "Gene_67" "Gene_11" # $CellType_2_Markers [1] "Gene_5" "Gene_42" "Gene_88" # $CellType_3_Markers [1] "Gene_77" "Gene_14" "Gene_61" ``` ``` -------------------------------- ### decontPro Source: https://context7.com/campbio/decontx/llms.txt Estimates and removes contamination in protein expression data (CITE-seq ADT counts) using variational Bayesian inference. It decomposes protein counts into native expression, ambient contamination, and background. ```APIDOC ## `decontPro` — Estimate and remove contamination in protein expression data (CITE-seq) Implements the DecontPro algorithm for CITE-seq ADT (antibody-derived tag) count matrices. Uses variational Bayesian inference via Stan to decompose each cell's protein counts into three components: native expression, ambient contamination, and non-specific background. Accepts `SingleCellExperiment`, `Seurat`, or plain matrices. Returns the decontaminated count matrix along with model parameters. ```r # --- Minimal example with a simulated count matrix --- set.seed(42) counts <- matrix(sample(1:100, 1000, replace = TRUE), ncol = 10) rownames(counts) <- paste0("ADT_", seq_len(nrow(counts))) # Cell-type indicator (1-based integer vector, same length as ncol(counts)) cell_type <- c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4) out <- decontPro(counts, cell_type = cell_type, delta_sd = 2e-5, # prior variance for ambient contamination background_sd = 2e-6 # prior variance for non-specific background ) ``` ``` -------------------------------- ### Retrieve and Set Decontaminated Counts with decontXcounts Source: https://context7.com/campbio/decontx/llms.txt Use `decontXcounts` to access the decontaminated counts assay from a SingleCellExperiment object. The counts can be rounded to integers and then set back into the assay. ```r library(SingleCellExperiment) sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Retrieve decontaminated counts dcounts <- decontXcounts(sce) dim(dcounts) # 100 genes × 300 cells class(dcounts) # "dgCMatrix" # Round to integers for Seurat or other tools that require integer counts integer_dcounts <- round(dcounts) # Set the assay manually (e.g. after rounding) decontXcounts(sce) <- integer_dcounts # Both "counts" and "decontXcounts" are now available assays assayNames(sce) # [1] "counts" "decontXcounts" ``` -------------------------------- ### Plot ADT density before and after decontamination Source: https://context7.com/campbio/decontx/llms.txt Generates density plots comparing original and decontaminated ADT counts for specified features. Useful for visualizing the removal of background peaks. The plot uses a pseudo-log x-axis. ```R counts <- matrix(c(60, 72, 52, 49, 89, 112), nrow = 2, dimnames = list( c("CD3", "CD4"), c("CTGTTTACACCGCTAG", "CTCTACGGTGTGGCTC", "AGCAGCCAGGCTCATT") ) ) decon_counts <- matrix(c(58, 36, 26, 45, 88, 110), nrow = 2, dimnames = list(rownames(counts), colnames(counts)) ) # Returns a patchwork ggplot object p <- plotDensity(counts, decon_counts, features = c("CD3", "CD4")) print(p) ``` ```R # Save directly to a PDF plotDensity(counts, decon_counts, features = c("CD3", "CD4"), file = "adt_density_qc" # writes "adt_density_qc.pdf" ) ``` -------------------------------- ### Plot ADT counts by cell cluster before and after decontamination Source: https://context7.com/campbio/decontx/llms.txt Creates boxplots comparing original and decontaminated ADT counts grouped by cell cluster. This helps confirm selective removal of contamination, particularly low counts in non-target clusters. Uses a pseudo-log y-axis. ```R counts <- matrix(c(60, 72, 52, 49, 89, 112), nrow = 2, dimnames = list( c("CD3", "CD4"), c("CTGTTTACACCGCTAG", "CTCTACGGTGTGGCTC", "AGCAGCCAGGCTCATT") ) ) decon_counts <- matrix(c(58, 36, 26, 45, 88, 110), nrow = 2, dimnames = list(rownames(counts), colnames(counts)) ) cell_type <- c(1, 2, 1) # Returns a patchwork ggplot object p <- plotBoxByCluster(counts, decon_counts, cell_type, features = c("CD3", "CD4")) print(p) ``` ```R # Save to PDF plotBoxByCluster(counts, decon_counts, cell_type, features = c("CD3", "CD4"), file = "adt_boxplot_qc" # writes "adt_boxplot_qc.pdf" ) ``` -------------------------------- ### Compare Marker Gene Detection Rates Before/After Decontamination Source: https://context7.com/campbio/decontx/llms.txt Generate a faceted barplot comparing the percentage of cells expressing marker genes above a threshold, using both original and decontaminated counts. This quantifies the removal of aberrant marker expression. ```r sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Compare contaminated vs. decontaminated marker detection plotDecontXMarkerPercentage(sce, markers = sim$markers, # named list of gene vectors assayName = c("counts", "decontXcounts") # side-by-side bars ) # With real PBMC data and manual cell-type groupings markers <- list( Tcell_Markers = c("CD3E", "CD3D"), Bcell_Markers = c("CD79A", "CD79B", "MS4A1"), Monocyte_Markers = c("S100A8", "S100A9", "LYZ"), NKcell_Markers = "GNLY" ) cellTypeMappings <- list(Tcells = 2, Bcells = 5, Monocytes = 1, NKcells = 6) plotDecontXMarkerPercentage(sce, markers = markers, groupClusters = cellTypeMappings, assayName = "decontXcounts", threshold = 1, # min count to be "expressed" labelBars = TRUE ) ``` -------------------------------- ### plotDecontXContamination Source: https://context7.com/campbio/decontx/llms.txt Generates a UMAP scatter plot where cells are colored by their estimated contamination level. This visualization is useful for identifying cell populations with high ambient RNA. ```APIDOC ## `plotDecontXContamination` — UMAP coloured by contamination level Scatter plot of the DecontX UMAP embedding with cells coloured by their estimated contamination fraction (0–1 scale). Useful for identifying which cell populations carry the highest levels of ambient RNA. ```r sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Default blue → red colour scale plotDecontXContamination(sce) # Custom colour scale and larger points plotDecontXContamination(sce, colorScale = c("purple", "white", "darkred"), size = 2 ) # Multi-batch: select a specific batch to plot # plotDecontXContamination(sce_multi, batch = "sample1") ``` ``` -------------------------------- ### Violin Plot of Marker Expression Before/After Decontamination Source: https://context7.com/campbio/decontx/llms.txt Visualize marker gene expression using side-by-side violin plots for raw counts and decontaminated counts. This allows for a direct comparison of expression levels across cell clusters, with options for log transformation and dot overlays. ```r sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Compare all marker genes before and after decontamination plotDecontXMarkerExpression(sce, markers = unlist(sim$markers), assayName = c("counts", "decontXcounts"), log1p = TRUE, plotDots = TRUE, dotSize = 0.2 ) # Focus on one set of markers with explicit cell-type groupings (real data) markers <- list(Monocyte_Markers = c("S100A8", "S100A9", "LYZ")) cellTypeMappings <- list(Tcells = 2, Bcells = 5, Monocytes = 1, NKcells = 6) plotDecontXMarkerExpression(sce, markers = markers(("Monocyte_Markers")), groupClusters = cellTypeMappings, ncol = 3 ) ``` -------------------------------- ### Visualize DecontX Contamination Levels with UMAP Source: https://context7.com/campbio/decontx/llms.txt Plot cells on a UMAP embedding colored by their estimated contamination fraction. This helps identify cell populations with high ambient RNA. Custom color scales and point sizes can be applied. ```r sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Default blue → red colour scale plotDecontXContamination(sce) # Custom colour scale and larger points plotDecontXContamination(sce, colorScale = c("purple", "white", "darkred"), size = 2 ) # Multi-batch: select a specific batch to plot # plotDecontXContamination(sce_multi, batch = "sample1") ``` -------------------------------- ### plotDecontXMarkerPercentage Source: https://context7.com/campbio/decontx/llms.txt Creates a faceted barplot showing the percentage of cells expressing marker genes above a threshold, per cell cluster. It allows comparison between original and decontaminated counts. ```APIDOC ## `plotDecontXMarkerPercentage` — Barplot of marker gene detection rates Generates a faceted barplot showing, for each cell cluster (or cell type), the percentage of cells that express at least one marker gene at or above a count threshold. Can compare original `counts` and `decontXcounts` side-by-side to quantify how much aberrant marker expression was removed. ```r sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Compare contaminated vs. decontaminated marker detection plotDecontXMarkerPercentage(sce, markers = sim$markers, # named list of gene vectors assayName = c("counts", "decontXcounts") # side-by-side bars ) # With real PBMC data and manual cell-type groupings markers <- list( Tcell_Markers = c("CD3E", "CD3D"), Bcell_Markers = c("CD79A", "CD79B", "MS4A1"), Monocyte_Markers = c("S100A8", "S100A9", "LYZ"), NKcell_Markers = "GNLY" ) cellTypeMappings <- list(Tcells = 2, Bcells = 5, Monocytes = 1, NKcells = 6) plotDecontXMarkerPercentage(sce, markers = markers, groupClusters = cellTypeMappings, assayName = "decontXcounts", threshold = 1, # min count to be "expressed" labelBars = TRUE ) ``` ``` -------------------------------- ### plotDecontXMarkerExpression Source: https://context7.com/campbio/decontx/llms.txt Displays side-by-side violin plots of marker gene expression counts across cell clusters, comparing raw counts and decontaminated counts. Supports log transformation and dot overlays. ```APIDOC ## `plotDecontXMarkerExpression` — Violin plot of marker gene expression before/after decontamination Renders side-by-side violin plots of raw gene expression counts across cell clusters for specified marker genes, enabling visual comparison of the `counts` and `decontXcounts` assays. Supports log1p transformation and optional dot overlay. ```r sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Compare all marker genes before and after decontamination plotDecontXMarkerExpression(sce, markers = unlist(sim$markers), assayName = c("counts", "decontXcounts"), log1p = TRUE, plotDots = TRUE, dotSize = 0.2 ) # Focus on one set of markers with explicit cell-type groupings (real data) markers <- list(Monocyte_Markers = c("S100A8", "S100A9", "LYZ")) cellTypeMappings <- list(Tcells = 2, Bcells = 5, Monocytes = 1, NKcells = 6) plotDecontXMarkerExpression(sce, markers = markers(("Monocyte_Markers")), groupClusters = cellTypeMappings, ncol = 3 ) ``` ``` -------------------------------- ### decontXcounts Source: https://context7.com/campbio/decontx/llms.txt Accessor and replacement for the `decontXcounts` assay in a `SingleCellExperiment` object after running `decontX`. It allows retrieval and manual setting of decontaminated counts. ```APIDOC ## `decontXcounts` — Get or set the decontaminated counts assay Accessor / replacement generic for the `decontXcounts` assay stored inside a `SingleCellExperiment` after running `decontX`. ```r library(SingleCellExperiment) sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) sce <- decontX(sce) # Retrieve decontaminated counts dcounts <- decontXcounts(sce) dim(dcounts) # 100 genes × 300 cells class(dcounts) # "dgCMatrix" # Round to integers for Seurat or other tools that require integer counts integer_dcounts <- round(dcounts) # Set the assay manually (e.g. after rounding) decontXcounts(sce) <- integer_dcounts # Both "counts" and "decontXcounts" are now available assays assayNames(sce) # [1] "counts" "decontXcounts" ``` ``` -------------------------------- ### decontX Source: https://context7.com/campbio/decontx/llms.txt The core DecontX function for estimating and removing ambient RNA contamination from scRNA-seq data. It runs an EM algorithm to estimate the per-cell contamination fraction and returns a decontaminated counts matrix. ```APIDOC ## decontX — Estimate and remove ambient RNA contamination (scRNA-seq) ### Description The core DecontX function. Accepts a `SingleCellExperiment` object or any matrix-like count object. Runs an EM algorithm to estimate the per-cell contamination fraction (`theta`) and returns a decontaminated counts matrix. Optionally accepts a `background` matrix of empty droplets for empirical ambient RNA estimation, and a `batch` vector to process multi-sample experiments independently. ### Parameters - **object**: A `SingleCellExperiment` object or a count matrix. - **z** (vector, optional): Known cell cluster labels. If not provided, auto-clustering is performed using UMAP + DBSCAN. - **background** (matrix, optional): A matrix of empty droplets for empirical background estimation. - **batch** (vector, optional): A vector indicating batch or sample assignments for multi-sample experiments. - **delta** (numeric vector, optional): Prior parameters for the Dirichlet distribution. Used to adjust contamination estimation aggressiveness. - **estimateDelta** (logical, optional): Whether to estimate delta parameters. Defaults to TRUE. ### Returns If the input is a `SingleCellExperiment` object, it returns the object with decontaminated counts added as a new assay (named `decontXcounts`) and contamination estimates in `colData` (named `decontX_contamination`). If the input is a matrix, it returns a list containing: - **runParams**: Parameters used for the run. - **estimates**: Estimated parameters from the EM algorithm. - **decontXcounts**: The decontaminated count matrix. - **contamination**: Per-cell contamination estimates. - **z**: Cell cluster labels used. ### Examples ```r library(SingleCellExperiment) # --- Using a simulated SCE object --- sim <- simulateContamination(seed = 12345) sce <- SingleCellExperiment(list(counts = sim$observedCounts)) # Basic run: DecontX auto-clusters cells with UMAP + DBSCAN sce <- decontX(sce) # Contamination estimates stored in colData head(colData(sce)$decontX_contamination) # [1] 0.1142 0.0823 0.0571 0.2201 0.0389 0.1673 # Decontaminated counts stored as a new assay decon_mat <- decontXcounts(sce) # sparse dgCMatrix round_mat <- round(decontXcounts(sce)) # integer matrix for downstream use # --- Supplying known cluster labels (faster, more accurate) --- sce2 <- decontX(sce, z = sim$z) # --- Multi-sample / multi-batch experiment --- batch <- rep(c("sample1", "sample2"), each = ncol(sce) / 2) sce3 <- decontX(sce, batch = batch) # --- Providing raw/empty-droplet matrix for empirical background --- # sce.raw contains empty droplets (e.g., from CellRanger raw output) # sce4 <- decontX(sce, background = sce.raw) # --- Adjusting prior aggressiveness (fix delta, force higher contamination) --- sce5 <- decontX(sce, delta = c(9, 20), estimateDelta = FALSE) # --- Working with a plain sparse matrix (returns a list, not SCE) --- result <- decontX(sim$observedCounts) str(result, max.level = 1) # List of 5 # $ runParams : list # $ estimates : list # $ decontXcounts: dgCMatrix # $ contamination: Named num [1:300] # $ z : chr [1:300] ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.