R/compareLists.R

Defines functions compareLists

Documented in compareLists

#' Comparing two lists of features
#'
#' Comparing two lists of features (ordered by p-value or logFC...)
#'
#' @param listeA a first vector of features
#' @param listeB a second vector of features
#' @param main common title of the two figures produced
#' @param labelA label for list A
#' @param labelB label for list B
#' @param choice what to plot: curve, venn diagram or both
#' @return A figure with a curve (left) and a Venn diagram (right)
#' @author Hugo Varet
#' @examples
#' listeA <- sort(sample(1:100,50,FALSE))
#' listeB <- sort(sample(1:100,50,FALSE))
#' compareLists(listeA,listeB)
#'
#' listeA <- sort(sample(1:100,10,FALSE))
#' listeB <- sort(sample(1:100,10,FALSE))
#' compareLists(listeA,listeB)
#'
#' listeA <- sort(sample(1:100,50,FALSE))
#' listeB <- sort(sample(1:100,80,FALSE))
#' compareLists(listeA,listeB)
#'
#' listeA <- sort(sample(1:100,80,FALSE))
#' listeB <- sort(sample(1:100,30,FALSE))
#' compareLists(listeA,listeB)

# created Dec 5th, 2013
# modified Feb 4th, 2016
# modified Feb 22nd, 2016
# modified Mar 23rd, 2017

compareLists <- function(listeA,listeB,main="",labelA=NULL,labelB=NULL,choice=c("both","curve","venn")){
  call <- match.call()
  labelA <- ifelse(!is.null(labelA),labelA,call[[2]])
  labelB <- ifelse(!is.null(labelB),labelB,call[[3]])
  choice <- choice[1]  

  main <- ifelse(main=="",paste(labelA,labelB,sep="-"),main)
  if (choice=="both") par(mfrow=c(1,2))

  # courbe selon le rang
  if (choice %in% c("both","curve")){
    stop <- max(length(listeA),length(listeB))
    res <- numeric(stop)
    for (i in 1:stop){
      tmpA <- listeA[1:i]
      tmpB <- listeB[1:i]
      num <- length(intersect(tmpA,tmpB))
      # den <- length(union(tmpA,tmpB))
      res[i] <- num/i
    }
    plot(res,type="l",lwd=2,xlab="rank",ylab="% common features",main=paste0("Curve - ",main),ylim=c(0,1))
    abline(v=length(listeA),lty=2,col="grey")
    abline(v=length(listeB),lty=2,col="grey")
    text(length(listeA),0.99,paste("#", labelA),pos=2)
    text(length(listeB),0.01,paste("#", labelB),pos=2)
  }

  # venn diagramm
  if (choice %in% c("both","venn")){
    areaA <- length(listeA)
    areaB <- length(listeB)
    areaAB <- length(intersect(listeA,listeB))

    rA <- 1
    rB <- 1
    rAB <- 0.5

    draw.circle <- function(x,y,r,...){
      xx <- x + r*cos( seq(0,2*pi, length.out=360) )
      yy <- y + r*sin( seq(0,2*pi, length.out=360) )
      lines(xx,yy,...)
    }
  
    plot(c(-rA+rAB/2,rB-rAB/2),c(0,0),xlim=c(-2*rA,2*rB),ylim=2*max(rA,rB)*c(-1,1),pch=19,
         bty="n",xaxt="n",yaxt="n",xlab="",ylab="",main=paste0("Venn diagram - ",main),col="white")
    # cercles
    draw.circle(x=-rA+rAB/2,y=0,r=rA,col="blue",lwd=2)
    draw.circle(x=rB-rAB/2,y=0,r=rB,col="red",lwd=2)
    # noms des listes
    text(x=-rA+rAB/2,y=1.2*max(rA,rB),labelA,col="blue")
    text(x=rB-rAB/2,y=1.2*max(rA,rB),labelB,col="red")
    # nombre d elements
    text(x=-rA+rAB/2,y=0,areaA-areaAB,col="blue")
    text(x=rB-rAB/2,y=0,areaB-areaAB,col="red")
    text(x=0,y=0,areaAB,col="purple")
  }
}
biomics-pasteur-fr/RNADiff documentation built on Aug. 27, 2020, 12:44 a.m.