Skip to main content

A practical guide to contaminant removal

Recommendations derived from Parts 1 and 2, and an end-to-end example

Author

Joan Gordó Guardia

Published

November 18, 2026

Show code
library(ggplot2)
library(dplyr)
library(tidyr)
library(knitr)

root <- normalizePath("..")   # Quarto runs chunks from analysis/
for (f in list.files(file.path(root, "R"), full.names = TRUE)) source(f)
theme_set(theme_minimal(base_size = 12))

sim <- readRDS(file.path(root, "analysis", "results", "simulation_results.rds"))

How the recommendations are chosen

A cleaning method is only useful if it removes contamination without removing real biology. So the rule is explicit and applied to the simulation results from Part 2, using only the scenarios with realistic (leaky) negative controls:

  1. Discard every method/threshold that, on average, removes more than 1% of genuine reads or more than 2 genuine taxa (out of 150).
  2. Among the rest, pick the one that removes the most contamination.

The thresholds are a judgement call, written down so they can be argued with. They reflect a typical clinical priority: losing a real organism is worse than leaving some contamination that can be flagged in the report.

Show code
max_reads_lost <- 1
max_taxa_lost <- 2

cand <- sim$results |>
  filter(leak > 0) |>
  mutate(choice = ifelse(is.na(threshold), method,
                         paste0(method, " ", threshold))) |>
  group_by(conc_noise, biomass_ratio, n_neg, choice) |>
  summarise(removed = mean(contam_reads_removed),
            reads_lost = mean(true_reads_lost),
            taxa_lost = mean(true_taxa_lost), .groups = "drop")

best <- cand |>
  filter(reads_lost <= max_reads_lost, taxa_lost <= max_taxa_lost) |>
  group_by(conc_noise, biomass_ratio, n_neg) |>
  slice_max(removed, n = 1, with_ties = FALSE) |>
  ungroup()

stopifnot(nrow(best) == nrow(distinct(cand, conc_noise, biomass_ratio, n_neg)))

Recommendation table

Each cell shows the best method and threshold, and the share of contamination it removes on average.

Show code
best |>
  mutate(
    Quantification = ifelse(conc_noise == 0.1, "Precise", "Imprecise"),
    Biomass = factor(biomass_ratio, levels = c(100, 10, 1),
                     labels = c("High (~1% contam.)", "Low (~6%)",
                                "Very low (~40%)")),
    cell = sprintf("%s (%.0f%%)", choice, removed),
    n_neg = paste(n_neg, ifelse(n_neg == 1, "control", "controls"))
  ) |>
  select(Quantification, Biomass, n_neg, cell) |>
  pivot_wider(names_from = n_neg, values_from = cell) |>
  arrange(desc(Quantification), Biomass) |>
  kable(align = "llllll")
Quantification Biomass 1 control 2 controls 4 controls 8 controls
Precise High (~1% contam.) either 0.1 (86%) frequency 0.1 (86%) frequency 0.1 (84%) frequency 0.1 (83%)
Precise Low (~6%) either 0.1 (92%) frequency 0.1 (92%) frequency 0.1 (93%) frequency 0.1 (91%)
Precise Very low (~40%) frequency 0.25 (89%) frequency 0.25 (88%) frequency 0.25 (88%) frequency 0.25 (84%)
Imprecise High (~1% contam.) either 0.1 (66%) frequency 0.1 (68%) frequency 0.1 (67%) frequency 0.1 (58%)
Imprecise Low (~6%) combined 0.25 (79%) frequency 0.1 (71%) frequency 0.1 (57%) frequency 0.1 (64%)
Imprecise Very low (~40%) frequency 0.25 (18%) combined 0.25 (16%) combined 0.1 (11%) frequency 0.1 (13%)
Show code
share_freq <- mean(grepl("^frequency", best$choice))
low_imprecise <- best |> filter(conc_noise == 0.5, biomass_ratio == 1)

# Where another method wins, how far behind is the best frequency option?
passing <- cand |>
  filter(reads_lost <= max_reads_lost, taxa_lost <= max_taxa_lost)
gap <- best |>
  filter(!grepl("^frequency", choice)) |>
  left_join(passing |>
              filter(grepl("^frequency", choice)) |>
              group_by(conc_noise, biomass_ratio, n_neg) |>
              summarise(best_freq = max(removed), .groups = "drop"),
            by = c("conc_noise", "biomass_ratio", "n_neg")) |>
  mutate(gap = removed - best_freq)
