R/HeatMapTree.R

Defines functions HeatMapTree

Documented in HeatMapTree

#' Heatmap representation of markers median values in meta-clusters.
#'
#' @param TreeMetaCl Object generated by the function BuildFSOMTree
#' @param Markers vector of Markers
#' @param clustering if true, a hierarichal clustring is applied to the heatmap
#' @return a heatmap figure
#' @export

HeatMapTree = function(TreeMetaCl,Markers,clustering = T) {
  if (length(intersect(Markers,TreeMetaCl$fSOMTree$prettyColnames)) < 1) {stop("Marker not found")}
  print(paste("Use markers ",paste(intersect(Markers,TreeMetaCl$fSOMTree$prettyColnames),collapse=" "),sep=""))
  MedianList = unlist(sapply(intersect(Markers,TreeMetaCl$fSOMTree$prettyColnames),function(Marker){
    MarkerIndex=which(TreeMetaCl$fSOMTree$prettyColnames == Marker)
    unlist(sapply(unique(TreeMetaCl$metaCl),function(MetaCl){
      clusterList=which(TreeMetaCl$metaCl == MetaCl)
      metaClustIndices=unlist(sapply(clusterList,
                                     function(cluster){which(TreeMetaCl$fSOMTree$map$mapping[,1] == cluster)}))
      return(median(TreeMetaCl$fSOMTree$data[metaClustIndices,MarkerIndex],na.rm=T))
    }))
  }))

  MedianMatrix=matrix(MedianList,nrow=length(unique(TreeMetaCl$metaCl)),
                      dimnames = list(unique(TreeMetaCl$metaCl),intersect(Markers,TreeMetaCl$fSOMTree$prettyColnames)))

  colMarginSize=20-18*exp(-max(sapply(colnames(MedianMatrix),nchar))/10)
  rowMarginSize=20-18*exp(-max(sapply(row.names(MedianMatrix),nchar))/10)
  colCex4Plot=exp(-max(sapply(colnames(MedianMatrix),nchar))/70)*exp(-length(colnames(MedianMatrix))/50)
  rowCex4Plot=exp(-max(sapply(row.names(MedianMatrix),nchar))/70)*exp(-length(row.names(MedianMatrix))/50)



  if (clustering) {
  gplots::heatmap.2(MedianMatrix,scale = "column",col = gplots::bluered(100),trace = "none",density.info = "none",
                    margins=c(colMarginSize,rowMarginSize),cexRow = rowCex4Plot,cexCol=colCex4Plot)}
  else {gplots::heatmap.2(MedianMatrix,scale = "column",col = gplots::bluered(100),trace = "none",density.info = "none",
    Rowv=F,Colv=F,dendrogram = "none",margins=c(colMarginSize,rowMarginSize),cexRow = rowCex4Plot,cexCol=colCex4Plot)}
}
gautierstoll/CytoSOM documentation built on May 16, 2020, 10:36 a.m.