1+ # Test for .normalizeGlobalStandards function ----------------------------------
2+
3+ # Setup test data ---------------------------------------------------------------
4+
5+ create_peptide_dictionary <- function () {
6+ data.table :: data.table(
7+ PeptideSequence = c(" AAAAAAAAAAAAAAGAGAGAK" , " AAAAAAAAAAAAAAGAGAGAK" , " AAAAAAAAAAAAVSRD" ),
8+ PrecursorCharge = c(3 , 2 , 3 ),
9+ PEPTIDE = c(" AAAAAAAAAAAAAAGAGAGAK_3" , " AAAAAAAAAAAAAAGAGAGAK_2" , " AAAAAAAAAAAAVSRD_3" )
10+ )
11+ }
12+
13+ create_test_input <- function (standard_intensities , peptide2_intensities , peptide3_intensities ) {
14+ # Constants
15+ n_proteins <- 3
16+ n_runs <- 48
17+ n_subjects <- 4
18+ n_fractions <- 6
19+
20+ # Create base structure
21+ input <- data.table :: data.table(
22+ PROTEIN = rep(c(" P1" , " P1" , " P3" ), each = n_runs ),
23+ PEPTIDE = rep(c(" AAAAAAAAAAAAAAGAGAGAK_3" , " AAAAAAAAAAAAAAGAGAGAK_2" , " AAAAAAAAAAAAVSRD_3" ),
24+ each = n_runs ),
25+ TRANSITION = rep(" NA_NA" , n_proteins * n_runs ),
26+ FEATURE = rep(c(" AAAAAAAAAAAAAAGAGAGAK_3_NA_NA" ,
27+ " AAAAAAAAAAAAAAGAGAGAK_2_NA_NA" ,
28+ " AAAAAAAAAAAAVSRD_3_NA_NA" ),
29+ each = n_runs ),
30+ LABEL = rep(" L" , n_proteins * n_runs ),
31+ GROUP_ORIGINAL = rep(rep(c(" Control" , " Treatment" ), each = n_runs / 2 ), n_proteins ),
32+ SUBJECT_ORIGINAL = rep(paste0(" Subject" , rep(1 : n_subjects , each = n_fractions )),
33+ n_proteins * 2 ),
34+ RUN = rep(1 : n_runs , n_proteins ),
35+ GROUP = rep(rep(c(" Control" , " Treatment" ), each = n_runs / 2 ), n_proteins ),
36+ SUBJECT = rep(paste0(" Subject" , rep(1 : n_subjects , each = n_fractions )),
37+ n_proteins * 2 ),
38+ FRACTION = rep(rep(1 : n_fractions , n_subjects ), n_proteins * 2 ),
39+ INTENSITY = c(standard_intensities , peptide2_intensities , peptide3_intensities ),
40+ ANOMALYSCORES = rep(NA , n_proteins * n_runs ),
41+ originalRUN = rep(paste0(" Run" , 1 : n_runs ), n_proteins )
42+ )
43+
44+ input [, ABUNDANCE : = log2(INTENSITY )]
45+ return (input )
46+ }
47+
48+ # Test 1: Standards with different intensities between groups -------------------
49+ test_different_group_intensities <- function () {
50+ peptide_dict <- create_peptide_dictionary()
51+ standards <- c(" AAAAAAAAAAAAAAGAGAGAK" )
52+
53+ # Control group: 262144, Treatment group: 524288
54+ standard_intensities <- c(
55+ rep(262144 , 24 ), # Control (runs 1-24)
56+ rep(524288 , 24 ) # Treatment (runs 25-48)
57+ )
58+
59+ # Non-standard peptides: all 262144
60+ peptide3_intensities <- rep(262144 , 48 )
61+
62+ input <- create_test_input(standard_intensities , standard_intensities , peptide3_intensities )
63+ output <- MSstats ::: .normalizeGlobalStandards(input , peptide_dict , standards )
64+
65+ # Verify normalization: Control runs should be shifted up, Treatment runs shifted down
66+ control_runs <- 1 : 24
67+ treatment_runs <- 25 : 48
68+
69+ # Check Control group (shifted up to match treatment standard)
70+ control_abundance <- output [RUN %in% control_runs &
71+ ! is.na(ABUNDANCE ) &
72+ ! grepl(standards , PEPTIDE )]$ ABUNDANCE
73+ expect_true(all(abs(control_abundance - 18.5 ) < 1e-10 ),
74+ info = " Control group non-standard peptides should be normalized to 18.5" )
75+
76+ # Check Treatment group (shifted down to match control standard)
77+ treatment_abundance <- output [RUN %in% treatment_runs &
78+ ! is.na(ABUNDANCE ) &
79+ ! grepl(standards , PEPTIDE )]$ ABUNDANCE
80+ expect_true(all(abs(treatment_abundance - 17.5 ) < 1e-10 ),
81+ info = " Treatment group non-standard peptides should be normalized to 17.5" )
82+ }
83+
84+ # Test 2: Standards with alternating intensities within fractions ---------------
85+ test_alternating_intensities_within_fractions <- function () {
86+ peptide_dict <- create_peptide_dictionary()
87+ standards <- c(" AAAAAAAAAAAAAAGAGAGAK" )
88+
89+ # Standard alternates between 262144 and 524288 within each fraction
90+ standard_intensities <- rep(c(262144 , 524288 ), 24 )
91+
92+ # Non-standard peptides: all 262144
93+ peptide3_intensities <- rep(262144 , 48 )
94+
95+ input <- create_test_input(standard_intensities , standard_intensities , peptide3_intensities )
96+ output <- MSstats ::: .normalizeGlobalStandards(input , peptide_dict , standards )
97+
98+ # When standards vary within fractions but average to same level,
99+ # no net normalization should occur
100+ all_runs <- 1 : 48
101+ normalized_abundance <- output [RUN %in% all_runs &
102+ ! is.na(ABUNDANCE ) &
103+ ! grepl(standards , PEPTIDE )]$ ABUNDANCE
104+
105+ expect_true(all(abs(normalized_abundance - 18 ) < 1e-10 ),
106+ info = " No normalization should occur when standard averages are equal across fractions" )
107+ }
108+
109+ # Run tests ---------------------------------------------------------------------
110+ test_different_group_intensities()
111+ test_alternating_intensities_within_fractions()
0 commit comments