n_05_pass <- sum(grepl(" 0.5$", passing$choice))

What the table says:

  • The frequency method is the backbone. It is the selected method in 75% of the designs. Where a method that also uses the negative controls wins, the best frequency option is on average 4.1 percentage points behind (at most 11.7). Leaked DNA in the controls is what stops those methods from doing better: push their thresholds up and they start removing real taxa.
  • Thresholds of 0.1–0.25 are the safe range. A threshold of 0.5 passes the “don’t remove real biology” rule in 0 of the 24 designs.
  • Very low biomass plus imprecise quantification cannot be fixed in software. The best option there removes only 11–18% of the contamination. The fix is upstream: better DNA quantification, more starting material, or reporting the data as exploratory.

Decision guide

The flowchart condenses the table above for a single sample type.

flowchart TD
    A["One sample type<br/>with its own negative controls"] --> B{"DNA measured precisely?<br/>(fluorometric or qPCR)"}
    B -- Yes --> C["decontam frequency<br/>threshold 0.1 to 0.25"]
    B -- No --> D{"Very low biomass?<br/>(e.g. blood, ascitic fluid)"}
    D -- No --> E["decontam frequency, threshold 0.1<br/>expect partial cleaning"]
    D -- Yes --> F["No method cleans reliably:<br/>report residual contamination<br/>and treat results as exploratory"]
    C --> G["Checks: no abundant taxon removed,<br/>threshold and reads removed reported"]
    E --> G
    F --> G

Three rules hold in every scenario:

  1. Do not remove every taxon found in the negative controls. It removed real taxa in the mock community (Part 1) and most genuine reads in the simulations (Part 2).
  2. Clean each sample type separately, against its own controls: contamination is a larger share of blood than of a swab, so the evidence differs.
  3. Report the decision, not just the result: method, threshold, number of taxa removed and share of reads removed per sample.

Worked example: two sample types in one study

A simulated study with swabs (low biomass, precise quantification) and blood (very low biomass, imprecise quantification, shallow sequencing), each with four negative controls, cleaned with clean_low_biomass(). Because it is simulated, we can check the result against the truth.

Show code
swab <- simulate_study(n_samples = 30, n_neg = 4, biomass_ratio = 10,
                       conc_noise = 0.1, leak = 0.005, seed = 2018)
blood <- simulate_study(n_samples = 30, n_neg = 4, biomass_ratio = 1,
                        conc_noise = 0.5, leak = 0.005,
                        depth_meanlog = log(6000), depth_sdlog = 0.8,
                        neg_depth_factor = 0.2, seed = 2019)
prefix <- function(s, p) {
  rownames(s$counts) <- s$samples$sample_id <- paste0(p, "_", s$samples$sample_id)
  s
}
swab <- prefix(swab, "SWAB")
blood <- prefix(blood, "BLOOD")

counts <- rbind(swab$counts, blood$counts)
samples <- rbind(cbind(swab$samples, sample_type = "swab"),
                 cbind(blood$samples, sample_type = "blood"))
expected <- setNames(swab$taxonomy$expected, swab$taxonomy$asv_id)

The settings follow the recommendation table: frequency method with threshold 0.1, the fixed 1,000-read rule for swabs and the “median of the controls” rule for blood.

Show code
cleaned <- clean_low_biomass(
  counts, samples, group = "sample_type",
  method = "frequency",
  threshold = c(swab = 0.1, blood = 0.1),
  low_read_rule = c(swab = "fixed", blood = "neg_median"),
  min_reads = 1000
)

cleaned$summary |>
  select(-notes) |>
  rename(`Sample type` = group, Method = method, Threshold = threshold,
         `Read-depth rule` = read_rule, `Samples in` = samples_in,
         `Samples dropped` = samples_dropped, `Neg. controls` = negatives,
         `Taxa scored` = taxa_scored, `Taxa removed` = taxa_removed,
         `Median % reads removed` = median_pct_reads_removed) |>
  mutate(Threshold = format(Threshold, nsmall = 2)) |>
  kable(digits = 1)
Sample type Method Threshold Read-depth rule Samples in Samples dropped Neg. controls Taxa scored Taxa removed Median % reads removed
swab frequency 0.10 fixed (cut-off 1000) 30 0 4 210 50 7.4
blood frequency 0.10 neg_median (cut-off 1850) 30 4 4 213 6 1.8

Checking against the truth

