| |
| |
| |
| |
|
|
| suppressPackageStartupMessages({ |
| library(MSstats) |
| }) |
|
|
| args <- commandArgs(trailingOnly = TRUE) |
| aligned_file <- args[1] |
| sample_sheet_file <- args[2] |
| output_dir <- args[3] |
|
|
| dir.create(output_dir, recursive = TRUE, showWarnings = FALSE) |
|
|
| cat("=== MSstats Analysis ===\n") |
|
|
| |
| aligned <- read.delim(aligned_file, stringsAsFactors = FALSE) |
| cat("Aligned features:", nrow(aligned), "\n") |
|
|
| |
| sample_sheet <- read.delim(sample_sheet_file, stringsAsFactors = FALSE) |
| cat("Sample sheet:\n") |
| print(sample_sheet) |
|
|
| |
| |
| |
|
|
| |
| tryCatch({ |
| |
| cat("Aligned columns:", paste(head(names(aligned), 20), collapse=", "), "\n") |
|
|
| |
| |
| if ("ProteinName" %in% names(aligned) && "filename" %in% names(aligned)) { |
| |
| run_to_condition <- setNames(sample_sheet$Condition, sample_sheet$Sample) |
|
|
| |
| msstats_input <- data.frame( |
| ProteinName = aligned$ProteinName, |
| PeptideSequence = if ("FullPeptideName" %in% names(aligned)) aligned$FullPeptideName else aligned$Sequence, |
| PrecursorCharge = if ("Charge" %in% names(aligned)) aligned$Charge else 2, |
| FragmentIon = if ("aggr_Fragment_Annotation" %in% names(aligned)) aligned$aggr_Fragment_Annotation else "y", |
| ProductCharge = 1, |
| IsotopeLabelType = "L", |
| Condition = sapply(aligned$filename, function(fn) { |
| |
| matched <- which(sapply(sample_sheet$Sample, function(s) grepl(s, fn, fixed=TRUE))) |
| if (length(matched) > 0) sample_sheet$Condition[matched[1]] else "Unknown" |
| }), |
| BioReplicate = aligned$filename, |
| Run = aligned$filename, |
| Intensity = if ("Intensity" %in% names(aligned)) aligned$Intensity else |
| if ("m_score" %in% names(aligned)) 10^6 else 0, |
| stringsAsFactors = FALSE |
| ) |
|
|
| cat("MSstats input rows:", nrow(msstats_input), "\n") |
| cat("Conditions:", paste(unique(msstats_input$Condition), collapse=", "), "\n") |
|
|
| |
| processed <- dataProcess(msstats_input, logTrans = 2, normalization = "equalizeMedians") |
|
|
| |
| conditions <- unique(msstats_input$Condition) |
| conditions <- conditions[conditions != "Unknown"] |
|
|
| if (length(conditions) >= 2) { |
| contrast_matrix <- matrix(c(1, -1), nrow=1) |
| colnames(contrast_matrix) <- conditions[1:2] |
| rownames(contrast_matrix) <- paste(conditions[1], "vs", conditions[2]) |
|
|
| result <- groupComparison(contrast.matrix = contrast_matrix, data = processed) |
| comparison <- result$ComparisonResult |
|
|
| write.csv(comparison, file.path(output_dir, "msstats_results.csv"), row.names = FALSE) |
| cat("DE proteins (adj.pvalue < 0.05):", sum(comparison$adj.pvalue < 0.05, na.rm=TRUE), "\n") |
| } else { |
| cat("Only one condition found, skipping differential analysis\n") |
| |
| protein_summary <- data.frame( |
| Protein = unique(msstats_input$ProteinName), |
| adj.pvalue = NA |
| ) |
| write.csv(protein_summary, file.path(output_dir, "msstats_results.csv"), row.names = FALSE) |
| } |
| } else { |
| cat("Required columns not found in aligned file\n") |
| cat("Available columns:", paste(names(aligned), collapse=", "), "\n") |
| |
| write.csv(data.frame(Protein=character(0), adj.pvalue=numeric(0)), |
| file.path(output_dir, "msstats_results.csv"), row.names = FALSE) |
| } |
| }, error = function(e) { |
| cat("MSstats error:", conditionMessage(e), "\n") |
| write.csv(data.frame(Protein=character(0), adj.pvalue=numeric(0)), |
| file.path(output_dir, "msstats_results.csv"), row.names = FALSE) |
| }) |
|
|
| cat("=== MSstats complete ===\n") |
|
|