R/run_corr.R

Defines functions run_corr

Documented in run_corr

#' Wrapper for correlation co-expression
#' 
#' Conducts co-expression analysis using correlation for association measure.
#' @param x The n by p matrix of counts.
#' @param threshold Cutoff for significant associations. If NULL, all correlations
#' are returned. Otherwise, correlations of magnitude at or below this threshold are 
#' set to zero.
#' @param method The method used to compute correlations. Should be either "pearson"
#'  or "spearman". The default is Spearman, which provides 
#'  the more conservative estimation of associations.
#' @return A p by p matrix of association scores.
#' @export
run_corr <- function(x, threshold = NULL, method = "pearson", ...) {
  if(!(method %in% c("pearson", "spearman"))) {
    stop('method should be one of c("pearson", "spearman").')
  }
  
  if(is.integer(x[1, 1])) {
    x <- x + 0.0
  }
  scores <- cor(x, method = method)
  scores[is.na(scores) | is.nan(scores)] <- 0
  diag(scores) <- 0
  
  if(!is.null(threshold)) {
    scores[abs(scores) <= threshold] <- 0
  }

  colnames(scores) <- colnames(x)
  
  return(scores)
}
tgrimes/dnapath2 documentation built on May 21, 2020, 5:53 p.m.