diff --git a/NEWS.md b/NEWS.md index 5a31af2c..93e6d186 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# MSstatsConvert 1.24.0 + +* **Spectronaut**: `SpectronauttoMSstatsFormat` now filters on q-values by default (`filter_with_Qvalue = TRUE`), matching the other converters. Pipelines that rely on the default and do not set the value explicitly may produce different results. + # MSstatsConvert 1.22.0 ## Protein Turnover Support diff --git a/R/converters_SpectronauttoMSstatsFormat.R b/R/converters_SpectronauttoMSstatsFormat.R index a2edc095..b94251d9 100644 --- a/R/converters_SpectronauttoMSstatsFormat.R +++ b/R/converters_SpectronauttoMSstatsFormat.R @@ -81,10 +81,13 @@ #' flagged \code{F.ExcludedFromQuantification = TRUE}: the intensity is set to #' NA, and a row is removed if it corresponds to a feature with no measurements #' across all MS runs. FALSE keeps the reported intensities. -#' @param filter_with_Qvalue FALSE (default) does not perform any filtering. -#' TRUE sets the intensity to NA for measurements that exceed qvalue_cutoff -#' in EG.Qvalue or 0.01 in PG.Qvalue. The rows are retained, and the NA -#' values are treated as censored missing values for imputation. +#' @param filter_with_Qvalue TRUE (default) excludes measurements that exceed +#' qvalue_cutoff in EG.Qvalue or 0.01 in PG.Qvalue: the intensity is set to +#' NA, and a row is removed if it corresponds to a feature with no +#' measurements across all MS runs. With removeFewMeasurements = TRUE +#' (default), features left with only one or two measurements are removed +#' too. The remaining NA values are treated as censored missing values for +#' imputation. FALSE does not perform any filtering. #' @param qvalue_cutoff Cutoff for EG.Qvalue. default is 0.01. #' @param calculateAnomalyScores Default is FALSE. If TRUE, will run anomaly detection model and calculate anomaly scores for each feature. Used downstream to weigh measurements in differential analysis. #' @param anomalyModelFeatures character vector of quality metric column names to be used as features in the anomaly detection model. List must not be empty if calculateAnomalyScores=TRUE. @@ -117,7 +120,7 @@ SpectronauttoMSstatsFormat = function( peptideSequenceColumn = "EG.ModifiedSequence", heavyLabels = NULL, excludedFromQuantificationFilter = TRUE, - filter_with_Qvalue = FALSE, qvalue_cutoff = 0.01, + filter_with_Qvalue = TRUE, qvalue_cutoff = 0.01, useUniquePeptide = TRUE, removeFewMeasurements=TRUE, removeProtein_with1Feature = FALSE, summaryforMultipleRows = max, diff --git a/R/utils_clean_features.R b/R/utils_clean_features.R index 2928909c..a0f654e1 100644 --- a/R/utils_clean_features.R +++ b/R/utils_clean_features.R @@ -105,13 +105,15 @@ with = FALSE]) if (is.element("isZero", colnames(input)) & length(anomaly_metrics) == 0) { - input = input[, list(Intensity = aggregator(Intensity, na.rm = TRUE), + input = input[, list(Intensity = .aggregateIntensity(Intensity, aggregator), isZero = all(isZero | is.na(Intensity)) & !all(is.na(Intensity))), by = feature_columns] } else if (length(anomaly_metrics) > 0){ input[, row_id := .I] + # Not .aggregateIntensity: rows are joined back on the max value, so + # returning NA here could change which rows survive the join. max_per_group = input[, .(max_intensity = max(Intensity, na.rm = TRUE)), by = feature_columns] @@ -121,7 +123,7 @@ input[, row_id := NULL] } else { - input = input[, list(Intensity = aggregator(Intensity, na.rm = TRUE)), + input = input[, list(Intensity = .aggregateIntensity(Intensity, aggregator)), by = c(feature_columns)] } merge(input, info, @@ -129,6 +131,22 @@ } +#' Aggregate the intensities of one feature in one run +#' @param intensity numeric vector of intensities. +#' @param aggregator function that will be used to aggregate duplicated values. +#' It should accept an `na.rm` parameter. +#' @return numeric scalar. `NA` if all intensities are missing, whatever the +#' aggregator: the intensity is unknown, so neither `-Inf` (with a warning) +#' from `max` nor `0` from `sum` is returned. +#' @keywords internal +.aggregateIntensity = function(intensity, aggregator) { + if (all(is.na(intensity))) { + return(NA_real_) + } + aggregator(intensity, na.rm = TRUE) +} + + #' Remove proteins only identified by a single feature #' @param input `data.table` pre-processed by one of the .cleanRaw* functions. #' @param remove_single_feature lgl, if TRUE, proteins with a single feature diff --git a/inst/tinytest/test_converters_SpectronauttoMSstatsFormat.R b/inst/tinytest/test_converters_SpectronauttoMSstatsFormat.R index b4a94cec..ff84f041 100644 --- a/inst/tinytest/test_converters_SpectronauttoMSstatsFormat.R +++ b/inst/tinytest/test_converters_SpectronauttoMSstatsFormat.R @@ -441,3 +441,63 @@ expect_equivalent(angyt_input_intensities, angyt_output_intensities) n_bioreplicates = length(unique(boxcar_raw$R.Replicate)) angyt_rows = subset(output_heavy, grepl("ANGYTTEYSASVK", PeptideSequence, fixed = TRUE)) expect_equal(nrow(angyt_rows), 2L * n_bioreplicates) + +# Test SpectronauttoMSstatsFormat Q-value Filtering --------------------------- +spectronaut_quality = system.file( + "tinytest/raw_data/Spectronaut/spectronaut_quality_input.csv", + package = "MSstatsConvert") +spectronaut_quality = data.table::fread(spectronaut_quality) + +output_qval = data.table::as.data.table(SpectronauttoMSstatsFormat( + data.table::copy(spectronaut_quality), use_log_file = FALSE)) +output_no_qval = data.table::as.data.table(SpectronauttoMSstatsFormat( + data.table::copy(spectronaut_quality), filter_with_Qvalue = FALSE, + use_log_file = FALSE)) +output_loose_qval = data.table::as.data.table(SpectronauttoMSstatsFormat( + data.table::copy(spectronaut_quality), qvalue_cutoff = 0.05, + use_log_file = FALSE)) + +# Measurements above the default cutoff that no other filter removes +feature_keys = c("Run", "PeptideSequence", "PrecursorCharge", + "FragmentIon", "ProductCharge") +above_cutoff = spectronaut_quality[ + EG.Qvalue > 0.01 & F.FrgLossType == "noloss" & + F.ExcludedFromQuantification == FALSE, + .(Run = R.FileName, + PeptideSequence = gsub("_", "", EG.ModifiedSequence), + PrecursorCharge = FG.Charge, FragmentIon = F.FrgIon, + ProductCharge = F.Charge)] +above_cutoff_no_qval = output_no_qval[above_cutoff, on = feature_keys, + nomatch = 0][!is.na(Intensity)] +above_cutoff_qval = output_qval[above_cutoff_no_qval, on = feature_keys, + nomatch = 0] + +# The fixture has 218 raw rows above the 0.01 cutoff, but only 13 reach the +# output with a real intensity when filtering is off. The rest are either +# excluded from quantification or have a peak area of 1 or less, which the +# converter already turns into NA. +expect_equal(nrow(above_cutoff_no_qval), 13) +expect_equal(nrow(above_cutoff_qval), 13) +expect_true(all(is.na(above_cutoff_qval$Intensity))) +expect_equal(nrow(output_qval), nrow(output_no_qval)) +expect_equal(sum(is.na(output_no_qval$Intensity)), 243) +expect_equal(sum(is.na(output_qval$Intensity)), 256) +expect_equal(sum(is.na(output_loose_qval$Intensity)), 245) +expect_true(sum(is.na(output_loose_qval$Intensity)) < + sum(is.na(output_qval$Intensity))) + +# Measurements at or below the default cutoff are left alone by the filter +below_cutoff = spectronaut_quality[ + EG.Qvalue <= 0.01 & F.FrgLossType == "noloss" & + F.ExcludedFromQuantification == FALSE, + .(Run = R.FileName, + PeptideSequence = gsub("_", "", EG.ModifiedSequence), + PrecursorCharge = FG.Charge, FragmentIon = F.FrgIon, + ProductCharge = F.Charge)] +below_cutoff_no_qval = output_no_qval[below_cutoff, on = feature_keys, + nomatch = 0] +below_cutoff_qval = output_qval[below_cutoff, on = feature_keys, + nomatch = 0] + +expect_equal(nrow(below_cutoff_qval), 1089) +expect_equal(below_cutoff_qval$Intensity, below_cutoff_no_qval$Intensity) diff --git a/inst/tinytest/test_utils_clean_features.R b/inst/tinytest/test_utils_clean_features.R index 4070faa8..3982fc89 100644 --- a/inst/tinytest/test_utils_clean_features.R +++ b/inst/tinytest/test_utils_clean_features.R @@ -66,6 +66,28 @@ expect_equal( MSstatsConvert:::.summarizeMultipleMeasurements(to_aggregate, max, c("PeptideSequence", "Run")), to_aggregate[c(2, 4, 6), ] ) +## A feature with no measurements in a run is NA, without a warning +all_missing = data.table::data.table(PeptideSequence = "A", + Run = rep(1:2, each = 2), + Intensity = c(1, 2, NA, NA)) +expect_silent( + MSstatsConvert:::.summarizeMultipleMeasurements( + data.table::copy(all_missing), max, c("PeptideSequence", "Run")) +) +expect_equal( + suppressWarnings(MSstatsConvert:::.summarizeMultipleMeasurements( + data.table::copy(all_missing), max, c("PeptideSequence", "Run")))$Intensity, + c(2, NA) +) +expect_silent( + MSstatsConvert:::.summarizeMultipleMeasurements( + data.table::copy(all_missing), sum, c("PeptideSequence", "Run")) +) +expect_equal( + MSstatsConvert:::.summarizeMultipleMeasurements( + data.table::copy(all_missing), sum, c("PeptideSequence", "Run"))$Intensity, + c(3, NA) +) ## Zeros are coded correctly ### with_zeros = data.table::data.table( @@ -83,11 +105,13 @@ correct_result = data.table::data.table( FragmentIon = NA, ProductCharge = NA, Run = 1:4, - Intensity = c(5, 0, 0, 0), + Intensity = c(5, NA, 0, 0), isZero = c(FALSE, FALSE, TRUE, TRUE), ProteinName = "A" ) MSstatsConvert:::.checkDDA(with_zeros) +# Run 2 has no measurements. An all-NA group now returns NA for every +# aggregator, sum included, because zero asserts a measurement that was not made. expect_equal( MSstatsConvert:::.summarizeMultipleMeasurements(with_zeros, sum, c("PeptideSequence", "Run")), correct_result[, -(2:3), with = FALSE] diff --git a/man/SpectronauttoMSstatsFormat.Rd b/man/SpectronauttoMSstatsFormat.Rd index 102a3cd1..8927d964 100644 --- a/man/SpectronauttoMSstatsFormat.Rd +++ b/man/SpectronauttoMSstatsFormat.Rd @@ -11,7 +11,7 @@ SpectronauttoMSstatsFormat( peptideSequenceColumn = "EG.ModifiedSequence", heavyLabels = NULL, excludedFromQuantificationFilter = TRUE, - filter_with_Qvalue = FALSE, + filter_with_Qvalue = TRUE, qvalue_cutoff = 0.01, useUniquePeptide = TRUE, removeFewMeasurements = TRUE, @@ -121,10 +121,13 @@ flagged \code{F.ExcludedFromQuantification = TRUE}: the intensity is set to NA, and a row is removed if it corresponds to a feature with no measurements across all MS runs. FALSE keeps the reported intensities.} -\item{filter_with_Qvalue}{FALSE (default) does not perform any filtering. -TRUE sets the intensity to NA for measurements that exceed qvalue_cutoff -in EG.Qvalue or 0.01 in PG.Qvalue. The rows are retained, and the NA -values are treated as censored missing values for imputation.} +\item{filter_with_Qvalue}{TRUE (default) excludes measurements that exceed +qvalue_cutoff in EG.Qvalue or 0.01 in PG.Qvalue: the intensity is set to +NA, and a row is removed if it corresponds to a feature with no +measurements across all MS runs. With removeFewMeasurements = TRUE +(default), features left with only one or two measurements are removed +too. The remaining NA values are treated as censored missing values for +imputation. FALSE does not perform any filtering.} \item{qvalue_cutoff}{Cutoff for EG.Qvalue. default is 0.01.} diff --git a/man/dot-aggregateIntensity.Rd b/man/dot-aggregateIntensity.Rd new file mode 100644 index 00000000..6a6373b2 --- /dev/null +++ b/man/dot-aggregateIntensity.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils_clean_features.R +\name{.aggregateIntensity} +\alias{.aggregateIntensity} +\title{Aggregate the intensities of one feature in one run} +\usage{ +.aggregateIntensity(intensity, aggregator) +} +\arguments{ +\item{intensity}{numeric vector of intensities.} + +\item{aggregator}{function that will be used to aggregate duplicated values. +It should accept an \code{na.rm} parameter.} +} +\value{ +numeric scalar. \code{NA} if all intensities are missing, whatever the +aggregator: the intensity is unknown, so neither \code{-Inf} (with a warning) +from \code{max} nor \code{0} from \code{sum} is returned. +} +\description{ +Aggregate the intensities of one feature in one run +} +\keyword{internal}