diff --git a/R/bedgraph.R b/R/bedgraph.R index f941f65..fda04ab 100644 --- a/R/bedgraph.R +++ b/R/bedgraph.R @@ -91,6 +91,9 @@ setMethod("bedgraph", signature(methylObj="methylDiff"), { stop("col.name argument is not one of 'pvalue','qvalue', 'meth.diff'") } + if (col.name == "meth.diff" && log.transform) { + stop("Log transformation is not allowed for 'meth.diff' values") + } mdata=getData(methylObj) df=data.frame(chr=mdata$chr, start=mdata$start-1, @@ -188,7 +191,7 @@ setMethod("bedgraph", signature(methylObj="methylRaw"), options(scipen = defsci) } - + } ) @@ -308,4 +311,3 @@ setMethod("bedgraph", signature(methylObj="methylRawList"), options(scipen = defsci) } }) - \ No newline at end of file diff --git a/R/methylDBFunctions.R b/R/methylDBFunctions.R index f59f1e2..b0cce3b 100644 --- a/R/methylDBFunctions.R +++ b/R/methylDBFunctions.R @@ -417,7 +417,7 @@ setMethod("adjustMethylC", c("methylRawDB","methylRawDB"), } - + }) @@ -1083,7 +1083,7 @@ setMethod("percMethylation", "methylBaseDB", chunk.size = 1e6) { meth.fun <- function(data, numCs.index, numTs.index, rowids) { dat = 100 * data[, numCs.index] / (data[, numCs.index] + - data[, numTs.index]) + data[, numTs.index]) if(rowids) { dat = cbind(pos = paste(as.character(data[, 1]), @@ -2632,6 +2632,9 @@ setMethod("bedgraph", signature(methylObj="methylDiffDB"), { stop("col.name argument is not one of 'pvalue','qvalue', 'meth.diff'") } + if (col.name == "meth.diff" && log.transform) { + stop("Log transformation is not allowed for 'meth.diff' values") + } bedgr <- function(data,col.name,file.name,log.transform,negative,add.on, sample.id){ diff --git a/tests/testthat/test-17-bedgraph.r b/tests/testthat/test-17-bedgraph.r index f8a184a..437e609 100644 --- a/tests/testthat/test-17-bedgraph.r +++ b/tests/testthat/test-17-bedgraph.r @@ -14,15 +14,13 @@ bedgraph(methylDiff.obj, file.name=outFile, col.name="meth.diff", unmeth=FALSE,log.transform=FALSE,negative=FALSE,add.on="") tt <- rtracklayer::import.bedGraph(outFile) outFile2 <- tempfile(pattern = "mdiff.",tmpdir = outDir,fileext = ".bed") -suppressWarnings(bedgraph(methylDiff.obj,col.name = "meth.diff", - log.transform = TRUE,negative = TRUE,file.name = outFile2)) -tt2 <- rtracklayer::import.bedGraph(outFile2) test_that("export methylDiff worked", { expect_true(file.exists(outFile)) expect_equal(length(tt),nrow(methylDiff.obj)) - expect_equal(length(tt2),nrow(methylDiff.obj)) + expect_error(bedgraph(methylDiff.obj,col.name = "meth.diff", + log.transform = TRUE,negative = TRUE,file.name = outFile2)) expect_is(bedgraph(methylDiff.obj,col.name = "meth.diff"), class = "data.frame") expect_error(bedgraph(methylDiff.obj,col.name = "coverage")) @@ -51,8 +49,6 @@ test_that("export methylRaw worked", { expect_error(bedgraph(methylRawList.obj[[1]],col.name = "meth.diff")) }) - - # getting a bedgraph file from a methylRawList object containing raw #methylation values outFile <- tempfile(pattern = "mRawL",tmpdir = outDir,fileext = ".bed") @@ -74,6 +70,24 @@ test_that("export methylRaw worked", { expect_error(bedgraph(methylRawList.obj,col.name = "meth.diff")) }) +# getting a bedgraph file from a methylDiffDB object containing differential +# methylation percentages +outFile <- tempfile(pattern = "mdiffDB.",tmpdir = outDir,fileext = ".bed") +methylDiffDB.obj <- makeMethylDB(methylDiff.obj, dbdir) +bedgraph(methylDiffDB.obj, file.name=outFile, col.name="meth.diff", + unmeth=FALSE,log.transform=FALSE,negative=FALSE,add.on="") +tt <- rtracklayer::import.bedGraph(outFile) +outFile2 <- tempfile(pattern = "mdiffDB.",tmpdir = outDir,fileext = ".bed") + +test_that("export methylDiffDB worked", { + expect_true(file.exists(outFile)) + expect_equal(length(tt),nrow(methylDiffDB.obj)) + expect_error(bedgraph(methylDiffDB.obj,col.name = "meth.diff", + log.transform = TRUE,negative = TRUE,file.name = outFile2)) + expect_is(bedgraph(methylDiffDB.obj,col.name = "meth.diff"), + class = "data.frame") + expect_error(bedgraph(methylDiffDB.obj,col.name = "coverage")) +}) # remove the file unlink(outDir,recursive = TRUE)