lingzhi227 commited on
Commit
8a54029
·
verified ·
1 Parent(s): 9a534df

Upload tasks/dia-proteomics/scripts/msstats_analysis.R with huggingface_hub

Browse files
tasks/dia-proteomics/scripts/msstats_analysis.R ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env Rscript
2
+ # MSstats analysis for DIA proteomics data
3
+ # Input: TRIC-aligned feature file + sample sheet
4
+ # Output: differential expression results
5
+
6
+ suppressPackageStartupMessages({
7
+ library(MSstats)
8
+ })
9
+
10
+ args <- commandArgs(trailingOnly = TRUE)
11
+ aligned_file <- args[1]
12
+ sample_sheet_file <- args[2]
13
+ output_dir <- args[3]
14
+
15
+ dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
16
+
17
+ cat("=== MSstats Analysis ===\n")
18
+
19
+ # Read aligned features from TRIC
20
+ aligned <- read.delim(aligned_file, stringsAsFactors = FALSE)
21
+ cat("Aligned features:", nrow(aligned), "\n")
22
+
23
+ # Read sample sheet
24
+ sample_sheet <- read.delim(sample_sheet_file, stringsAsFactors = FALSE)
25
+ cat("Sample sheet:\n")
26
+ print(sample_sheet)
27
+
28
+ # Prepare MSstats input format
29
+ # The aligned file from TRIC has columns like:
30
+ # transition_group_id, run_id, Intensity, etc.
31
+
32
+ # Map run names to conditions from sample sheet
33
+ tryCatch({
34
+ # Check available columns
35
+ cat("Aligned columns:", paste(head(names(aligned), 20), collapse=", "), "\n")
36
+
37
+ # Try to build MSstats-compatible input
38
+ # OpenSWATH/TRIC output needs conversion
39
+ if ("ProteinName" %in% names(aligned) && "filename" %in% names(aligned)) {
40
+ # Create condition mapping
41
+ run_to_condition <- setNames(sample_sheet$Condition, sample_sheet$Sample)
42
+
43
+ # Build MSstats input
44
+ msstats_input <- data.frame(
45
+ ProteinName = aligned$ProteinName,
46
+ PeptideSequence = if ("FullPeptideName" %in% names(aligned)) aligned$FullPeptideName else aligned$Sequence,
47
+ PrecursorCharge = if ("Charge" %in% names(aligned)) aligned$Charge else 2,
48
+ FragmentIon = if ("aggr_Fragment_Annotation" %in% names(aligned)) aligned$aggr_Fragment_Annotation else "y",
49
+ ProductCharge = 1,
50
+ IsotopeLabelType = "L",
51
+ Condition = sapply(aligned$filename, function(fn) {
52
+ # Match filename to sample sheet
53
+ matched <- which(sapply(sample_sheet$Sample, function(s) grepl(s, fn, fixed=TRUE)))
54
+ if (length(matched) > 0) sample_sheet$Condition[matched[1]] else "Unknown"
55
+ }),
56
+ BioReplicate = aligned$filename,
57
+ Run = aligned$filename,
58
+ Intensity = if ("Intensity" %in% names(aligned)) aligned$Intensity else
59
+ if ("m_score" %in% names(aligned)) 10^6 else 0,
60
+ stringsAsFactors = FALSE
61
+ )
62
+
63
+ cat("MSstats input rows:", nrow(msstats_input), "\n")
64
+ cat("Conditions:", paste(unique(msstats_input$Condition), collapse=", "), "\n")
65
+
66
+ # Run MSstats
67
+ processed <- dataProcess(msstats_input, logTrans = 2, normalization = "equalizeMedians")
68
+
69
+ # Check if we have 2+ conditions
70
+ conditions <- unique(msstats_input$Condition)
71
+ conditions <- conditions[conditions != "Unknown"]
72
+
73
+ if (length(conditions) >= 2) {
74
+ contrast_matrix <- matrix(c(1, -1), nrow=1)
75
+ colnames(contrast_matrix) <- conditions[1:2]
76
+ rownames(contrast_matrix) <- paste(conditions[1], "vs", conditions[2])
77
+
78
+ result <- groupComparison(contrast.matrix = contrast_matrix, data = processed)
79
+ comparison <- result$ComparisonResult
80
+
81
+ write.csv(comparison, file.path(output_dir, "msstats_results.csv"), row.names = FALSE)
82
+ cat("DE proteins (adj.pvalue < 0.05):", sum(comparison$adj.pvalue < 0.05, na.rm=TRUE), "\n")
83
+ } else {
84
+ cat("Only one condition found, skipping differential analysis\n")
85
+ # Write protein-level summary instead
86
+ protein_summary <- data.frame(
87
+ Protein = unique(msstats_input$ProteinName),
88
+ adj.pvalue = NA
89
+ )
90
+ write.csv(protein_summary, file.path(output_dir, "msstats_results.csv"), row.names = FALSE)
91
+ }
92
+ } else {
93
+ cat("Required columns not found in aligned file\n")
94
+ cat("Available columns:", paste(names(aligned), collapse=", "), "\n")
95
+ # Write placeholder
96
+ write.csv(data.frame(Protein=character(0), adj.pvalue=numeric(0)),
97
+ file.path(output_dir, "msstats_results.csv"), row.names = FALSE)
98
+ }
99
+ }, error = function(e) {
100
+ cat("MSstats error:", conditionMessage(e), "\n")
101
+ write.csv(data.frame(Protein=character(0), adj.pvalue=numeric(0)),
102
+ file.path(output_dir, "msstats_results.csv"), row.names = FALSE)
103
+ })
104
+
105
+ cat("=== MSstats complete ===\n")