Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export significant test results (#37)
Make the list that stores hypothesis-test results into an S3 object sig_test_summary. Add function to export top-tables and significant gene-lists from a sig_test_summary object to consistently-named files. * reordered package imports alphabetically * make class for sig test summaries * save sig-test results using export() generic * drop `test_type` from filenames `test_type` (eg, single_contrast, nested_lrt) should be part of the user-specified `output_dir` for simplicity. * fix roxygen for export.default
- Loading branch information
Showing
9 changed files
with
198 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' Export a data structure to a (set of) file(s) | ||
#' | ||
#' @param x Some datastructure. Typically a | ||
#' `sig_test_summary` as created by `reeq::get_sig_test_summary`. | ||
#' @param ... Further arguments. Currently unused. | ||
#' | ||
#' @export | ||
|
||
export <- function(x, ...) { | ||
UseMethod("export") | ||
} | ||
|
||
#' Default function for exporting a data-structure. | ||
#' | ||
#' @inheritParams export | ||
#' | ||
#' @noRd | ||
#' | ||
|
||
export.default <- function(x, ...) { | ||
stop("Not implemented") | ||
} | ||
|
||
#' Export a `sig_test_summary` object to files | ||
#' | ||
#' Write the (unfiltered) top-table and significant gene lists from a | ||
#' `sig_test_summary` object to some files. The file format is | ||
#' `<output_dir>/<test_name>.top_tags.tsv` or | ||
#' `<output_dir>/<test_name>.sig_features.<p_threshold>.tsv`, where the p-value | ||
#' threshold is obtained from inside the `sig_test_summary` object and is | ||
#' formatted to 4 decimal places. | ||
#' | ||
#' @inheritParams export | ||
#' | ||
#' @param output_dir Which directory should the results be output to | ||
#' @param test_name A name for the hypothesis test that is | ||
#' summarised in the dataset `x`. | ||
#' | ||
#' @importFrom readr write_tsv | ||
#' | ||
|
||
export.sig_test_summary <- function(x, | ||
output_dir, | ||
test_name, | ||
...) { | ||
p_threshold <- sprintf("p%.4f", x$p_threshold) | ||
|
||
# obtain the top-table for all features | ||
tt_file <- paste( | ||
test_name, "top_tags", "tsv", | ||
sep = "." | ||
) | ||
tt_path <- file.path(output_dir, tt_file) | ||
tt_data <- as.data.frame(x$top_table) | ||
|
||
# obtain the list of all significant features | ||
gene_file <- paste( | ||
test_name, "sig_features", p_threshold, "tsv", | ||
sep = "." | ||
) | ||
gene_path <- file.path(output_dir, gene_file) | ||
gene_data <- x$sig_features | ||
|
||
# write out files containing the results | ||
readr::write_tsv(tt_data, tt_path) | ||
write(gene_data, gene_path, ncolumns = 1) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
############################################################################### | ||
|
||
context("Tests for methods on `sig_test_summary` objects") | ||
|
||
############################################################################### | ||
|
||
test_that("export - invalid input", { | ||
expect_error( | ||
object = export("NOT A sig_test_summary"), | ||
info = "`reeq::export` is only defined for `sig_test_summary` objects" | ||
) | ||
}) | ||
|
||
test_that("export - creates the expected files", { | ||
# TODO: explicit constructor for `sig_test_summary` objects | ||
|
||
# `export.sig_test_summary` requires that `p_threshold`, `sig_features` and | ||
# `top_table` are present in the `sig_test_summary` that is passed in. | ||
|
||
input <- structure( | ||
list( | ||
# 4 significant digits of the p-threshold are present in the output | ||
# "significant features" file | ||
p_threshold = 0.001111, | ||
top_table = data.frame(x = 1:3, y = 4:6, row.names = LETTERS[1:3]), | ||
sig_features = "A" | ||
), | ||
class = "sig_test_summary" | ||
) | ||
|
||
out_dir <- file.path(tempdir(), "export1") | ||
dir.create(out_dir) | ||
top_table_file <- file.path( | ||
out_dir, "some_contrast.top_tags.tsv" | ||
) | ||
sig_features_file <- file.path( | ||
out_dir, "some_contrast.sig_features.p0.0011.tsv" | ||
) | ||
|
||
expect_silent( | ||
export( | ||
input, | ||
output_dir = out_dir, | ||
test_name = "some_contrast" | ||
) | ||
) | ||
|
||
expect_true( | ||
file.exists(top_table_file), | ||
info = "`export` should make a top-tags file" | ||
) | ||
expect_true( | ||
file.exists(sig_features_file), | ||
info = "`export` should make a significant-features file" | ||
) | ||
expect_equal( | ||
object = scan(sig_features_file, what = character()), | ||
expected = input$sig_features, | ||
info = "contents of significant-features file should match the input" | ||
) | ||
}) |