Skip to content

Estimate paired species effects for retained features

The comparison estimates the mean Galium minus Dactylis difference within the six field plots. Each plot contributes at most one biological pair. Repeated QCs, blanks, isotope peaks, and correlated features cannot increase that sample size.

The primary analysis uses the accepted QC correction followed by PQN. It tests only features that pass the declared raw control and detection rules, retain at least five complete plot pairs, and have nonzero residual variance. Missing measurements remain missing.

For each feature, the mean within-plot log2 difference equals the species coefficient in an ordinary linear model with plot intercepts. Its standard error is the standard deviation of those differences divided by the square root of the complete-pair count. The test and ordinary pointwise 95% confidence interval use that count minus one degree of freedom.

These intervals treat the estimated QC corrections and sample factors as fixed. They do not propagate uncertainty from those estimates. The display selects the first ten tested features by adjusted p-value, then feature identifier, without adjusting their intervals for that selection.

The complete result table preserves untested features and their reasons. Benjamini-Hochberg adjustment applies once across the primary tested family, and the displayed threshold is 0.05. Its nominal false discovery rate control requires independent tests or suitable positive dependence. Correlated ion features and shared normalization do not establish those conditions. A small adjusted p-value identifies an association for review under this analysis; it does not identify a compound or establish a causal species mechanism.

Inference is conditional on the pipeline’s feature correspondences across injections. The result table retains each feature’s QC and biological observation counts and exported retention-time spans. Those coordinates precede the linker’s internal LOWESS transformation, so they do not measure its final matching distance. Large spans require chromatogram review, and statistical significance does not confirm that the linked observations represent the same ion. These diagnostics do not introduce another exclusion rule.

The displayed BH findings include e_1338204943940715562 and e_10495494543718811890, whose biological observations have broad exported retention-time spans. Read their biological_exported_rt_span_seconds values in the complete result table before interpreting their effects. Their adjusted p-values do not resolve that correspondence uncertainty.

The source data are Dietz and colleagues’ MTBLS865 deposit, released publicly by MetaboLights in 2020 under EMBL-EBI Terms of Use. The repository’s public data policy permits reuse, and its data guide permits modification. The source notice identifies the authors, original files, and this analysis’s selection and transformations.

