From 794b7cb7b8196743123916f00849d06a62445cf0 Mon Sep 17 00:00:00 2001 From: Haim Bar Date: Tue, 16 Mar 2021 10:28:46 -0400 Subject: [PATCH] Add a title option the the cluster plot, as well as edge colors (negative/positive) correlations --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/edgefinder.R | 26 ++++++++++++++++++++++---- man/plotCluster.Rd | 12 +++++++++++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 58c8db0..45c4364 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: edgefinder Type: Package Title: Detect Edges in Sparse Co-expression Graphs -Version: 0.1.8 +Version: 0.1.9 Author: Haim Bar and Seojin Bang Maintainer: Haim Bar Description: Finding edges in co-expression graphs, based on "A Mixture Model to Detect Edges in Sparse Co-expression Graphs", Haim Bar and Seojin Bang. See more details in the vignettes. diff --git a/NAMESPACE b/NAMESPACE index c66d5a0..bb26f9b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,4 +15,5 @@ import(graphics) import(stats) importFrom(Matrix,Matrix) importFrom(grDevices,col2rgb) +importFrom(grDevices,colours) importFrom(grDevices,rgb) diff --git a/R/edgefinder.R b/R/edgefinder.R index 5382dba..8a4e6b8 100644 --- a/R/edgefinder.R +++ b/R/edgefinder.R @@ -40,6 +40,7 @@ #' @importFrom Matrix Matrix #' @importFrom grDevices rgb #' @importFrom grDevices col2rgb +#' @importFrom grDevices colours #' @examples #' \donttest{ #' data(WT) @@ -604,6 +605,9 @@ plotBitmapCC <- function(AdjMat, clusterInfo=NULL, orderByCluster=FALSE, showMin #' @param labels If set to TRUE, show node names (default=FALSE). #' @param nodecol The color(s) of the nodes. Can be a single value or a vector of length equal to the number of rows in AdjMat #' @param labelsize Text size of node labels. +#' @param figtitle The title of the plot (default=NULL). +#' @param negcor The pairs which are negatively correlated, to be drawn as red edges (default=NULL). If set to null, all edges will have the same color. +#' @param edgecols The colors to be used for edges. Default="grey88". If one value is given, all edges will be drawn using this color. If negcor is used to specify which edges are negatively correlated and edgecol contains two valid colors, the first is used for positive correlations, and the second for negative ones. #' @export #' @examples #' \donttest{ @@ -611,12 +615,21 @@ plotBitmapCC <- function(AdjMat, clusterInfo=NULL, orderByCluster=FALSE, showMin #' WTres <- edgefinder(WT, ttl = "Wild Type") #' WTComp <- graphComponents(WTres$AdjMat) #' plotCluster(WTres$AdjMat, 5, WTComp) +#' # plotCluster(WTres$AdjMat, 5, WTComp, negcor = WTres$lt, edgecols = c("grey88","orange")) #' } -plotCluster <- function(AdjMat, clustNo, clusterInfo=NULL, labels=FALSE, nodecol="blue",labelsize=1) { +plotCluster <- function(AdjMat, clustNo, clusterInfo=NULL, labels=FALSE, nodecol="blue", labelsize=1, figtitle=NULL, negcor=NULL, edgecols="grey88") { if(is.null(clusterInfo)) clusterInfo <- graphComponents(AdjMat) if(length(nodecol) < nrow(AdjMat)) nodecol <- rep(nodecol[1],length=nrow(AdjMat)) + if (length(negcor) > 0) { + tmpmat <- Matrix::Matrix(0,nrow(AdjMat), ncol(AdjMat)) + vec <- rep(0,choose(nrow(AdjMat), 2)) + vec[negcor] <- -2 + tmpmat[upper.tri(tmpmat)] <- vec + AdjMat <- AdjMat + (tmpmat+Matrix::t(tmpmat)) + } + ids <- which(clusterInfo$clustNo == clustNo) if (length(ids) > 0) { tmpA <- AdjMat[ids,ids] @@ -633,14 +646,19 @@ plotCluster <- function(AdjMat, clustNo, clusterInfo=NULL, labels=FALSE, nodecol opacity <- opacity/max(opacity) nodecol <- rgb(t(col2rgb(nodecol)/255),alpha=opacity)[ids] plot(rads*cos(thetas), rads*sin(thetas),cex=sizes*3, pch=19,axes=F, - xlab="",ylab="",col=nodecol) + xlab="",ylab="",col=nodecol, main=figtitle) for (i in 1:ncol(tmpA)) { - nbrs <- setdiff(which(tmpA[i,] == 1), 1:i) + nbrs <- setdiff(which(abs(tmpA[i,]) == 1), 1:i) if(length(nbrs) > 0) { + edgecol <- rep(edgecols[1], ncol(tmpA)) + edgecol[which(tmpA[i,nbrs] == -1)] <- "deepskyblue4" + if (edgecols[2] %in% colours()) { + edgecol[which(tmpA[i,nbrs] == -1)] <- edgecols[2] + } for (j in nbrs) { lines(c(rads[i]*cos(thetas[i]), rads[j]*cos(thetas[j])), c(rads[i]*sin(thetas[i]), rads[j]*sin(thetas[j])), - col="grey88", lwd=0.5) + col=edgecol[j], lwd=0.5) } } } diff --git a/man/plotCluster.Rd b/man/plotCluster.Rd index 4d148bb..2d4e830 100644 --- a/man/plotCluster.Rd +++ b/man/plotCluster.Rd @@ -10,7 +10,10 @@ plotCluster( clusterInfo = NULL, labels = FALSE, nodecol = "blue", - labelsize = 1 + labelsize = 1, + figtitle = NULL, + negcor = NULL, + edgecols = "grey88" ) } \arguments{ @@ -25,6 +28,12 @@ plotCluster( \item{nodecol}{The color(s) of the nodes. Can be a single value or a vector of length equal to the number of rows in AdjMat} \item{labelsize}{Text size of node labels.} + +\item{figtitle}{The title of the plot (default=NULL).} + +\item{negcor}{The pairs which are negatively correlated, to be drawn as red edges (default=NULL). If set to null, all edges will have the same color.} + +\item{edgecols}{The colors to be used for edges. Default="grey88". If one value is given, all edges will be drawn using this color. If negcor is used to specify which edges are negatively correlated and edgecol contains two valid colors, the first is used for positive correlations, and the second for negative ones.} } \description{ Plot a cluster network with all the nodes and edges - the central node is marked by a black circle. The radius of each point corresponds to its degree. The opacity corresponds to the percentage of edges from the node that is in the cluster (the darker it is, the larger the percentage of edges is within the cluster.) The distance from the center corresponds to the relative dissimilarity with the central node. This is computed as the number of neighbors the node and the central node do not have in common. @@ -35,5 +44,6 @@ Plot a cluster network with all the nodes and edges - the central node is marked WTres <- edgefinder(WT, ttl = "Wild Type") WTComp <- graphComponents(WTres$AdjMat) plotCluster(WTres$AdjMat, 5, WTComp) + # plotCluster(WTres$AdjMat, 5, WTComp, negcor = WTres$lt, edgecols = c("grey88","orange")) } }