|
| 1 | +#' Clean raw MZMine files |
| 2 | +#' |
| 3 | +#' Operates on the column names produced by MZMine after MSstatsConvert's |
| 4 | +#' internal column-name standardization (spaces collapsed and dots removed): |
| 5 | +#' "row ID" becomes `rowID`, and each "<sample> Peak area" becomes |
| 6 | +#' `<standardized-sample>Peakarea`. |
| 7 | +#' |
| 8 | +#' @param msstats_object an object of class `MSstatsMZMineFiles`. |
| 9 | +#' @param mzmine_annotations `data.frame` of MZMine spectral-library |
| 10 | +#' annotations with columns `id`, `compound_name`, `score`. Required; |
| 11 | +#' passing `NULL` raises an error. The highest-scoring `compound_name` |
| 12 | +#' per feature is used as `ProteinName`, and features in the quant |
| 13 | +#' table with no matching annotation row are dropped from the output. |
| 14 | +#' These are MSI Level 2 annotations (putative identification via |
| 15 | +#' MS/MS spectral matching). See the public `MZMinetoMSstatsFormat` |
| 16 | +#' docstring for the full scope discussion. |
| 17 | +#' @return data.table |
| 18 | +#' @keywords internal |
| 19 | +.cleanRawMZMine <- function(msstats_object, mzmine_annotations) { |
| 20 | + ProteinName = PeptideSequence = Intensity = Run = NULL |
| 21 | + PrecursorCharge = FragmentIon = ProductCharge = NULL |
| 22 | + id = score = compound_name = i.compound_name = NULL |
| 23 | + |
| 24 | + mz_input = getInputFile(msstats_object, "input") |
| 25 | + mz_input = data.table::as.data.table(mz_input) |
| 26 | + |
| 27 | + peak_area_suffix <- "Peakarea" |
| 28 | + peak_area_cols <- grep(paste0(peak_area_suffix, "$"), |
| 29 | + colnames(mz_input), value = TRUE) |
| 30 | + if (length(peak_area_cols) == 0) { |
| 31 | + stop("No 'Peak area' columns found in the input. Expected per-sample ", |
| 32 | + "columns named '<run> Peak area' (e.g. 'sampleA.mzML Peak area').") |
| 33 | + } |
| 34 | + id_col <- "rowID" |
| 35 | + required_meta <- id_col |
| 36 | + missing_meta <- setdiff(required_meta, colnames(mz_input)) |
| 37 | + if (length(missing_meta) > 0) { |
| 38 | + stop("Missing required MZMine metadata column (expected 'row ID'). ", |
| 39 | + "After standardization, looked for: ", |
| 40 | + paste(missing_meta, collapse = ", "), ".") |
| 41 | + } |
| 42 | + |
| 43 | + if (is.null(mzmine_annotations)) { |
| 44 | + stop("mzmine_annotations is required. Pass a data.frame with ", |
| 45 | + "columns 'id', 'compound_name', 'score'.") |
| 46 | + } |
| 47 | + feature_to_compound <- data.table::as.data.table(mzmine_annotations) |
| 48 | + required_ann <- c("id", "compound_name", "score") |
| 49 | + missing_ann <- setdiff(required_ann, colnames(feature_to_compound)) |
| 50 | + if (length(missing_ann) > 0) { |
| 51 | + stop("mzmine_annotations is missing required column(s): ", |
| 52 | + paste(missing_ann, collapse = ", "), ".") |
| 53 | + } |
| 54 | + feature_to_compound[, score := suppressWarnings(as.numeric(score))] |
| 55 | + if (anyNA(feature_to_compound$score)) { |
| 56 | + stop("The 'score' column in the mzmine annotations file must contain numeric values.") |
| 57 | + } |
| 58 | + data.table::setorder(feature_to_compound, id, -score) |
| 59 | + feature_to_compound <- unique(feature_to_compound, by = "id") |
| 60 | + # Inner-join filter: drop quant rows with no matching annotation. |
| 61 | + mz_input[ |
| 62 | + feature_to_compound, |
| 63 | + ProteinName := i.compound_name, |
| 64 | + on = setNames("id", id_col) |
| 65 | + ] |
| 66 | + mz_input <- mz_input[!is.na(ProteinName)] |
| 67 | + |
| 68 | + retained_ids <- feature_to_compound$id |
| 69 | + retained_msg <- paste0("** MZMine: retained ", length(retained_ids), |
| 70 | + " feature(s) after annotation join: ", |
| 71 | + paste(retained_ids, collapse = ", ")) |
| 72 | + getOption("MSstatsLog")("INFO", retained_msg) |
| 73 | + getOption("MSstatsMsg")("INFO", retained_msg) |
| 74 | + |
| 75 | + mz_input[, PeptideSequence := as.character(get(id_col))] |
| 76 | + |
| 77 | + long <- data.table::melt( |
| 78 | + mz_input, |
| 79 | + id.vars = c("ProteinName", "PeptideSequence"), |
| 80 | + measure.vars = peak_area_cols, |
| 81 | + variable.name = "Run", |
| 82 | + value.name = "Intensity", |
| 83 | + variable.factor = FALSE) |
| 84 | + |
| 85 | + long[, PrecursorCharge := NA_integer_] |
| 86 | + long[, FragmentIon := NA_character_] |
| 87 | + long[, ProductCharge := NA_integer_] |
| 88 | + long[, Run := sub(paste0(peak_area_suffix, "$"), "", Run)] |
| 89 | + |
| 90 | + final_cols <- c("ProteinName", "PeptideSequence", "PrecursorCharge", |
| 91 | + "FragmentIon", "ProductCharge", |
| 92 | + "Run", "Intensity") |
| 93 | + long <- long[, final_cols, with = FALSE] |
| 94 | + |
| 95 | + .logSuccess("MZMine", "clean") |
| 96 | + long |
| 97 | +} |
0 commit comments