Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
38 changes: 30 additions & 8 deletions R/calcDiffFunction.r
Original file line number Diff line number Diff line change
Expand Up @@ -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")



Expand Down Expand Up @@ -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)){
Expand All @@ -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))

Expand All @@ -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])}
Expand Down
7 changes: 5 additions & 2 deletions R/calcPropsFunction.r
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
39 changes: 31 additions & 8 deletions R/calcSamplesFunction.r
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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)){
Expand All @@ -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)

}

Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions R/plotFunction.r
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
1 change: 0 additions & 1 deletion man/calcDiff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions man/calcProps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/calcSamples.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions man/plotPower.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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