Skip to content

Commit 2b96a3d

Browse files
authored
tests(anomaly-models): Fix unit tests around anomaly models (#123)
1 parent 5360aaf commit 2b96a3d

4 files changed

Lines changed: 58 additions & 22 deletions

File tree

R/clean_DIANN.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
getOption("MSstatsLog")("INFO", msg)
176176
getOption("MSstatsMsg")("INFO", msg)
177177

178-
dn_input = dn_input[QValue >= global_qvalue_cutoff, quantificationColumn := 0]
178+
dn_input = dn_input[QValue >= global_qvalue_cutoff, (quantificationColumn) := 0]
179179
if (MBR) {
180180
msg = '** MBR was used to analyze the data. Now setting names and filtering'
181181
msg_1_mbr = paste0('-- LibPGQValue < ', pg_qvalue_cutoff)

inst/tinytest/test_clean_DIANN.R

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ output = MSstatsConvert:::.cleanRawDIANN(input, quantificationColumn = "Fragment
2626
.validateOutput(output)
2727

2828
# Q-value filtering
29-
output = MSstatsConvert:::.cleanRawDIANN(input, global_qvalue_cutoff = 0.005)
30-
expect_equal(sum(output$DetectionQValue < 0.005), nrow(output))
31-
output = MSstatsConvert:::.cleanRawDIANN(input, qvalue_cutoff = 0.00001)
32-
expect_equal(sum(output$LibQValue < 0.00001), nrow(output))
33-
output = MSstatsConvert:::.cleanRawDIANN(input, pg_qvalue_cutoff = 0.001)
34-
expect_equal(sum(output$LibPGQValue < 0.001), nrow(output))
35-
output = MSstatsConvert:::.cleanRawDIANN(input, MBR = TRUE, qvalue_cutoff = 0.005)
36-
expect_equal(sum(output$LibQValue < 0.005), nrow(output))
37-
output = MSstatsConvert:::.cleanRawDIANN(input, MBR = TRUE, pg_qvalue_cutoff = 0.001)
38-
expect_equal(sum(output$LibPGQValue < 0.001), nrow(output))
29+
expect_qvalue_cutoff <- function(output, col, cutoff) {
30+
expect_equal(
31+
sum(output[[col]] > cutoff),
32+
sum(output[["Intensity"]] == 0 & output[[col]] > cutoff),
33+
info = sprintf(
34+
"All rows with %s > %s should have %s == 0",
35+
col, cutoff, "Intensity"
36+
)
37+
)
38+
expect_equal(
39+
sum(output[[col]] <= cutoff),
40+
nrow(output) - sum(output[[col]] > cutoff),
41+
info = sprintf(
42+
"Rows with %s <= %s should account for all rows not above the cutoff",
43+
col, cutoff
44+
)
45+
)
46+
}
47+
output <- MSstatsConvert:::.cleanRawDIANN(input, global_qvalue_cutoff = 0.005)
48+
expect_qvalue_cutoff(output, "DetectionQValue", 0.005)
49+
output <- MSstatsConvert:::.cleanRawDIANN(input, qvalue_cutoff = 0.00001)
50+
expect_qvalue_cutoff(output, "LibQValue", 0.00001)
51+
output <- MSstatsConvert:::.cleanRawDIANN(input, pg_qvalue_cutoff = 0.001)
52+
expect_qvalue_cutoff(output, "LibPGQValue", 0.001)
53+
output <- MSstatsConvert:::.cleanRawDIANN(input, MBR = FALSE, qvalue_cutoff = 0.001)
54+
expect_qvalue_cutoff(output, "GlobalQValue", 0.001)
55+
output <- MSstatsConvert:::.cleanRawDIANN(input, MBR = FALSE, pg_qvalue_cutoff = 0.0002)
56+
expect_qvalue_cutoff(output, "GlobalPGQValue", 0.0002)

inst/tinytest/test_converters_DIANNtoMSstatsFormat.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ input = data.table::fread(input_file_path)
55
annot = data.table::fread(annotation_file_path)
66
output = DIANNtoMSstatsFormat(input, annotation = annot, MBR = FALSE, use_log_file = FALSE)
77
expect_equal(ncol(output), 11)
8-
expect_equal(nrow(output), 174)
8+
expect_equal(nrow(output), 348)
99
expect_true("Run" %in% colnames(output))
1010
expect_true("ProteinName" %in% colnames(output))
1111
expect_true("PeptideSequence" %in% colnames(output))
@@ -25,7 +25,7 @@ input = arrow::read_parquet(input_file_path)
2525
annot = data.table::fread(annotation_file_path)
2626
output = DIANNtoMSstatsFormat(input, annotation = annot, MBR = FALSE, use_log_file = FALSE, quantificationColumn = 'auto')
2727
expect_equal(ncol(output), 11)
28-
expect_equal(nrow(output), 180)
28+
expect_equal(nrow(output), 192)
2929
expect_true("Run" %in% colnames(output))
3030
expect_true("ProteinName" %in% colnames(output))
3131
expect_true("PeptideSequence" %in% colnames(output))

inst/tinytest/test_utils_anomaly_score.R

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ baseline_scores = run_quality_metrics(
3737
# Data with progressively higher cumulative sums
3838
high_scores = run_quality_metrics(
3939
base_df_10,
40-
c(rep(0.1, 5), seq(2.0, 5.0, length.out = 5)), # mean_increase
41-
c(rep(0.1, 5), seq(2.0, 5.0, length.out = 5)), # mean_decrease
42-
c(rep(0.1, 5), seq(2.0, 5.0, length.out = 5)) # dispersion_increase
40+
c(seq(0, 0.1, length.out = 5), seq(2.0, 5.0, length.out = 5)), # mean_increase
41+
c(seq(0, 0.1, length.out = 5), seq(2.0, 5.0, length.out = 5)), # mean_decrease
42+
c(seq(0, 0.1, length.out = 5), seq(2.0, 5.0, length.out = 5)) # dispersion_increase
4343
)
4444

4545
# The last 5 rows (with high values) should have higher mean anomaly scores
@@ -51,9 +51,9 @@ base_df_20 = create_base_df(20)
5151

5252
extreme_scores = run_quality_metrics(
5353
base_df_20,
54-
c(rep(0.1, 19), 10.0), # Last value is extreme
55-
c(rep(0.1, 19), 8.0), # Last value is extreme
56-
c(rep(0.1, 19), 12.0) # Last value is extreme
54+
c(seq(0, 0.1, length.out = 19), 10.0), # Last value is extreme
55+
c(seq(0, 0.1, length.out = 19), 8.0), # Last value is extreme
56+
c(seq(0, 0.1, length.out = 19), 12.0) # Last value is extreme
5757
)
5858

5959
# The extreme outlier (last row) should have the highest anomaly score
@@ -267,9 +267,9 @@ base_df_6_rank = create_base_df(6)
267267
# Create data with obvious ranking: Row 6 > Row 5 > Row 4 > Rows 1,2,3
268268
ranking_scores = run_quality_metrics(
269269
base_df_6_rank,
270-
c(0.1, 0.1, 0.1, 1.0, 2.0, 5.0),
271-
c(0.1, 0.1, 0.1, 1.0, 2.0, 5.0),
272-
c(0.1, 0.1, 0.1, 1.0, 2.0, 5.0)
270+
c(0.1, 0.11, 0.12, 1.0, 2.0, 5.0),
271+
c(0.1, 0.11, 0.12, 1.0, 2.0, 5.0),
272+
c(0.1, 0.11, 0.12, 1.0, 2.0, 5.0)
273273
)
274274

275275
# Row 5 should have highest score, Row 4 second highest, etc.
@@ -367,3 +367,21 @@ low_abundance_excluded = MSstatsConvert:::.prepareSpectronautAnomalyInput(
367367
missing_run_count = 0.95)
368368
expect_true("AFPLAEWQPSDVDQR" %in% low_abundance_excluded$PeptideSequence)
369369
expect_false("LowAbundancePeptide" %in% low_abundance_excluded$PeptideSequence)
370+
371+
372+
# Test 11: Testing duplicity of quality metrics, applicable considering
373+
# multiple fragments share the same precursor level metrics
374+
375+
# Data with progressively higher cumulative sums
376+
duplicate_metrics = run_quality_metrics(
377+
base_df_10,
378+
c(rep(0.1, 5), seq(2.0, 4.0, length.out = 5)), # mean_increase
379+
c(rep(0.1, 5), seq(2.0, 4.0, length.out = 5)), # mean_decrease
380+
c(rep(0.1, 5), seq(2.0, 4.0, length.out = 5)) # dispersion_increase
381+
)
382+
383+
# The last 5 rows (with high values) should have lower mean anomaly scores
384+
# Since they are all clumped between 2 and 4, whereas 0.1 is by itself
385+
expect_true(mean(duplicate_metrics$AnomalyScores[6:10]) < mean(duplicate_metrics$AnomalyScores[1:5]),
386+
info = "Rows 6-10 (values clumped 2-4) should have lower
387+
anomaly scores than rows 1-5 (isolated value of 0.1)")

0 commit comments

Comments
 (0)