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 e38ee38..a532b1e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,15 +12,17 @@ License: GPL-3 URL: https://github.com/ew367/CellPower Encoding: UTF-8 LazyData: true +LazyDataCompression: gzip Imports: doParallel, + parallel, dplyr, foreach, ggplot2, pwr, reshape2, - data.table Suggests: knitr +Depends: R (>= 2.10) VignetteBuilder: knitr -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index 1419df2..254a112 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,3 +4,15 @@ export(calcDiff) export(calcProps) export(calcSamples) export(plotPower) +import(doParallel) +import(foreach) +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 702534a..3751963 100644 --- a/R/calcDiffFunction.r +++ b/R/calcDiffFunction.r @@ -10,10 +10,21 @@ #' #' @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 +#' @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") @@ -62,13 +73,13 @@ 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) # 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)){ @@ -77,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)) @@ -89,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 5f98a8e..3302a01 100644 --- a/R/calcSamplesFunction.r +++ b/R/calcSamplesFunction.r @@ -10,10 +10,21 @@ #' #' @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 +#' @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){ @@ -85,13 +96,13 @@ 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 # 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)){ @@ -102,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) } @@ -115,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 9e8de25..9b37fe8 100644 --- a/R/plotFunction.r +++ b/R/plotFunction.r @@ -7,9 +7,13 @@ #' #' @return - ggplot object with power curve plot #' @export +#' @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