R/make_STRING_igraph.R

Defines functions make_STRING_igraph

Documented in make_STRING_igraph

#' this function takes an input matrix having one column named "gene" 
#' containing genes of interest and a stringDB object
#' it returns an igraph graph of the STRING network containing those genes
#' the node attributes are "name" (STRING id) and "geneName" (original gene names from input matrix)
#' genes that do not have STRING annotation are omitted
#' Phu T. Van, FHCRC 2017, w/ substantial help and input from C.Murie & V.Voillet
#'
#' @param inputMatrix a matrix of gene expression values
#' @param STRINGdbObj a STRINGdbObj generated by connecting via STRINGdb
#' 
#' @return table of genes' mean expression values for each group
#' @examples
#' \dontrun{
#' # DEGout <- topTable(fitBayes, number=nrow(vDat), coef="pttype", sort="P")
#' DEGout$gene <- rownames(DEGout)
#' string_db <- STRINGdb$new(version="10"
#'                           , species=9606
#'                           , score_threshold=0
#'                           , input_directory="/STRINGdb/HomoSapiens/")
#' n <- make_STRING_igraph(DEGout, string_db)
#' plot(n, vertex.label=V(n)$geneName)
#' }
#' @import igraph
#' @import STRINGdb

make_STRING_igraph <- function(inputMatrix, STRINGdbObj){
  # get gene names <-> STRING_id mapping
  smap <- STRINGdbObj$map(inputMatrix, "gene", removeUnmappedRows = TRUE )
  
  # get network of all STRING edges (HUGE!!!)
  stringNet <- STRINGdbObj$get_graph()
  
  # trim the network down to just our genes and potential edges
  smap <- subset(smap, STRING_id%in%V(stringNet)$name)
  g <- induced_subgraph(stringNet, v=smap$STRING_id)
  
  # add the gene names to the vertexes since STRING ids are non-informative
  V(g)$geneName <- smap[match(vertex_attr(g, "name"), smap$STRING_id),]$gene
  
  return(g)
}
ptvan/Pmisc documentation built on Nov. 19, 2020, 10:27 p.m.