Transforming and calibrating LFQ data

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.

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()
#> [1] "log2_intensity"
head(logged$data_long()[c("protein_Id", "sampleName", lfq$response(), logged$response())])
#> # A tibble: 6 × 4
#>   protein_Id sampleName intensity log2_intensity
#>   <chr>      <chr>          <dbl>          <dbl>
#> 1 StdA       S1                64              6
#> 2 StdB       S1               256              8
#> 3 Target     S1               512              9
#> 4 StdA       S2               128              7
#> 5 StdB       S2               512              9
#> 6 Target     S2              1024             10

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.

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.

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.

external <- data.frame(
  sampleName = c("S3", "S1", "S2"),
  measured_intensity = c(64, 128, 256)
)
offsets <- setNames(log2(external$measured_intensity / 128), external$sampleName)
offsets
#> S3 S1 S2 
#> -1  0  1

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

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."
)
Target log2 intensity before and after calibration.
Sample Original log2 Internal standards External calibrant
S1 9 2 9
S2 10 2 9
S3 9 3 10

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

sessionInfo()
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04.1 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_4.0.5    prolfqua_1.10.0  dplyr_1.2.1      BiocStyle_2.43.0
#> 
#> loaded via a namespace (and not attached):
#>   [1] Rdpack_2.6.6           gridExtra_2.3.1        rlang_1.3.0           
#>   [4] magrittr_2.0.5         clue_0.3-68            GetoptLong_1.1.1      
#>   [7] otel_0.3.0             matrixStats_1.5.0      compiler_4.6.2        
#>  [10] mgcv_1.9-5             png_0.1-9              vctrs_0.7.3           
#>  [13] stringr_1.6.0          pkgconfig_2.0.3        shape_1.4.6.1         
#>  [16] crayon_1.5.3           fastmap_1.2.0          backports_1.5.1       
#>  [19] labeling_0.4.3         utf8_1.2.6             promises_1.5.0        
#>  [22] rmarkdown_2.32         nloptr_2.2.1           purrr_1.2.2           
#>  [25] xfun_0.63              logistf_1.26.1         cachem_1.1.0          
#>  [28] jsonlite_2.0.0         progress_1.2.3         later_1.4.8           
#>  [31] Rfit_0.27.0            broom_1.0.13           parallel_4.6.2        
#>  [34] prettyunits_1.2.0      cluster_2.1.8.3        R6_2.6.1              
#>  [37] bslib_0.12.0           stringi_1.8.9          RColorBrewer_1.1-3    
#>  [40] limma_4.1.3            boot_1.3-32            rpart_4.1.27          
#>  [43] numDeriv_2016.8-1.1    jquerylib_0.1.4        Rcpp_1.1.2            
#>  [46] iterators_1.0.14       knitr_1.52             IRanges_2.49.0        
#>  [49] nnet_7.3-21            httpuv_1.6.17          Matrix_1.7-6          
#>  [52] splines_4.6.2          tidyselect_1.2.1       yaml_2.3.12           
#>  [55] doParallel_1.0.17      codetools_0.2-20       lmerTest_3.2-1        
#>  [58] lattice_0.23-1         tibble_3.3.1           shiny_1.15.0          
#>  [61] withr_3.0.3            S7_1.0.0               evaluate_1.0.5        
#>  [64] circlize_0.4.18        pillar_1.11.1          BiocManager_1.30.27   
#>  [67] mice_3.20.0            KernSmooth_2.23-27     foreach_1.5.2         
#>  [70] stats4_4.6.2           reformulas_0.4.4       plotly_4.12.1         
#>  [73] generics_0.1.4         S4Vectors_0.53.1       hms_1.1.4             
#>  [76] scales_1.4.0           minqa_1.2.8            xtable_1.8-8          
#>  [79] glue_1.8.1             maketools_1.3.2        tools_4.6.2           
#>  [82] sys_3.4.3              data.table_1.18.6.1    lme4_2.1-0            
#>  [85] buildtools_1.0.0       grid_4.6.2             limpa_1.7.0           
#>  [88] tidyr_1.3.2            rbibutils_2.4.1        crosstalk_1.2.2       
#>  [91] colorspace_2.1-4       nlme_3.1-172           formula.tools_1.7.1   
#>  [94] cli_3.6.6              viridisLite_0.4.3      ComplexHeatmap_2.31.0 
#>  [97] gtable_0.3.6           sass_0.5.0             digest_0.6.39         
#> [100] operator.tools_1.6.3.1 BiocGenerics_0.61.1    ggrepel_0.9.8         
#> [103] rjson_0.2.23           htmlwidgets_1.6.4      farver_2.1.2          
#> [106] htmltools_0.5.9        lifecycle_1.0.5        httr_1.4.9            
#> [109] GlobalOptions_0.1.4    statmod_1.5.2          mime_0.13             
#> [112] MASS_7.3-66