Skip to content

Commit

Permalink
Fix roxygen note. Compress data. Update rworkflows yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
bschilder committed Mar 2, 2023
1 parent b1f49d0 commit c5fa944
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 164 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/rworkflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ jobs:
matrix:
config:
- os: ubuntu-latest
r: devel
bioc: devel
r: auto
cont: bioconductor/bioconductor_docker:devel
rspm: https://packagemanager.rstudio.com/cran/__linux__/focal/release
- os: macOS-latest
r: latest
bioc: release
r: auto
cont: ~
rspm: ~
- os: windows-latest
r: latest
bioc: release
r: auto
cont: ~
rspm: ~
steps:
- uses: neurogenomics/rworkflows@master
with:
Expand All @@ -41,6 +45,11 @@ jobs:
run_covr: ${{ true }}
run_pkgdown: ${{ true }}
has_runit: ${{ false }}
has_latex: ${{ false }}
GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}
run_docker: ${{ true }}
docker_user: bschilder
docker_org: neurogenomicslab
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
runner_os: ${{ runner.os }}
cache_version: cache-v1
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ThreeWayTest
Type: Package
Title: ThreeWayTest
Version: 1.0.0
Version: 1.0.1
Authors@R: c(
person(given = "Deliang",
family = "Bu",
Expand All @@ -14,10 +14,11 @@ Description: Summary statistics-based association test for
URL: https://github.com/bschilder/ThreeWayTest
BugReports: https://github.com/bschilder/ThreeWayTest/issues
Encoding: UTF-8
Depends: R (>= 4.0.2)
Depends: R (>= 4.0.0)
biocViews:
Imports:
MASS,
methods
Suggests:
rworkflows,
markdown,
Expand All @@ -34,3 +35,4 @@ VignetteBuilder: knitr
License: GPL-3
Config/testthat/edition: 3
LazyData: true
LazyDataCompression: gzip
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export(generate_null_distribution_T3)
export(metaCCA)
importFrom(MASS,ginv)
importFrom(MASS,mvrnorm)
importFrom(stats,pchisq)
19 changes: 18 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# templateR 0.99.0
# ThreeWayTest 1.0.1

## New features

* Add hex sticker!

## Bug fixes

* Shorten function widths
* Shorten Roxygen note widths
* Remove `templateR` instructions from README
* Recompress built-in data with `tools::resaveRdaFiles`
* Fix `Dependence on R version ‘4.0.2’ not with patchlevel 0`
* Rename vignette *getting_started.Rmd* to *ThreeWayTest.Rmd* convention.



# ThreeWayTest 0.99.0

## New features

Expand Down
88 changes: 59 additions & 29 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @examples
#' ThreeWayTest::gen_autoregressive(6,0.5)
gen_autoregressive<-function(m,
rho){
rho){
cov_mat<-matrix(0,m,m)
for (i in 1:m){
for (j in 1:m){
Expand All @@ -22,22 +22,26 @@ gen_autoregressive<-function(m,
#' MGAS
#'
#' This is the method proposed by Van_MGAS
#' @param z_vector Column vectorized data matrix with rows represent phenotype and columns represent genotype.
#' @param z_vector Column vectorized data matrix with rows represent
#' phenotype and columns represent genotype.
#' @param est_genetic_cor Estimated phenotype correlation matrix.
#' @param est_pheno_cor Estimated genotype correlation matrix.
#' @returns A numeric value represents the p value of MGAS.
#'
#' @export
#' @importFrom stats pchisq
#' @examples
#' z_vector<-MASS::mvrnorm(1,mu=rep(0,9),Sigma = diag(nrow = 9, ncol = 9))
#' genotype_covariance<-diag(nrow = 3,ncol = 3)
#' phenotype_covariance<-diag(nrow = 3,ncol = 3)
#' ThreeWayTest::MGAS(z_vector=z_vector,
#' est_genetic_cor = genotype_covariance, est_pheno_cor =phenotype_covariance)
#' ThreeWayTest::MGAS(z_vector=z_vector,
#' est_genetic_cor = genotype_covariance,
#' est_pheno_cor =phenotype_covariance)
MGAS<-function(z_vector,est_genetic_cor,est_pheno_cor){
X <- kronecker(est_genetic_cor,est_pheno_cor,FUN = "*")
beta = c(0.3867,0.0021,-0.1347,-0.0104,0.7276,0.0068)
p_value<-stats::pchisq(z_vector^2,1,lower.tail = F)
tmp=sort(p_value,index.return=T)
p_value<-stats::pchisq(z_vector^2,1,lower.tail = FALSE)
tmp=sort(p_value,index.return=TRUE)
pj=tmp$x
iorder=tmp$ix
length = ncol(est_genetic_cor)*ncol(est_pheno_cor)
Expand All @@ -48,7 +52,8 @@ MGAS<-function(z_vector,est_genetic_cor,est_pheno_cor){
for (i2 in 1:i1) {
if (i1>i2) {
er=r2[i1,i2]
ro[i1,i2]=ro[i2,i1]= beta[1]*er^6+beta[2]*er^5+beta[3]*er^4+beta[4]*er^3+beta[5]*er^2+beta[6]*er
ro[i1,i2]=ro[i2,i1]= beta[1]*er^6+beta[2]*er^5+beta[3]*er^4+beta[4]*
er^3+beta[5]*er^2+beta[6]*er
}}}
alllam=eigen(ro[1:length,1:length])$values
qepj=length
Expand Down Expand Up @@ -157,7 +162,8 @@ shrinkPSD <- function(R,
#' This is the main function of metaCCA.
#' @param est_genetic_cov Estimated genotype correlation matrix.
#' @param est_pheno_cov Estimated phenotype correlation matrix.
#' @param est_XY_cov Estimated correlation matrix between genotype and phenotype.
#' @param est_XY_cov Estimated correlation matrix between
#' genotype and phenotype.
#' @param N Sample size.
#' @returns A numeric value represents the p value of metaCCA.
#' @export
Expand All @@ -171,7 +177,8 @@ metaCCA <- function(est_genetic_cov,
est_pheno_cov,
est_XY_cov,
N){
M<-rbind(cbind(est_genetic_cov,est_XY_cov),cbind(t(est_XY_cov),est_pheno_cov))
M<-rbind(cbind(est_genetic_cov,est_XY_cov),
cbind(t(est_XY_cov),est_pheno_cov))
M_shrink <- shrinkPSD(M,ncol(est_genetic_cov))
p_metacca<-myCCA(M_shrink[[1]],M_shrink[[2]],M_shrink[[3]],N)
return(p_metacca[1])
Expand All @@ -181,14 +188,18 @@ metaCCA <- function(est_genetic_cov,
#' chisq_test
#'
#' This is traditional chisq_test.
#' @param z_vector Column vectorized data matrix with rows represent phenotype and columns represent genotype.
#' @param z_vector Column vectorized data matrix with rows represent
#' phenotype and columns represent genotype.
#' @param cov_mat Estimated covariance matrix of z_vector.
#' @returns A numeric value represent the p value of chi-square test.
#' @importFrom MASS ginv
#' @export
#' @examples
#' z_vector<-MASS::mvrnorm(1,mu=rep(0,9),Sigma = diag(nrow = 9, ncol = 9))
#' ThreeWayTest::chisq_test(z_vector=z_vector, cov_mat=diag(nrow = 9, ncol = 9))
#' z_vector<-MASS::mvrnorm(n = 1,
#' mu=rep(0,9),
#' Sigma = diag(nrow = 9, ncol = 9))
#' ThreeWayTest::chisq_test(z_vector=z_vector,
#' cov_mat=diag(nrow = 9, ncol = 9))
chisq_test<-function(z_vector,
cov_mat){
chi_stat<-t(z_vector)%*%MASS::ginv(cov_mat)%*%z_vector
Expand All @@ -200,7 +211,8 @@ chisq_test<-function(z_vector,
#' T_eta
#'
#' Calculation of T_eta with eta fixed.
#' @param z_vector Column vectorized data matrix with rows represent phenotype and columns represent genotype.
#' @param z_vector Column vectorized data matrix with rows represent
#' phenotype and columns represent genotype.
#' @param cov_mat Estimated covariance matrix of z_vector.
#' @param eta Truncated value.
#' @returns A numeric value represent calculated statistic T_eta.
Expand All @@ -211,8 +223,8 @@ chisq_test<-function(z_vector,
#' ThreeWayTest::T_eta(z_vector=z_vector,
#' cov_mat=diag(nrow = 9, ncol = 9), eta=0.5)
T_eta<-function(z_vector,
cov_mat,
eta){
cov_mat,
eta){
index<-ceiling(length(z_vector)*eta)
z_vector_order<-order(abs(z_vector),decreasing = TRUE)
ordered_z_vector<-z_vector[z_vector_order]
Expand All @@ -226,13 +238,15 @@ T_eta<-function(z_vector,

#' generate_null_distribution_T3
#'
#' This function generate the null distribution of T_eta, should be run before calculating TWT.
#' This function generate the null distribution of T_eta,
#' should be run before calculating TWT.
#' Please see Setp 2 in our manuscript for more information.
#' @param m Dimension.
#' @param n Number of the generated sample size.
#' @param cov_mat Covariance matrix for generating random samples.
#' @param cutoff_value Truncated value, should be in 0 to 1 and must contain 1.
#' @returns Null distribution matrix with number of rows equal n and number of columns equal length of cutoff value minus 1.
#' @returns Null distribution matrix with number of rows equal n and number of
#' columns equal length of cutoff value minus 1.
#' @importFrom MASS mvrnorm
#' @export
#' @examples
Expand All @@ -247,15 +261,17 @@ generate_null_distribution_T3<-function(m,
null_distribution_matrix<-matrix(0,nrow = n, ncol = length(cutoff_value)-1)
for (i in 1:(length(cutoff_value)-1)) {
for (j in 1:n) {
null_distribution_matrix[j,i]<-T_eta(bootstrap_data[j,],cov_mat,cutoff_value[i])
null_distribution_matrix[j,i]<-T_eta(z_vector = bootstrap_data[j,],
cov_mat,cutoff_value[i])
}
}
return(null_distribution_matrix)
}

#' coefficient_estimate
#'
#' This function estimate the coeffcient of T3 with null distribution generated by generate_null_distribution_T3.
#' This function estimate the coeffcient of T3 with null distribution
#' generated by generate_null_distribution_T3.
#' Please see Setp 2 in our manuscript for more information.
#' @param null_distribution Generated by generate_null_distribution_T3
#' @returns List of coefficient of alpha, beta and d.
Expand All @@ -278,16 +294,20 @@ coefficient_estimate<-function(null_distribution){

#' approximate_distribution_coefficient_estimate_T3
#'
#' This function estimate the coeffcient of T3 with null distribution generated by generate_null_distribution_T3.
#' This function estimate the coeffcient of T3 with null distribution
#' generated by generate_null_distribution_T3.
#' Please see Setp 2 in our manuscript for more information.
#' @param null_distribution_matrix Generated by generate_null_distribution_T3
#' @returns Coefficient matrix of alpha, beta and d.
#' @export
#' @examples
#' null_distribution<-ThreeWayTest::generate_null_distribution_T3(m=6,n=1000,
#' cov_mat=diag(nrow = 6, ncol = 6), cutoff_value=c(0.2,0.4,0.6,0.8,1))
#' ThreeWayTest::approximate_distribution_coefficient_estimate_T3(null_distribution)
approximate_distribution_coefficient_estimate_T3<-function(null_distribution_matrix){
#' coefficient_matrix <-
#' ThreeWayTest::approximate_distribution_coefficient_estimate_T3(
#' null_distribution_matrix = null_distribution)
approximate_distribution_coefficient_estimate_T3<-
function(null_distribution_matrix){
coefficient_matrix<-matrix(0,nrow = 3,ncol = ncol(null_distribution_matrix))
for (i in 1:ncol(null_distribution_matrix)) {
coefficient<-coefficient_estimate(null_distribution_matrix[,i])
Expand All @@ -301,7 +321,8 @@ approximate_distribution_coefficient_estimate_T3<-function(null_distribution_mat
#' T_3
#'
#' This is the function calculating T3.
#' @param z_vector Column vectorized data matrix with rows represent phenotype and columns represent genotype.
#' @param z_vector Column vectorized data matrix with rows
#' represent phenotype and columns represent genotype.
#' @param cov_mat Estimated covariance matrix of z_vector.
#' @param cutoff_value Vector of truncated value eta.
#' @param coefficient_matrix Matrix of alpha, beta and d.
Expand All @@ -312,7 +333,9 @@ approximate_distribution_coefficient_estimate_T3<-function(null_distribution_mat
#' z_vector<-MASS::mvrnorm(1,mu=rep(0,10), Sigma = diag(nrow = 10, ncol = 10))
#' null_distribution<-ThreeWayTest::generate_null_distribution_T3(m=10,n=1000,
#' cov_mat=diag(nrow = 10,ncol= 10), cutoff_value=c(0.2,0.4,0.6,0.8,1))
#' coefficient_matrix<-ThreeWayTest::approximate_distribution_coefficient_estimate_T3(null_distribution)
#' coefficient_matrix<-
#' ThreeWayTest::approximate_distribution_coefficient_estimate_T3(
#' null_distribution_matrix = null_distribution)
#' ThreeWayTest::T_3(z_vector=z_vector, cov_mat=diag(nrow = 10, ncol = 10),
#' cutoff_value=c(0.2,0.4,0.6,0.8,1), coefficient_matrix=coefficient_matrix)
T_3<-function(z_vector,
Expand All @@ -322,7 +345,9 @@ T_3<-function(z_vector,
pvalue<-rep(0,length(cutoff_value))
for (i in 1:(length(cutoff_value)-1)) {
stat_TWT<-T_eta(z_vector,cov_mat,cutoff_value[i])
pvalue[i]<-stats::pgamma((stat_TWT-coefficient_matrix[2,i])/(coefficient_matrix[1,i]),shape = coefficient_matrix[3,i]/2,rate = 1/2,lower.tail = F)
pvalue[i]<-stats::pgamma(
(stat_TWT-coefficient_matrix[2,i])/(coefficient_matrix[1,i]),
shape = coefficient_matrix[3,i]/2,rate = 1/2,lower.tail = FALSE)
}
pvalue[length(cutoff_value)]<-chisq_test(z_vector,cov_mat)
cauchy_statistic<-(1/length(pvalue))*sum(tan((0.5-pvalue)*pi))
Expand All @@ -333,25 +358,30 @@ T_3<-function(z_vector,
#' TWT
#'
#' This is the method of TWT.
#' @param z_mat is the matrix of z_scores with row number of rows stands for number of phenotypes and number of columns stands for the number of variants.
#' @param z_mat is the matrix of z_scores with row number of rows stands for
#' the number of phenotypes and number of columns
#' stands for the number of variants.
#' @param est_genetic_cor Estimated correlation matrix of genetic variants.
#' @param est_pheno_cor Estimated correlation matrix of phenotypes.
#' @param cutoff_value Set Omega.
#' @param coefficient_matrix Calculated based on function approximate_distribution_coefficient_estimate_T3.
#' @param coefficient_matrix Calculated based on function
#' \link[ThreeWayTest]{approximate_distribution_coefficient_estimate_T3}.
#' @returns p_value_final P_value of TWT.
#' @returns p_1 P_value of T_1.
#' @returns p_2 P_value of T_2.
#' @returns p_3 P_value of T_3.
#'
#' @export
#' @examples
#' z_mat<-MASS::mvrnorm(5,mu=rep(0,5), Sigma = diag(nrow = 5, ncol = 5))
#' null_distribution<-ThreeWayTest::generate_null_distribution_T3(m=25,
#' n=1000,cov_mat=diag(nrow = 25,ncol= 25), cutoff_value=c(0.2,0.4,0.6,0.8,1))
#' coefficient_matrix<-ThreeWayTest::approximate_distribution_coefficient_estimate_T3(null_distribution)
#' coefficient_matrix<-
#' ThreeWayTest::approximate_distribution_coefficient_estimate_T3(
#' null_distribution_matrix = null_distribution)
#' ThreeWayTest::TWT(z_mat=z_mat, est_genetic_cor=diag(nrow = 5, ncol = 5),
#' est_pheno_cor=diag(nrow = 5, ncol = 5), cutoff_value=c(0.2,0.4,0.6,0.8,1),
#' coefficient_matrix=coefficient_matrix)

TWT<-function(z_mat,
est_genetic_cor,
est_pheno_cor,
Expand Down
35 changes: 0 additions & 35 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,6 @@ URL <- read.dcf('DESCRIPTION', fields = 'URL')[1]
owner <- strsplit(URL,"/")[[1]][4]
repo <- strsplit(URL,"/")[[1]][5]
```

*\<---(Start of section that can be deleted after forking the template)---\>*

## Setup

`templateR` is a template designed to make developing new R packages easy by providing:

- **DESCRIPTION, vignettes/, tests/, R/, man/, inst/**: Basic skeleton for the package itself.
- **README.Rmd**: Auto-populates based on the *DESCRIPTION* file (that can be knit to create *README.md*). Also contains [badges](https://github.com/GuangchuangYu/badger) that automatically update themselves.
- **inst/hex/hexSticker.Rmd**: Template for creating [hex stickers](https://github.com/GuangchuangYu/hexSticker).
- **.github/workflows/\*.yml**: [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions) workflow files that will automatically trigger code checks and pushing images to [DockerHub](https://hub-stage.docker.com/) every time you make a push to your GitHub repo (from [`r_workflows`](https://github.com/neurogenomics/r_workflows)).
- **inst/CITATIONS** : Allows users of your package to call `citation("<packageName>")`. Also used to auto-populate the *README.Rmd*.

To get started, simply click on the green **Use this template** button at the top right of the `templateR` repo.

### GitHub Secrets

Before pushing changes to your new R package,
you will need to add two [GitHub Secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets):

1. `PAT_GITHUB`: Speeds up installations and gives access to private repos on GitHub Actions. You can generate your very own Personal Authentication Token using [these instructions](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token).
2. `CODECOV_TOKEN`: Allows [code coverage](https://app.codecov.io/) results to be displayed via the `rworkflows::use_badges()` at the top of the *README.Rmd*. This [Codecov](https://app.codecov.io/) token is repository-specific and can be found [here](https://app.codecov.io/gh/`r owner`/`r pkg`).
3. `DOCKER_TOKEN`: Allows GitHub Actions to push to the [neurogenomicslab DockerHub](https://hub.docker.com/orgs/neurogenomicslab) account. Ask a fellow [Neurogenomics Lab](https://www.neurogenomics.co.uk/) member for this token.


### GitHub Pages

The [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions) workflows included in this template will automatically create a documentation website for your package via [GitHub Pages](https://pages.github.com/). This requires several initial setup steps (you only need to do these once per repo):

1. After *check-bioc-docker.yml* has successfully run on Ubuntu for the first, a new branch will appear in your repo called "gh-pages".
2. In the **Settings** tab of your repo, go to **Pages** on the left. Then under **Source** select `"branch: gh-pages"` and `"root"` from the respective dropdown menus. Finally, click the **Save** button.

*Note*: If you don't have admin access to your repo, you will need to ask someone who does to change this for you.

*\<---(End of section that can be deleted after forking the template)---\>*

# Introduction

Expand Down
Loading

0 comments on commit c5fa944

Please sign in to comment.