YuLab-SMU / ChIPseeker

:dart: ChIP peak Annotation, Comparison and Visualization
https://onlinelibrary.wiley.com/share/author/GYJGUBYCTRMYJFN2JFZZ?target=10.1002/cpz1.585
219 stars 74 forks source link

Feature Request: upset plots for peak overlaps #195

Open apelonero-GladstoneInstitutes opened 2 years ago

apelonero-GladstoneInstitutes commented 2 years ago

A small request: support for upset plots when visualizing peak overlaps.

I am currently using a modified version of the vennplot function, but having it accessible somewhere in ChIPseeker itself would be very convenient.

This modified function accepts the same inputs as vennplot, but when by = UpSetR the function takes overlapDF and copies rows n times based on the Weight column, subsequently drops the Weight column, and passes the resultant overlapDF to a standard UpSetR plot call. This quick-fix has worked well enough for my purposes, although I admit there are probably better ways to achieve this.

Thanks for considering! See below for function code & a couple example plots.

overlapPlot <- function (Sets, by = "gplots")
{
  if (is.null(names(Sets))) {
    nn <- paste0("Set", seq_along(Sets))
    warning("input is not a named list, set the name automatically to ",
            paste(nn, collapse = " "))
    names(Sets) <- nn
  }
  overlapDF <- overlap(Sets)
  if (by == "Vennerable") {
    pkg <- "Vennerable"
    require(pkg, character.only = TRUE)
    Venn <- eval(parse(text = "Venn"))
    v <- Venn(SetNames = names(Sets), Weight = overlapDF$Weight)
    plotVenn <- eval(parse(text = "Vennerable:::plotVenn"))
    plotVenn(v)
  }
  else if (by == "gplots") {
    n <- ncol(overlapDF)
    colnames(overlapDF)[n] <- "num"
    overlapDF <- overlapDF[, c(n, 1:(n - 1))]
    rownames(overlapDF) = apply(overlapDF, 1, function(i) paste(i[-1],
                                                                sep = "", collapse = ""))
    vennCount <- as.matrix(overlapDF)
    class(vennCount) <- "venn"
    plot.venn(vennCount)
  }
  else if (by == "UpSetR") {
    pkg <- "UpSetR"
    require(pkg, character.only = TRUE)
    n <- ncol(overlapDF)
    overlapDF <- overlapDF[rep(seq(nrow(overlapDF)), overlapDF$Weight),]
    overlapDF <- overlapDF[, c(1:(n-1))]
    upset(overlapDF)
  }
  else {
    stop("not supported...")
  }
}

Screen Shot 2022-08-30 at 3 05 20 PM

Screen Shot 2022-08-30 at 3 46 31 PM