R/neighbor_finder.R

Defines functions neighbor_finder

Documented in neighbor_finder

#' neighbor_finder
#'
#' This function allows the user to find neighbor genes network of interested gene.
#' @param countdata RNAseq countdata
#' @param gene string, gene of interest to find a network for.
#' @param cor.cut.off cut off value of correlation coeffiecint to build network. Defaults to .39
#' @param weight.cut.off cut off value of network weight to reduce edges. Defaults to .5
#' @return character vector, genes that are considered neighbors of the gene of interest (including the gene of interest itself).
#' @examples
#' example.file <- system.file("extdata", "example.RData", package="cgNetwork")
#' load(example.file)
#' common_neighbor <- neighbor_finder(countdata, "CDKN1A", 0.39, 0.5)
#' @export
#' @import bigmemory
#' @importFrom igraph graph_from_adjacency_matrix simplify delete_edges neighbors E V
#' @importFrom stats cor na.omit sd

neighbor_finder <- function(countdata, gene=gene_of_interest, cor.cut.off=.39, weight.cut.off=.5){

  countdata <- data.frame(na.omit(countdata))
  countdata_t <- data.frame(t(countdata))
  countdata_t <- countdata_t[, !sapply(countdata_t, function(x) { sd(x) == 0} )]

  big_cor_matrix <- as.big.matrix(cor(countdata_t, method = "spearman"))

  #check if the gene is at all present. Otherwise there would be a calculation first (which might take long) and then the error.
  if(!any(rownames(big_cor_matrix[,]) %in% gene)) stop("Your gene of interest is not in the matrix.")

  big_cor_matrix[which(big_cor_matrix[,] == 1)] <- 0
  big_cor_matrix[which(big_cor_matrix[,] < cor.cut.off)] <- 0
  net <- graph_from_adjacency_matrix(big_cor_matrix[,], mode='undirected', weighted = T, diag=F)
  net <- simplify(net, remove.multiple = T, remove.loops = T)
  net.sp <- delete_edges(net, E(net)[weight<weight.cut.off])
  neigh.nodes <- neighbors(net.sp, V(net.sp)[gene])
  common_neig <- c(names(neigh.nodes),gene)
  return(common_neig)
}
ilwookkim/TCGANetwork documentation built on March 31, 2021, 4:03 p.m.