Show code
kept_ids <- rownames(cleaned$counts)
before <- counts[kept_ids, ]
after <- cleaned$counts

truth <- tibble(
  sample_id = kept_ids,
  type = sub("_.*", "", kept_ids),
  Before = rowSums(before[, !expected]) / rowSums(before),
  After = rowSums(after[, !expected]) / rowSums(after),
  genuine_lost = 1 - rowSums(after[, expected]) / rowSums(before[, expected])
)

truth |>
  pivot_longer(c(Before, After), names_to = "stage", values_to = "contam") |>
  mutate(stage = factor(stage, levels = c("Before", "After")),
         type = factor(type, levels = c("SWAB", "BLOOD"),
                       labels = c("Swab", "Blood"))) |>
  ggplot(aes(stage, contam, group = sample_id)) +
  geom_line(alpha = 0.35, colour = "grey40") +
  geom_point(aes(colour = stage), size = 1.6) +
  facet_wrap(~type) +
  scale_colour_manual(values = c(Before = "#BDBDBD", After = "#2C7FB8"),
                      guide = "none") +
  scale_y_continuous(labels = scales::percent) +
  labs(x = NULL, y = "Contaminant share of reads")

Contaminant share per sample before and after cleaning, by sample type. Every genuine taxon is known because the data are simulated.
Show code
by_type <- truth |>
  group_by(type) |>
  summarise(before = 100 * median(Before), after = 100 * median(After),
            lost = 100 * max(genuine_lost))
sw <- by_type[by_type$type == "SWAB", ]
bl <- by_type[by_type$type == "BLOOD", ]

In swabs, the median contaminant share fell from 8.4% to 0.8%, with at most 0.1% of any sample’s genuine reads removed. In blood the median contaminant share was 58% before cleaning and 58% after: exactly the situation the guide flags as unfixable, and the reason the methods text below says so.

Methods text, generated from the run

Reporting is part of the method. The paragraph below is written by the code from cleaned$summary, so it always matches what was actually done:

Show code
s <- cleaned$summary
describe <- function(i) {
  sprintf(
    "%s samples (n = %d; %d negative controls) were cleaned with the decontam %s method (threshold %.2f) after excluding %d sample(s) under the %s read-depth rule; %d of %d scored taxa were classified as contaminants, removing a median of %.1f%% of reads per sample.",
    tools::toTitleCase(s$group[i]), s$samples_in[i], s$negatives[i], s$method[i],
    s$threshold[i], s$samples_dropped[i], s$read_rule[i], s$taxa_removed[i],
    s$taxa_scored[i], s$median_pct_reads_removed[i])
}
cat("> Contaminant sequences were identified separately for each sample type",
    "with decontam", as.character(packageVersion("decontam")),
    "(Davis et al., 2018), using DNA concentration measured before library",
    "preparation.", paste(vapply(seq_len(nrow(s)), describe, ""), collapse = " "),
    "Given the low biomass of the blood samples, residual contamination is",
    "expected and results for this sample type are reported as exploratory.\n")

Contaminant sequences were identified separately for each sample type with decontam 1.32.0 (Davis et al., 2018), using DNA concentration measured before library preparation. Swab samples (n = 30; 4 negative controls) were cleaned with the decontam frequency method (threshold 0.10) after excluding 0 sample(s) under the fixed (cut-off 1000) read-depth rule; 50 of 210 scored taxa were classified as contaminants, removing a median of 7.4% of reads per sample. Blood samples (n = 30; 4 negative controls) were cleaned with the decontam frequency method (threshold 0.10) after excluding 4 sample(s) under the neg_median (cut-off 1850) read-depth rule; 6 of 213 scored taxa were classified as contaminants, removing a median of 1.8% of reads per sample. Given the low biomass of the blood samples, residual contamination is expected and results for this sample type are reported as exploratory.

Summary

Question Recommendation Evidence
Which decontam method? Frequency first; prevalence only with clean, well-separated controls Parts 1 and 2
Which threshold? 0.1–0.25, chosen in advance and reported Part 2, Result 2
Remove what appears in the controls? Never everything; a control-dominant rule works as a check, not as the main filter Part 1
How to measure input DNA? Fluorometric or qPCR quantification, precise at low concentrations Part 2, Result 1
Which samples to drop? A read-depth rule per sample type (fixed cut-off or median of the controls), reported Part 1 and this example
Pool sample types? No: clean each one against its own controls All parts