Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 8 additions & 5 deletions R/converters_SpectronauttoMSstatsFormat.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions R/utils_clean_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -121,14 +123,30 @@
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,
by = intersect(colnames(input), colnames(info)), sort = FALSE)
}


#' 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
Expand Down
60 changes: 60 additions & 0 deletions inst/tinytest/test_converters_SpectronauttoMSstatsFormat.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 25 additions & 1 deletion inst/tinytest/test_utils_clean_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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]
Expand Down
13 changes: 8 additions & 5 deletions man/SpectronauttoMSstatsFormat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/dot-aggregateIntensity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading