Skip to content

Commit 53fed81

Browse files
committed
Address Tony review feedback for workstream A
- Let MSstatsPreprocess fill IsotopeLabelType - Hardcode IsotopeLabelType in converter - Remove redundant inherited @params - Simplify score coercion - Improve non-numeric score error - Rename `ann` → feature_to_compound - Rename melt variable to Run - Refactor compound-name assignment with explicit data.table join
1 parent 2957f6d commit 53fed81

3 files changed

Lines changed: 29 additions & 32 deletions

File tree

R/clean_MZMine.R

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#' @keywords internal
1818
.cleanRawMZMine <- function(msstats_object, mzmine_annotations = NULL) {
1919
ProteinName = PeptideSequence = Intensity = Run = NULL
20-
PrecursorCharge = FragmentIon = ProductCharge = IsotopeLabelType = NULL
21-
sample_col = id = score = compound_name = NULL
20+
PrecursorCharge = FragmentIon = ProductCharge = NULL
21+
id = score = compound_name = i.compound_name = NULL
2222

2323
mz_input <- getInputFile(msstats_object, "input")
2424
mz_input <- data.table::as.data.table(mz_input)
@@ -43,47 +43,53 @@
4343

4444
mz_rt_fallback <- paste0(round(mz_input[[mz_col]], 4), "_",
4545
round(mz_input[[rt_col]], 2))
46+
mz_input[, ProteinName := mz_rt_fallback]
4647

4748
if (!is.null(mzmine_annotations)) {
48-
ann <- data.table::as.data.table(mzmine_annotations)
49+
feature_to_compound <- data.table::as.data.table(mzmine_annotations)
4950
required_ann <- c("id", "compound_name", "score")
50-
missing_ann <- setdiff(required_ann, colnames(ann))
51+
missing_ann <- setdiff(required_ann, colnames(feature_to_compound))
5152
if (length(missing_ann) > 0) {
5253
stop("mzmine_annotations is missing required column(s): ",
5354
paste(missing_ann, collapse = ", "), ".")
5455
}
55-
ann[, score := suppressWarnings(as.numeric(as.character(score)))]
56-
if (anyNA(ann$score)) {
57-
stop("mzmine_annotations$score must be numeric (or coercible to numeric).")
56+
feature_to_compound[, score := suppressWarnings(as.numeric(score))]
57+
if (anyNA(feature_to_compound$score)) {
58+
stop("The 'score' column in the mzmine annotations file must contain numeric values.")
5859
}
59-
data.table::setorder(ann, id, -score)
60-
ann_top <- unique(ann, by = "id")
61-
matched <- ann_top[match(mz_input[[id_col]], ann_top[["id"]]),
62-
compound_name]
63-
compound <- ifelse(is.na(matched), mz_rt_fallback, matched)
64-
} else {
65-
compound <- mz_rt_fallback
60+
# Sort by id ascending and score descending so the highest-scoring
61+
# annotation per id is the first row in each group.
62+
data.table::setorder(feature_to_compound, id, -score)
63+
# Collapse to one row per id (the highest-scoring). data.table's
64+
# unique() with a 'by' arg keeps the first row per group, which after
65+
# the sort above is the highest-scoring annotation.
66+
feature_to_compound <- unique(feature_to_compound, by = "id")
67+
# Join: unmatched mz_input rows keep the mz_rt_fallback ProteinName
68+
# set above.
69+
mz_input[
70+
feature_to_compound,
71+
ProteinName := i.compound_name,
72+
on = setNames("id", id_col)
73+
]
6674
}
6775

68-
mz_input[, ProteinName := compound]
6976
mz_input[, PeptideSequence := as.character(get(id_col))]
7077

7178
long <- data.table::melt(
7279
mz_input,
7380
id.vars = c("ProteinName", "PeptideSequence"),
7481
measure.vars = peak_area_cols,
75-
variable.name = "sample_col",
82+
variable.name = "Run",
7683
value.name = "Intensity",
7784
variable.factor = FALSE)
7885

7986
long[, PrecursorCharge := NA_integer_]
8087
long[, FragmentIon := NA_character_]
8188
long[, ProductCharge := NA_integer_]
82-
long[, IsotopeLabelType := "Light"]
83-
long[, Run := sub(paste0(peak_area_suffix, "$"), "", sample_col)]
89+
long[, Run := sub(paste0(peak_area_suffix, "$"), "", Run)]
8490

8591
final_cols <- c("ProteinName", "PeptideSequence", "PrecursorCharge",
86-
"FragmentIon", "ProductCharge", "IsotopeLabelType",
92+
"FragmentIon", "ProductCharge",
8793
"Run", "Intensity")
8894
long <- long[, final_cols, with = FALSE]
8995

R/converters_MZMinetoMSstatsFormat.R

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#' features without a matching annotation row fall back to an mz_rt string
1818
#' `paste0(round(mz, 4), "_", round(rt, 2))`. When `NULL`, every feature
1919
#' uses the mz_rt fallback.
20-
#' @param removeProtein_with1Feature `TRUE` will remove proteins (compounds)
21-
#' represented by a single feature. Default `FALSE`.
22-
#' @param summaryforMultipleRows `max` (default) or `sum` — used when multiple
23-
#' rows map to the same feature/run combination.
2420
#'
2521
#' @return data.table in the MSstats required format.
2622
#'
@@ -60,10 +56,7 @@ MZMinetoMSstatsFormat = function(
6056
input, mzmine_annotations = mzmine_annotations)
6157
annotation = MSstatsConvert::MSstatsMakeAnnotation(input, annotation)
6258

63-
feature_columns = c("PeptideSequence", "PrecursorCharge",
64-
"FragmentIon", "ProductCharge")
65-
fill_isotope_label_type = if ("IsotopeLabelType" %in% colnames(input))
66-
list() else list("IsotopeLabelType" = "Light")
59+
feature_columns = c("PeptideSequence", "PrecursorCharge", "FragmentIon", "ProductCharge")
6760

6861
input = MSstatsConvert::MSstatsPreprocess(
6962
input,
@@ -77,7 +70,7 @@ MZMinetoMSstatsFormat = function(
7770
feature_cleaning = list(
7871
remove_features_with_few_measurements = FALSE,
7972
summarize_multiple_psms = summaryforMultipleRows),
80-
columns_to_fill = c(list(Fraction = 1), fill_isotope_label_type))
73+
columns_to_fill = list(Fraction = 1, IsotopeLabelType = "Light"))
8174
input[, Intensity := ifelse(Intensity == 0, NA, Intensity)]
8275

8376
input = MSstatsConvert::MSstatsBalancedDesign(input, feature_columns,

man/MZMinetoMSstatsFormat.Rd

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)