Browse code

Added forwarding of sample information from NanoMethResult to BSSeq objects

shians authored on 07/05/2024 06:48:02
Showing 4 changed files

... ...
@@ -149,6 +149,7 @@ importFrom(tibble,tibble)
149 149
 importFrom(tidyr,unnest)
150 150
 importFrom(utils,installed.packages)
151 151
 importFrom(utils,packageVersion)
152
+importFrom(utils,read.delim)
152 153
 importFrom(utils,read.table)
153 154
 importFrom(withr,defer)
154 155
 useDynLib(NanoMethViz)
... ...
@@ -1,6 +1,6 @@
1 1
 #' Create BSSeq object from methylation tabix file
2 2
 #'
3
-#' @param methy the path to the methylation tabix file.
3
+#' @param methy the NanoMethResult object or path to the methylation tabix file.
4 4
 #' @param out_folder the folder to store intermediate files. One file is created
5 5
 #'   for each sample and contains columns "chr", "pos", "total" and
6 6
 #'   "methylated".
... ...
@@ -18,24 +18,27 @@ methy_to_bsseq <- function(
18 18
     verbose = TRUE
19 19
 ) {
20 20
     if (is(methy, "NanoMethResult")) {
21
-        methy <- NanoMethViz::methy(methy)
21
+        methy_path <- NanoMethViz::methy(methy)
22
+    } else {
23
+        methy_path <- methy
24
+        assert_that(fs::file_exists(methy_path))
22 25
     }
23 26
 
24
-    if (verbose) {
25
-        timed_log("creating intermediate files...")
26
-    }
27
+    timed_log("creating intermediate files...", verbose = verbose)
27 28
 
28
-    files <- convert_methy_to_dss(methy, out_folder)
29
+    files <- convert_methy_to_dss(methy_path, out_folder)
30
+
31
+    if (is(methy, "NanoMethResult")) {
32
+        sample_anno <- NanoMethViz::samples(methy)
33
+    } else {
34
+        sample_anno <- tibble::tibble(sample = files$sample)
35
+    }
29 36
 
30 37
     if (verbose) {
31 38
         timed_log("creating bsseq object...")
32 39
     }
33 40
 
34
-    out <- create_bsseq_from_files(
35
-        files$file_path,
36
-        files$sample,
37
-        verbose = verbose
38
-    )
41
+    out <- create_bsseq_from_files(files$file_path, sample_anno, verbose = verbose)
39 42
 
40 43
     if (verbose) {
41 44
         timed_log("done")
... ...
@@ -72,7 +75,7 @@ convert_methy_to_dss <- function(
72 75
 #' @importFrom purrr map
73 76
 #' @importFrom dplyr select distinct arrange mutate
74 77
 #' @importFrom bsseq BSseq
75
-create_bsseq_from_files <- function(paths, samples, verbose = TRUE) {
78
+create_bsseq_from_files <- function(paths, sample_anno, verbose = TRUE) {
76 79
     readr::local_edition(1) # temporary fix for vroom bad value
77 80
     read_dss <- purrr::partial(
78 81
         read_tsv,
... ...
@@ -84,6 +87,8 @@ create_bsseq_from_files <- function(paths, samples, verbose = TRUE) {
84 87
         )
85 88
     )
86 89
 
90
+    samples <- sample_anno$sample
91
+
87 92
     if (verbose) {
88 93
         timed_log("reading in parsed data...")
89 94
     }
... ...
@@ -145,9 +150,7 @@ create_bsseq_from_files <- function(paths, samples, verbose = TRUE) {
145 150
         sampleNames = samples
146 151
     )
147 152
 
148
-    SummarizedExperiment::colData(result) <- S4Vectors::DataFrame(
149
-        sample = samples
150
-    )
153
+    SummarizedExperiment::colData(result) <- S4Vectors::DataFrame(sample_anno)
151 154
 
152 155
     rownames(SummarizedExperiment::colData(result)) <- samples
153 156
 
... ...
@@ -94,9 +94,11 @@ assert_has_columns <- function(x, cols) {
94 94
     }
95 95
 }
96 96
 
97
-timed_log <- function(...) {
97
+timed_log <- function(..., verbose = TRUE) {
98 98
     time_stamp <- paste0("[", format(Sys.time(), "%Y-%m-%d %H:%M:%S"), "] ")
99
-    message(time_stamp, ...)
99
+    if (verbose) {
100
+        message(time_stamp, ...)
101
+    }
100 102
 }
101 103
 
102 104
 gene_pos_range <- function(nmr, gene) {
... ...
@@ -1,11 +1,24 @@
1 1
 test_that("methy_to_bsseq works", {
2 2
     # setup
3 3
     nmr <- load_example_nanomethresult()
4
-    bss <- methy_to_bsseq(NanoMethViz::methy(nmr))
4
+    bss <- methy_to_bsseq(nmr)
5 5
 
6 6
     # test
7
+    expect_no_error(methy_to_bsseq(methy(nmr)))
7 8
     expect_true(is(methy_to_bsseq(nmr), "BSseq"))
8 9
     expect_equal(ncol(bss), 6)
10
+    expect_equal(
11
+        nrow(SummarizedExperiment::colData(bss)),
12
+        nrow(NanoMethViz::samples(nmr))
13
+    )
14
+    expect_equal(
15
+        ncol(SummarizedExperiment::colData(bss)),
16
+        ncol(NanoMethViz::samples(nmr))
17
+    )
18
+    expect_equal(
19
+        colnames(SummarizedExperiment::colData(bss)),
20
+        colnames(NanoMethViz::samples(nmr))
21
+    )
9 22
 })
10 23
 
11 24
 test_that("bsseq_to_* works", {