--- title: "Transforming and calibrating LFQ data" author: - name: Witold E. Wolski affiliation: Functional Genomics Center Zurich (FGCZ), ETH Zurich and University of Zurich package: prolfqua output: BiocStyle::html_document vignette: > %\VignetteIndexEntry{9. Transforming and calibrating LFQ data} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} --- ```{r setup, include=FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message = FALSE) ``` # The active response `LFQData` holds the measurements and their configuration. Its `response()` method gives the name of the intensity column used by downstream operations. `transform_lfqdata()` normalizes the response and `transform_response()` applies any function to it; both return a new `LFQData` and leave their input unchanged. This example uses protein-level data from three samples. The two standards, `StdA` and `StdB`, each have a constant amount across samples. Their measured signals are doubled in `S2` and halved in `S3` relative to `S1`. ```{r example-data} library(prolfqua) samples <- c("S1", "S2", "S3") quant <- data.frame( protein_Id = rep(c("StdA", "StdB", "Target"), each = 3), sampleName = rep(samples, 3), raw.file = rep(paste0(samples, ".raw"), 3), condition = rep(c("Control", "Control", "Treatment"), 3), intensity = c(64, 128, 32, 256, 512, 128, 512, 1024, 512), nr_children = 1 ) cfg <- AnalysisConfiguration$new() cfg$file_name <- "raw.file" cfg$hierarchy <- list(protein_Id = "protein_Id") cfg$factors <- list(condition = "condition") cfg$set_response("intensity") lfq <- LFQData$new(quant, cfg, setup = TRUE) logged <- transform_lfqdata(lfq, "log2") logged$response() head(logged$data_long()[c("protein_Id", "sampleName", lfq$response(), logged$response())]) ``` The log2 transformation expects positive measured intensities and carries missing intensities through as `NA`. Convert zeros that encode non-detection to `NA` before logging. The `is_transformed()` flag records transformation status. Track the scale separately when using an operation that requires a particular scale. | Call | Operation on the active response | |---|---| | `transform_lfqdata(lfq, "log2")` | Take the base-2 logarithm of each intensity; `"none"` does the same, without further normalization. | | `transform_lfqdata(lfq, "robscale")` | log2, then centre each sample by its median and scale by its relative median absolute deviation, restoring a common level based on the original medians. | | `transform_lfqdata(lfq, "robscale", reference = subset)` | As `"robscale"`, with the medians and median absolute deviations estimated from a subset of features. | | `transform_lfqdata(lfq, "vsn")` | Fit a variance-stabilizing transformation to the raw intensity matrix (`vsn::justvsn()`). | | `transform_lfqdata(lfq, "center_to_reference", reference = subset)` | Subtract the median of reference features per sample (see below). | | `transform_lfqdata(lfq, "subtract_offsets", offsets = offsets)` | Subtract one given log2 offset per sample (see below). | | `transform_response(lfq, fun)` | Apply a function such as `asinh` or `sqrt` to every intensity. | # Calibrate against internal standards Internal standards are reference proteins measured in the same samples as the targets. Select proteins expected to have stable amounts across samples. `transform_lfqdata(method = "center_to_reference")` subtracts the median log2 intensity of the reference proteins from every feature in each sample. The median of the reference proteins is then zero in each sample. When all reference intensities of a sample are missing, it stops and names the sample. ```{r internal-standards} reference_ids <- data.frame(protein_Id = c("StdA", "StdB")) reference <- logged$get_subset(reference_ids) calibrated_internal <- transform_lfqdata(logged, "center_to_reference", reference = reference) ``` In prolfquapp, give the measured protein IDs in the DEA YAML under `processing_options$internal`. For a protein-level analysis, `transform: none` means a log2 transform without additional global normalization. With `internal` configured, prolfquapp centres the data with `transform_lfqdata(method = "center_to_reference")`, which stops and lists the sample IDs without a finite reference intensity. ```yaml processing_options: transform: none internal: [StdA, StdB] ``` The VSN and robscale paths centre on their respective transformed scales. For nested analyses, prolfquapp uses the internal standards for the protein abundances shown in the report. The peptide model uses the transformed peptide data before that protein-level centring. # Apply offsets from external calibrants Use `transform_lfqdata(method = "subtract_offsets")` when the correction comes from measurements outside the LFQ table. Supply one finite log2 offset per sample, named by the configured sample ID. For example, suppose an external calibrant has a reference signal of 128, and you have matched one calibrant measurement to each sample. Divide each measured signal by 128 to obtain the fold change in response, then take its log2. ```{r external-calibrants} external <- data.frame( sampleName = c("S3", "S1", "S2"), measured_intensity = c(64, 128, 256) ) offsets <- setNames(log2(external$measured_intensity / 128), external$sampleName) offsets calibrated_external <- transform_lfqdata(logged, "subtract_offsets", offsets = offsets) ``` The order of the calibrant rows can differ from the order in the LFQ table. The offsets are matched by sample ID; missing, duplicate or unknown IDs are rejected. The caller is responsible for establishing that the external measurement tracks a multiplicative effect shared by the features in that sample. If the log2 intensity is $y$ and the offset is $d$, the calibrated intensity is $y - d$. On the original scale this divides the measurement by $2^d$. All features in a sample receive the same shift, which preserves within-sample log2 differences. Between-sample differences change by the difference between their offsets. # Compare the two calibrations ```{r comparison} target_values <- function(lfq) { rows <- lfq$data_long() target <- rows[rows$protein_Id == "Target", ] setNames(target[[lfq$response()]], target$sampleName)[samples] } knitr::kable( data.frame( Sample = samples, `Original log2` = unname(target_values(logged)), `Internal standards` = unname(target_values(calibrated_internal)), `External calibrant` = unname(target_values(calibrated_external)), check.names = FALSE ), caption = "Target log2 intensity before and after calibration." ) ``` Both calibrations recover a target that is equal in `S1` and `S2` and twice as abundant in `S3`. The absolute log2 levels differ because internal centring puts the reference median at zero, while the external offsets are anchored to the calibrant's reference signal of 128. The example treats them as two estimates of the same sample effect, so it applies each to a separate copy of the logged data. # Session info ```{r session_info} sessionInfo() ```