Skip to content

Commit 4230512

Browse files
committed
Add more unit tests
1 parent 90aaf40 commit 4230512

8 files changed

Lines changed: 241 additions & 1 deletion

R/checkArgumentsFragPipe.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
if (length(fpConfigFile) > 1) {
5555
stop("There are more than one config file in the FragPipe directory")
5656
}
57+
fpWorkflowFile <- list.files(fragpipeDir, pattern = "^fragpipe.*.workflow$",
58+
full.names = TRUE)
59+
if (length(fpWorkflowFile) > 1) {
60+
stop("There are more than one workflow file in the FragPipe directory")
61+
}
5762
fpLogFile <- list.files(fragpipeDir, pattern = "^log_.+.txt$",
5863
full.names = TRUE)
5964
if (length(fpLogFile) > 1) {

tests/testthat/test-checkArgumentsFragPipe.R

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_that("argument checking for FP works", {
1616
idCol = function(df) combineIds(df, combineCols = c("Gene", "Protein.ID")),
1717
labelCol = function(df) combineIds(df, combineCols = c("Gene", "Protein.ID")),
1818
geneIdCol = function(df) getFirstId(df, colName = "Gene"),
19-
proteinIdCol = "Protein.ID",
19+
proteinIdCol = function(df) getFirstId(df, colName = "Protein.ID"),
2020
stringIdCol = function(df) combineIds(df, combineCols = c("Gene", "Protein.ID"),
2121
combineWhen = "missing", makeUnique = FALSE),
2222
iColPattern = "\\.MaxLFQ\\.Intensity$",
@@ -167,6 +167,26 @@ test_that("argument checking for FP works", {
167167
expect_error(do.call(.checkArgumentsFragPipe, args),
168168
"The file missing/combined_protein.tsv doesn't exist")
169169

170+
## Multiple log files
171+
dir.create(file.path(tempdir(), "fragpipe_dir_temp"))
172+
file.copy(system.file("extdata", "fp_example", package = "einprot"),
173+
file.path(tempdir(), "fragpipe_dir_temp"), recursive = TRUE)
174+
file.copy(file.path(tempdir(), "fragpipe_dir_temp", "fp_example",
175+
"log_2023-04-12_20-12-46.txt"),
176+
file.path(tempdir(), "fragpipe_dir_temp", "fp_example",
177+
"log_2024-04-12_20-12-46.txt"))
178+
args <- args0
179+
args$fragpipeDir <- file.path(tempdir(), "fragpipe_dir_temp", "fp_example")
180+
expect_error(do.call(.checkArgumentsFragPipe, args),
181+
"There are more than one log file")
182+
183+
file.copy(file.path(tempdir(), "fragpipe_dir_temp", "fp_example",
184+
"fragpipe.workflow"),
185+
file.path(tempdir(), "fragpipe_dir_temp", "fp_example",
186+
"fragpipe2.workflow"))
187+
expect_error(do.call(.checkArgumentsFragPipe, args),
188+
"There are more than one workflow file")
189+
170190
## idCol
171191
args <- args0
172192
args$idCol <- 1
@@ -190,6 +210,9 @@ test_that("argument checking for FP works", {
190210
args$proteinIdCol <- 1
191211
expect_error(do.call(.checkArgumentsFragPipe, args),
192212
"'proteinIdCol' must be of class 'character'")
213+
args <- args0
214+
args$proteinIdCol <- "Protein.ID"
215+
expect_null(do.call(.checkArgumentsFragPipe, args))
193216

194217
## stringIdCol
195218
args <- args0

tests/testthat/test-plotMissingValues.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ test_that("missing value plots work", {
4343
expect_s3_class(out, "ggplot")
4444
expect_named(out$data, c("sample", "nNA", "pNA", "assay"))
4545

46+
out <- plotFractionDetectedPerSample(
47+
dfNA = DataFrame(as.data.frame(nbr_na_mq$nNAcols) %>%
48+
dplyr::rename(sample = name)))
49+
expect_s3_class(out, "ggplot")
50+
expect_named(out$data, c("sample", "nNA", "pNA", "assay"))
51+
4652
## --------------------------------------------------------------------- ##
4753
## plotDetectedInSamples
4854
## --------------------------------------------------------------------- ##
@@ -66,6 +72,15 @@ test_that("missing value plots work", {
6672
}
6773
expect_equal(levels(out$data$nObs), as.character(c(0, seq_len(9))))
6874

75+
out <- plotDetectedInSamples(dfNA = DataFrame(nbr_na_mq$nNArows))
76+
expect_s3_class(out, "ggplot")
77+
expect_named(out$data, c("nNA", "n", "nObs"))
78+
for (i in c(0, seq_len(9))) {
79+
expect_equal(sum(nbr_na_mq$nNArows$nNA == 9 - i),
80+
out$data$n[out$data$nObs == i])
81+
}
82+
expect_equal(levels(out$data$nObs), as.character(c(0, seq_len(9))))
83+
6984
## PD data
7085
out <- plotDetectedInSamples(dfNA = as.data.frame(nbr_na_pd$nNArows))
7186
expect_s3_class(out, "ggplot")

tests/testthat/test-prepareFinalSCE.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ test_that("assembling the SCE works", {
2727
expType = "ProteomeDiscoverer"
2828
)
2929

30+
args0_fp <- list(
31+
sce = sce_fp_final,
32+
baseFileName = tempfile(),
33+
featureCollections = fcoll_fp_final,
34+
expType = "FragPipe"
35+
)
36+
3037
## Fail with wrong arguments
3138
## --------------------------------------------------------------------- ##
3239
## sce
@@ -185,4 +192,39 @@ test_that("assembling the SCE works", {
185192
expect_equal(SummarizedExperiment::rowData(sce)$Gene.Symbol,
186193
SummarizedExperiment::rowData(args0_pd$sce)$Gene.Symbol)
187194

195+
## Works with correct arguments - FragPipe
196+
## --------------------------------------------------------------------- ##
197+
sce <- do.call(prepareFinalSCE, args0_fp)
198+
expect_s4_class(sce, "SingleCellExperiment")
199+
expect_equal(nrow(sce), 150)
200+
expect_equal(ncol(sce), 9)
201+
expect_true(all(c("MaxLFQ.intensity", "log2_MaxLFQ.intensity",
202+
"log2_MaxLFQ.intensity_withNA", "imputed_MaxLFQ.intensity",
203+
"Unique.spectral.count", "Total.spectral.count",
204+
"Spectral.count", "Intensity") %in%
205+
SummarizedExperiment::assayNames(sce)))
206+
expect_true(all(c("sample", "group") %in%
207+
colnames(SummarizedExperiment::colData(sce))))
208+
expect_true(all(c("Gene", "Protein.ID") %in%
209+
colnames(SummarizedExperiment::rowData(sce))))
210+
expect_false(file.exists(paste0(args0_fp$baseFileName,
211+
"_sce_extra_annots.tsv")))
212+
# tmp <- read.delim(paste0(args0_pd$baseFileName,
213+
# "_sce_extra_annots.tsv"), nrow = 2)
214+
# expect_named(tmp, c("ID", "Proteins.Unique.Sequence.ID",
215+
# "GO.Accessions"))
216+
md <- S4Vectors::metadata(sce)
217+
expect_type(md, "list")
218+
expect_type(md$iSEE$options, "list")
219+
expect_equal(length(md$iSEE$options), 4)
220+
expect_s4_class(md$iSEE$options$iSEEu_FeatureSetTable_collections$complexes,
221+
"CharacterList")
222+
expect_equal(md$iSEE$options$iSEEu_LogFC_Fields, character(0))
223+
expect_equal(md$iSEE$options$iSEEu_AveAb_Fields, character(0))
224+
expect_equal(md$iSEE$options$iSEEu_PValue_Fields, character(0))
225+
expect_equal(SummarizedExperiment::assay(sce, "MaxLFQ.intensity")[, 1],
226+
SummarizedExperiment::assay(args0_fp$sce, "MaxLFQ.intensity")[, 1])
227+
expect_equal(SummarizedExperiment::rowData(sce)$Gene,
228+
SummarizedExperiment::rowData(args0_fp$sce)$Gene)
229+
188230
})

tests/testthat/test-readFragPipeInfo.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,48 @@ test_that("readFragPipeInfo works", {
7979
expect_equal(fp$`Variable modifications`, "M(15.9949), N-term(42.0106)")
8080
expect_equal(fp$`Fixed modifications`, "C(57.0215)")
8181
expect_equal(fp$`Database decoy tag`, "rev_")
82+
83+
## -------------------------------------------------------------------------
84+
## Create file where msfragger.search_enzyme_name_2 is not null
85+
## Copy also workflow file to folder above
86+
file.copy(from = system.file("extdata", "fp_example", "fragpipe.workflow",
87+
package = "einprot"),
88+
to = file.path(tempdir(), "tempfp"))
89+
tmp <- readLines(file.path(tempdir(), "tempfp", "fragpipe.workflow"))
90+
i <- grep("msfragger.search_enzyme_name_2", tmp)
91+
expect_equal(i, 142L)
92+
tmp[i] <- "msfragger.search_enzyme_name_2=trypsin"
93+
writeLines(tmp, file.path(tempdir(), "tempfp", "fragpipe.workflow"))
94+
fp <- readFragPipeInfo(file.path(tempdir(), "tempfp"))
95+
expect_type(fp, "list")
96+
expect_equal(length(fp), 16)
97+
expect_named(fp, c("FragPipe version", "FragPipe parameter file",
98+
"FragPipe log file", "Search engine",
99+
"Raw file location", "Raw files", "Sample names",
100+
"Databases", "Contaminants", "Peptides (ranges)",
101+
"Mass error tolerances", "Quantification settings (LFQ)",
102+
"Enzymes", "Variable modifications",
103+
"Fixed modifications", "Database decoy tag"))
104+
105+
## All entries should be scalar values
106+
expect_true(all(vapply(fp, length, 0) == 1))
107+
108+
## Check individual values
109+
expect_equal(fp$`FragPipe version`, "19.1")
110+
expect_equal(basename(fp$`FragPipe parameter file`), "fragpipe.workflow")
111+
expect_equal(basename(fp$`FragPipe log file`), "log_2023-04-12_20-12-46.txt")
112+
expect_equal(fp$`Search engine`, "MSFragger-3.7")
113+
expect_equal(fp$`Raw file location`, "D:/Data/FUSION")
114+
expect_equal(fp$`Raw files`, "F_160817_AdnpFB_IP06.raw, F_160817_AdnpFB_IP05.raw, F_160817_RBC_ctrl_IP02.raw, F_160817_AdnpFB_IP04.raw, F_160817_RBC_ctrl_IP01.raw, F_160817_Chd4BF_IP09.raw, F_160817_Chd4BF_IP07.raw, F_160817_Chd4BF_IP08.raw, F_160817_RBC_ctrl_IP03.raw")
115+
expect_equal(fp$`Sample names`, "Adnp_IP04, Adnp_IP05, Adnp_IP06, Chd4BF_IP07, Chd4BF_IP08, Chd4BF_IP09, RBC_ctrl_IP01, RBC_ctrl_IP02, RBC_ctrl_IP03")
116+
expect_equal(fp$Databases, "D/://Data//FASTA//2023-04-12-decoys-contam_MOUSE__190410.fasta.fas")
117+
expect_equal(fp$Contaminants, "cRAP")
118+
expect_equal(fp$`Peptides (ranges)`, "length: 7-50 AA; mass: 500-5000 Da")
119+
expect_equal(fp$`Mass error tolerances`, "precursor:-20-20 [ppm]; fragment:0.7 [Da] (after optimization:200 PPM)")
120+
expect_equal(fp$`Quantification settings (LFQ)`, "IonQuant: TRUE, Calculate MaxLFQ intensity: TRUE, Normalization: TRUE, match-between runs (MBR): FALSE, min. ions: 2")
121+
expect_equal(fp$Enzymes, "stricttrypsin[KR, C-terminal, 2 missed cleavages]; trypsin; [, C-terminal, 2 missed cleavages]")
122+
expect_equal(fp$`Variable modifications`, "M(15.9949), N-term(42.0106)")
123+
expect_equal(fp$`Fixed modifications`, "C(57.0215)")
124+
expect_equal(fp$`Database decoy tag`, "rev_")
125+
82126
})

tests/testthat/test-runPDTMTAnalysis.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,22 @@ test_that("runPDTMTAnalysis works", {
808808
"already exists but forceOverwrite = TRUE")
809809
expect_true(file.exists(file.path(outDir, paste0(outBaseName, "_PDTMTqc.pdf"))))
810810

811+
## Not all files present -> no QC plot
812+
dir.create(file.path(outDir, "pdtmt_missing_files"), showWarnings = FALSE,
813+
recursive = TRUE)
814+
file.copy(system.file("extdata", "pdtmt_example",
815+
"Fig2_m23139_RTS_QC_varMods_Proteins.txt",
816+
package = "einprot"),
817+
file.path(outDir, "pdtmt_missing_files"))
818+
args <- args0
819+
args$pdOutputFolder <- file.path(outDir, "pdtmt_missing_files")
820+
args$outputDir <- file.path(outDir, "pdtmt_missing_files")
821+
args$generateQCPlot <- TRUE
822+
expect_warning(res <- do.call(runPDTMTAnalysis, args),
823+
"The following files were not found, will not generate")
824+
expect_false(file.exists(file.path(outDir, "pdtmt_missing_files",
825+
paste0(outBaseName, "_PDTMTqc.pdf"))))
826+
811827
## iColPattern without escaped period
812828
args <- args0
813829
args$forceOverwrite <- TRUE

tests/testthat/test-runPDTMTptmAnalysis.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,15 @@ test_that("runPDTMTptmAnalysis works", {
399399
expect_equal(basename(res), paste0(outBaseName, ".Rmd"))
400400
expect_true(file.exists(file.path(outDir, paste0(outBaseName, ".Rmd"))))
401401

402+
## Non-existing output directory
403+
args <- args0
404+
args$outputDir <- file.path(outDir, "new_directory_pdtmtptm")
405+
res <- do.call(runPDTMTptmAnalysis, args)
406+
expect_type(res, "character")
407+
expect_equal(basename(res), paste0(outBaseName, ".Rmd"))
408+
expect_true(file.exists(file.path(outDir, "new_directory_pdtmtptm",
409+
paste0(outBaseName, ".Rmd"))))
410+
402411
## Stop if forceOverwrite = FALSE
403412
args <- args0
404413
args$forceOverwrite <- FALSE

tests/testthat/test-runTests.R

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,92 @@ test_that("testing works", {
12861286
expect_equal(out$tests[[1]]$logFC / out$tests[[1]]$se.logFC,
12871287
out$tests[[1]]$t, ignore_attr = TRUE)
12881288

1289+
## -------------------------------------------------------------------------
1290+
## Merged groups, with batch effect, with sample weights
1291+
args <- args0
1292+
args$groupComposition <- list(rbc_adnp = c("RBC_ctrl", "Adnp"))
1293+
args$comparisons <- list(c("Adnp", "RBC_ctrl"), c("rbc_adnp", "Chd4BF"))
1294+
args$sce$batch <- c("B1", "B2", "B3", "B1", "B2", "B3", "B1", "B2", "B3")
1295+
args$sce$sampleweight <-
1296+
c(Adnp_IP04 = 1, Adnp_IP05 = 6, Adnp_IP06 = 2,
1297+
Chd4BF_IP07 = 6, Chd4BF_IP08 = 1, Chd4BF_IP09 = 5,
1298+
RBC_ctrl_IP01 = 7, RBC_ctrl_IP02 = 1, RBC_ctrl_IP03 = 2)[colnames(args$sce)]
1299+
out <- do.call(runTest, args)
1300+
expect_type(out, "list")
1301+
expect_length(out, 9)
1302+
expect_named(out, c("plottitles", "plotsubtitles", "plotnotes",
1303+
"tests", "curveparams", "topsets", "messages",
1304+
"design", "featureCollections"))
1305+
expect_s3_class(out$tests[[1]], "data.frame")
1306+
expect_type(out$plotnotes[[1]], "character")
1307+
expect_type(out$plottitles[[1]], "character")
1308+
expect_s3_class(out$tests[[2]], "data.frame")
1309+
expect_type(out$plotnotes[[2]], "character")
1310+
expect_type(out$plottitles[[2]], "character")
1311+
expect_type(out$featureCollections, "list")
1312+
expect_type(out$design, "list")
1313+
expect_named(out$design, c("RBC_ctrl_vs_Adnp", "Chd4BF_vs_rbc_adnp"))
1314+
expect_type(out$design$RBC_ctrl_vs_Adnp, "list")
1315+
expect_named(out$design$RBC_ctrl_vs_Adnp, c("design", "sampleData", "contrast",
1316+
"sampleWeights"))
1317+
expect_named(out$design$RBC_ctrl_vs_Adnp$sampleData, c("fc", "bc"))
1318+
expect_equal(out$design$RBC_ctrl_vs_Adnp$contrast, c(0, 0, 0, 1))
1319+
expect_equal(out$design$RBC_ctrl_vs_Adnp$sampleWeights,
1320+
args$sce$sampleweight[rownames(out$design$RBC_ctrl_vs_Adnp$sampleData)])
1321+
expect_type(out$design$Chd4BF_vs_rbc_adnp, "list")
1322+
expect_named(out$design$Chd4BF_vs_rbc_adnp, c("design", "sampleData", "contrast",
1323+
"sampleWeights"))
1324+
expect_named(out$design$Chd4BF_vs_rbc_adnp$sampleData, c("fc", "bc"))
1325+
expect_equal(out$design$Chd4BF_vs_rbc_adnp$contrast, c(0, 0, 0, 1))
1326+
expect_equal(out$design$Chd4BF_vs_rbc_adnp$sampleWeights,
1327+
args$sce$sampleweight[rownames(out$design$Chd4BF_vs_rbc_adnp$sampleData)])
1328+
expect_type(out$curveparams[[1]], "list")
1329+
expect_equal(nrow(out$tests[[1]]), 150)
1330+
expect_type(out$curveparams[[2]], "list")
1331+
expect_equal(nrow(out$tests[[2]]), 150)
1332+
expect_true(all(c("adj.P.Val", "iBAQ.Adnp_IP04",
1333+
"showInVolcano", "IDsForSTRING") %in% colnames(out$tests[[1]])))
1334+
expect_true(all(c("adj.P.Val", "iBAQ.Adnp_IP04",
1335+
"showInVolcano", "IDsForSTRING") %in% colnames(out$tests[[2]])))
1336+
expect_equal(out$tests[[1]]$pid, rownames(sce_mq_final))
1337+
expect_equal(out$tests[[2]]$pid, rownames(sce_mq_final))
1338+
expect_equal(substr(out$plotnotes[[1]], 1, 8), "df.prior")
1339+
expect_equal(substr(out$plotnotes[[2]], 1, 8), "df.prior")
1340+
expect_equal(out$plottitles[[1]], "RBC_ctrl vs Adnp, limma")
1341+
expect_equal(out$plottitles[[2]], "Chd4BF vs rbc_adnp, limma")
1342+
expect_s4_class(out$featureCollections$complexes, "CharacterList")
1343+
expect_s4_class(S4Vectors::mcols(out$featureCollections$complexes), "DFrame")
1344+
expect_true("RBC_ctrl_vs_Adnp_FDR" %in%
1345+
colnames(S4Vectors::mcols(out$featureCollections$complexes)))
1346+
expect_equal(out$tests[[1]]$iBAQ.Adnp_IP04,
1347+
SummarizedExperiment::assay(args$sce, "iBAQ")[, "Adnp_IP04"],
1348+
ignore_attr = TRUE)
1349+
expect_equal(out$tests[[2]]$iBAQ.Adnp_IP04,
1350+
SummarizedExperiment::assay(args$sce, "iBAQ")[, "Adnp_IP04"],
1351+
ignore_attr = TRUE)
1352+
## Compare to values calculated manually
1353+
expect_equal(out$tests[[2]][c("Mbd3", "Mta1.F8WHY8", "Pogz", "Zfp462.B1AWL2"), "logFC"],
1354+
c(13.237967, 15.502178, 9.519576, 10.742178),
1355+
tolerance = 0.001)
1356+
expect_equal(out$tests[[2]][c("Mbd3", "Mta1.F8WHY8", "Pogz", "Zfp462.B1AWL2"), "t"],
1357+
c(10.793285, 10.408011, 8.845311, 8.634606),
1358+
tolerance = 0.001)
1359+
## Check consistency of values
1360+
## logFC +/- t * se = CI.R/CI.L
1361+
expect_equal(out$tests[[1]]$logFC + qt(p = 0.975, df = out$tests[[1]]$df.total) *
1362+
out$tests[[1]]$se.logFC,
1363+
out$tests[[1]]$CI.R, ignore_attr = TRUE)
1364+
expect_equal(out$tests[[1]]$logFC - qt(p = 0.975, df = out$tests[[1]]$df.total) *
1365+
out$tests[[1]]$se.logFC,
1366+
out$tests[[1]]$CI.L, ignore_attr = TRUE)
1367+
## p-values
1368+
expect_equal(2 * stats::pt(abs(out$tests[[1]]$t),
1369+
out$tests[[1]]$df.total, lower.tail = FALSE),
1370+
out$tests[[1]]$P.Value, ignore_attr = TRUE)
1371+
## t-statistics
1372+
expect_equal(out$tests[[1]]$logFC / out$tests[[1]]$se.logFC,
1373+
out$tests[[1]]$t, ignore_attr = TRUE)
1374+
12891375
## -------------------------------------------------------------------------
12901376
## Merged groups, with batch effect, single fit
12911377
args <- args0

0 commit comments

Comments
 (0)