This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Gene Set Enrichment between two dataset

Dear all,

I want to perform gene set enrichment analysis between two datasets. There are packages for GO or KEGG pathway enrichment but I could not find one to calculate enrichment score between user given geneset and genelist. Is there any package for that?

Thank you

gsea rna-seq r language

1 answer

Here you go if you want to use fisher's exact test.

enrich.test = function(PathGenes, interestGenes, totalGenes) {
    ################################################
    ###           | inPath | not inPath |
    ###-------------------------------------------
    ###   Predict |  a     |   b        | nPredict
    ###notPredict |  c     |   d        |
    ###-------------------------------------------
    ###           |  nPath |            | nAll
    ################################################
      ##remove genes not included in reference gene set
      PathGenes = intersect(PathGenes,totalGenes)
      interestGenes = intersect(interestGenes, totalGenes)

      nPath <- length(PathGenes)
      nPredict <- length(interestGenes)
      a <- length(intersect(PathGenes,interestGenes))
      b <- nPredict - a
      c <- nPath - a
      nAll = length(totalGenes)
      d <- nAll - (a + b + c)
      m <- matrix(c(a,b,c,d), ncol=2,byrow=T)
      colnames(m) <- c('inPath','notInPath');rownames(m) <- c('Predict','notPredict')
      p <- fisher.test(m,alternative='greater')$p.value

      ##using phyper for hypergeometric test
      p1 <- phyper(a-1,nPath, nAll-nPath,nPredict,lower.tail=F)
      p2 <-1 - phyper(a-1,nPath, nAll-nPath,nPredict)
      tmp <- c(a,b,c,d,p,p1,p2)


      res = list(mat=m,p=list('fisher.exact'=p,
                              'phyper1'=p1,
                              'phyper2'=p2),
                overlapped=intersect(PathGenes, interestGenes))
      res
    }

Log in to answer this question.