Skip to content

Commit

Permalink
Merge 894ba78 into db7cbf7
Browse files Browse the repository at this point in the history
  • Loading branch information
soelmicheletti committed Nov 9, 2023
2 parents db7cbf7 + 894ba78 commit 390ec35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Imports:
cmdstanr,
GeneNet,
loo,
rARPACK
rARPACK,
corpcor
License: GPL-3
Encoding: UTF-8
LazyData: false
Expand Down
50 changes: 35 additions & 15 deletions R/COBRA.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' Inputs:
#' @param X : design matrix of size (n, q), n = number of samples, q = number of covariates
#' @param expressionData : gene expression as a matrix of size (g, n), g = number of genes
#' @param standardize : boolean flag to standardize the gene expression as a pre-processing step
#' @param method : if pearson, the decomposition of the co-expression matrix is compouted. If pcorsh, COBRA decomposes the regularized partial co-expression
#'
#' Outputs:
#' @return psi : impact of each covariate on the eigenvalues as a matrix of size (q, n)
Expand All @@ -31,32 +31,52 @@
#'
#' @export

cobra <- function(X, expressionData, standardize=T){
numSamples <- ncol(expressionData)
N <- min(ncol(expressionData),nrow(expressionData))
cobra <- function(X, expressionData, method = "pearson"){

if(!(method %in% c("pearson", "pcorsh", "dragon"))){
stop("Only Pearson and pcor methods are supported. Please make sure to provide a valid method argument.")

Check warning on line 37 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L36-L37

Added lines #L36 - L37 were not covered by tests
}

if (standardize){
numSamples <- ncol(expressionData)
if(method != "dragon"){
N <- min(ncol(expressionData),nrow(expressionData))

Check warning on line 42 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L40-L42

Added lines #L40 - L42 were not covered by tests
}
C <- 0
if(method == "pearson"){

Check warning on line 45 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L44-L45

Added lines #L44 - L45 were not covered by tests
G_star <- expressionData-rowMeans(expressionData)
G <- (G_star/sqrt(rowSums(G_star^2)))
G <- as.matrix(G)
} else {
G <- expressionData
G <- (G/sqrt(rowSums(G^2)))
G <- as.matrix(G)
expressionData <- (G_star/sqrt(rowSums(G_star^2)))
expressionData <- as.matrix(expressionData)
C <- tcrossprod(expressionData)

Check warning on line 49 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L47-L49

Added lines #L47 - L49 were not covered by tests
}
if(method == "dragon"){
if(length(expressionData) != 2){
stop("Dragon needs two layers, please provide a list of two matrices in expressionData or consider using the pcorsh argument")

Check warning on line 53 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L51-L53

Added lines #L51 - L53 were not covered by tests
}
if(dim(expressionData[[1]])[2] != dim(expressionData[[2]])[2]){
stop("Dragon layers in expressionData list must have the same number of samples")

Check warning on line 56 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L55-L56

Added lines #L55 - L56 were not covered by tests
}
C <- dragon(t(expressionData[[1]]), layer2 = t(expressionData[[2]]), pval=FALSE)$cov
N <- dim(expressionData[[1]])[2]
numSamples <- N
expressionData <- rbind(expressionData[[1]], expressionData[[2]])
print(dim(expressionData))

Check warning on line 62 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L58-L62

Added lines #L58 - L62 were not covered by tests
}
if(method == "pcorsh"){
C <- matrix(as.numeric(pcor.shrink(t(expressionData))), dim(expressionData)[1], dim(expressionData)[1])

Check warning on line 65 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L64-L65

Added lines #L64 - L65 were not covered by tests
}

eigenG <- rARPACK::eigs_sym(tcrossprod(G),N)
eigenG <- rARPACK::eigs_sym(C,N)

Check warning on line 68 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L68

Added line #L68 was not covered by tests

Q <- eigenG$vectors
D <- diag(eigenG$values)

hatmat <- ginv(crossprod(X))%*%t(X)
Qinv <- ginv(Q)
QinvG <- Qinv%*%(G)
QinvG <- Qinv%*%(expressionData)

Check warning on line 75 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L75

Added line #L75 was not covered by tests

est <- t(sapply(seq_len(nrow(hatmat)), function(hatmatRow){
diag(QinvG%*%(numSamples*diag(hatmat[hatmatRow,]))%*%t(QinvG))
}))

list(psi=est, Q=Q, D=eigenG$values, G=G)
}
list(psi=est, Q=Q, D=eigenG$values, G=expressionData)

Check warning on line 81 in R/COBRA.R

View check run for this annotation

Codecov / codecov/patch

R/COBRA.R#L81

Added line #L81 was not covered by tests
}

0 comments on commit 390ec35

Please sign in to comment.