library(ggplot2)
library(jsonlite)
library(patchwork)
source("/shared/statistics.R")
options(digits = 17)
data <- LoadFeatureData()
analysis <- RunFeatureAnalysis(data)
primary <- analysis$variants$primary
result <- primary$results
observations <- read.delim(
gzfile("/fixtures/feature_observations.tsv.gz"), check.names = FALSE,
colClasses = c(feature_id = "character", injection_id = "character",
source_feature_id = "character")
)
sample_index <- match(observations$injection_id, data$metadata$injection_id)
stopifnot(!anyNA(sample_index),
all(observations$feature_id %in% result$feature_id),
!anyDuplicated(observations[c("feature_id", "injection_id")]),
all(is.finite(observations$aligned_retention_time_seconds)))
roles <- c(qc = "pooled_qc", biological = "biological")
for (prefix in names(roles)) {
selected <- observations[data$metadata$role[sample_index] == roles[prefix], ]
index <- factor(selected$feature_id, levels = result$feature_id)
times <- selected$aligned_retention_time_seconds
result[[paste0(prefix, "_rt_observations")]] <- as.integer(table(index))
result[[paste0(prefix, "_exported_rt_span_seconds")]] <- as.numeric(
tapply(times, index, max) - tapply(times, index, min)
)
}
write.csv(result, "outputs/paired_results_r.csv", row.names = FALSE,
na = "NA")
write.csv(analysis$factors, "outputs/scaling_factors_r.csv", row.names = FALSE)
write.csv(analysis$threshold_results, "outputs/threshold_results_r.csv",
row.names = FALSE, na = "NA")
variant_tables <- list()
for (name in names(analysis$variants)) {
table <- analysis$variants[[name]]$results
table$analysis <- name
variant_tables[[name]] <- table
}
variants <- do.call(rbind, variant_tables)
rownames(variants) <- NULL
write.csv(variants, "outputs/normalization_results_r.csv", row.names = FALSE,
na = "NA")
pairs <- PairColumns(primary$metadata)
differences <- primary$matrix[, pairs$galium, drop = FALSE] -
primary$matrix[, pairs$dactylis, drop = FALSE]
diagnostics <- data.frame(
feature_id = rep(data$features$feature_id, each = length(pairs$plots)),
plot = rep(pairs$plots, nrow(differences)),
paired_difference = as.vector(t(differences)),
residual = as.vector(t(sweep(differences, 1, result$log2_fold_change, "-")))
)
write.csv(diagnostics, "outputs/paired_diagnostics_r.csv", row.names = FALSE,
na = "NA")
stopifnot(sum(result$tested) > 0L)
first <- which(result$tested)[1]
observed <- is.finite(differences[first, ])
indices <- c(pairs$dactylis[observed], pairs$galium[observed])
model_data <- primary$metadata[indices, ]
model_data$species <- factor(model_data$species,
c("Dactylis glomerata", "Galium mollugo"))
model_data$area <- primary$matrix[first, indices]
fit <- lm(area ~ factor(plot) + species, data = model_data)
coefficient <- summary(fit)$coefficients["speciesGalium mollugo", ]
stopifnot(isTRUE(all.equal(unname(coefficient[1]),
result$log2_fold_change[first], tolerance = 1e-10)),
isTRUE(all.equal(unname(coefficient[2]),
result$standard_error[first], tolerance = 1e-10)),
df.residual(fit) == result$degrees_freedom[first])
metrics <- list(
retained_features = sum(analysis$quality$main_retained),
tested_features = sum(result$tested),
five_pair_tests = sum(result$tested & result$complete_pairs == 5L),
six_pair_tests = sum(result$tested & result$complete_pairs == 6L),
discoveries_005 = sum(result$adjusted_p_value < 0.05, na.rm = TRUE),
zero_variance_features = sum(result$fit_status == "zero_residual_variance"),
retained_qc_rt_span_above_10 = sum(
analysis$quality$main_retained & result$qc_exported_rt_span_seconds > 10,
na.rm = TRUE
),
retained_biological_rt_span_above_10 = sum(
analysis$quality$main_retained &
result$biological_exported_rt_span_seconds > 10, na.rm = TRUE
)
)
write_json(metrics, "outputs/metrics_r.json", auto_unbox = TRUE)
cat("Primary tested features:", metrics$tested_features, "\n")
cat("Features with five complete pairs:", metrics$five_pair_tests, "\n")
cat("Features with six complete pairs:", metrics$six_pair_tests, "\n")
cat("Features with adjusted p-value below 0.05:",
metrics$discoveries_005, "\n")
cat("Retained features with exported RT spans above 10 seconds, QC/biology:",
metrics$retained_qc_rt_span_above_10,
metrics$retained_biological_rt_span_above_10, "\n")
Primary tested features: 42
Features with five complete pairs: 24
Features with six complete pairs: 18
Features with adjusted p-value below 0.05: 3
Retained features with exported RT spans above 10 seconds, QC/biology: 5 13
tested <- result[result$tested, ]
tested$discovery <- tested$adjusted_p_value < 0.05
volcano <- ggplot(tested, aes(log2_fold_change,
-log10(pmax(adjusted_p_value, 1e-300)),
color = discovery)) +
geom_point(size = 1.2, alpha = 0.6) +
geom_hline(yintercept = -log10(0.05), linetype = 2) +
scale_color_manual(values = c("#aab4bc", "#286b86")) +
labs(x = "Galium minus Dactylis, log2 area", y = "-log10 adjusted p-value",
color = "Below 0.05", title = "Primary tested family") +
theme_minimal(base_size = 11)
selected <- tested[order(tested$adjusted_p_value, tested$feature_id), ]
selected <- head(selected, 10)
selected$feature_id <- factor(selected$feature_id,
levels = rev(selected$feature_id))
intervals <- ggplot(selected, aes(log2_fold_change, feature_id)) +
geom_vline(xintercept = 0, linetype = 2, color = "gray60") +
geom_segment(aes(x = ci_lower, xend = ci_upper, yend = feature_id)) +
geom_point(color = "#286b86", size = 2) +
labs(x = "Galium minus Dactylis, 95% confidence interval", y = "Feature",
title = "First ten features by adjusted p-value") +
theme_minimal(base_size = 11)
ggsave("outputs/paired_effects_r.png", volcano + intervals,
width = 12, height = 5, dpi = 160, bg = "white")
sensitivity <- variants[variants$analysis != "primary", ]
sensitivity$primary_effect <- result$log2_fold_change[
match(sensitivity$feature_id, result$feature_id)]
sensitivity_plot <- ggplot(sensitivity,
aes(primary_effect, log2_fold_change)) +
geom_point(size = 0.8, alpha = 0.4, na.rm = TRUE) +
geom_abline(slope = 1, intercept = 0, color = "#b85c38") +
facet_wrap(~analysis, nrow = 1) +
labs(x = "Primary effect", y = "Alternative effect",
title = "Effects under declared normalization alternatives") +
theme_minimal(base_size = 11)
ggsave("outputs/normalization_sensitivity_r.png", sensitivity_plot,
width = 11, height = 4, dpi = 160, bg = "white")

The R figure shows adjusted p-values and ordinary pointwise confidence intervals for selected paired effects.

The R figure compares primary effects with the declared normalization alternatives.

Read sensitivity as a measurement question

Section titled “Read sensitivity as a measurement question”

The normalization alternatives use the same primary retained universe. No sample scaling preserves accepted drift corrections but applies no sample factor. Median scaling replaces PQN with sample medians. Uncorrected PQN refits its quotient factors to raw log2 areas while preserving the same feature universe, so it compares the complete normalization procedures.

The no-sample-scaling panel shifts relative to the primary PQN estimates. This comparison cannot separate sample concentration differences from a broad species shift, and PQN can remove either. Interpret the reported effects conditional on the declared normalization reference.

The threshold table varies blank ratios of 3, 5, and 10, or QC CV limits of 0.20, 0.30, and 0.40, one rule at a time. Those comparisons preserve the primary corrected matrix and PQN factors. Each threshold defines its own tested family and BH adjustment. Changed significance can therefore reflect a different family as well as a changed eligibility decision.

The diagnostic table preserves every observed paired difference. Residuals from the feature mean exist only for retained features with at least five complete pairs; other residual entries remain missing. For tested features, the result table reports the largest change in the mean after one complete pair is removed and identifies that plot. Inspect those records alongside the paired displays; an influential plot is evidence to examine, not permission for automatic deletion.

  • R paired t-test reference

    The reference explains complete-pair treatment, standard errors, and confidence intervals. Its paired example provides a comparison for the within-plot differences used here.

  • R multiple-testing adjustment reference

    The reference distinguishes false discovery rate from family-wise error control and identifies the BH and BY methods. It explains why the tested family and dependence assumptions matter.