From 66b8f3031e31118964b383a59f7286c8680c699c Mon Sep 17 00:00:00 2001 From: ew367 Date: Mon, 4 Nov 2024 11:05:33 +0000 Subject: [PATCH 1/3] add imports --- NAMESPACE | 7 +++++++ R/calcDiffFunction.r | 6 ++++++ R/calcSamplesFunction.r | 5 +++++ R/plotFunction.r | 2 ++ 4 files changed, 20 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 1419df2..d819b9c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,3 +4,10 @@ export(calcDiff) export(calcProps) export(calcSamples) export(plotPower) +import(doParallel) +import(foreach) +import(ggplot2) +import(pwr) +importFrom(dplyr,left_join) +importFrom(dplyr,mutate) +importFrom(reshape2,melt) diff --git a/R/calcDiffFunction.r b/R/calcDiffFunction.r index 702534a..54729dc 100644 --- a/R/calcDiffFunction.r +++ b/R/calcDiffFunction.r @@ -10,6 +10,12 @@ #' #' @return - Returns a list of matrices with one matrix per cell type/condition. Each matrix contains the power for each site (columns) and different mean differences (rows). #' @export +#' @import pwr +#' @import doParallel +#' @import foreach +#' @importFrom dplyr mutate +#' @importFrom dplyr left_join + #' #' @examples #' allSamples <- calcDiff(allSDs, dataType = "SDs") diff --git a/R/calcSamplesFunction.r b/R/calcSamplesFunction.r index 5f98a8e..63ec863 100644 --- a/R/calcSamplesFunction.r +++ b/R/calcSamplesFunction.r @@ -10,6 +10,11 @@ #' #' @return - Returns a list of matrices with one matrix per cell type/condition. Each matrix contains the power for each site (columns) and different sample sizes (rows). #' @export +#' @import pwr +#' @import doParallel +#' @import foreach +#' @importFrom dplyr mutate +#' @importFrom dplyr left_join #' #' @examples #' allSamples <- calcSamples(allSDs, dataType = "SDs") diff --git a/R/plotFunction.r b/R/plotFunction.r index 9e8de25..39b86a9 100644 --- a/R/plotFunction.r +++ b/R/plotFunction.r @@ -7,6 +7,8 @@ #' #' @return - ggplot object with power curve plot #' @export +#' @import ggplot2 +#' @importFrom reshape2 melt #' #' @examples #' myPlot <- plotPower(allProps, "samples") From 1e4562d6d8b5e79c17abbc93280efc15fcf8e011 Mon Sep 17 00:00:00 2001 From: ew367 Date: Mon, 4 Nov 2024 11:07:04 +0000 Subject: [PATCH 2/3] change library to require --- DESCRIPTION | 1 - R/calcDiffFunction.r | 2 +- R/calcSamplesFunction.r | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e38ee38..7bcef79 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,6 @@ Imports: ggplot2, pwr, reshape2, - data.table Suggests: knitr VignetteBuilder: knitr diff --git a/R/calcDiffFunction.r b/R/calcDiffFunction.r index 54729dc..1d0b97f 100644 --- a/R/calcDiffFunction.r +++ b/R/calcDiffFunction.r @@ -68,7 +68,7 @@ calcDiff <- function(betasOrSDs, pheno=NULL, nSamples=100, dataType, binSize = 5 #define function to calculate power for each sample size powerCpG <- function(betasSD){ - library(pwr) + require(pwr) power1 <- c() meanDiff <- seq(0.001, 0.1, 0.001) diff --git a/R/calcSamplesFunction.r b/R/calcSamplesFunction.r index 63ec863..e778581 100644 --- a/R/calcSamplesFunction.r +++ b/R/calcSamplesFunction.r @@ -90,7 +90,7 @@ calcSamples <- function(betasOrSDs, pheno=NULL, meanDiff=5, dataType, binSize = #define function to calculate power for each sample size powerCpG <- function(betasSD){ - library(pwr) + require(pwr) power1 <- c() Samples <- seq(nSamples/100, nSamples, nSamples/100) meandiffdec <- meanDiffDec From 7ab686e82374b3c133dea384ca3ab5eaacd54e06 Mon Sep 17 00:00:00 2001 From: ew367 Date: Mon, 4 Nov 2024 14:33:28 +0000 Subject: [PATCH 3/3] resolve check() output warnings --- BrainPower.Rproj => CellPower.Rproj | 0 DESCRIPTION | 5 ++- NAMESPACE | 5 +++ R/calcDiffFunction.r | 32 ++++++++++++---- R/calcPropsFunction.r | 7 +++- R/calcSamplesFunction.r | 32 ++++++++++++---- R/plotFunction.r | 6 ++- man/calcDiff.Rd | 1 - man/calcProps.Rd | 3 -- man/calcSamples.Rd | 1 - man/plotPower.Rd | 3 -- ...er-vignette.Rmd => CellPower-vignette.Rmd} | 38 ++++++++++++++++--- 12 files changed, 99 insertions(+), 34 deletions(-) rename BrainPower.Rproj => CellPower.Rproj (100%) rename vignettes/{BrainPower-vignette.Rmd => CellPower-vignette.Rmd} (75%) diff --git a/BrainPower.Rproj b/CellPower.Rproj similarity index 100% rename from BrainPower.Rproj rename to CellPower.Rproj diff --git a/DESCRIPTION b/DESCRIPTION index 7bcef79..a532b1e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,8 +12,10 @@ License: GPL-3 URL: https://github.com/ew367/CellPower Encoding: UTF-8 LazyData: true +LazyDataCompression: gzip Imports: doParallel, + parallel, dplyr, foreach, ggplot2, @@ -21,5 +23,6 @@ Imports: reshape2, Suggests: knitr +Depends: R (>= 2.10) VignetteBuilder: knitr -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index d819b9c..254a112 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,4 +10,9 @@ import(ggplot2) import(pwr) importFrom(dplyr,left_join) importFrom(dplyr,mutate) +importFrom(dplyr,ntile) +importFrom(parallel,clusterExport) +importFrom(parallel,detectCores) +importFrom(parallel,makeForkCluster) importFrom(reshape2,melt) +importFrom(stats,complete.cases) diff --git a/R/calcDiffFunction.r b/R/calcDiffFunction.r index 1d0b97f..3751963 100644 --- a/R/calcDiffFunction.r +++ b/R/calcDiffFunction.r @@ -15,11 +15,16 @@ #' @import foreach #' @importFrom dplyr mutate #' @importFrom dplyr left_join - +#' @importFrom dplyr ntile +#' @importFrom stats complete.cases +#' @importFrom parallel detectCores +#' @importFrom parallel makeForkCluster +#' @importFrom parallel clusterExport #' #' @examples #' allSamples <- calcDiff(allSDs, dataType = "SDs") -#' allSamples <- calcDiff(betas, phenoFile, nSamples = 100, dataType = "betaMatrix") + +# or for beta values (not supplied in this example): allSamples <- calcDiff(betas, phenoFile, nSamples = 100, dataType = "betaMatrix") @@ -74,7 +79,7 @@ calcDiff <- function(betasOrSDs, pheno=NULL, nSamples=100, dataType, binSize = 5 # bin data - betasSD <- as.data.frame(betasSD) %>% mutate(points_bin = ntile(betasSD, n=binSize)) + betasSD <- as.data.frame(betasSD) %>% dplyr::mutate(points_bin = dplyr::ntile(betasSD, n=binSize)) meanSDs <- c() for(j in unique(betasSD$points_bin)){ @@ -83,7 +88,7 @@ calcDiff <- function(betasOrSDs, pheno=NULL, nSamples=100, dataType, binSize = 5 } for(diff in meanDiff){ - power1 <- c(power1, pwr.t.test(d=diff/meanSDs,n=nSamples,sig.level=9e-8,type="two.sample",alternative="two.sided")$power) + power1 <- c(power1, pwr::pwr.t.test(d=diff/meanSDs,n=nSamples,sig.level=9e-8,type="two.sample",alternative="two.sided")$power) } power1 <- as.data.frame(matrix(power1, nrow=length(meanDiff), byrow = T)) @@ -95,10 +100,21 @@ calcDiff <- function(betasOrSDs, pheno=NULL, nSamples=100, dataType, binSize = 5 print("Setting up parallel processors...") - no_cores <- detectCores() - 1 - cl <- makeForkCluster(no_cores) - registerDoParallel(cl) - clusterExport(cl, c("allSDs", "nSamples", "binSize"), envir=environment()) + require(dplyr) + require(foreach) + require(doParallel) + +chk <- Sys.getenv("_R_CHECK_LIMIT_CORES_", "") + +if (nzchar(chk) && chk == "TRUE") { + no_cores <- 2L +} else { + no_cores <- parallel::detectCores() - 1 +} + + cl <- parallel::makeForkCluster(no_cores) + doParallel::registerDoParallel(cl) + parallel::clusterExport(cl, c("allSDs", "nSamples", "binSize"), envir=environment()) meanDiff <- seq(0.001, 0.1, 0.001) results <- foreach(i = colnames(allSDs)) %dopar% {powerCpG(allSDs[,i])} diff --git a/R/calcPropsFunction.r b/R/calcPropsFunction.r index 0fbaa4e..950e60a 100644 --- a/R/calcPropsFunction.r +++ b/R/calcPropsFunction.r @@ -6,8 +6,11 @@ #' @return - a dataframe containing the proportion of probes with power > 0.8 where rows are either mean differences or number of samples tested, and columns are each cell type #' @export #' -#' @examples -#' allProps <-calcProps(allSamples) + +# Example usage: + +# allSamples <- calcSamples(allSDs, dataType = "SDs") +# allProps <-calcProps(allSamples) calcProps <- function(allCellRes){ diff --git a/R/calcSamplesFunction.r b/R/calcSamplesFunction.r index e778581..3302a01 100644 --- a/R/calcSamplesFunction.r +++ b/R/calcSamplesFunction.r @@ -15,10 +15,16 @@ #' @import foreach #' @importFrom dplyr mutate #' @importFrom dplyr left_join +#' @importFrom dplyr ntile +#' @importFrom stats complete.cases +#' @importFrom parallel detectCores +#' @importFrom parallel makeForkCluster +#' @importFrom parallel clusterExport #' #' @examples #' allSamples <- calcSamples(allSDs, dataType = "SDs") -#' allSamples <- calcSamples(betas, phenoFile, meanDiff = 5, dataType = "betaMatrix") +# +# or for beta values (not supplied in this example): allSamples <- calcSamples(betas, phenoFile, meanDiff = 5, dataType = "betaMatrix") calcSamples <- function(betasOrSDs, pheno=NULL, meanDiff=5, dataType, binSize = 500){ @@ -96,7 +102,7 @@ calcSamples <- function(betasOrSDs, pheno=NULL, meanDiff=5, dataType, binSize = meandiffdec <- meanDiffDec # bin data - betasSD <- as.data.frame(betasSD) %>% mutate(points_bin = ntile(betasSD, n=binSize)) + betasSD <- as.data.frame(betasSD) %>% dplyr::mutate(points_bin = dplyr::ntile(betasSD, n=binSize)) meanSDs <- c() for(j in unique(betasSD$points_bin)){ @@ -107,7 +113,7 @@ calcSamples <- function(betasOrSDs, pheno=NULL, meanDiff=5, dataType, binSize = for(sample in Samples){ - power1 <- c(power1, pwr.t.test(n=sample, d = meandiffdec/(meanSDs), sig.level = 9e-8,type="two.sample",alternative="two.sided")$power) + power1 <- c(power1, pwr::pwr.t.test(n=sample, d = meandiffdec/(meanSDs), sig.level = 9e-8,type="two.sample",alternative="two.sided")$power) } @@ -120,10 +126,22 @@ calcSamples <- function(betasOrSDs, pheno=NULL, meanDiff=5, dataType, binSize = print("Setting up parallel processors...") - no_cores <- detectCores() - 1 - cl <- makeForkCluster(no_cores) - registerDoParallel(cl) - clusterExport(cl, c("allSDs", "nSamples", "meanDiffDec", "binSize"), envir=environment()) + require(dplyr) + require(foreach) + require(doParallel) + + +chk <- Sys.getenv("_R_CHECK_LIMIT_CORES_", "") + +if (nzchar(chk) && chk == "TRUE") { + no_cores <- 2L +} else { + no_cores <- parallel::detectCores() - 1 +} + + cl <- parallel::makeForkCluster(no_cores) + doParallel::registerDoParallel(cl) + parallel::clusterExport(cl, c("allSDs", "nSamples", "meanDiffDec", "binSize"), envir=environment()) samples <- seq(nSamples/100, nSamples, nSamples/100) diff --git a/R/plotFunction.r b/R/plotFunction.r index 39b86a9..9b37fe8 100644 --- a/R/plotFunction.r +++ b/R/plotFunction.r @@ -10,8 +10,10 @@ #' @import ggplot2 #' @importFrom reshape2 melt #' -#' @examples -#' myPlot <- plotPower(allProps, "samples") + +# Example usage: + +# myPlot <- plotPower(allProps, "samples") plotPower <- function(resFile, calcType){ diff --git a/man/calcDiff.Rd b/man/calcDiff.Rd index ea72912..6c42262 100644 --- a/man/calcDiff.Rd +++ b/man/calcDiff.Rd @@ -25,5 +25,4 @@ Calculate the percentage difference in methylation detectable from the specified } \examples{ allSamples <- calcDiff(allSDs, dataType = "SDs") -allSamples <- calcDiff(betas, phenoFile, nSamples = 100, dataType = "betaMatrix") } diff --git a/man/calcProps.Rd b/man/calcProps.Rd index 6f4d57a..e32472e 100644 --- a/man/calcProps.Rd +++ b/man/calcProps.Rd @@ -15,6 +15,3 @@ calcProps(allCellRes) \description{ Calculates the proportion of probes (CpGs) with power > 0.8 } -\examples{ -allProps <-calcProps(allSamples) -} diff --git a/man/calcSamples.Rd b/man/calcSamples.Rd index d54df5c..14b2d9e 100644 --- a/man/calcSamples.Rd +++ b/man/calcSamples.Rd @@ -25,5 +25,4 @@ Calculate the number of samples needed in each group to have power to detect the } \examples{ allSamples <- calcSamples(allSDs, dataType = "SDs") -allSamples <- calcSamples(betas, phenoFile, meanDiff = 5, dataType = "betaMatrix") } diff --git a/man/plotPower.Rd b/man/plotPower.Rd index 1def42f..fb32c1c 100644 --- a/man/plotPower.Rd +++ b/man/plotPower.Rd @@ -19,6 +19,3 @@ calcDiff() was used).} \description{ plot power curve } -\examples{ -myPlot <- plotPower(allProps, "samples") -} diff --git a/vignettes/BrainPower-vignette.Rmd b/vignettes/CellPower-vignette.Rmd similarity index 75% rename from vignettes/BrainPower-vignette.Rmd rename to vignettes/CellPower-vignette.Rmd index 9ab9a69..5c3706c 100644 --- a/vignettes/BrainPower-vignette.Rmd +++ b/vignettes/CellPower-vignette.Rmd @@ -1,11 +1,11 @@ --- -title: "BrainPower-vignette" +title: "CellPower-vignette" author: name: Emma Walker affiliation: University of Exeter email: E.M.Walker@exeter.ac.uk abstract: > - A tutorial for using the Brain Power package for power calculations of cell type specific array data + A tutorial for using the CellPower package for power calculations of cell type specific array data output: BiocStyle::html_document: toc_float: true @@ -15,18 +15,33 @@ vignette: > %\VignetteEncoding{UTF-8} --- -# How to use the BrainPower package +```{r setup, include=FALSE} -## Installing CETYGO +if (!requireNamespace("BiocManager", quietly = TRUE)) { + install.packages("BiocManager") +} -The BrainPower package is available via GitHub +# Now install BiocStyle +BiocManager::install("BiocStyle") + +library(BiocStyle) + +``` + + + +# How to use the CellPower package + +## Installing CellPower + +The CellPower package is available via GitHub and can be installed using the devtools package. However, there are some pre-requistite packages that need to be installed from Bioconductor first. #### -Now we can install BrainPower direct from GitHub. +Now we can install CellPower direct from GitHub. Once installed we can load the package. @@ -58,6 +73,17 @@ The plot below shows how the proportion of CpGs with > 0.8 power changes as binS allSamples <- calcSamples(allSDs, meanDiff = 5, dataType = "SDs") +#calculating the proportion of CpGs with > 0.8 power + +allProps <- calcProps(allSamples) + + + +#visualising the proportion of CpGs with > 0.8 power + +myPlot <- plotPower(allProps, "samples") + + ## add link to power calcualtions tutorial