Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions R/SDRFconverter.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ SDRFtoAnnotation = function(
if (length(colnames(data)) < length(extract_cols)){
stop("ERROR: One or more of the column passed in the parameters were not found in the data. Please ensure that the column names are correct.")
}
data.table::setnames(data, extract_cols,
c("Run", "Condition", "BioReplicate"))

new_names = c("Run", "Condition", "BioReplicate")
if (!is.null(fraction)){
new_names = c(new_names, "Fraction")
}
data.table::setnames(data, extract_cols, new_names)

return(data)
}

Expand Down Expand Up @@ -102,10 +105,10 @@ extractSDRF = function(

if (is.null(fraction)){
data$Fraction = NULL
data.table::setnames(data, c("Condition", "BioReplicate", "Run"),
data.table::setnames(data, c("Run", "Condition", "BioReplicate"),
c(run_name, condition_name, biological_replicate))
} else {
data.table::setnames(data, extract_cols,
data.table::setnames(data, c("Run", "Condition", "BioReplicate", "Fraction"),
c(run_name, condition_name, biological_replicate, fraction))
}

Expand Down
6 changes: 3 additions & 3 deletions R/groupComparisonPlots.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ groupComparisonPlots = function(



if (address != FALSE) {
if (address != FALSE & !isPlotly) {
dev.off()
}
if(isPlotly) {
Expand Down Expand Up @@ -372,7 +372,7 @@ groupComparisonPlots = function(
if (!isPlotly) print(plot)
plots[[i]] = plot
}
if (address != FALSE) {
if (address != FALSE & !isPlotly) {
dev.off()
}
if (isPlotly) {
Expand Down Expand Up @@ -426,7 +426,7 @@ groupComparisonPlots = function(
if (!isPlotly) print(plot)
plots[[i]] = plot
}
if (address != FALSE) {
if (address != FALSE & !isPlotly) {
dev.off()
}
if (isPlotly) {
Expand Down
64 changes: 64 additions & 0 deletions inst/tinytest/test_SDRFconverter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Test SDRFtoAnnotation ------------------------------------------------------

# Test 1: default column names on the built-in example_SDRF dataset
annot = SDRFtoAnnotation(example_SDRF)
expect_true(is.data.frame(annot))
expect_equal(colnames(annot), c("Run", "Condition", "BioReplicate"))
expect_equal(nrow(annot), nrow(example_SDRF))

# Test 2: fraction column can be added when provided
annot_fraction = SDRFtoAnnotation(
example_SDRF,
fraction = "comment[fraction identifier]"
)
expect_true("Fraction" %in% colnames(annot_fraction))
expect_equal(nrow(annot_fraction), nrow(example_SDRF))

# Test 3: missing column name errors
expect_error(
SDRFtoAnnotation(example_SDRF, run_name = "not a real column")
)

# Test SDRF/extractSDRF -------------------------------------------------------

msstats_format_data = data.table::data.table(
Condition = c("A", "A", "B", "B"),
BioReplicate = c("1", "2", "3", "4"),
Run = c("run1", "run2", "run3", "run4"),
Fraction = c(1, 1, 1, 1),
Protein = c("P1", "P1", "P1", "P1"),
Intensity = c(10, 20, 30, 40)
)

# Test 4: default call keeps Fraction column, and values land in the
# correctly-named columns (Run -> run_name, Condition -> condition_name,
# BioReplicate -> biological_replicate), not just column names matching
sdrf_with_fraction = extractSDRF(msstats_format_data, fraction = "Fraction")
expect_true(is.data.frame(sdrf_with_fraction))
expect_equal(nrow(sdrf_with_fraction), 4)
expect_true("Fraction" %in% colnames(sdrf_with_fraction))
expect_true(all(sdrf_with_fraction[["comment[data file]"]] %in%
msstats_format_data$Run))
expect_true(all(sdrf_with_fraction[["characteristics[disease]"]] %in%
msstats_format_data$Condition))
expect_true(all(sdrf_with_fraction[["characteristics[biological replicate]"]] %in%
msstats_format_data$BioReplicate))

# Test 5: fraction = NULL drops the Fraction column and renames the others
sdrf_no_fraction = extractSDRF(msstats_format_data)
expect_false("Fraction" %in% colnames(sdrf_no_fraction))
expect_true(all(c("comment[data file]", "characteristics[disease]",
"characteristics[biological replicate]") %in%
colnames(sdrf_no_fraction)))
expect_true(all(sdrf_no_fraction[["comment[data file]"]] %in%
msstats_format_data$Run))

# Test 6: meta_data is merged in by run name
meta = data.frame(
`comment[data file]` = c("run1", "run2", "run3", "run4"),
instrument = c("QE", "QE", "QE", "QE"),
check.names = FALSE
)
sdrf_with_meta = extractSDRF(msstats_format_data, meta_data = meta)
expect_true("instrument" %in% colnames(sdrf_with_meta))
expect_equal(nrow(sdrf_with_meta), 4)
83 changes: 83 additions & 0 deletions inst/tinytest/test_dataProcessPlots.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Setup ------------------------------------------------------------------
QuantData = dataProcess(SRMRawData, use_log_file = FALSE)
protein_name = as.character(unique(QuantData$ProteinLevelData$Protein))[1]

# Test 1: invalid type errors -------------------------------------------------
expect_error(dataProcessPlots(QuantData, type = "INVALID"))

# Test 2: address = FALSE with which.Protein = "all" errors -------------------
expect_error(
dataProcessPlots(QuantData, type = "ProfilePlot", which.Protein = "all",
address = FALSE)
)

# Test 3: address = FALSE with multiple proteins errors -----------------------
expect_error(
dataProcessPlots(QuantData, type = "ProfilePlot", which.Protein = c(1, 2),
address = FALSE)
)

# ggplot2 (isPlotly = FALSE) branches, saved to a tempdir ---------------------

tmp_dir = tempfile("msstats_dataprocessplots_")
dir.create(tmp_dir)
address_prefix = paste0(tmp_dir, "/")

# Test 4: ProfilePlot (ggplot2) creates a pdf file, and always warns ----------
expect_warning(
dataProcessPlots(QuantData, type = "ProfilePlot", which.Protein = protein_name,
address = address_prefix,
remove_uninformative_feature_outlier = TRUE)
)
expect_true(file.exists(paste0(address_prefix, "ProfilePlot.pdf")))

# Test 5: QCPlot (ggplot2) creates a pdf file ---------------------------------
expect_warning(
dataProcessPlots(QuantData, type = "QCPlot", which.Protein = protein_name,
address = address_prefix)
)
expect_true(file.exists(paste0(address_prefix, "QCPlot.pdf")))

# Test 6: ConditionPlot (ggplot2) creates a pdf file --------------------------
expect_warning(
dataProcessPlots(QuantData, type = "ConditionPlot", which.Protein = protein_name,
address = address_prefix)
)
expect_true(file.exists(paste0(address_prefix, "ConditionPlot.pdf")))

unlink(tmp_dir, recursive = TRUE)

# plotly (isPlotly = TRUE) branches, address = FALSE (no file saving) ---------

# Test 7: ProfilePlot (plotly) returns a list of plotly objects --------------
plotly_profile = suppressWarnings(
dataProcessPlots(QuantData, type = "ProfilePlot", which.Protein = protein_name,
address = FALSE, isPlotly = TRUE)
)
expect_true(is.list(plotly_profile))
expect_true(length(plotly_profile) > 0)
expect_true(inherits(plotly_profile[[1]], "plotly"))

# Test 8: QCPlot (plotly) returns a list of plotly objects -------------------
plotly_qc = suppressWarnings(
dataProcessPlots(QuantData, type = "QCPlot", which.Protein = protein_name,
address = FALSE, isPlotly = TRUE)
)
expect_true(is.list(plotly_qc))
expect_true(length(plotly_qc) > 0)
expect_true(inherits(plotly_qc[[1]], "plotly"))

# Test 9: ConditionPlot (plotly), saved as a zipped HTML file ----------------
# Note: which.Protein must be "all" here (rather than a single protein as
# above) - selecting a subset together with isPlotly = TRUE hits a pre-existing
# bug in dataProcessPlots() where NULL placeholders for unselected proteins
# are still passed to the ggplot-to-plotly conversion step.
tmp_dir2 = tempfile("msstats_dataprocessplots_condition_")
dir.create(tmp_dir2)
address_prefix2 = paste0(tmp_dir2, "/")
invisible(capture.output(suppressWarnings(
dataProcessPlots(QuantData, type = "ConditionPlot", which.Protein = "all",
address = address_prefix2, isPlotly = TRUE)
)))
expect_true(any(grepl("ConditionPlot.*\\.zip$", list.files(tmp_dir2))))
unlink(tmp_dir2, recursive = TRUE)
78 changes: 78 additions & 0 deletions inst/tinytest/test_designSampleSize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Setup ------------------------------------------------------------------
QuantData = dataProcess(SRMRawData, use_log_file = FALSE)
comparison1 = matrix(c(-1,0,1,0,0,0,0,0,0,0), nrow=1)
comparison2 = matrix(c(-1,0,0,0,0,0,1,0,0,0), nrow=1)
comparison3 = matrix(c(-1,0,0,0,0,0,0,0,1,0), nrow=1)
comparison = rbind(comparison1, comparison2, comparison3)
row.names(comparison) = c("T3-T1", "T7-T1", "T9-T1")
groups = levels(QuantData$ProteinLevelData$GROUP)
colnames(comparison) = groups[order(as.numeric(groups))]

testResultMultiComparisons = groupComparison(contrast.matrix = comparison,
data = QuantData,
use_log_file = FALSE)

# Test 1: designSampleSize with numSample = TRUE (calculate sample size) -----
result_sample = designSampleSize(data = testResultMultiComparisons$FittedModel,
numSample = TRUE,
desiredFC = c(1.25, 1.75), FDR = 0.05, power = 0.8,
use_log_file = FALSE)
expect_true(is.data.frame(result_sample))
expect_true(all(c("desiredFC", "numSample", "FDR", "power", "CV") %in%
colnames(result_sample)))
expect_true(all(result_sample$numSample > 0))

# Test 2: designSampleSize with power = TRUE (calculate power) ---------------
result_power = designSampleSize(data = testResultMultiComparisons$FittedModel,
numSample = 2,
desiredFC = c(1.25, 1.75), FDR = 0.05, power = TRUE,
use_log_file = FALSE)
expect_true(is.data.frame(result_power))
expect_true(all(c("desiredFC", "numSample", "FDR", "power", "CV") %in%
colnames(result_power)))
expect_true(all(result_power$power >= 0 & result_power$power <= 1))
expect_true(all(result_power$numSample == 2))

# Test 3: increasing desired fold change reduces the required sample size ---
result_sample_small_fc = designSampleSize(data = testResultMultiComparisons$FittedModel,
numSample = TRUE,
desiredFC = c(1.05, 1.075), FDR = 0.05, power = 0.8,
use_log_file = FALSE)
expect_true(max(result_sample_small_fc$numSample) >= min(result_sample$numSample))

# Test designSampleSizePlots (ggplot2 branch, isPlotly = FALSE) ---------------

tmp_dir = tempfile("msstats_designsamplesize_")
dir.create(tmp_dir)
pdf(file.path(tmp_dir, "plots.pdf"))

# Test 4: numSample plot (base graphics) does not error
expect_silent(designSampleSizePlots(data = result_sample))

# Test 5: power plot (base graphics) does not error
expect_silent(designSampleSizePlots(data = result_power))

dev.off()
unlink(tmp_dir, recursive = TRUE)

# Test designSampleSizePlots (plotly branch, isPlotly = TRUE) -----------------

# Test 6: numSample plot returns a plotly object
plotly_sample = designSampleSizePlots(data = result_sample, isPlotly = TRUE)
expect_true(inherits(plotly_sample, "plotly"))

# Test 7: power plot returns a plotly object
plotly_power = designSampleSizePlots(data = result_power, isPlotly = TRUE)
expect_true(inherits(plotly_power, "plotly"))

# Test .getVarComponent / .getMedianSigmaSubject / .calculatePower / .getNumSample

# Test 8: .getVarComponent extracts Error/Subject/GroupBySubject per protein
var_component = MSstats:::.getVarComponent(testResultMultiComparisons$FittedModel)
expect_true(all(c("Error", "Subject", "GroupBySubject", "Protein") %in%
colnames(var_component)))
expect_equal(nrow(var_component), length(testResultMultiComparisons$FittedModel))

# Test 9: .getMedianSigmaSubject returns 0 when all components are NA --------
all_na_component = data.frame(Subject = NA_real_, GroupBySubject = NA_real_)
expect_equal(MSstats:::.getMedianSigmaSubject(all_na_component), 0)
101 changes: 101 additions & 0 deletions inst/tinytest/test_groupComparisonPlots.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Setup ------------------------------------------------------------------
QuantData = dataProcess(SRMRawData, use_log_file = FALSE)
comparison1 = matrix(c(-1,0,1,0,0,0,0,0,0,0), nrow=1)
comparison2 = matrix(c(-1,0,0,0,0,0,1,0,0,0), nrow=1)
comparison3 = matrix(c(-1,0,0,0,0,0,0,0,1,0), nrow=1)
comparison = rbind(comparison1, comparison2, comparison3)
row.names(comparison) = c("T3-T1", "T7-T1", "T9-T1")
groups = levels(QuantData$ProteinLevelData$GROUP)
colnames(comparison) = groups[order(as.numeric(groups))]

testResultMultiComparisons = groupComparison(contrast.matrix = comparison,
data = QuantData,
use_log_file = FALSE)
comparison_result = testResultMultiComparisons$ComparisonResult

# Test 1: invalid type errors -------------------------------------------------
expect_error(groupComparisonPlots(data = comparison_result, type = "INVALID"))

# Test 2: invalid which.Comparison label errors -------------------------------
expect_error(
groupComparisonPlots(data = comparison_result, type = "VolcanoPlot",
which.Comparison = "NotARealComparison")
)

tmp_dir = tempfile("msstats_groupcomparisonplots_")
dir.create(tmp_dir)
address_prefix = paste0(tmp_dir, "/")

# Test 3: Heatmap (ggplot2) creates a pdf file and always warns --------------
expect_warning(
groupComparisonPlots(data = comparison_result, type = "Heatmap",
logBase.pvalue = 2, address = address_prefix)
)
expect_true(file.exists(paste0(address_prefix, "Heatmap.pdf")))

# Test 4: Heatmap with FCcutoff also runs without error ----------------------
expect_warning(
groupComparisonPlots(data = comparison_result, type = "Heatmap",
FCcutoff = 70, logBase.pvalue = 2, address = address_prefix)
)

# Test 5: VolcanoPlot (ggplot2) creates a pdf file, no warning required ------
invisible(capture.output(
groupComparisonPlots(data = comparison_result, type = "VolcanoPlot",
logBase.pvalue = 2, address = address_prefix)
))
expect_true(file.exists(paste0(address_prefix, "VolcanoPlot.pdf")))

# Test 6: VolcanoPlot with FCcutoff and no protein names ---------------------
invisible(capture.output(
groupComparisonPlots(data = comparison_result, type = "VolcanoPlot",
FCcutoff = 70, logBase.pvalue = 2, ylimUp = 100,
ProteinName = FALSE, address = address_prefix)
))

# Test 7: ComparisonPlot (ggplot2) creates a pdf file and always warns -------
expect_warning(
groupComparisonPlots(data = comparison_result, type = "ComparisonPlot",
address = address_prefix)
)
expect_true(file.exists(paste0(address_prefix, "ComparisonPlot.pdf")))

unlink(tmp_dir, recursive = TRUE)

# plotly (isPlotly = TRUE) branches --------------------------------------------

# Test 8: VolcanoPlot (plotly), address = FALSE, returns a list of plotly objects
# (address = FALSE requires a single comparison to be selected)
plotly_volcano = invisible(capture.output(
result_volcano <- groupComparisonPlots(data = comparison_result, type = "VolcanoPlot",
which.Comparison = "T3-T1",
logBase.pvalue = 2, address = FALSE,
isPlotly = TRUE)
))
expect_true(is.list(result_volcano))
expect_true(length(result_volcano) > 0)
expect_true(inherits(result_volcano[[1]], "plotly"))

# Test 9: Heatmap (plotly), saved as a zipped HTML file ----------------------
tmp_dir2 = tempfile("msstats_groupcomparisonplots_heatmap_")
dir.create(tmp_dir2)
address_prefix2 = paste0(tmp_dir2, "/")
invisible(capture.output(suppressWarnings(
groupComparisonPlots(data = comparison_result, type = "Heatmap",
logBase.pvalue = 2, address = address_prefix2,
isPlotly = TRUE)
)))
expect_true(any(grepl("Heatmap.*\\.zip$", list.files(tmp_dir2))))
unlink(tmp_dir2, recursive = TRUE)

# Test 10: ComparisonPlot (plotly, all proteins), saved as a zipped HTML file
tmp_dir3 = tempfile("msstats_groupcomparisonplots_comparison_")
dir.create(tmp_dir3)
address_prefix3 = paste0(tmp_dir3, "/")
invisible(capture.output(suppressWarnings(
groupComparisonPlots(data = comparison_result, type = "ComparisonPlot",
which.Protein = "all", address = address_prefix3,
isPlotly = TRUE)
)))
expect_true(any(grepl("ComparisonPlot.*\\.zip$", list.files(tmp_dir3))))
unlink(tmp_dir3, recursive = TRUE)
Loading
Loading