R/drawModules.R

Defines functions drawModules

Documented in drawModules

#' Draw Modules
#'
#'
#' @param graph an \code{\link[igraph]{igraph}} object, which can be generated by
#' \code{\link{generateNetwork}}.
#' @param title a name for the Cytoscape session.
#' @param summary a list containing the modules and nodes
#' @param close.cycnets.afterwards TRUE, if the windows in the cytoscape environment should be closed
#' after drawing. This might be useful repeated function call.
#' @param save.image TRUE, if modules should be saved as png files.
#' @param modules.to.draw a vector containing the IDs of the modules, that should be drawn.
#' If \code{NULL}, all modules will be drawn.
#' @param only.overview.network TRUE if only the overall network should be drawn,
#' but not the individual networks for the modules
#'
#' @import RCy3
#' @import data.table
#' @import igraph
#' @importFrom grDevices rainbow
#' @export drawModules
#' @usage drawModules(graph, summary, title="",
#' close.cycnets.afterwards=FALSE, save.image=FALSE, modules.to.draw=NULL,
#' only.overview.network = TRUE)
#' @examples
#' data(qmdiab.data)
#' data(qmdiab.annos)
#' data(qmdiab.phenos)
#' 
#' data <- qmdiab.data[, 1:75]
#' annotations <- qmdiab.annos[1:75]
#' 
#' net_graph <- generateNetwork(data = data, annotations = annotations)
#' mods <- identifyModules(
#'   graph = net_graph, data = data, annotations = annotations,
#'   phenotype = qmdiab.phenos$T2D
#' )
#' \donttest{
#' drawModules(
#'   graph = net_graph, summary = mods, title = "modules",
#'   save.image = FALSE
#' )
#' }
#' 
#' @references
#' \insertRef{Shannon2003}{MoDentify}
#' @references
#' \insertRef{Smoot2011}{MoDentify}
#' @references
#' \insertRef{RCy3}{MoDentify}

drawModules <- function(graph, summary, title = "", close.cycnets.afterwards = FALSE,
                        save.image = FALSE, modules.to.draw = NULL, only.overview.network = TRUE) {
  message("Cytoscape output could take a few minutes...")

  nodes <- summary$nodes
  modules <- summary$modules

  nodes <- nodes[, .(moduleID, nodeID, name, label, order.added,
    node.pval = seed.score,
    after.adding.pval = exp(-score.after.adding)
  )]
  nodes[, is.significant := node.pval < (0.05 / vcount(graph))]
  nodes[is.na(moduleID), moduleID := 0]
  if (is.null(modules.to.draw)) {
    modules.to.draw <- modules$moduleID
  }

  colors <- rainbow(length(modules.to.draw))
  names(colors) <- modules.to.draw

  # supressWarnings because of Skipping names on vector!
  suppressWarnings(drawNetworkWithModules(
    graph = graph, title = title,
    nodes = nodes, colors = colors,
    save.image = save.image,
    close.cycnets.afterwards = close.cycnets.afterwards
  ))

  if (!only.overview.network | save.image) {
    # supressWarnings because of Skipping names on vector
    l <- suppressWarnings(lapply(modules.to.draw, drawModule,
      graph = graph, title = title,
      nodes = nodes[!is.na(moduleID)], colors = colors,
      save.image = save.image,
      close.cycnets.afterwards = close.cycnets.afterwards
    ))
  }


  # if(close.cycnets.afterwards){
  #   deleteAllWindows(cy)
  # }
}
krumsiek/MoDentify documentation built on March 25, 2021, 8:32 a.m.