├── _config.yml ├── data ├── hsdo.rdata ├── kegg.rdata ├── godata.rdata ├── pfanno.rdata ├── rodata.rdata └── interproanno.rdata ├── src ├── EnrichR.so ├── unique.cpp ├── name_table.cpp ├── hyper.cpp ├── sf.cpp └── RcppExports.cpp ├── R ├── zzz.R ├── overlap.R ├── reverseList.R ├── GO_child.R ├── RcppExports.R ├── getdetail.R ├── DE.R ├── makeown.R ├── annot.R ├── PE.R ├── IE.R ├── GSEA.R ├── RE.R ├── richplot.R ├── KE.R ├── msigdbr.R ├── getrodata.R ├── gnet.R ├── netmap_m.R ├── netmap.R ├── GE.R ├── biomart.R ├── get_org.R └── enrich.R ├── man ├── hello.Rd ├── msigdbinfo.Rd ├── rcpp_hello.Rd ├── dot-getmsig.Rd ├── reverseList.Rd ├── showData.Rd ├── showAvailableRO.Rd ├── getann.Rd ├── showAvailableplantsRO.Rd ├── GO_child.Rd ├── makeROdata.Rd ├── getdetail.Rd ├── makeKOdata.Rd ├── makeplantROdata.Rd ├── DE.Rd ├── makeGOdata.Rd ├── plotgsea.Rd ├── makeOwnGO.Rd ├── showensemble.Rd ├── makeOwn.Rd ├── showplant.Rd ├── idconvert.Rd ├── makeesanno.Rd ├── makeplantann.Rd ├── msigdbr.Rd ├── lheatmap.Rd ├── PE.ensemble.Rd ├── PE.plant.Rd ├── enrich.Rd ├── gsea.Rd ├── GE.Rd ├── IE.ensemble.Rd ├── IE.plant.Rd ├── RE.plant.Rd ├── RE.Rd ├── KE.Rd ├── enrichdot.Rd ├── enrichbar.Rd ├── KE.plot.Rd ├── GE.plot.Rd ├── richplot.Rd ├── mnetmap.Rd ├── gnet.Rd └── netmap.Rd ├── EnrichR.Rproj ├── DESCRIPTION ├── README.md ├── NAMESPACE └── vignettes ├── EnrichR.R └── EnrichR.Rmd /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /data/hsdo.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/data/hsdo.rdata -------------------------------------------------------------------------------- /data/kegg.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/data/kegg.rdata -------------------------------------------------------------------------------- /src/EnrichR.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/src/EnrichR.so -------------------------------------------------------------------------------- /data/godata.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/data/godata.rdata -------------------------------------------------------------------------------- /data/pfanno.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/data/pfanno.rdata -------------------------------------------------------------------------------- /data/rodata.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/data/rodata.rdata -------------------------------------------------------------------------------- /data/interproanno.rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guokai8/EnrichR/HEAD/data/interproanno.rdata -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onLoad <- function(libname, pkgname) { 2 | options(stringsAsFactors = FALSE) # nocov 3 | } 4 | -------------------------------------------------------------------------------- /R/overlap.R: -------------------------------------------------------------------------------- 1 | overlap <- function(df,x, y) { 2 | x <- unlist(x) 3 | y <- unlist(y) 4 | length(intersect(x, y))/length(unique(c(x,y))) 5 | } 6 | -------------------------------------------------------------------------------- /man/hello.Rd: -------------------------------------------------------------------------------- 1 | \name{hello} 2 | \alias{hello} 3 | \title{Hello, World!} 4 | \usage{ 5 | hello() 6 | } 7 | \description{ 8 | Prints 'Hello, world!'. 9 | } 10 | \examples{ 11 | hello() 12 | } 13 | -------------------------------------------------------------------------------- /man/msigdbinfo.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/msigdbr.R 3 | \name{msigdbinfo} 4 | \alias{msigdbinfo} 5 | \title{Print MSIGDB infomation} 6 | \usage{ 7 | msigdbinfo() 8 | } 9 | \description{ 10 | Print MSIGDB infomation 11 | } 12 | -------------------------------------------------------------------------------- /R/reverseList.R: -------------------------------------------------------------------------------- 1 | #' reverse List 2 | #' @param lhs: list with names 3 | #' @export 4 | #' @author Kai Guo 5 | reverseList<-function(lhs){ 6 | lhs_n<-rep(names(lhs),times=lapply(lhs,function(x)length(x))) 7 | res<-sf(as.data.frame(cbind(lhs_n,unlist(lhs)))) 8 | return(res) 9 | } 10 | -------------------------------------------------------------------------------- /src/unique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace Rcpp; 3 | //[[Rcpp::export]] 4 | StringVector uniq(StringVector& xa){ 5 | StringVector rhs=unique(xa); 6 | return(rhs); 7 | } 8 | NumericVector uniq(NumericVector& xa){ 9 | NumericVector rhs=unique(xa); 10 | return(rhs); 11 | } 12 | -------------------------------------------------------------------------------- /man/rcpp_hello.Rd: -------------------------------------------------------------------------------- 1 | \name{rcpp_hello} 2 | \alias{rcpp_hello} 3 | \title{Hello, Rcpp!} 4 | \usage{ 5 | rcpp_hello() 6 | } 7 | \description{ 8 | Returns an \R \code{list} containing the character vector 9 | \code{c("foo", "bar")} and the numeric vector \code{c(0, 1)}. 10 | } 11 | \examples{ 12 | rcpp_hello() 13 | } 14 | -------------------------------------------------------------------------------- /man/dot-getmsig.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/msigdbr.R 3 | \name{.getmsig} 4 | \alias{.getmsig} 5 | \title{msigdb support species} 6 | \usage{ 7 | .getmsig(species = "human") 8 | } 9 | \arguments{ 10 | \item{species}{with common name} 11 | } 12 | \description{ 13 | msigdb support species 14 | } 15 | -------------------------------------------------------------------------------- /man/reverseList.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/reverseList.R 3 | \name{reverseList} 4 | \alias{reverseList} 5 | \title{reverse List} 6 | \usage{ 7 | reverseList(lhs) 8 | } 9 | \arguments{ 10 | \item{lhs:}{list with names} 11 | } 12 | \description{ 13 | reverse List 14 | } 15 | \author{ 16 | Kai Guo 17 | } 18 | -------------------------------------------------------------------------------- /man/showData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_org.R 3 | \name{showData} 4 | \alias{showData} 5 | \title{show avaliable data based on bioconductor annotation package} 6 | \usage{ 7 | showData() 8 | } 9 | \description{ 10 | show avaliable data based on bioconductor annotation package 11 | } 12 | \author{ 13 | Kai Guo 14 | } 15 | -------------------------------------------------------------------------------- /man/showAvailableRO.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getrodata.R 3 | \name{showAvailableRO} 4 | \alias{showAvailableRO} 5 | \title{show avaliable Reactome annotation data except plant} 6 | \usage{ 7 | showAvailableRO() 8 | } 9 | \description{ 10 | show avaliable Reactome annotation data except plant 11 | } 12 | \author{ 13 | Kai Guo 14 | } 15 | -------------------------------------------------------------------------------- /man/getann.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/annot.R 3 | \name{getann} 4 | \alias{getann} 5 | \title{build annotaion for kegg} 6 | \usage{ 7 | getann(ontype = "GO") 8 | } 9 | \arguments{ 10 | \item{ontype}{GO or KEGG} 11 | } 12 | \description{ 13 | build annotaion for kegg 14 | } 15 | \examples{ 16 | annot = getann("GO") 17 | } 18 | \author{ 19 | Kai Guo 20 | } 21 | -------------------------------------------------------------------------------- /src/name_table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace Rcpp; 3 | 4 | // [[Rcpp::export]] 5 | NumericVector name_table(List& lh) { 6 | int n=lh.length(); 7 | StringVector names=lh.names(); 8 | NumericVector res(n); 9 | for(int i=0;i do not edit by hand 2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | hyper_bench_vector <- function(xin, yin, N, n) { 5 | .Call('_EnrichR_hyper_bench_vector', PACKAGE = 'EnrichR', xin, yin, N, n) 6 | } 7 | 8 | name_table <- function(lh) { 9 | .Call('_EnrichR_name_table', PACKAGE = 'EnrichR', lh) 10 | } 11 | 12 | fast_factor <- function(x) { 13 | .Call('_EnrichR_fast_factor', PACKAGE = 'EnrichR', x) 14 | } 15 | 16 | sf <- function(x) { 17 | .Call('_EnrichR_sf', PACKAGE = 'EnrichR', x) 18 | } 19 | 20 | uniq <- function(xa) { 21 | .Call('_EnrichR_uniq', PACKAGE = 'EnrichR', xa) 22 | } 23 | 24 | -------------------------------------------------------------------------------- /man/showplant.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/biomart.R 3 | \name{showplant} 4 | \alias{showplant} 5 | \title{Show avaliable plant annotation based on ENSEMBLE} 6 | \usage{ 7 | showplant(host = "plants.ensembl.org") 8 | } 9 | \arguments{ 10 | \item{host}{the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org} 11 | 12 | \item{species}{the species you want to search} 13 | 14 | \item{ann_type}{the type of function annotation you want get from ensemble} 15 | } 16 | \description{ 17 | Show avaliable plant annotation based on ENSEMBLE 18 | } 19 | \author{ 20 | Kai Guo 21 | } 22 | -------------------------------------------------------------------------------- /man/idconvert.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_org.R 3 | \name{idconvert} 4 | \alias{idconvert} 5 | \title{Convert ID between ENTREZID to SYMBOL or other type ID based on bioconductor annotation package} 6 | \usage{ 7 | idconvert(species, keys, fkeytype, tkeytype) 8 | } 9 | \arguments{ 10 | \item{species}{you can check the support species by using showData()} 11 | 12 | \item{keys}{input genes} 13 | 14 | \item{fkeytype}{the gene type you want to convert} 15 | 16 | \item{tkeytype}{the gene type you want to get} 17 | } 18 | \description{ 19 | Convert ID between ENTREZID to SYMBOL or other type ID based on bioconductor annotation package 20 | } 21 | \author{ 22 | Kai Guo 23 | } 24 | -------------------------------------------------------------------------------- /man/makeesanno.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/biomart.R 3 | \name{makeesanno} 4 | \alias{makeesanno} 5 | \title{make annotation data except plant based on ENSEMBLE} 6 | \usage{ 7 | makeesanno(species = "Human", host = "uswest.ensembl.org", ann_type = "GO") 8 | } 9 | \arguments{ 10 | \item{species}{the species you want to search, you can use showplant to get the species name} 11 | 12 | \item{host}{the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org} 13 | 14 | \item{ann_type}{the type of function annotation(GO,KEGG,PFAM,InterPro) you want get from ensemble} 15 | } 16 | \description{ 17 | make annotation data except plant based on ENSEMBLE 18 | } 19 | \author{ 20 | Kai Guo 21 | } 22 | -------------------------------------------------------------------------------- /man/makeplantann.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/biomart.R 3 | \name{makeplantann} 4 | \alias{makeplantann} 5 | \title{make plant annotation data based on ENSEMBLE} 6 | \usage{ 7 | makeplantann( 8 | species = "Arabidopsis t", 9 | host = "plants.ensembl.org", 10 | ann_type = "GO" 11 | ) 12 | } 13 | \arguments{ 14 | \item{species}{the sepcies you want to search, you can use showplant to get the species name} 15 | 16 | \item{host}{the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org} 17 | 18 | \item{ann_type}{the type of function annotation(GO,KEGG,PFAM,InterPro) you want get from ensemble} 19 | } 20 | \description{ 21 | make plant annotation data based on ENSEMBLE 22 | } 23 | \author{ 24 | Kai Guo 25 | } 26 | -------------------------------------------------------------------------------- /R/getdetail.R: -------------------------------------------------------------------------------- 1 | #' Get detail information from enrichment results 2 | #' @param rese Functional enrichment results 3 | #' @param resd DEG result with one gene name column or vector of input genes 4 | #' @export 5 | #' @author Kai Guo 6 | getdetail<-function(rese,resd){ 7 | require(tidyverse) 8 | if(!is.data.frame(resd)){ 9 | resd=data.frame(gene=resd) 10 | } 11 | if(!("gene"%in%colnames(resd))){ 12 | resd$gene=rownames(resd) 13 | } 14 | gene<-strsplit(as.vector(rese$GeneID),split="\\,") 15 | names(gene)<-rese$Annot 16 | gened<-data.frame("TERM"=rep(names(gene),times=unlist(lapply(gene,length))),"Annot"=rep(rese$Term,times=unlist(lapply(gene,length))),"GeneID"=unlist(gene),row.names=NULL) 17 | gened$GeneID<-as.character(gened$GeneID) 18 | res<-left_join(gened,resd,by=c("GeneID"="gene")) 19 | return(res) 20 | } 21 | -------------------------------------------------------------------------------- /src/hyper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace Rcpp; 3 | double hyp_test( std::vector& xa) { 4 | double k=xa[0]; 5 | double M=xa[1]; 6 | double NM=xa[2]; 7 | double n=xa[3]; 8 | return R::phyper(k,M,NM,n,FALSE,FALSE); 9 | } 10 | //[[Rcpp::export]] 11 | NumericVector hyper_bench_vector(NumericVector& xin,NumericVector& yin,double N,double n){ 12 | int xsize=xin.size(); 13 | //double xt; 14 | NumericVector res; 15 | StringVector xnames=xin.names(); 16 | for(int i=0;ixres; 19 | xres.push_back(xin[tname]-1.0); 20 | xres.push_back(yin[tname]); 21 | xres.push_back(N-yin[tname]); 22 | xres.push_back(n); 23 | double pp=hyp_test(xres); 24 | res.push_back(pp); 25 | } 26 | res.attr("names")=xnames; 27 | return res; 28 | } 29 | -------------------------------------------------------------------------------- /man/msigdbr.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/msigdbr.R 3 | \name{msigdbr} 4 | \alias{msigdbr} 5 | \alias{makeMSIGDB} 6 | \title{Download database from Msigdb and prepare for enrichment analysis} 7 | \usage{ 8 | makeMSIGDB( 9 | species = "human", 10 | keytype = "SYMBOL", 11 | subcategory = NULL, 12 | category = NULL, 13 | path = "./", 14 | save = FALSE 15 | ) 16 | } 17 | \arguments{ 18 | \item{species}{the species for query} 19 | 20 | \item{keytype}{the gene ID type} 21 | 22 | \item{subcategory}{Gene Set subcategory} 23 | 24 | \item{category}{Gene set category} 25 | 26 | \item{path}{path to save the dataset} 27 | 28 | \item{save}{save the dataset or not} 29 | } 30 | \description{ 31 | Download database from Msigdb and prepare for enrichment analysis 32 | } 33 | \author{ 34 | Kai Guo 35 | } 36 | -------------------------------------------------------------------------------- /man/lheatmap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/netmap_m.R 3 | \name{lheatmap} 4 | \alias{lheatmap} 5 | \title{Plot compare heatmap of Enrichment result among DEG groups} 6 | \usage{ 7 | lheatmap( 8 | rhs, 9 | top = 50, 10 | colnames = NULL, 11 | xsize = 6, 12 | ysize = 6, 13 | padj = NULL, 14 | horizontal = FALSE, 15 | pval = 0.05, 16 | returnData = FALSE, 17 | ... 18 | ) 19 | } 20 | \arguments{ 21 | \item{top}{the number of Terms you want to display} 22 | 23 | \item{colnames}{the compare DEG group names} 24 | 25 | \item{xsize}{cex of group name} 26 | 27 | \item{ysize}{cex of Terms name} 28 | 29 | \item{rhslist}{of enrchment analysis result among DEG groups} 30 | } 31 | \description{ 32 | Plot compare heatmap of Enrichment result among DEG groups 33 | } 34 | \author{ 35 | Kai Guo 36 | } 37 | -------------------------------------------------------------------------------- /man/PE.ensemble.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/PE.R 3 | \name{PE.ensemble} 4 | \alias{PE.ensemble} 5 | \title{PFAM Enrichment analysis function based on ensemble annotation data} 6 | \usage{ 7 | PE.ensemble( 8 | df, 9 | PO_FILE, 10 | gene.cutoff = 0.01, 11 | padj.method = "BH", 12 | minSize = 2, 13 | maxSize = 500, 14 | keepRich = TRUE, 15 | filename = NULL, 16 | cutoff = 0.05 17 | ) 18 | } 19 | \arguments{ 20 | \item{df:}{DGE files (DESeq2 result files) or vector contains gene names} 21 | 22 | \item{PO_FILE:}{PFAM annotation data, you can get the data by using makeesanno function} 23 | 24 | \item{filename:}{output filename} 25 | 26 | \item{gene.cutoff:}{DGE singificant cutoff value} 27 | } 28 | \description{ 29 | PFAM Enrichment analysis function based on ensemble annotation data 30 | } 31 | \author{ 32 | Kai Guo 33 | } 34 | -------------------------------------------------------------------------------- /man/PE.plant.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/PE.R 3 | \name{PE.plant} 4 | \alias{PE.plant} 5 | \title{PFAM Enrichment analysis function based on ensemble plant annotation data} 6 | \usage{ 7 | PE.plant( 8 | df, 9 | PO_FILE, 10 | gene.cutoff = 0.01, 11 | padj.method = "BH", 12 | minSize = 2, 13 | maxSize = 500, 14 | keepRich = TRUE, 15 | filename = NULL, 16 | cutoff = 0.05 17 | ) 18 | } 19 | \arguments{ 20 | \item{df:}{DGE files (DESeq2 result files) or vector contains gene names} 21 | 22 | \item{PO_FILE:}{PFAM annotation data, you can get the data by using makeplantann function} 23 | 24 | \item{filename:}{output filename} 25 | 26 | \item{gene.cutoff:}{DGE singificant cutoff value} 27 | } 28 | \description{ 29 | PFAM Enrichment analysis function based on ensemble plant annotation data 30 | } 31 | \author{ 32 | Kai Guo 33 | } 34 | -------------------------------------------------------------------------------- /man/enrich.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/enrich.R 3 | \name{enrich} 4 | \alias{enrich} 5 | \title{Enrichment analysis for any type of annotation data} 6 | \usage{ 7 | enrich( 8 | df, 9 | annot, 10 | annot.info = NULL, 11 | minSize = 1, 12 | maxSize = 500, 13 | keepRich = TRUE, 14 | filename = NULL, 15 | gene.cutoff = 0.01, 16 | padj.method = "BH" 17 | ) 18 | } 19 | \arguments{ 20 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 21 | 22 | \item{annot}{annotation file for all genes} 23 | 24 | \item{annot.info}{Term of all annotation} 25 | 26 | \item{keepRich}{keep terms with high rich factor} 27 | 28 | \item{filename}{output filename} 29 | 30 | \item{gene.cutoff}{DGE singificant cutoff value} 31 | 32 | \item{padj.method}{p value adjust method} 33 | } 34 | \description{ 35 | Enrichment analysis for any type of annotation data 36 | } 37 | \author{ 38 | Kai Guo 39 | } 40 | -------------------------------------------------------------------------------- /src/sf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace Rcpp; 3 | using namespace std; 4 | template 5 | SEXP fast_factor_template( const Vector& x ) { 6 | Vector levs = sort_unique(x); 7 | IntegerVector out = match(x, levs); 8 | out.attr("levels") = as(levs); 9 | out.attr("class") = "factor"; 10 | return out; 11 | } 12 | 13 | //[[Rcpp::export]] 14 | SEXP fast_factor( SEXP x ) { //modified from RcppCore 15 | switch( TYPEOF(x) ) { 16 | case INTSXP: return fast_factor_template(x); 17 | case REALSXP: return fast_factor_template(x); 18 | case STRSXP: return fast_factor_template(x); 19 | } 20 | return R_NilValue; 21 | } 22 | 23 | //[[Rcpp::export]] 24 | List sf(DataFrame &x){ 25 | StringVector Gene=x(0); 26 | StringVector Fa=x(1); 27 | StringVector tmp=unique(Fa); 28 | List res(Fa.size()); 29 | Function sp("split"); 30 | res=sp(Gene,fast_factor(Fa)); 31 | return(res); 32 | } 33 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: EnrichR 2 | Type: Package 3 | Title: Functional Enrichment analysis 4 | Version: 0.3.0 5 | Author: Kai Guo 6 | Maintainer: Kai Guo 7 | Description: Functional Enrichment Analysis and Network Construction 8 | License: GPL-2 9 | LazyData: TRUE 10 | Imports: 11 | Rcpp, 12 | GO.db, 13 | ggplot2, 14 | dplyr, 15 | tidyr, 16 | igraph, 17 | biomaRt, 18 | visNetwork, 19 | AnnotationDbi, 20 | reshape2, 21 | fgsea, 22 | GGally, 23 | ggrepel, 24 | DiagrammeR, 25 | sna, 26 | intergraph, 27 | webshot, 28 | msigdbr, 29 | KEGGREST 30 | LinkingTo: Rcpp 31 | Suggests: 32 | biomaRt, 33 | GO.db, 34 | igraph, 35 | ggplot2, 36 | visNetwork, 37 | knitr, 38 | fgsea, 39 | rmarkdown 40 | VignetteBuilder: knitr 41 | biocViews: Annotation, Clustering, GeneSetEnrichment, GO, KEGG, 42 | MultipleComparison, Pathways, Reactome, Visualization 43 | RoxygenNote: 6.1.1 44 | -------------------------------------------------------------------------------- /R/DE.R: -------------------------------------------------------------------------------- 1 | #' Disease Ontology Enrichment analysis function 2 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 3 | #' @param keytype Keytype 4 | #' @param filename output filename 5 | #' @export 6 | #' @author Kai Guo 7 | DE<-function(df, keytype = "SYMBOL",padj.method = "BH", filename = NULL) { 8 | require(DO.db) 9 | data(hsdo) 10 | annot<-hsdo 11 | annotf <- toTable(DOTERM) 12 | annotf <- annotf[, c("do_id", "Term")] 13 | annotf <- unique(annotf) 14 | rownames(annotf)<-annotf[,1] 15 | annot.info =annotf 16 | if (keytype != "ENTREZID") { 17 | id = idconvert(species = "human", keys = annot$gene, 18 | fkeytype = "ENTREZID", tkeytype = keytype) 19 | annot2 = annot[annot$gene %in% names(id), ] 20 | annot2$gene = id[annot2$gene %in% names(id)] 21 | annot=annot2 22 | } 23 | res = enrich(df, annot = annot, annot.info = annotf, 24 | filename = filename, padj.method = padj.method) 25 | res<-res[res$Pvalue<0.05,] 26 | return(res) 27 | } 28 | -------------------------------------------------------------------------------- /man/gsea.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/GSEA.R 3 | \name{gsea} 4 | \alias{gsea} 5 | \title{Enrichment analysis for any type of annotation data} 6 | \usage{ 7 | gsea( 8 | x, 9 | annot, 10 | annot.info = NULL, 11 | minSize = 15, 12 | maxSize = 500, 13 | nperm = 5000, 14 | filename = NULL, 15 | padj.method = "BH", 16 | table = TRUE 17 | ) 18 | } 19 | \arguments{ 20 | \item{x}{a vector include all log2FC with gene name} 21 | 22 | \item{annot}{annotation file for all genes} 23 | 24 | \item{annot.info}{Term with annotation details} 25 | 26 | \item{minSize}{Minimal size of a gene set to test. All pathways below the threshold are excluded.} 27 | 28 | \item{maxSize}{Maximal size of a gene set to test. All pathways above the threshold are excluded.} 29 | 30 | \item{nperm}{Number of permutations to do. Minimial possible nominal p-value is about 1/nperm} 31 | 32 | \item{filename}{output filename} 33 | 34 | \item{padj.method}{p value adjust method} 35 | } 36 | \description{ 37 | Enrichment analysis for any type of annotation data 38 | } 39 | \author{ 40 | Kai Guo 41 | } 42 | -------------------------------------------------------------------------------- /man/GE.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/GE.R 3 | \name{GE} 4 | \alias{GE} 5 | \title{GO Enrichment analysis function} 6 | \usage{ 7 | GE( 8 | df, 9 | GO_FILE, 10 | OP = "BP", 11 | gene.cutoff = 0.01, 12 | minSize = 2, 13 | maxSize = 500, 14 | keepRich = TRUE, 15 | filename = NULL, 16 | padj.method = "BH", 17 | cutoff = 0.05 18 | ) 19 | } 20 | \arguments{ 21 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 22 | 23 | \item{GO_FILE}{GO annotation data} 24 | 25 | \item{OP}{use BP,CC or MF} 26 | 27 | \item{gene.cutoff}{the cut-off value for select DEGs (default: 0.01)} 28 | 29 | \item{minSize}{minimal number of genes included in significant terms} 30 | 31 | \item{maxSize}{maximum number of genes included in significant terms} 32 | 33 | \item{keepRich}{keep terms with rich factor value equal 1 or not (default: TRUE)} 34 | 35 | \item{filename}{output filename} 36 | 37 | \item{padj.method}{p value adjust method (default: BH)} 38 | 39 | \item{cutoff}{cutoff value for filtering significant terms (default: 0.05)} 40 | } 41 | \description{ 42 | GO Enrichment analysis function 43 | } 44 | \author{ 45 | Kai Guo 46 | } 47 | -------------------------------------------------------------------------------- /man/IE.ensemble.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/IE.R 3 | \name{IE.ensemble} 4 | \alias{IE.ensemble} 5 | \title{InterPro Enrichment analysis function} 6 | \usage{ 7 | IE.ensemble( 8 | df, 9 | IO_FILE, 10 | gene.cutoff = 0.01, 11 | minSize = 2, 12 | maxSize = 500, 13 | keepRich = TRUE, 14 | padj.method = "BH", 15 | filename = NULL, 16 | cutoff = 0.05 17 | ) 18 | } 19 | \arguments{ 20 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 21 | 22 | \item{IO_FILE}{InterPro annotation data} 23 | 24 | \item{gene.cutoff}{the cut-off value for select DEGs (default: 0.01)} 25 | 26 | \item{minSize}{minimal number of genes included in significant terms} 27 | 28 | \item{maxSize}{maximum number of genes included in significant terms} 29 | 30 | \item{keepRich}{keep terms with rich factor value equal 1 or not (default: TRUE)} 31 | 32 | \item{padj.method}{p value adjust method (default: BH)} 33 | 34 | \item{filename}{output filename} 35 | 36 | \item{cutoff}{cutoff value for filtering significant terms (default: 0.05)} 37 | } 38 | \description{ 39 | InterPro Enrichment analysis function 40 | } 41 | \author{ 42 | Kai Guo 43 | } 44 | -------------------------------------------------------------------------------- /man/IE.plant.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/IE.R 3 | \name{IE.plant} 4 | \alias{IE.plant} 5 | \title{InterPro Enrichment analysis function for plant} 6 | \usage{ 7 | IE.plant( 8 | df, 9 | IO_FILE, 10 | gene.cutoff = 0.01, 11 | minSize = 2, 12 | maxSize = 500, 13 | keepRich = TRUE, 14 | padj.method = "BH", 15 | filename = NULL, 16 | cutoff = 0.05 17 | ) 18 | } 19 | \arguments{ 20 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 21 | 22 | \item{IO_FILE}{InterPro annotation data} 23 | 24 | \item{gene.cutoff}{the cut-off value for select DEGs (default: 0.01)} 25 | 26 | \item{minSize}{minimal number of genes included in significant terms} 27 | 28 | \item{maxSize}{maximum number of genes included in significant terms} 29 | 30 | \item{keepRich}{keep terms with rich factor value equal 1 or not (default: TRUE)} 31 | 32 | \item{padj.method}{p value adjust method (default: BH)} 33 | 34 | \item{filename}{output filename} 35 | 36 | \item{cutoff}{cutoff value for filtering significant terms (default: 0.05)} 37 | } 38 | \description{ 39 | InterPro Enrichment analysis function for plant 40 | } 41 | \author{ 42 | Kai Guo 43 | } 44 | -------------------------------------------------------------------------------- /man/RE.plant.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/RE.R 3 | \name{RE.plant} 4 | \alias{RE.plant} 5 | \title{Reactome Pathway Enrichment analysis function for plant} 6 | \usage{ 7 | RE.plant( 8 | df, 9 | RO_FILE, 10 | gene.cutoff = 0.01, 11 | padj.method = "BH", 12 | minSize = 2, 13 | maxSize = 500, 14 | keepRich = TRUE, 15 | filename = NULL, 16 | cutoff = 0.05 17 | ) 18 | } 19 | \arguments{ 20 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 21 | 22 | \item{RO_FILE}{GO annotation data} 23 | 24 | \item{gene.cutoff}{the cut-off value for select DEGs (default: 0.01)} 25 | 26 | \item{padj.method}{p value adjust method (default: BH)} 27 | 28 | \item{minSize}{minimal number of genes included in significant terms} 29 | 30 | \item{maxSize}{maximum number of genes included in significant terms} 31 | 32 | \item{keepRich}{keep terms with rich factor value equal 1 or not (default: TRUE)} 33 | 34 | \item{filename}{output filename} 35 | 36 | \item{cutoff}{cutoff value for filtering significant terms (default: 0.05)} 37 | } 38 | \description{ 39 | Reactome Pathway Enrichment analysis function for plant 40 | } 41 | \author{ 42 | Kai Guo 43 | } 44 | -------------------------------------------------------------------------------- /man/RE.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/RE.R 3 | \name{RE} 4 | \alias{RE} 5 | \title{Reactome Pathway Enrichment analysis function} 6 | \usage{ 7 | RE( 8 | df, 9 | RO_FILE, 10 | keytype = "SYMBOL", 11 | species = "human", 12 | padj.method = "BH", 13 | minSize = 2, 14 | maxSize = 500, 15 | keepRich = TRUE, 16 | filename = NULL, 17 | cutoff = 0.05 18 | ) 19 | } 20 | \arguments{ 21 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 22 | 23 | \item{RO_FILE}{Reactome annotation data} 24 | 25 | \item{species}{species name} 26 | 27 | \item{padj.method}{p value adjust method (default: BH)} 28 | 29 | \item{minSize}{minimal number of genes included in significant terms} 30 | 31 | \item{maxSize}{maximum number of genes included in significant terms} 32 | 33 | \item{keepRich}{keep terms with rich factor value equal 1 or not (default: TRUE)} 34 | 35 | \item{filename}{output filename} 36 | 37 | \item{cutoff}{cutoff value for filtering significant terms (default: 0.05)} 38 | 39 | \item{gene.cutoff}{the cut-off value for select DEGs (default: 0.01)} 40 | } 41 | \description{ 42 | Reactome Pathway Enrichment analysis function 43 | } 44 | \author{ 45 | Kai Guo 46 | } 47 | -------------------------------------------------------------------------------- /man/KE.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/KE.R 3 | \name{KE} 4 | \alias{KE} 5 | \title{KEGG Pathway Enrichment analysis function} 6 | \usage{ 7 | KE( 8 | df, 9 | KO_FILE, 10 | filename = NULL, 11 | gene.cutoff = 0.01, 12 | minSize = 2, 13 | maxSize = 500, 14 | keepRich = TRUE, 15 | padj.method = "BH", 16 | cutoff = 0.05, 17 | builtin = TRUE 18 | ) 19 | } 20 | \arguments{ 21 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 22 | 23 | \item{KO_FILE}{KEGG annotation data} 24 | 25 | \item{filename}{output filename} 26 | 27 | \item{gene.cutoff}{the cut-off value for select DEGs (default: 0.01)} 28 | 29 | \item{minSize}{minimal number of genes included in significant terms} 30 | 31 | \item{maxSize}{maximum number of genes included in significant terms} 32 | 33 | \item{keepRich}{keep terms with rich factor value equal 1 or not (default: TRUE)} 34 | 35 | \item{padj.method}{p value adjust method (default: BH)} 36 | 37 | \item{cutoff}{cutoff value for filtering significant terms (default: 0.05)} 38 | 39 | \item{bulitin}{use KEGG bulit in KEGG annotation or not(set FALSE if you want use newest KEGG data)} 40 | } 41 | \description{ 42 | KEGG Pathway Enrichment analysis function 43 | } 44 | -------------------------------------------------------------------------------- /man/enrichdot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/enrich.R 3 | \name{enrichdot} 4 | \alias{enrichdot} 5 | \title{Display enrichment result By using dotchart} 6 | \usage{ 7 | enrichdot( 8 | resultFis, 9 | top = 50, 10 | pvalue.cutoff = 0.05, 11 | order = FALSE, 12 | low = "lightpink", 13 | high = "red", 14 | alpha = 0.7, 15 | padj.cutoff = NULL, 16 | fontsize.x = 10, 17 | fontsize.y = 10, 18 | usePadj = TRUE, 19 | filename = NULL, 20 | width = 10, 21 | height = 8 22 | ) 23 | } 24 | \arguments{ 25 | \item{low}{color used for small value} 26 | 27 | \item{high}{color used for large value} 28 | 29 | \item{alpha}{alpha-transparency scales} 30 | 31 | \item{fontsize.x}{fontsize for x axis} 32 | 33 | \item{fontsize.y}{fontsize for y axis} 34 | 35 | \item{filename}{output filename} 36 | 37 | \item{width}{width for output file} 38 | 39 | \item{height}{height for output file} 40 | 41 | \item{resultFis:}{Ennrichment analysis result data.frame} 42 | 43 | \item{top:}{Number of Terms you want to display} 44 | 45 | \item{pvalue.cutoff:}{the cut-off value for selecting Term} 46 | 47 | \item{padj.cutoff:}{the padj cut-off value for selecting Term} 48 | } 49 | \description{ 50 | Display enrichment result By using dotchart 51 | } 52 | \author{ 53 | Kai Guo 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EnrichR 2 | Functional Enrichment Analysis and Network Construction 3 | #### PS: EnrichR are not supported any more. Please use the richR("https://www.github.com/guokai8/richR") 4 | ## Description 5 | __EnrichR__ is a package can be used for functional enrichment analysis and network construction based on enrichment analysis results. It supported almost all species pubished by ENSEMBL and included with Bioconductor. Now the _EnrichR_ provide function to direct download annotation dataset from the MsigDB. 6 | ## Dependencies 7 | R>2.15 8 | ## Installation 9 | ``` 10 | library(devtools) 11 | install_github("guokai8/EnrichR") 12 | ``` 13 | ## Getting started 14 | ``` 15 | library(EnrichR) 16 | ``` 17 | More detail please see [vignette](https://github.com/guokai8/EnrichR/wiki) 18 | ``` 19 | vignette("EnrichR") 20 | ``` 21 | ## Some useful commands 22 | If you want tranform ID from one type another type("SYMBOL"->"ENSEMBL") 23 | ``` 24 | idconvert(keys=vector_of_symbols,species="human",fkeytype= "SYMBOL",tkeytype="ENSEMBL") 25 | ``` 26 | If you want have more details include input data and enrichment results 27 | ``` 28 | getdetail(enrichres,input_data) 29 | ``` 30 | ## Contact information 31 | I still working on this package and will add more functions here. 32 | For any questions please contact guokai8@gmail.com 33 | -------------------------------------------------------------------------------- /man/enrichbar.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/enrich.R 3 | \name{enrichbar} 4 | \alias{enrichbar} 5 | \title{Display enrichment result By using barchart} 6 | \usage{ 7 | enrichbar( 8 | resultFis, 9 | top = 50, 10 | pvalue.cutoff = 0.05, 11 | padj.cutoff = NULL, 12 | low = "lightpink", 13 | high = "red", 14 | order = FALSE, 15 | horiz = FALSE, 16 | fontsize.x = 10, 17 | fontsize.y = 10, 18 | fontsize.text = 3, 19 | angle = 75, 20 | usePadj = TRUE, 21 | filename = NULL, 22 | width = 10, 23 | height = 8 24 | ) 25 | } 26 | \arguments{ 27 | \item{resultFis}{Ennrichment analysis result data.frame} 28 | 29 | \item{top}{Number of Terms you want to display} 30 | 31 | \item{pvalue.cutoff}{the cut-off value for selecting Term} 32 | 33 | \item{padj.cutoff}{the padj cut-off value for selecting Term} 34 | 35 | \item{low}{color used for small value} 36 | 37 | \item{high}{color used for large value} 38 | 39 | \item{horiz}{show horiz or not (default: FALSE)} 40 | 41 | \item{fontsize.x}{fontsize for x axis} 42 | 43 | \item{fontsize.y}{fontsize for y axis} 44 | 45 | \item{filename}{output filename} 46 | 47 | \item{width}{width for output file} 48 | 49 | \item{height}{height for output file} 50 | } 51 | \description{ 52 | Display enrichment result By using barchart 53 | } 54 | \author{ 55 | Kai Guo 56 | } 57 | -------------------------------------------------------------------------------- /R/makeown.R: -------------------------------------------------------------------------------- 1 | #' make annotation database using bioAnno results 2 | #' @importFrom AnnotationDbi keys 3 | #' @importFrom AnnotationDbi select 4 | #' @param dbname database name from bioAnno 5 | #' @param anntype GO or KEGG 6 | #' @param OP BP,CC,MF default use all 7 | #' @examples 8 | #' \dontrun{ 9 | #' fromKEGG(species="ath") 10 | #' athgo<-makeOwn(dbname="org.ath.eg.db",anntype="GO") 11 | #' } 12 | #' @author Kai Guo 13 | #' @export 14 | 15 | 16 | makeOwn<-function(dbname,anntype="GO",OP=NULL){ 17 | if (!require(dbname,character.only=TRUE)){ 18 | stop("Please give the package name") 19 | }else{ 20 | suppressMessages(require(dbname,character.only = T,quietly = T)) 21 | } 22 | dbname<-eval(parse(text=dbname)) 23 | if(anntype=="GO"){ 24 | annof<-select(dbname,keys=keys(dbname),columns=c("GOALL","ONTOLOGYALL")) 25 | colnames(annof)[1]<-"GeneID" 26 | annof<-distinct_(annof,~GeneID, ~GOALL, ~ONTOLOGYALL) 27 | annot <- getann("GO") 28 | annof$Annot <- annot[annof[,2],"annotation"] 29 | if(!is.null(OP)){ 30 | annof<-annof[annof$ONTOLOGYALL==OP,] 31 | } 32 | } 33 | if(anntype=="KEGG"){ 34 | annof=select(dbname,keys=keys(dbname),columns="PATH") 35 | annof<-na.omit(annof) 36 | annot<-getann("KEGG") 37 | annof[,1]<-as.vector(annof[,1]) 38 | annof[,2]<-as.vector(annof[,2]) 39 | annof$Annot<-annot[annof[,2],"annotation"] 40 | } 41 | return(annof) 42 | } 43 | -------------------------------------------------------------------------------- /R/annot.R: -------------------------------------------------------------------------------- 1 | ##' @importFrom dplyr filter_ 2 | ##' @importFrom AnnotationDbi select 3 | .get_go_dat<-function(ont="BP"){ 4 | require(GO.db) 5 | key<-keys(GO.db) 6 | suppressMessages(go_dat<-select(GO.db, keys=key, columns=c("TERM","ONTOLOGY"),keytype="GOID")) 7 | if(ont=="BP") res<-as.data.frame(filter_(go_dat,~ONTOLOGY=="BP")) 8 | if(ont=="CC") res<-as.data.frame(filter_(go_dat,~ONTOLOGY=="CC")) 9 | if(ont=="MF") res<-as.data.frame(filter_(go_dat,~ONTOLOGY=="MF")) 10 | rownames(res)<-res[,1] 11 | res<-res[, 2, drop = FALSE] 12 | colnames(res)<-"annotation" 13 | return(res) 14 | } 15 | ##' @importFrom KEGGREST keggList 16 | .get_kg_dat<-function(builtin=TRUE){ 17 | if(isTRUE(builtin)){ 18 | data(kegg) 19 | return(kegg.db) 20 | }else{ 21 | pathway<-cbind(keggList('pathway')) 22 | rownames(pathway)<-sub('path:map','',rownames(pathway)) 23 | colnames(pathway)<-"annotation" 24 | pathway<-as.data.frame(pathway) 25 | pathway$annotation<-as.vector(pathway$annotation) 26 | return(pathway) 27 | } 28 | } 29 | ##' build annotaion for kegg 30 | ##' @param ontype GO or KEGG 31 | ##' @examples 32 | ##' annot = getann("GO") 33 | ##' @export 34 | ##' @author Kai Guo 35 | getann<-function(ontype="GO"){ 36 | if(ontype=="GO"){ 37 | res<-rbind(.get_go_dat("BP"),.get_go_dat("MF"),.get_go_dat("CC")) 38 | } 39 | if(ontype=="KEGG"){ 40 | res<-.get_kg_dat(builtin=F) 41 | } 42 | return(res) 43 | } 44 | -------------------------------------------------------------------------------- /R/PE.R: -------------------------------------------------------------------------------- 1 | #' PFAM Enrichment analysis function based on ensemble annotation data 2 | #' @param df: DGE files (DESeq2 result files) or vector contains gene names 3 | #' @param PO_FILE: PFAM annotation data, you can get the data by using makeesanno function 4 | #' @param filename: output filename 5 | #' @param gene.cutoff: DGE singificant cutoff value 6 | #' @export 7 | #' @author Kai Guo 8 | PE.ensemble<-function(df,PO_FILE,gene.cutoff=0.01,padj.method="BH",minSize=2,maxSize=500,keepRich=TRUE,filename=NULL,cutoff=0.05){ 9 | data(pfanno) 10 | annot.info=pfanno 11 | res=enrich(df,annot=PO_FILE,annot.info =pfanno,filename=filename,gene.cutoff = gene.cutoff,padj.method = padj.method) 12 | res<-res[res$Pvalue%") 70 | importFrom(msigdbr,msigdbr) 71 | importFrom(plyr,.) 72 | importFrom(reshape2,melt) 73 | -------------------------------------------------------------------------------- /man/mnetmap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/netmap_m.R 3 | \name{mnetmap} 4 | \alias{mnetmap} 5 | \title{plot combine network of Terms} 6 | \usage{ 7 | mnetmap( 8 | df, 9 | gores = NULL, 10 | kores = NULL, 11 | rores = NULL, 12 | pfres = NULL, 13 | itres = NULL, 14 | top = NULL, 15 | top.display = NULL, 16 | pvalue.cutoff = 0.05, 17 | padj.cutoff = NULL, 18 | visNet = F, 19 | weightcut = 0.2, 20 | useTerm = TRUE, 21 | layout = NULL, 22 | vertex.label.cex = 0.5, 23 | gnet = FALSE, 24 | vertex.node.shape = NULL, 25 | ... 26 | ) 27 | } 28 | \arguments{ 29 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 30 | 31 | \item{gores}{GO Enrichment analsyis result} 32 | 33 | \item{kores}{KEGG Enrichment analsyis result} 34 | 35 | \item{rores}{Reactome Pathway Enrichment analsyis result} 36 | 37 | \item{pfres}{PFAM Enrichment analysis result result} 38 | 39 | \item{itres}{InterPro Enrichment analysis result} 40 | 41 | \item{top}{number of Terms you want to display(defaultthe total number of all Significant number)} 42 | 43 | \item{pvalue.cutoff}{the cut-off P value for selecting significant Terms} 44 | 45 | \item{padj.cutoff}{the cut-off P adjust value for selecting significant Terms} 46 | 47 | \item{visNet}{use VisNetwork method to display network(defaultFALSE)} 48 | 49 | \item{weightcut}{the weight cut value for remove edges} 50 | 51 | \item{useTerm}{use the Term description or not(defalutTRUE)} 52 | 53 | \item{layout}{layout format (defultlayout.fruchterman.reingold)} 54 | 55 | \item{vertex.label.cex}{size of label(default0.5)} 56 | 57 | \item{vertex.node.shape}{vector of shape and names of the vector should be the terms (default 20)} 58 | 59 | \item{writeCyt}{export file for Cyt software} 60 | 61 | \item{vertex.label.color}{color of label(defaultblack)} 62 | } 63 | \description{ 64 | plot combine network of Terms 65 | } 66 | \author{ 67 | Kai Guo 68 | } 69 | -------------------------------------------------------------------------------- /man/gnet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/gnet.R 3 | \name{gnet} 4 | \alias{gnet} 5 | \title{Draw network with ggplot2 style} 6 | \usage{ 7 | gnet( 8 | df, 9 | rhs, 10 | top = 50, 11 | pvalue.cutoff = 0.05, 12 | padj.cutoff = NULL, 13 | low = "orange", 14 | high = "red", 15 | weightcut = 0.2, 16 | useTerm = TRUE, 17 | writeCyt = FALSE, 18 | cytoscapeFile = "network-file-for-cytoscape.txt", 19 | vertex.label.color = "black", 20 | vertex.label.cex = 2, 21 | vertex.node.shape = NULL, 22 | layout = layout.fruchterman.reingold, 23 | savefig = FALSE, 24 | filename = "network", 25 | width = 7, 26 | height = 7, 27 | segment.size = 0.2, 28 | node.alpha = 0.7, 29 | ... 30 | ) 31 | } 32 | \arguments{ 33 | \item{df}{Differential expression result or vector of genes} 34 | 35 | \item{rhs}{Enrichment results} 36 | 37 | \item{top}{number of terms to show (default: 50)} 38 | 39 | \item{pvalue.cutoff}{cutoff p value for enrichment result} 40 | 41 | \item{padj.cutoff}{cutoff p adjust value for enrichment result} 42 | 43 | \item{low}{color used for small value} 44 | 45 | \item{high}{color used for large value} 46 | 47 | \item{weightcut}{cutoff value for the edges} 48 | 49 | \item{useTerm}{use terms for nodes (default: TRUE)} 50 | 51 | \item{writeCyt}{write out the cytoscape file} 52 | 53 | \item{cytoscapeFile}{output cytoscape File} 54 | 55 | \item{vertex.label.color}{label color} 56 | 57 | \item{vertex.label.cex}{label size} 58 | 59 | \item{vertex.node.shape}{vector of shape and names of the vector should be the terms (default: 20)} 60 | 61 | \item{layout}{layout method} 62 | 63 | \item{savefig}{save figures or not} 64 | 65 | \item{filename}{output figure name} 66 | 67 | \item{width}{width for output figure} 68 | 69 | \item{height}{height for output figure} 70 | 71 | \item{segment.size}{size for label segment} 72 | 73 | \item{node.alpha}{alpha-transparency scales} 74 | } 75 | \description{ 76 | gnet provides method to draw the enrichment results with a ggplot2 style network 77 | } 78 | \author{ 79 | Kai Guo 80 | } 81 | -------------------------------------------------------------------------------- /man/netmap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/netmap.R 3 | \name{netmap} 4 | \alias{netmap} 5 | \title{Plot network of Terms} 6 | \usage{ 7 | netmap( 8 | df, 9 | rhs, 10 | top = 50, 11 | pvalue.cutoff = 0.05, 12 | padj.cutoff = NULL, 13 | low = "orange", 14 | high = "red", 15 | weightcut = 0.2, 16 | useTerm = TRUE, 17 | writeCyt = FALSE, 18 | cytoscapeFile = "network-file-for-cytoscape.txt", 19 | vertex.label.font = 2, 20 | vertex.label.color = "black", 21 | vertex.label.cex = 0.5, 22 | layout = layout.fruchterman.reingold, 23 | visNet = FALSE, 24 | smooth = TRUE, 25 | nodeselect = FALSE, 26 | editvis = FALSE, 27 | savevis = FALSE, 28 | savefig = FALSE, 29 | filename = "network", 30 | ... 31 | ) 32 | } 33 | \arguments{ 34 | \item{df}{DGE files (DESeq2 result files) or vector contains gene names} 35 | 36 | \item{rhs}{Enrichment analsyis result} 37 | 38 | \item{top}{number of Terms you want to display} 39 | 40 | \item{pvalue.cutoff}{the cut-off P value for selecting significant Terms} 41 | 42 | \item{padj.cutoff}{the cut-off P adjust value for selecting significant Terms} 43 | 44 | \item{low}{color used for small value} 45 | 46 | \item{high}{color used for large value} 47 | 48 | \item{weightcut}{the weight cut value for remove edges} 49 | 50 | \item{useTerm}{use the Term description or not(defalutTRUE)} 51 | 52 | \item{writeCyt}{export file for Cyt software} 53 | 54 | \item{vertex.label.color}{color of label(defaultblack)} 55 | 56 | \item{vertex.label.cex}{size of label(default0.5)} 57 | 58 | \item{layout}{layout format (defultlayout.fruchterman.reingold)} 59 | 60 | \item{visNet}{use VisNetwork method to display network(defaultFALSE)} 61 | 62 | \item{smooth}{use smooth edge for visNetwork} 63 | 64 | \item{nodeselect}{select some interesting node(defaultFALSE)} 65 | 66 | \item{editvis}{choose to edit network(defaultFALSE)} 67 | 68 | \item{savevis}{save visnetwork to html} 69 | 70 | \item{savefig}{save figure to pdf} 71 | 72 | \item{filename}{output filename} 73 | } 74 | \description{ 75 | Plot network of Terms 76 | } 77 | \author{ 78 | Kai Guo 79 | } 80 | -------------------------------------------------------------------------------- /R/IE.R: -------------------------------------------------------------------------------- 1 | #' InterPro Enrichment analysis function 2 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 3 | #' @param IO_FILE InterPro annotation data 4 | #' @param minSize minimal number of genes included in significant terms 5 | #' @param padj.method p value adjust method (default: BH) 6 | #' @param cutoff cutoff value for filtering significant terms (default: 0.05) 7 | #' @param maxSize maximum number of genes included in significant terms 8 | #' @param keepRich keep terms with rich factor value equal 1 or not (default: TRUE) 9 | #' @param filename output filename 10 | #' @param gene.cutoff the cut-off value for select DEGs (default: 0.01) 11 | #' @export 12 | #' @author Kai Guo 13 | IE.ensemble<-function(df,IO_FILE,gene.cutoff=0.01,minSize=2,maxSize=500,keepRich=TRUE,padj.method="BH",filename=NULL,cutoff=0.05){ 14 | data(interproanno) 15 | rownames(interano)<-interano[,1] 16 | res=enrich(df,annot=IO_FILE,annot.info = interano,filename=filename,gene.cutoff = gene.cutoff,padj.method = padj.method,minSize=minSize,maxSize=maxSize,keepRich=keepRich) 17 | res<-res[res$Pvalue1&!is.null(gseaRes)){ 60 | plotGseaTable(annod[term],stats=x,gseaRes,gseaParam=0.5) 61 | }else{ 62 | plotEnrichment(annod[[term]],stats=x) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /vignettes/EnrichR.R: -------------------------------------------------------------------------------- 1 | ## ------------------------------------------------------------------------ 2 | set.seed(1234) 3 | library(EnrichR) 4 | # To check if your the current species if supported !!! 5 | showData() 6 | showensemble() 7 | showplant() 8 | # Make the GO and KEGG Pathway data for your analysis 9 | # find suitable species name by using showensemble() 10 | hsa_go<-makeGOdat(species="human",keytype="SYMBOL") 11 | hsa_ko<-makeKOdat(species = "human",keytype="SYMBOL",builtin = F) 12 | # find suitable species name supported by reactome by using showAvailableRO() 13 | # hsa_ro<-makeROdata(species = "Homo_sapiens") 14 | 15 | ## ------------------------------------------------------------------------ 16 | # rice_go<-makeplantann(species="Oryza sativa Japonica",ann_type = "GO") #check the species name by using showplant() 17 | # rice_ko<-makeplantann(species="Oryza sativa Japonica",ann_type = "KEGG") 18 | # rice_pfam<-makeplantann(species="Oryza sativa Japonica",ann_type = "PFAM") 19 | # rice_inter<-makeplantann(species="Oryza sativa Japonica",ann_type = "InterPro") 20 | # rice_ro<-makePlantROdat(species = "Oryza_sativa") #check the species name by using showAvailablePlants() 21 | ## MSU version GO and KEGG infromation also supported named ricego,riceko 22 | ## Zea may V2 GO and KEGG annotation data also supported named zm_v2_go and zm_v2_ko 23 | # we also collect Reactome database for plant, you can just use makePlantROdat function to get RO data. 24 | 25 | ## ----fig.height=6,fig.width=6,fig.align="center",dpi=300----------------- 26 | df<-data.frame(gene=sample(unique(hsa_go$SYMBOL),2000),padj=abs(rnorm(2000,0,0.01))) 27 | rownames(df)<-df$gene 28 | res<-GE(df,GO_FILE = hsa_go,gene.cutoff = 0.01) 29 | head(res) 30 | 31 | ## ----fig.height=6,fig.width=6,fig.align="center"------------------------- 32 | GE.plot(resultFis =res,top=20,usePadj=F,pvalue.cutoff=0.05) 33 | resk<-KE(df,KO_FILE = hsa_ko,gene.cutoff = 0.05,builtin = F) 34 | head(resk) 35 | KE.plot(resultFis = resk,top=10,pvalue.cutoff = 0.05) 36 | 37 | ## ----fig.height=6,fig.width=6,fig.align="center",dpi=300----------------- 38 | richplot(res,usePadj=F,top=20) 39 | ## ----fig.height=6,fig.width=6,fig.align="center",dpi=300----------------- 40 | ###df could also be a vector of the genes you used for enrichment analysis 41 | netmap(df=df,rhs=res,top=20,pvalue.cutoff = 0.05,weightcut = 0.01,visNet = T,nodeselect=T) 42 | ## ----fig.height=6,fig.width=6,fig.align="center",dpi=300----------------- 43 | gnet(df=df,rhs=res,top=20,pvalue.cutoff = 0.05,weightcut = 0.01,vertex.label.cex=4) 44 | ## ----fig.height=6,fig.width=6,fig.align="center",dpi=300----------------- 45 | mnetmap(df=df,gores=res[1:30,],kores=resk,pvalue.cutoff = 0.05,top=50) 46 | ## ------------------------------------------------------------------------ 47 | resgo<-getdetail(res,df) 48 | head(resgo,6) 49 | resko<-getdetail(resk,df) 50 | head(resko,6) 51 | -------------------------------------------------------------------------------- /R/RE.R: -------------------------------------------------------------------------------- 1 | #' Reactome Pathway Enrichment analysis function 2 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 3 | #' @param species species name 4 | #' @param RO_FILE Reactome annotation data 5 | #' @param minSize minimal number of genes included in significant terms 6 | #' @param padj.method p value adjust method (default: BH) 7 | #' @param cutoff cutoff value for filtering significant terms (default: 0.05) 8 | #' @param maxSize maximum number of genes included in significant terms 9 | #' @param keepRich keep terms with rich factor value equal 1 or not (default: TRUE) 10 | #' @param filename output filename 11 | #' @param gene.cutoff the cut-off value for select DEGs (default: 0.01) 12 | #' @export 13 | #' @author Kai Guo 14 | RE<-function(df, RO_FILE, keytype = "SYMBOL", species = "human",padj.method = "BH", minSize=2,maxSize=500,keepRich=TRUE,filename = NULL,cutoff=0.05) { 15 | annot.info = RO_FILE$roan 16 | rownames(annot.info) <- annot.info$RO 17 | RO_FILE = RO_FILE$ro 18 | if (keytype != "ENTREZID") { 19 | id = idconvert(species = species, keys = RO_FILE$GeneID, 20 | fkeytype = "ENTREZID", tkeytype = keytype) 21 | RO_FILE2 = RO_FILE[RO_FILE$GeneID %in% names(id), ] 22 | RO_FILE2$GeneID = id[RO_FILE2$GeneID %in% names(id)] 23 | RO_FILE=RO_FILE2 24 | } 25 | res = enrich(df, annot = RO_FILE, annot.info = annot.info, 26 | filename = filename, padj.method = padj.method,minSize=minSize,maxSize=maxSize,keepRich=keepRich) 27 | res<-res[res$Pvalue do not edit by hand 2 | // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | #include 5 | 6 | using namespace Rcpp; 7 | 8 | // hyper_bench_vector 9 | NumericVector hyper_bench_vector(NumericVector& xin, NumericVector& yin, double N, double n); 10 | RcppExport SEXP _EnrichR_hyper_bench_vector(SEXP xinSEXP, SEXP yinSEXP, SEXP NSEXP, SEXP nSEXP) { 11 | BEGIN_RCPP 12 | Rcpp::RObject rcpp_result_gen; 13 | Rcpp::RNGScope rcpp_rngScope_gen; 14 | Rcpp::traits::input_parameter< NumericVector& >::type xin(xinSEXP); 15 | Rcpp::traits::input_parameter< NumericVector& >::type yin(yinSEXP); 16 | Rcpp::traits::input_parameter< double >::type N(NSEXP); 17 | Rcpp::traits::input_parameter< double >::type n(nSEXP); 18 | rcpp_result_gen = Rcpp::wrap(hyper_bench_vector(xin, yin, N, n)); 19 | return rcpp_result_gen; 20 | END_RCPP 21 | } 22 | // name_table 23 | NumericVector name_table(List& lh); 24 | RcppExport SEXP _EnrichR_name_table(SEXP lhSEXP) { 25 | BEGIN_RCPP 26 | Rcpp::RObject rcpp_result_gen; 27 | Rcpp::RNGScope rcpp_rngScope_gen; 28 | Rcpp::traits::input_parameter< List& >::type lh(lhSEXP); 29 | rcpp_result_gen = Rcpp::wrap(name_table(lh)); 30 | return rcpp_result_gen; 31 | END_RCPP 32 | } 33 | // fast_factor 34 | SEXP fast_factor(SEXP x); 35 | RcppExport SEXP _EnrichR_fast_factor(SEXP xSEXP) { 36 | BEGIN_RCPP 37 | Rcpp::RObject rcpp_result_gen; 38 | Rcpp::RNGScope rcpp_rngScope_gen; 39 | Rcpp::traits::input_parameter< SEXP >::type x(xSEXP); 40 | rcpp_result_gen = Rcpp::wrap(fast_factor(x)); 41 | return rcpp_result_gen; 42 | END_RCPP 43 | } 44 | // sf 45 | List sf(DataFrame& x); 46 | RcppExport SEXP _EnrichR_sf(SEXP xSEXP) { 47 | BEGIN_RCPP 48 | Rcpp::RObject rcpp_result_gen; 49 | Rcpp::RNGScope rcpp_rngScope_gen; 50 | Rcpp::traits::input_parameter< DataFrame& >::type x(xSEXP); 51 | rcpp_result_gen = Rcpp::wrap(sf(x)); 52 | return rcpp_result_gen; 53 | END_RCPP 54 | } 55 | // uniq 56 | StringVector uniq(StringVector& xa); 57 | RcppExport SEXP _EnrichR_uniq(SEXP xaSEXP) { 58 | BEGIN_RCPP 59 | Rcpp::RObject rcpp_result_gen; 60 | Rcpp::RNGScope rcpp_rngScope_gen; 61 | Rcpp::traits::input_parameter< StringVector& >::type xa(xaSEXP); 62 | rcpp_result_gen = Rcpp::wrap(uniq(xa)); 63 | return rcpp_result_gen; 64 | END_RCPP 65 | } 66 | 67 | static const R_CallMethodDef CallEntries[] = { 68 | {"_EnrichR_hyper_bench_vector", (DL_FUNC) &_EnrichR_hyper_bench_vector, 4}, 69 | {"_EnrichR_name_table", (DL_FUNC) &_EnrichR_name_table, 1}, 70 | {"_EnrichR_fast_factor", (DL_FUNC) &_EnrichR_fast_factor, 1}, 71 | {"_EnrichR_sf", (DL_FUNC) &_EnrichR_sf, 1}, 72 | {"_EnrichR_uniq", (DL_FUNC) &_EnrichR_uniq, 1}, 73 | {NULL, NULL, 0} 74 | }; 75 | 76 | RcppExport void R_init_EnrichR(DllInfo *dll) { 77 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 78 | R_useDynamicSymbols(dll, FALSE); 79 | } 80 | -------------------------------------------------------------------------------- /R/richplot.R: -------------------------------------------------------------------------------- 1 | ##' richplot 2 | ##' @description plot the sigificant terms and shared genes with network format 3 | ##' @importFrom GGally ggnet2 4 | ##' @importFrom ggrepel geom_text_repel 5 | ##' @importFrom igraph graph_from_data_frame 6 | ##' @importFrom igraph simplify 7 | ##' @importFrom ggrepel geom_text_repel 8 | ##' @importFrom igraph V 9 | ##' @importFrom ggplot2 geom_text 10 | ##' @param resultFis Enrichment results 11 | ##' @param top number of terms to show (default: 50) 12 | ##' @param pvalue.cutoff cutoff p value for enrichment result 13 | ##' @param padj.cutoff cutoff p adjust value for enrichment result 14 | ##' @param low color used for small value 15 | ##' @param high color used for large value 16 | ##' @param useTerm use terms for nodes (default: TRUE) 17 | ##' @param writeCyt write out the cytoscape file 18 | ##' @param cytoscapeFile output cytoscape File 19 | ##' @param label.color label color 20 | ##' @param label.size label size 21 | ##' @param node.shape vector of shape and names of the vector should be the terms (default: 20) 22 | ##' @param layout layout method 23 | ##' @param savefig save figures or not 24 | ##' @param filename output figure name 25 | ##' @param width width for output figure 26 | ##' @param height height for output figure 27 | ##' @param node.alpha alpha-transparency scales 28 | ##' @param node.shape shape of the node 29 | ##' @param repel use ggrepel text function or not 30 | ##' @param segment.size segment size for ggrepel text 31 | ##' @export 32 | richplot <- function(resultFis,top=50, pvalue.cutoff=0.05, padj.cutoff=NULL, 33 | usePadj =TRUE, useTerm=TRUE,low="orange",high="red", 34 | writeCyt=FALSE, cytoscapeFile="network-file-for-cytoscape.txt", 35 | label.color = "black", label.size = 2, node.shape=NULL, 36 | layout = layout.fruchterman.reingold,savefig=FALSE,filename="network", 37 | width=7,height=7,node.alpha=0.7,repel=TRUE,segment.size=0.2){ 38 | suppressMessages(library(igraph)) 39 | if(!is.null(padj.cutoff)){ 40 | resultFis<-resultFis[resultFis$Padj=top){ 45 | resultFis<-resultFis[1:top,] 46 | } 47 | resultFis$GeneID<-as.vector(resultFis$GeneID) 48 | lhs <- strsplit(resultFis$GeneID,",") 49 | if(isTRUE(useTerm)){ 50 | rhs <- data.frame(from=unlist(lhs),to=rep(resultFis$Term,lapply(lhs,length))) 51 | rownames(resultFis) <- resultFis$Term 52 | }else{ 53 | rhs <- data.frame(from=unlist(lhs),to=rep(resultFis$Annot,lapply(lhs,length))) 54 | rownames(resultFis) <- resultFis$Annot 55 | } 56 | if(isTRUE(usePadj)){ 57 | rhs$value <- -log10(resultFis[rhs$to,"Padj"]) 58 | 59 | }else{ 60 | rhs$value <- -log10(resultFis[rhs$to,"Pvalue"]) 61 | } 62 | if (writeCyt == TRUE) { 63 | write.table(rhs, file = cytoscapeFile, sep = "\t", 64 | row.names = F, quote = F) 65 | } 66 | pvalue1 <- rhs$value 67 | names(pvalue1) <- rhs$from 68 | pvalue1 <- unlist(lapply(split(pvalue1,names(pvalue1)),mean)) 69 | pvalue2 <- rhs$value 70 | names(pvalue2)<-rhs$to 71 | pvalue2 <- unlist(lapply(split(pvalue2,names(pvalue2)),mean)) 72 | pvalue<-c(pvalue1,pvalue2) 73 | g <- graph_from_data_frame(rhs, directed = F) 74 | pvalue = pvalue[V(g)$name] 75 | cols <- .color_scale(high, low) 76 | V(g)$color <- cols[sapply(pvalue, .getIdx, min(pvalue), max(pvalue))] 77 | if(!is.null(node.shape)){ 78 | node.shape=rep(20,length(V(g)$name)) 79 | names(node.shape)<-V(g)$name 80 | node.shape[names(node.shape)]<-node.shape 81 | }else{ 82 | node.shape=rep(20,length(V(g)$name)) 83 | names(node.shape)<-V(g)$name 84 | } 85 | p <- ggnet2(g, node.size = degree(g), node.color = V(g)$color, 86 | node.shape=node.shape,legend.position = "none", 87 | node.alpha=node.alpha) 88 | if(isTRUE(repel)){ 89 | p <- p + geom_text_repel(label = V(g)$name,size=label.size, color=label.color, 90 | segment.size=segment.size) 91 | }else{ 92 | p <- p +geom_text(label = V(g)$name,size=label.size, color=label.color) 93 | } 94 | if(savefig==TRUE){ 95 | ggsave(p,file=paste(filename,"pdf",sep="."),width=width,height = height) 96 | } 97 | print(p) 98 | } 99 | -------------------------------------------------------------------------------- /vignettes/EnrichR.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Functional Enrichment Analysis and Network Construction" 3 | author: "Kai Guo" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Vignette Title} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | %\VignetteEncoding{UTF-8} 10 | --- 11 | 12 | This document describes how to use the EnrichR package. 13 | Below is a simple manual for using the package 14 | 15 | ## Introduction 16 | You can do enrichment analysis for different type of annotation data (GO,KEGG,Reactome(may need reactome.db if you work with Human,PFAM and InterPro) 17 | 18 | ## Quick tour 19 | ```{r} 20 | set.seed(1234) 21 | library(EnrichR) 22 | # To check if your the current species if supported !!! 23 | showData() 24 | #showensemble() 25 | #showplant() 26 | # Make the GO and KEGG Pathway data for your analysis 27 | # find suitable species name by using showensemble() 28 | hsa_go<-makeGOdat(species="human",keytype="SYMBOL") 29 | hsa_ko<-makeKOdat(species = "human",keytype="SYMBOL",builtin = F) 30 | # find suitable species name supported by reactome by using showAvailableRO() 31 | # if you want have RO data just run 32 | # hsa_ro<-makeROdata(species = "Homo_sapiens") 33 | ``` 34 | ____ 35 | 36 | ### Make annotation from Ensemble 37 | * If you work with plant such as rice,you can use ensemble API make annotation data 38 | ```{r} 39 | # rice_go<-makeplantann(species="Oryza sativa Japonica",ann_type = "GO") #check the species name by using showplant() 40 | # rice_ko<-makeplantann(species="Oryza sativa Japonica",ann_type = "KEGG") 41 | # rice_pfam<-makeplantann(species="Oryza sativa Japonica",ann_type = "PFAM") 42 | # rice_inter<-makeplantann(species="Oryza sativa Japonica",ann_type = "InterPro") 43 | # rice_ro<-makePlantROdat(species = "Oryza_sativa") #check the species name by using showAvailablePlants() 44 | ## MSU version GO and KEGG infromation also supported named ricego,riceko 45 | ## Zea may V2 GO and KEGG annotation data also supported named zm_v2_go and zm_v2_ko 46 | # we also collect Reactome database for plant, you can just use makePlantROdat function to get RO data. 47 | ``` 48 | ---- 49 | 50 | ### Functional Enrichment Analysis 51 | #### Gene ontology enrichment analysis 52 | ```{r,fig.height=6,fig.width=6,fig.align="center",,fig.cap="Gene ontology enrichment analysis results"} 53 | df<-data.frame(gene=sample(unique(hsa_go$SYMBOL),2000),padj=abs(rnorm(2000,0,0.01))) 54 | rownames(df)<-df$gene 55 | res<-GE(df,GO_FILE = hsa_go,gene.cutoff = 0.01) 56 | head(res) 57 | ## Gene ontology enrichment analysis results 58 | GE.plot(resultFis =res,top=20,usePadj=F,pvalue.cutoff=0.05) 59 | ## You can use default paramters, the command above just to show if you want use pvalue as cut off value. 60 | ## Rich Factor: The proportion of numbers of genes in specific GO terms and numbers of all genes in the specific GO terms among the whole genomes. Color scale indicates significance level. 61 | ``` 62 | #### KEGG pathway enrichment analysis 63 | ```{r,fig.height=6,fig.width=6,fig.align="center",fig.cap="KEGG pathway enrichment analysis results"} 64 | ## KEGG pathway Enrichment analysis results 65 | resk<-KE(df,KO_FILE = hsa_ko,gene.cutoff = 0.05,builtin = F) 66 | head(resk) 67 | KE.plot(resultFis = resk,top=10,pvalue.cutoff = 0.05) 68 | ## Size indicates gene numbers in specific KEGG pathway     69 | 70 | ``` 71 | ### Network Generation with enrichment results 72 | You can also get network graphic for any type of enrichment analysis result and also combine different enrichment result 73 | ```{r,fig.height=6,fig.width=6} 74 | richplot(res,top=20,usePadj=F) 75 | ``` 76 | ```{r,fig.height=6,fig.width=6,fig.align="center",fig.cap="Functional relation network"} 77 | netmap(df=df,rhs=res,top=20,pvalue.cutoff = 0.05,weightcut = 0.01,visNet = T,nodeselect=T) 78 | ## df could be the vector you used for enrichment analysis 79 | ``` 80 | ```{r,fig.height=6,fig.width=6,fig.align="center",fig.cap="Functional relation network"} 81 | gnet(df=df,rhs=res,top=20,pvalue.cutoff = 0.05,weightcut = 0.01,vertex.label.cex=4) 82 | ``` 83 | ```{r,fig.height=6,fig.width=6,fig.align="center",fig.cap="Functional relation network"} 84 | mnetmap(df=df,gores=res[1:30,],kores=resk,pvalue.cutoff = 0.05,top=50) 85 | ``` 86 | #### Get details include the input information 87 | ```{r} 88 | resgo<-getdetail(res,df); 89 | head(resgo,6); 90 | resko<-getdetail(resk,df); 91 | head(resko,6) 92 | ``` 93 | ## Have funs! 94 | -------------------------------------------------------------------------------- /R/KE.R: -------------------------------------------------------------------------------- 1 | #' KEGG Pathway Enrichment analysis function 2 | #' @importFrom dplyr filter_ 3 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 4 | #' @param KO_FILE KEGG annotation data 5 | #' @param minSize minimal number of genes included in significant terms 6 | #' @param padj.method p value adjust method (default: BH) 7 | #' @param cutoff cutoff value for filtering significant terms (default: 0.05) 8 | #' @param maxSize maximum number of genes included in significant terms 9 | #' @param keepRich keep terms with rich factor value equal 1 or not (default: TRUE) 10 | #' @param filename output filename 11 | #' @param gene.cutoff the cut-off value for select DEGs (default: 0.01) 12 | #' @param bulitin use KEGG bulit in KEGG annotation or not(set FALSE if you want use newest KEGG data) 13 | #' @export 14 | KE<-function(df,KO_FILE,filename=NULL,gene.cutoff=0.01,minSize=2,maxSize=500,keepRich=TRUE,padj.method="BH",cutoff=0.05,builtin=TRUE){ 15 | suppressMessages(require(tidyr)) 16 | ko2gene<-sf(KO_FILE) 17 | ko2gene_num<-name_table(ko2gene) 18 | gene2ko<-sf(KO_FILE[,c(2,1)]) 19 | if(is.data.frame(df)){ 20 | IGE<-rownames(df)[df$padj=minSize) 47 | }else{ 48 | resultFis<-filter_(resultFis, ~Significant>=minSize|(~Annotated/~Significant)==1) 49 | } 50 | if(!is.null(filename)){ 51 | write.table(resultFis,file=paste(filename,".txt",sep=""),sep="\t",quote=F,row.names=F) 52 | } 53 | return(resultFis) 54 | } 55 | #' Display KEGG enrichment result 56 | #' @param resultFis: KEGG ennrichment analysis result data.frame 57 | #' @param top: Number of Terms you want to display 58 | #' @param filename: output filename 59 | #' @param pvalue.cutoff: the cut-off value for selecting Term 60 | #' @param padj.cutoff: the padj cut-off value for selecting Term 61 | #' @param usePadj use adjust pvalue or not 62 | #' @param order order bar or not 63 | #' @param low color used for small value 64 | #' @param high color used for large value 65 | #' @param alpha alpha-transparency scales 66 | #' @param horiz use horiz or not 67 | #' @param fontsize.x fontsize for x axis 68 | #' @param fontsize.y fontsize for y axis 69 | #' @param filename output filename 70 | #' @param width width for output file 71 | #' @param height height for output file 72 | #' @export 73 | #' @author Kai Guo 74 | KE.plot<-function(resultFis,pvalue.cutoff=0.05,top=50,order=FALSE, 75 | low="lightpink",high="red",alpha=0.7, 76 | font.x="bold",font.y="bold",fontsize.x=10,fontsize.y=10, 77 | padj.cutoff=NULL,usePadj=TRUE,filename=NULL,width=10,height=8){ 78 | library(ggplot2) 79 | if(!is.null(padj.cutoff)){ 80 | resultFis<-resultFis[resultFis$Padj=top){ 85 | dd<-resultFis[1:top,] 86 | }else{ 87 | dd<-resultFis 88 | } 89 | if(nrow(dd)>=1){ 90 | dd[,3]<-dd[,4]/dd[,3] 91 | colnames(dd)[3]<-"rich"; 92 | if(order==TRUE){ 93 | dd$Term<-factor(dd$Term,levels=dd$Term[order(dd$rich)]) 94 | } 95 | if(usePadj==FALSE){ 96 | p<-ggplot(dd,aes(x=rich,y=Term))+geom_point(aes(size=Significant,color=-log10(Pvalue)),alpha=alpha)+theme_minimal()+ 97 | theme(axis.text.y=element_text(face=font.y,size=fontsize.y),axis.text.x=element_text(face=font.x,color="black",size=fontsize.x))+ 98 | scale_colour_gradient(low=low,high=high)+ylab("Pathway name")+ 99 | xlab("Rich factor")+labs(size="Gene number")+guides(color=guide_colourbar(order = 1),size=guide_legend(order = 2)) 100 | print(p) 101 | }else{ 102 | p<-ggplot(dd,aes(x=rich,y=Term))+geom_point(aes(size=Significant,color=-log10(Padj)),alpha=alpha)+theme_minimal()+ 103 | theme(axis.text.y=element_text(face=font.y,size=fontsize.y),axis.text.x=element_text(face=font.x,color="black",size=fontsize.x))+ 104 | scale_colour_gradient(low=low,high=high)+ylab("Pathway name")+ 105 | xlab("Rich factor")+labs(size="Gene number")+guides(color=guide_colourbar(order = 1),size=guide_legend(order = 2)) 106 | print(p) 107 | }}else{ 108 | cat("No Pathway enrichment results were found!\n") 109 | } 110 | if(!is.null(filename)){ 111 | ggsave(p,file=paste(filename,"KEGG.pdf",sep="_"),width=width,height=height) 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /R/msigdbr.R: -------------------------------------------------------------------------------- 1 | ##' Download database from Msigdb and prepare for enrichment analysis 2 | ##' @name msigdbr 3 | ##' @importFrom msigdbr msigdbr 4 | ##' @importFrom dplyr filter_ 5 | ##' @importFrom dplyr select_ 6 | ##' @importFrom magrittr %>% 7 | ##' @param species the species for query 8 | ##' @param keytype the gene ID type 9 | ##' @param category Gene set category 10 | ##' @param subcategory Gene Set subcategory 11 | ##' @param save save the dataset or not 12 | ##' @param path path to save the dataset 13 | ##' @export 14 | ##' @author Kai Guo 15 | makeMSIGDB<-function(species="human",keytype="SYMBOL",subcategory=NULL, 16 | category=NULL,path="./",save=FALSE){ 17 | flag=0 18 | if(!is.null(subcategory)){ 19 | if(subcategory=="CGP"){ 20 | category<-"C2" 21 | } 22 | if(subcategory=="CP"){ 23 | category<-"C2" 24 | } 25 | if(subcategory=="KEGG"){ 26 | subcategory<-"CP:KEGG" 27 | category<-"C2" 28 | } 29 | if(subcategory=="REACTOME"){ 30 | subcategory<-"CP:REACTOME" 31 | category<-"C2" 32 | } 33 | if(subcategory=="BIOCARTA"){ 34 | subcategory<-"CP:BIOCARTA" 35 | category<-"C2" 36 | } 37 | if(subcategory=="MIR"){ 38 | category<-"C3" 39 | } 40 | if(subcategory=="TFT"){ 41 | category<-"C3" 42 | } 43 | if(subcategory=="CGN"){ 44 | category=="C4" 45 | } 46 | if(subcategory=="CM"){ 47 | category<-"C4" 48 | } 49 | if(subcategory=="BP"){ 50 | category<-"C5" 51 | } 52 | if(subcategory=="CC"){ 53 | category<-"C5" 54 | } 55 | if(subcategory=="MF"){ 56 | category<-"C5" 57 | } 58 | } 59 | mspe<-.getmsig(species) 60 | if(is.null(mspe)){ 61 | stop(cat("can't find support species!\n")) 62 | } 63 | if(keytype=="SYMBOL"){ 64 | key="gene_symbol" 65 | }else if(keytype=="ENTREZID"){ 66 | key="entrez_gene" 67 | }else{ 68 | key="entrez_gene" 69 | flag=1 70 | } 71 | cat("Downloading msigdb datasets ...\n") 72 | res <- msigdbr(species=mspe) 73 | res <- res%>%filter_(~gs_cat==category) 74 | if(!is.null(subcategory)){ 75 | res <- res%>%filter_(~gs_subcat==subcategory) 76 | } 77 | res<-res%>%select_(~key,~gs_name) 78 | if(flag==1){ 79 | res[,1]<-idconvert(species,keys=res[,1],fkeytype="ENTREZID", tkeytype=keytype) 80 | res<-na.omit(res) 81 | } 82 | if(isTRUE(save)){ 83 | write.csv(res%>%select_(~key,~gs_name,~gs_cat,~gs_subcat), 84 | file=paste(path,species,"_msigdb.csv",sep="")) 85 | } 86 | return(res) 87 | } 88 | 89 | 90 | ##' msigdb support species 91 | ##' @param species with common name 92 | .getmsig<-function(species="human"){ 93 | out<-NULL 94 | if(species=="human"){ 95 | out<-"Homo sapiens" 96 | }else if(species=="mouse"){ 97 | out<-"Mus musculus" 98 | }else if(species=="rat"){ 99 | out<-"Rattus norvegicus" 100 | }else if(species=="celegans"){ 101 | out<-"Caenorhabditis elegans" 102 | }else if(species=="fly"){ 103 | out<-"rosophila melanogaster" 104 | }else if(species=="yeast"){ 105 | out<-"Saccharomyces cerevisiae" 106 | }else if(species=="bovine"){ 107 | out<-"Bos taurus" 108 | }else if(species=="canine"){ 109 | out<-"Canis lupus familiaris" 110 | }else if(species=="pig"){ 111 | out<-"Sus scrofa" 112 | }else if(species=="chicken"){ 113 | out<-"Gallus gallus" 114 | }else if(species=="zebrafish"){ 115 | out<-"Danio rerio" 116 | }else{ 117 | out<-NULL 118 | } 119 | } 120 | ##' Print MSIGDB infomation 121 | ##' @export 122 | msigdbinfo <- function() { 123 | cat("#--------------------------------------------------------------#\n") 124 | cat("# Molecular Signatures Database v6.2.1 #\n") 125 | cat("#--------------------------------------------------------------#\n") 126 | cat("# Category | Subcategory # Details ----------------------------#\n") 127 | cat("# C1 # Positional (326) #\n") 128 | cat("# C2 | CGP # Chemical and Genetic Perturbations (3433) #\n") 129 | cat("# C2 | CP # Canonical Pathways (252) #\n") 130 | cat("# C2 | BIOCARTA # Canonical BIOCARTA (217) #\n") 131 | cat("# C2 | KEGG # Canonical KEGG (186) #\n") 132 | cat("# C2 | CPREACTOME # Canonical REACTOME (674) #\n") 133 | cat("# C3 | MIR # Motif miRNA Targets (221) #\n") 134 | cat("# C3 | TFT # Motif Transcription Factor Targets (615) #\n") 135 | cat("# C4 | CGN # Cancer Gene Neighborhoods (427) #\n") 136 | cat("# C4 | CM # Cancer Modules (431) #\n") 137 | cat("# C5 | BP # GO Biological Process (4436) #\n") 138 | cat("# C5 | CC # GO Cellular Component (580) #\n") 139 | cat("# C5 | MF # GO Molecular Function (901) #\n") 140 | cat("# C6 # Oncogenic Signatures (189) #\n") 141 | cat("# C7 # Immunologic Signatures (4872) #\n") 142 | cat("# H # Hallmark (50) #\n") 143 | cat("#--------------------------------------------------------------#\n") 144 | cat("# Source: http://software.broadinstitute.org/gsea/msigdb #\n") 145 | cat("#--------------------------------------------------------------#\n") 146 | listspe<-c("human","mouse","rat","celegans","fly","yeast","bovine","canine", 147 | "pig","chicken","zebrafish") 148 | cat("# Support species: #\n") 149 | cat(sort(listspe),"\n") 150 | } 151 | -------------------------------------------------------------------------------- /R/getrodata.R: -------------------------------------------------------------------------------- 1 | #' make Reactome annotation data function 2 | #' @importFrom AnnotationDbi as.list 3 | #' @param species you can check the supported species by using showAvailableRO and showAvailablePlants 4 | #' @export 5 | #' @author Kai Guo 6 | makeROdata<-function(species="Homo_sapiens"){ 7 | dbname<-.getrodbname(species=species); 8 | if (!require("reactome.db",character.only=TRUE)){ 9 | source("http://bioconductor.org/biocLite.R") 10 | biocLite("reactome.db") 11 | }else{ 12 | suppressMessages(require("reactome.db",character.only = T,quietly = T)) 13 | } 14 | dbname=sapply(strsplit(dbname,"_"),'[[',1) 15 | lhs<-as.list(reactomePATHNAME2ID) 16 | lhs<-lhs[grep(dbname,names(lhs))] 17 | roid<-as.list(reactomePATHID2EXTID)[unique(as.vector(unlist(lhs)))] 18 | roid<-lapply(roid, function(x)unique(x)) 19 | roid<-data.frame("GeneID"=unlist(roid),"RO"=rep(names(roid),times=lapply(roid, length)),row.names=NULL) 20 | ll<-lapply(lhs,function(x)unique(x)) 21 | roan<-data.frame("RO"=unlist(ll),"Description"=rep(names(ll),times=lapply(ll,length)),row.names=NULL) 22 | res<-list() 23 | res$ro<-roid 24 | res$roan<-roan 25 | return(res) 26 | } 27 | #' make Plant Reactome annotation data function 28 | #' @importFrom dplyr filter_ 29 | #' @importFrom dplyr select_ 30 | #' @param species: you can check the supported species by using showAvailableRO and showAvailablePlants 31 | #' @export 32 | #' @author Kai Guo 33 | makeplantROdata<-function(species="Arabidopsis_thaliana"){ 34 | data(rodata) 35 | dbname<-.getplantrodbname(species=species); 36 | RO_FILE<-filter_(rodata,~species==dbname) 37 | RO_FILE<-select_(RO_FILE,~GeneID,~RO,~Description,~species) 38 | return(as.data.frame(RO_FILE)); 39 | } 40 | simpleCap <- function(x) { 41 | s <- strsplit(x, " ")[[1]] 42 | paste(toupper(substring(s, 1,1)), substring(s, 2), 43 | sep="", collapse=" ") 44 | } 45 | .getplantrodbname<-function(species="Arabidopsis_thaliana"){ 46 | species=simpleCap(species); 47 | dbname=.getplantrodb(species=species); 48 | if(is.null(dbname)){ 49 | cat("You must check if your request database is avaliable by using showAvailablePlants()\n") 50 | stop("databse error!") 51 | } 52 | return(dbname) 53 | } 54 | .getrodbname<-function(species){ 55 | species=simpleCap(species); 56 | dbname=.getrodb(species=species); 57 | if(is.null(dbname)){ 58 | cat("You must check if your request database is avaliable by using showAvailableRO()\n") 59 | stop("databse error!") 60 | } 61 | return(dbname) 62 | } 63 | .getrodb<-function(species){ 64 | dbname=tryCatch(match.arg(species,c("Homo_sapiens","Dictyostelium_discoideum","Plasmodium_falciparum","Schizosaccharomyces_pombe","Saccharomyces_cerevisiae", 65 | "Caenorhabditis_elegans","Sus_scrofa","Bos_taurus","Canis_familiaris","Mus_musculus","Rattus_norvegicus","Taeniopygia_guttata", 66 | "Xenopus_tropicalis","Danio_rerio","Drosophila_melanogaster","Arabidopsis_thaliana","Oryza_sativa","Gallus_gallus", 67 | "Mycobacterium_tuberculosis")), 68 | error=function(cond){return(NULL)}) 69 | return(dbname) 70 | } 71 | .getplantrodb<-function(species="Arabidopsis_thaliana"){ 72 | speices=simpleCap(species) 73 | dbname=tryCatch(match.arg(species,c("Arachis_duranensis","Arachis_ipaensis","Arabidopsis_lyrata","Aegilops_tauschii","Arabidopsis_thaliana", 74 | "Amborella_trichopoda","Brachypodium_distachyon","Brassica_napus","Brassica_oleracea","Brassica_rapa", 75 | "Beta_vulgaris","Capsicum_annuum","Cicer_arietinum","Cajanus_cajan","Coffea_canephora","Cyanidioschyzon_merolae", 76 | "Chlamydomonas_reinhardtii","Citrus_sinensis","Eucalyptus_grandis","Fragaria_vesca","Glycine_max","Gossypium_raimondii", 77 | "Hordeum_vulgare","Jatropha_curcas","Leersia_perrieri","Musa_acuminata","Malus_domestica","Manihot_esculenta","Mimulus_guttatus", 78 | "Medicago_truncatula","Oryza_australiensis","Oryza_barthii","Oryza_brachyantha","Oryza_glaberrima","Oryza_granulata", 79 | "Oryza_glumaepatula","Oryza_kasalath","Oryza_longistaminata","Ostreococcus_lucimarinus","Oryza_meridionalis","Oryza_minuta", 80 | "Oryza_nivara","Oryza_officinalis","Oryza_punctata","Oryza_rufipogon","Oryza_sativa","Oryza_sativa_Indica","Picea_abies", 81 | "Phoenix_dactylifera","Physcomitrella_patens","Prunus_persica","Pinus_taeda","Populus_trichocarpa","Phaseolus_vulgaris", 82 | "Sorghum_bicolor","Setaria_italica","Solanum_lycopersicum","Selaginella_moellendorffii","Synechocystis_pcc6803","Solanum_tuberosum", 83 | "Triticum_aestivum","Theobroma_cacao","Trifolium_pratense","Triticum_turgidum","Triticum_urartu","Vitis_vinifera","Zea_mays")), 84 | error=function(cond){return(NULL)}) 85 | return(dbname) 86 | } 87 | #' show avaliable plant Reactome annotation data function 88 | #' @export 89 | #' @author Kai Guo 90 | showAvailableplantsRO<-function(){ 91 | cbind(unique(rodata$species)) 92 | } 93 | #' show avaliable Reactome annotation data except plant 94 | #' @export 95 | #' @author Kai Guo 96 | showAvailableRO<-function(){ 97 | cbind(c("Homo_sapiens","Dictyostelium_discoideum","Plasmodium_falciparum","Schizosaccharomyces_pombe","Saccharomyces_cerevisiae", 98 | "Caenorhabditis_elegans","Sus_scrofa","Bos_taurus","Canis_familiaris","Mus_musculus","Rattus_norvegicus","Taeniopygia_guttata", 99 | "Xenopus_tropicalis","Danio_rerio","Drosophila_melanogaster","Arabidopsis_thaliana","Oryza_sativa","Gallus_gallus", 100 | "Mycobacterium_tuberculosis")) 101 | } 102 | -------------------------------------------------------------------------------- /R/gnet.R: -------------------------------------------------------------------------------- 1 | ##' @name gnet 2 | ##' @title Draw network with ggplot2 style 3 | ##' @description gnet provides method to draw the enrichment results with a ggplot2 style network 4 | ##' @rdname gnet 5 | ##' @importFrom GGally ggnet2 6 | ##' @importFrom ggrepel geom_text_repel 7 | ##' @importFrom reshape2 melt 8 | ##' @importFrom igraph write_graph 9 | ##' @param df Differential expression result or vector of genes 10 | ##' @param rhs Enrichment results 11 | ##' @param top number of terms to show (default: 50) 12 | ##' @param pvalue.cutoff cutoff p value for enrichment result 13 | ##' @param padj.cutoff cutoff p adjust value for enrichment result 14 | ##' @param low color used for small value 15 | ##' @param high color used for large value 16 | ##' @param weightcut cutoff value for the edges 17 | ##' @param useTerm use terms for nodes (default: TRUE) 18 | ##' @param writeCyt write out the cytoscape file 19 | ##' @param cytoscapeFile output cytoscape File 20 | ##' @param segment.size size for label segment 21 | ##' @param vertex.label.color label color 22 | ##' @param vertex.label.cex label size 23 | ##' @param vertex.node.shape vector of shape and names of the vector should be the terms (default: 20) 24 | ##' @param layout layout method 25 | ##' @param savefig save figures or not 26 | ##' @param filename output figure name 27 | ##' @param width width for output figure 28 | ##' @param height height for output figure 29 | ##' @param node.alpha alpha-transparency scales 30 | ##' @export 31 | ##' @author Kai Guo 32 | 33 | gnet<-function (df, rhs, top = 50, pvalue.cutoff = 0.05, padj.cutoff = NULL,low = "orange",high = "red", 34 | weightcut = 0.2, useTerm = TRUE, writeCyt = FALSE,cytoscapeFile = "network-file-for-cytoscape.txt", 35 | vertex.label.color = "black", vertex.label.cex = 2,vertex.node.shape=NULL, layout = layout.fruchterman.reingold,savefig=FALSE,filename="network", 36 | width=7,height=7,segment.size=0.2,node.alpha=0.7,...) 37 | { 38 | options(stringsAsFactors = F) 39 | suppressMessages(library(reshape2)) 40 | suppressMessages(library(igraph)) 41 | suppressMessages(library(GGally)) 42 | suppressMessages(library(ggrepel)) 43 | suppressMessages(library(intergraph)) 44 | if (!is.null(padj.cutoff)) { 45 | rhs <- rhs[rhs$Padj < padj.cutoff, ] 46 | } 47 | else { 48 | rhs <- rhs[rhs$Pvalue < pvalue.cutoff, ] 49 | } 50 | if (nrow(rhs) <= top) { 51 | rhs <- rhs 52 | } 53 | else { 54 | rhs <- rhs[1:top, ] 55 | } 56 | if (is.data.frame(df)) { 57 | gene_p <- -log10(df$padj) 58 | names(gene_p) <- rownames(df) 59 | } 60 | else { 61 | gene_p <- rep(1, length(df)) 62 | names(gene_p) <- df 63 | } 64 | pvalue = rhs$Pvalue 65 | names(pvalue) <- rownames(rhs) 66 | go2gen <- strsplit(x = as.vector(rhs$GeneID), split = ",") 67 | names(go2gen) <- rownames(rhs) 68 | gen2go <- reverseList(go2gen) 69 | golen <- rhs$Significant 70 | names(golen) <- rownames(rhs) 71 | gen2golen <- lapply(gen2go, function(x) golen[x]) 72 | gen2gosum <- lapply(gen2golen, function(x) sum(x)/x) 73 | gen2res <- lapply(gen2gosum, function(x) x/sum(x)) 74 | id <- rownames(rhs) 75 | n = nrow(rhs) 76 | w <- matrix(NA, nrow = n, ncol = n) 77 | colnames(w) <- rownames(w) <- rownames(rhs) 78 | for (i in 1:n) { 79 | ni <- id[i] 80 | for (j in i:n) { 81 | nj <- id[j] 82 | genein = intersect(go2gen[[ni]], go2gen[[nj]]) 83 | geneup <- sum(gene_p[genein] * unlist(lapply(lapply(gen2res[genein], 84 | "[", c(ni, nj)), sum))) 85 | genei <- setdiff(go2gen[[ni]], go2gen[[nj]]) 86 | genej <- setdiff(go2gen[[nj]], go2gen[[ni]]) 87 | geneid <- sum(gene_p[genei] * unlist(lapply(lapply(gen2res[genei], 88 | "[", ni), sum))) 89 | genejd <- sum(gene_p[genej] * unlist(lapply(lapply(gen2res[genej], 90 | "[", nj), sum))) 91 | gened <- geneup + geneid + genejd 92 | w[i, j] <- geneup/gened 93 | } 94 | } 95 | if (useTerm == TRUE) { 96 | colnames(w) <- rownames(w) <- rhs$Term 97 | names(pvalue) = rhs$Term 98 | } 99 | wn <- melt(w, as.is = TRUE) 100 | wn <- wn[wn[, 1] != wn[, 2], ] 101 | wn <- wn[!is.na(wn[, 3]), ] 102 | wn <- wn[wn[, 3] > 0, ] 103 | g <- igraph::graph.data.frame(wn[, -3], directed = F) 104 | E(g)$width = sqrt(wn[, 3] * 5) 105 | pvalue = pvalue[V(g)$name] 106 | if (useTerm == TRUE) { 107 | idx <- unlist(sapply(V(g)$name, function(x) which(x == 108 | rhs$Term))) 109 | } 110 | else { 111 | idx <- unlist(sapply(V(g)$name, function(x) which(x == 112 | rownames(rhs)))) 113 | } 114 | cols <- .color_scale(high, low) 115 | V(g)$color <- cols[sapply(pvalue, .getIdx, min(pvalue), max(pvalue))] 116 | g <- igraph::delete.edges(g, E(g)[wn[, 3] < weightcut]) 117 | gs <- rhs$Significant 118 | if (useTerm == TRUE) { 119 | names(gs) <- rhs$Term 120 | } 121 | else { 122 | names(gs) <- rownames(rhs) 123 | } 124 | V(g)$size <- log(gs[V(g)$name], base = 10) * 10 125 | if(!is.null(vertex.node.shape)){ 126 | node.shape=rep(20,length(V(g)$name)) 127 | names(node.shape)<-V(g)$name 128 | node.shape[names(vertex.node.shape)]<-vertex.node.shape 129 | }else{ 130 | node.shape=rep(20,length(V(g)$name)) 131 | names(node.shape)<-V(g)$name 132 | } 133 | 134 | p<-ggnet2(g, node.size = V(g)$size, node.color = V(g)$color, 135 | edge.size = E(g)$width/10,node.shape=node.shape,node.alpha=node.alpha) + 136 | geom_text_repel(label = V(g)$name, 137 | size=vertex.label.cex,segment.size=segment.size,color=vertex.label.color)+ 138 | theme(legend.position = "none") 139 | if (writeCyt == TRUE) { 140 | write_graph(g, file = cytoscapeFile, format="graphml") 141 | } 142 | print(p) 143 | if(savefig==TRUE){ 144 | ggsave(p,file=paste(filename,"pdf",sep="."),width=width,height = height) 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /R/netmap_m.R: -------------------------------------------------------------------------------- 1 | #' plot combine network of Terms 2 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 3 | #' @param gores GO Enrichment analsyis result 4 | #' @param kores KEGG Enrichment analsyis result 5 | #' @param rores Reactome Pathway Enrichment analsyis result 6 | #' @param pfres PFAM Enrichment analysis result result 7 | #' @param itres InterPro Enrichment analysis result 8 | #' @param pvalue.cutoff the cut-off P value for selecting significant Terms 9 | #' @param padj.cutoff the cut-off P adjust value for selecting significant Terms 10 | #' @param weightcut the weight cut value for remove edges 11 | #' @param useTerm use the Term description or not(defalutTRUE) 12 | #' @param writeCyt export file for Cyt software 13 | #' @param vertex.label.color color of label(defaultblack) 14 | #' @param vertex.label.cex size of label(default0.5) 15 | #' @param vertex.node.shape vector of shape and names of the vector should be the terms (default 20) 16 | #' @param layout layout format (defultlayout.fruchterman.reingold) 17 | #' @param visNet use VisNetwork method to display network(defaultFALSE) 18 | #' @param top number of Terms you want to display(defaultthe total number of all Significant number) 19 | #' @export 20 | #' @author Kai Guo 21 | mnetmap<-function(df,gores=NULL,kores=NULL,rores=NULL,pfres=NULL, 22 | itres=NULL,top=NULL,top.display=NULL,pvalue.cutoff=0.05,padj.cutoff=NULL, 23 | visNet=F,weightcut=0.2,useTerm=TRUE,layout=NULL,vertex.label.cex=0.5,gnet=FALSE,vertex.node.shape=NULL,...){ 24 | if(!is.null(top)){ 25 | rhs<-Reduce(function(x, y) rbind(x, y), list(gores[1:top,], kores[1:top,], rores[1:top,],pfres[1:top,],itres[1:top,])) 26 | } 27 | rhs<-Reduce(function(x, y) rbind(x, y), list(gores, kores, rores,pfres,itres)) 28 | rhs<-rhs[order(rhs$Pvalue),] 29 | rhs<-rhs[rhs$Pvalue% 44 | #' @importFrom dplyr pull 45 | #' @importFrom plyr . 46 | #' @param rhslist of enrchment analysis result among DEG groups 47 | #' @param top the number of Terms you want to display 48 | #' @param colnames the compare DEG group names 49 | #' @param xsize cex of group name 50 | #' @param ysize cex of Terms name 51 | #' @export 52 | #' @author Kai Guo 53 | lheatmap<-function (rhs, top = 50, colnames = NULL, xsize = 6, ysize = 6,padj=NULL,horizontal=FALSE,pval=0.05,returnData=FALSE,...) 54 | { 55 | options(stringsAsFactors = F) 56 | suppressMessages(library(ggplot2)) 57 | if(!is.null(padj)){ 58 | rhs <- lapply(rhs, function(x) x[, c("Term", "Padj")]) 59 | rhs<-lapply(rhs,function(x)x[x$Padj%arrange(Padj)%>% 61 | select_(~Term)%>%pull(1)%>%.[1:top]))) 62 | }else{ 63 | rhs <- lapply(rhs, function(x) x[, c("Term", "Pvalue")]) 64 | rhs<-lapply(rhs,function(x)x[x$Pval%arrange(Pvalue)%>% 66 | select_(~Term)%>%pull(1)%>%.[1:top]))) 67 | } 68 | res <- Reduce(function(x, y) full_join(x, y, by = "Term"),rhs) 69 | if (!is.null(colnames)) { 70 | colnames(res)[2:ncol(res)] <- colnames 71 | } 72 | else { 73 | colnames(res)[2:ncol(res)] <- paste("Group", 1:(ncol(res) - 74 | 1), sep = "_") 75 | } 76 | res[is.na(res)] <- 1 77 | res<-res%>%filter_(~Term%in%sel) 78 | res<-as.data.frame(res) 79 | rownames(res)<-res$Term 80 | cor_mat<-cor(t(res[,2:ncol(res)])) 81 | dd <- as.dist((1-cor_mat)/2); 82 | hc <- hclust(dd); 83 | melted <- melt(res[hc$order,]) 84 | melted$Term<-factor(melted$Term,levels=res$Term[hc$order]) 85 | maxp = max(-log10(melted[, 3])) + 0.5 86 | if(!is.null(padj)){ 87 | colnames(melted)[3] <- "Padj" 88 | p<-ggplot(melted, aes(x = variable, y = Term, fill = -log10(Padj))) +coord_equal(ratio = 0.8)+ 89 | geom_tile(color = "white") + scale_fill_gradient2(low = "white", 90 | high = "red", midpoint = 0, limit = c(0, maxp)) +theme_minimal()+theme(axis.text.y = element_text(size = ysize), axis.text.x = element_text(angle = 70, 91 | vjust = 1, size = xsize, hjust = 1,face = "bold")) + 92 | theme(axis.title.x = element_blank(),axis.title.y = element_blank(), panel.grid.major = element_blank(), 93 | panel.grid.minor = element_blank()) 94 | }else{ 95 | colnames(melted)[3] <- "Pvalue" 96 | p<-ggplot(melted, aes(x = variable, y = Term, fill = -log10(Pvalue))) +coord_equal(ratio = 0.8)+ 97 | geom_tile(color = "white") + scale_fill_gradient2(low = "white", 98 | high = "red", midpoint = 0, limit = c(0, maxp)) +theme_minimal()+theme(axis.text.y = element_text(size = ysize), axis.text.x = element_text(angle = 70, 99 | vjust = 1, size = xsize, hjust = 1,face = "bold")) + 100 | theme(axis.title.x = element_blank(),axis.title.y = element_blank(), panel.grid.major = element_blank(), 101 | panel.grid.minor = element_blank()) 102 | } 103 | if(horizontal==TRUE){ 104 | p<-p+coord_flip() 105 | } 106 | if(returnData==TRUE){ 107 | return(res) 108 | }else{ 109 | print(p) 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /R/netmap.R: -------------------------------------------------------------------------------- 1 | #' Plot network of Terms 2 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 3 | #' @param rhs Enrichment analsyis result 4 | #' @param pvalue.cutoff the cut-off P value for selecting significant Terms 5 | #' @param padj.cutoff the cut-off P adjust value for selecting significant Terms 6 | #' @param low color used for small value 7 | #' @param high color used for large value 8 | #' @param weightcut the weight cut value for remove edges 9 | #' @param useTerm use the Term description or not(defalutTRUE) 10 | #' @param writeCyt export file for Cyt software 11 | #' @param vertex.label.color color of label(defaultblack) 12 | #' @param vertex.label.cex size of label(default0.5) 13 | #' @param layout layout format (defultlayout.fruchterman.reingold) 14 | #' @param visNet use VisNetwork method to display network(defaultFALSE) 15 | #' @param smooth use smooth edge for visNetwork 16 | #' @param nodeselect select some interesting node(defaultFALSE) 17 | #' @param editvis choose to edit network(defaultFALSE) 18 | #' @param savevis save visnetwork to html 19 | #' @param savefig save figure to pdf 20 | #' @param filename output filename 21 | #' @param top number of Terms you want to display 22 | #' @export 23 | #' @author Kai Guo 24 | netmap<-function (df, rhs, top = 50, pvalue.cutoff = 0.05, padj.cutoff = NULL,low = "orange",high = "red", 25 | weightcut = 0.2, useTerm = TRUE, writeCyt = FALSE,cytoscapeFile = "network-file-for-cytoscape.txt", vertex.label.font = 2, 26 | vertex.label.color = "black", vertex.label.cex = 0.5, layout = layout.fruchterman.reingold, 27 | visNet = FALSE,smooth=TRUE,nodeselect=FALSE,editvis=FALSE,savevis=FALSE,savefig=FALSE,filename="network",...) 28 | { 29 | options(stringsAsFactors = F) 30 | suppressMessages(library(reshape2)) 31 | suppressMessages(library(igraph)) 32 | if (!is.null(padj.cutoff)) { 33 | rhs <- rhs[rhs$Padj < padj.cutoff, ] 34 | } 35 | else { 36 | rhs <- rhs[rhs$Pvalue < pvalue.cutoff, ] 37 | } 38 | if (nrow(rhs) <= top) { 39 | rhs <- rhs 40 | } 41 | else { 42 | rhs <- rhs[1:top, ] 43 | } 44 | if (is.data.frame(df)) { 45 | gene_p <- -log10(df$padj) 46 | names(gene_p) <- rownames(df) 47 | } 48 | else { 49 | gene_p <- rep(1, length(df)) 50 | names(gene_p) <- df 51 | } 52 | pvalue = rhs$Pvalue 53 | names(pvalue) <- rownames(rhs) 54 | go2gen <- strsplit(x = as.vector(rhs$GeneID), split = ",") 55 | names(go2gen) <- rownames(rhs) 56 | gen2go <- reverseList(go2gen) 57 | golen <- rhs$Significant 58 | names(golen) <- rownames(rhs) 59 | gen2golen <- lapply(gen2go, function(x) golen[x]) 60 | gen2gosum <- lapply(gen2golen, function(x) sum(x)/x) 61 | gen2res <- lapply(gen2gosum, function(x) x/sum(x)) 62 | id <- rownames(rhs) 63 | n = nrow(rhs) 64 | w <- matrix(NA, nrow = n, ncol = n) 65 | colnames(w) <- rownames(w) <- rownames(rhs) 66 | for (i in 1:n) { 67 | ni <- id[i] 68 | for (j in i:n) { 69 | nj <- id[j] 70 | genein = intersect(go2gen[[ni]], go2gen[[nj]]) 71 | geneup <- sum(gene_p[genein] * unlist(lapply(lapply(gen2res[genein], 72 | "[", c(ni, nj)), sum))) 73 | genei <- setdiff(go2gen[[ni]], go2gen[[nj]]) 74 | genej <- setdiff(go2gen[[nj]], go2gen[[ni]]) 75 | geneid <- sum(gene_p[genei] * unlist(lapply(lapply(gen2res[genei], 76 | "[", ni), sum))) 77 | genejd <- sum(gene_p[genej] * unlist(lapply(lapply(gen2res[genej], 78 | "[", nj), sum))) 79 | gened <- geneup + geneid + genejd 80 | w[i, j] <- geneup/gened 81 | } 82 | } 83 | if (useTerm == TRUE) { 84 | colnames(w) <- rownames(w) <- rhs$Term 85 | names(pvalue) = rhs$Term 86 | } 87 | wn <- melt(w, as.is = TRUE) 88 | wn <- wn[wn[, 1] != wn[, 2], ] 89 | wn <- wn[!is.na(wn[, 3]), ] 90 | wn <- wn[wn[, 3] > 0, ] 91 | g <- igraph::graph.data.frame(wn[, -3], directed = F) 92 | E(g)$width = sqrt(wn[, 3] * 5) 93 | pvalue = pvalue[V(g)$name] 94 | if (useTerm == TRUE) { 95 | idx <- unlist(sapply(V(g)$name, function(x) which(x == 96 | rhs$Term))) 97 | } 98 | else { 99 | idx <- unlist(sapply(V(g)$name, function(x) which(x == 100 | rownames(rhs)))) 101 | } 102 | cols <- .color_scale(high, low) 103 | V(g)$color <- cols[sapply(pvalue, .getIdx, min(pvalue), max(pvalue))] 104 | g <- igraph::delete.edges(g, E(g)[wn[, 3] < weightcut]) 105 | gs <- rhs$Significant 106 | if (useTerm == TRUE) { 107 | names(gs) <- rhs$Term 108 | } 109 | else { 110 | names(gs) <- rownames(rhs) 111 | } 112 | V(g)$size <- log(gs[V(g)$name], base = 10) * 10 113 | if (writeCyt == TRUE) { 114 | write_graph(g, file = cytoscapeFile, format="graphml") 115 | } 116 | if (visNet == TRUE) { 117 | suppressMessages(library(visNetwork)) 118 | graph <- visIgraph(g, smooth = smooth) 119 | if(nodeselect==TRUE){ 120 | graph<-graph%>%visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)%>% 121 | visInteraction(navigationButtons = TRUE) 122 | } 123 | if(editvis==TRUE){ 124 | graph<-graph%>%visInteraction(navigationButtons = TRUE)%>%visOptions(manipulation = TRUE) 125 | 126 | } 127 | if(savevis==TRUE){ 128 | visSave(graph,file = paste(filename,"html",sep=".")) 129 | } 130 | graph 131 | } 132 | else { 133 | l <- layout_with_fr(g) 134 | l <- norm_coords(l, ymin = -1, ymax = 1, xmin = -1, xmax = 1) 135 | par(mar = c(2, 2, 2, 2)) 136 | plot.igraph(g, vertex.label.font = vertex.label.font, 137 | vertex.label.color = "black", vertex.label.cex = vertex.label.cex, 138 | vertex.frame.color = V(g)$color, rescale = F, layout = l * 139 | 0.8) 140 | if(savefig==TRUE){ 141 | dev.print(pdf,file=paste(filename,"pdf",sep=".")) 142 | } 143 | } 144 | } 145 | .color_scale <- function(c1="pink", c2="red") { #modified from DOSE 146 | pal <- colorRampPalette(c(c1, c2)) 147 | colors <- pal(200) 148 | return(colors) 149 | } 150 | .getIdx <- function(v, MIN, MAX) { #modified from DOSE 151 | # if ( MIN == MAX ) { 152 | # return(200) 153 | # } 154 | intervals <- seq(MIN, MAX, length.out=200) 155 | max(which(intervals <= v)) 156 | } 157 | -------------------------------------------------------------------------------- /R/GE.R: -------------------------------------------------------------------------------- 1 | #' GO Enrichment analysis function 2 | #' @importFrom dplyr filter_ 3 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 4 | #' @param GO_FILE GO annotation data 5 | #' @param OP use BP,CC or MF 6 | #' @param minSize minimal number of genes included in significant terms 7 | #' @param padj.method p value adjust method (default: BH) 8 | #' @param cutoff cutoff value for filtering significant terms (default: 0.05) 9 | #' @param maxSize maximum number of genes included in significant terms 10 | #' @param keepRich keep terms with rich factor value equal 1 or not (default: TRUE) 11 | #' @param filename output filename 12 | #' @param gene.cutoff the cut-off value for select DEGs (default: 0.01) 13 | #' @export 14 | #' @author Kai Guo 15 | GE<-function(df,GO_FILE,OP="BP",gene.cutoff=0.01,minSize=2,maxSize=500,keepRich=TRUE,filename=NULL,padj.method="BH",cutoff=0.05){ 16 | go2gene<-sf(GO_FILE) 17 | all_go<-.get_go_dat(ont = OP) 18 | go2gene<-go2gene[names(go2gene)%in%rownames(all_go)]; 19 | gene2go<-reverseList(go2gene) 20 | if(is.data.frame(df)){ 21 | IGE<-rownames(df)[df$padj=minSize) 44 | }else{ 45 | resultFis<-filter_(resultFis, ~Significant>=minSize|(~Annotated/~Significant)==1) 46 | } 47 | if(!is.null(filename)){ 48 | write.table(resultFis,file=paste(filename,OP,"res.txt",sep="_"),sep="\t",quote=F,row.names=F) 49 | } 50 | return(resultFis); 51 | } 52 | #' Display GO enrichment result 53 | #' @param resultFis: GO ennrichment analysis result data.frame 54 | #' @param top: Number of Terms you want to display 55 | #' @param filename: output filename 56 | #' @param pvalue.cutoff: the cut-off value for selecting Term 57 | #' @param padj.cutoff: the padj cut-off value for selecting Term 58 | #' @param usePadj use adjust pvalue or not 59 | #' @param low color used for small value 60 | #' @param high color used for large value 61 | #' @param order order bar or not 62 | #' @param horiz use horiz or not 63 | #' @param fontsize.x fontsize for x axis 64 | #' @param fontsize.y fontsize for y axis 65 | #' @param filename output filename 66 | #' @param width width for output file 67 | #' @param height height for output file 68 | #' @param angle angle for x ticks label 69 | #' @param horiz horizontal plot or not (default: FALSE) 70 | #' @export 71 | #' @author Kai Guo 72 | GE.plot<-function(resultFis,top=50,pvalue.cutoff=0.05,order=FALSE,horiz=FALSE, 73 | low="lightpink",high="red", 74 | font.x="bold",font.y="bold",fontsize.x=10,fontsize.y=10, 75 | fontsize.text=3,angle=75,padj.cutoff=NULL,usePadj=TRUE, 76 | filename=NULL,width=10,height=8){ 77 | require(ggplot2) 78 | if(!is.null(padj.cutoff)){ 79 | resultFis<-resultFis[resultFis$Padj=top){ 84 | resultFis<-resultFis[1:top,] 85 | } 86 | if(max(resultFis$Significant/(resultFis$Annotated+0.1))<=1){ 87 | yheight=max(resultFis$Significant/resultFis$Annotated)+0.1 88 | }else{ 89 | yheight=1 90 | } 91 | if(order==TRUE){ 92 | resultFis$rich<-as.numeric(resultFis$Significant)/as.numeric(resultFis$Annotated) 93 | resultFis$Term<-factor(resultFis$Term,levels=resultFis$Term[order(resultFis$rich)]) 94 | } 95 | if(usePadj==FALSE){ 96 | p<-ggplot(resultFis,aes(x=Term,y=round(as.numeric(Significant/Annotated),2)))+geom_bar(stat="identity",aes(fill=-log10(as.numeric(Pvalue)))) 97 | p<-p+scale_fill_gradient(low=low,high=high)+theme_light() 98 | if(horiz==TRUE){ 99 | p<-p+theme(axis.text.y=element_text(face=font.y,size=fontsize.y),axis.text.x=element_text(face=font.x,color="black",size=fontsize.x,angle=angle))+labs(fill="-log10(Pvalue)") 100 | p<-p+coord_flip() 101 | p<-p+geom_text(aes(label=Significant),hjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 102 | }else{ 103 | p<-p+theme(axis.text.y=element_text(face=font.y,size=fontsize.y),axis.text.x=element_text(face=font.x,color="black",size=fontsize.x,angle=angle,vjust=1,hjust=1))+labs(fill="-log10(Pvalue)") 104 | p<-p+geom_text(aes(label=Significant),vjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 105 | } 106 | print(p) 107 | }else{ 108 | p<-ggplot(resultFis,aes(x=Term,y=round(as.numeric(Significant/Annotated),2)))+geom_bar(stat="identity",aes(fill=-log10(as.numeric(Padj)))) 109 | p<-p+scale_fill_gradient2(low=low,high=high)+theme_light() 110 | if(horiz==TRUE){ 111 | p<-p+theme(axis.text.y=element_text(face=font.y,size=fontsize.y),axis.text.x=element_text(face=font.x,color="black",size=fontsize.x,angle=angle))+labs(fill="-log10(Padj)") 112 | p<-p+coord_flip() 113 | p<-p+geom_text(aes(label=Significant),hjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 114 | 115 | }else{ 116 | p<-p+theme(axis.text.y=element_text(face=font.y,size=fontsize.y),axis.text.x=element_text(face=font.x,color="black",size=fontsize.x,angle=angle,vjust=1,hjust=1))+labs(fill="-log10(Padj)") 117 | p<-p+geom_text(aes(label=Significant),vjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 118 | } 119 | print(p) 120 | } 121 | if(!is.null(filename)){ 122 | ggsave(p,file=paste(filename,OP,"enrich.pdf",sep="_"),width=width,height=height) 123 | } 124 | } 125 | 126 | -------------------------------------------------------------------------------- /R/biomart.R: -------------------------------------------------------------------------------- 1 | #' Show avaliable plant annotation based on ENSEMBLE 2 | #' @param host the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org 3 | #' @param species the species you want to search 4 | #' @param ann_type the type of function annotation you want get from ensemble 5 | #' @export 6 | #' @author Kai Guo 7 | showplant<-function(host="plants.ensembl.org"){ 8 | suppressMessages(require(biomaRt)) 9 | mart=useMart("plants_mart",host="plants.ensembl.org") 10 | res<-listDatasets(mart) 11 | colnames(res)[2]<-"species" 12 | res 13 | } 14 | #' Show avaliable annotation except plant based on ENSEMBLE 15 | #' @importFrom biomaRt useMart 16 | #' @importFrom biomaRt listDatasets 17 | #' @param host the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org 18 | #' @param species the sepcies you want to search 19 | #' @export 20 | #' @author Kai Guo 21 | showensemble<-function(host="uswest.ensembl.org"){ 22 | cat("You could choose different host to get high speed!\n") 23 | cat("Ensembl US West: uswest.ensembl.org\nEnsembl US East: useast.ensembl.org\nEnsembl Asia: asia.ensembl.org\n" ) 24 | mart=useMart("ENSEMBL_MART_ENSEMBL",host=host) 25 | res<-listDatasets(mart) 26 | colnames(res)[2]<-"species" 27 | res 28 | } 29 | #' make plant annotation data based on ENSEMBLE 30 | #' @importFrom biomaRt useMart 31 | #' @importFrom biomaRt useDataset 32 | #' @importFrom biomaRt getBM 33 | #' @param host the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org 34 | #' @param species the sepcies you want to search, you can use showplant to get the species name 35 | #' @param ann_type the type of function annotation(GO,KEGG,PFAM,InterPro) you want get from ensemble 36 | #' @export 37 | #' @author Kai Guo 38 | makeplantann<-function(species="Arabidopsis t",host="plants.ensembl.org",ann_type="GO"){ 39 | mart=useMart("plants_mart",host=host) 40 | dbinfo<-.getmartdb(species,mart) 41 | dbname=as.character(dbinfo$dbname) 42 | dataset<-useDataset(dbname,mart=mart) 43 | if(ann_type=="GO"){ 44 | res<-getBM(attributes = c("ensembl_gene_id","go_id","name_1006"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 45 | }else if(ann_type=="KEGG"){ 46 | res<-getBM(attributes = c("ensembl_gene_id","kegg_enzyme"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 47 | res[,2]<-sub('\\+.*','',res[,2]) 48 | #res$Annot<-kegg.db[res[,2],] 49 | #res<-res[,c(1,3,2)] 50 | }else if(ann_type=="PFAM"){ 51 | res<-getBM(attributes = c("ensembl_gene_id","pfam"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 52 | }else if(ann_type=="InterPro"){ 53 | res<-getBM(attributes = c("ensembl_gene_id","interpro"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 54 | }else if(ann_type=="Reactome"){ 55 | res<-getBM(attributes = c("ensembl_gene_id","plant_reactome_pathway"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 56 | }else{ 57 | stop("You must specify one type of annotation!\n") 58 | } 59 | res<-res[nchar(res[,2])>1,] 60 | if(ann_type=="KEGG"){ 61 | res$Annot<-kegg.db[res[,2],] 62 | # res<-res[,c(1,3,2)] 63 | } 64 | colnames(res)[ncol(res)]<-"Annot" 65 | rownames(res)<-NULL 66 | return(res) 67 | } 68 | #' make annotation data except plant based on ENSEMBLE 69 | #' @importFrom biomaRt useMart 70 | #' @importFrom biomaRt useDataset 71 | #' @importFrom biomaRt getBM 72 | #' @param host the ensemble API host,for plant you can use plants.ensembl.org and for human and other species you can use uswest.ensembl.org 73 | #' @param species the species you want to search, you can use showplant to get the species name 74 | #' @param ann_type the type of function annotation(GO,KEGG,PFAM,InterPro) you want get from ensemble 75 | #' @export 76 | #' @author Kai Guo 77 | makeesanno<-function(species="Human",host="uswest.ensembl.org",ann_type="GO"){ 78 | mart=useMart("ENSEMBL_MART_ENSEMBL",host=host) 79 | dbinfo<-.getmartdb(species,mart) 80 | dbname=as.character(dbinfo$dbname) 81 | dataset<-useDataset(dbname,mart=mart) 82 | if(ann_type=="GO"){ 83 | res<-getBM(attributes = c("ensembl_gene_id","go_id","name_1006"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 84 | }else if(ann_type=="KEGG"){ 85 | res<-getBM(attributes = c("ensembl_gene_id","kegg_enzyme"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 86 | res[,2]<-sub('\\+.*','',res[,2]) 87 | }else if(ann_type=="PFAM"){ 88 | res<-getBM(attributes = c("ensembl_gene_id","pfam"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 89 | }else if(ann_type=="InterPro"){ 90 | res<-getBM(attributes = c("ensembl_gene_id","interpro"),filters ="chromosome_name",values = as.vector(dbinfo$chr_info$name),dataset) 91 | }else{ 92 | stop("You must specify one type of annotation!\n") 93 | } 94 | res<-res[nchar(res[,2])>1,] 95 | if(ann_type=="KEGG"){ 96 | res$Annot<-kegg.db[res[,2],] 97 | # res<-res[,c(1,3,2)] 98 | } 99 | colnames(res)[ncol(res)]<-"Annot" 100 | rownames(res)<-NULL 101 | return(res) 102 | } 103 | ##' @importFrom dplyr select_ 104 | ##' @importFrom dplyr collect 105 | ##' @importFrom dplyr pull 106 | ##' @importFrom magrittr %>% 107 | ##' @importFrom biomaRt listDatasets 108 | ##' @importFrom jsonlite fromJSON 109 | .getmartdb<-function(spe,mart){ 110 | lhs<- listDatasets(mart) 111 | spe=simpleCap(spe); 112 | spe<-gsub(' ','\\\\s',spe) 113 | sel<-grepl(spe,lhs$description,ignore.case = T) 114 | tmp<-lhs[sel,] 115 | dataset<-tmp%>%select_(~dataset)%>%collect%>%pull(1) 116 | if((length(dataset)==0)|(length(dataset)>1)){ 117 | stop("Maybe you need first check the avaliable database by using showensemble() or showplant()\n") 118 | } 119 | chr<-tmp%>%select_(~description)%>%collect%>%pull(1) 120 | organism<-gsub(' ','_',sub(' genes.*','',chr)) 121 | if(organism=="Oryza_sativa_Japonica"){ 122 | organism="Oryza_sativa" 123 | } 124 | if(mart@biomart=="plants_mart"){ 125 | pre_site="http://rest.ensembl.org/info/assembly/" 126 | }else{ 127 | pre_site="http://rest.ensembl.org/info/assembly/" 128 | } 129 | tryCatch({ 130 | chr_d <- 131 | fromJSON( 132 | paste0( 133 | pre_site, 134 | organism, 135 | "?content-type=application/json" 136 | ) 137 | ) 138 | }, error = function(e) 139 | stop( 140 | "The API 'http://rest.ensemblgenomes.org' does not seem to work properly. Are you connected to the internet? Is the homepage 'http://rest.ensemblgenomes.org' currently available?", call. = FALSE 141 | )) 142 | chr_info<-chr_d$top_level_region 143 | chr_version<-chr_d$assembly_name 144 | chr_assembly_date<-chr_d$assembly_date 145 | rhs<-list(dbname=dataset,chr_info=chr_info,chr_version=chr_version,chr_assembly_date=chr_assembly_date) 146 | return(rhs) 147 | } 148 | 149 | -------------------------------------------------------------------------------- /R/get_org.R: -------------------------------------------------------------------------------- 1 | #' make GO annotation data function 2 | #' @importFrom AnnotationDbi keys 3 | #' @importFrom AnnotationDbi select 4 | #' @param species you can check the support species by using showData() 5 | #' @param keytype the gene ID type 6 | #' @param OP BP,CC,MF default use all 7 | #' @importFrom dplyr distinct_ 8 | #' @export 9 | #' @author Kai Guo 10 | makeGOdata<-function(species="human",keytype="ENTREZID",OP=NULL){ 11 | dbname<-.getdbname(species); 12 | if (!require(dbname,character.only=TRUE)){ 13 | if(!require("BiocManager",character.only=TRUE)){ 14 | install.packages("BiocManager") 15 | }else{ 16 | BiocManager::install(dbname) 17 | } 18 | }else{ 19 | suppressMessages(require(dbname,character.only = T,quietly = T)) 20 | } 21 | dbname<-eval(parse(text=dbname)) 22 | GO_FILE<-select(dbname,keys=keys(dbname,keytype=keytype),keytype=keytype,columns=c("GOALL","ONTOLOGYALL")) 23 | colnames(GO_FILE)[1]<-"GeneID" 24 | GO_FILE<-distinct_(GO_FILE,~GeneID, ~GOALL, ~ONTOLOGYALL) 25 | annot <- getann("GO") 26 | GO_FILE$Annot <- annot[GO_FILE[,2],"annotation"] 27 | if(!is.null(OP)){ 28 | GO_FILE<-GO_FILE[GO_FILE$ONTOLOGYALL==OP,] 29 | } 30 | return(GO_FILE) 31 | } 32 | #' make KEGG annotation data function 33 | #' @importFrom AnnotationDbi keys 34 | #' @importFrom AnnotationDbi select 35 | #' @importFrom KEGGREST keggLink 36 | #' @param species you can check the support species by using showData() 37 | #' @param keytype the gene ID type 38 | #' @export 39 | #' @author Kai Guo 40 | makeKOdata<-function(species="human",keytype="ENTREZID",builtin=TRUE){ 41 | dbname<-.getdbname(species=species); 42 | if(builtin==TRUE){ 43 | # suppressMessages(require(AnnotationDbi)) 44 | # sel<-AnnotationDbi::select 45 | if (!require(dbname,character.only=TRUE)){ 46 | if(!require("BiocManager",character.only=TRUE)){ 47 | install.packages("BiocManager") 48 | }else{ 49 | BiocManager::install(dbname) 50 | } 51 | }else{ 52 | suppressMessages(require(dbname,character.only = T,quietly = T)) 53 | } 54 | dbname<-eval(parse(text=dbname)) 55 | KO_FILE=select(dbname,keys=keys(dbname,keytype=keytype),keytype=keytype,columns="PATH") 56 | KO_FILE<-na.omit(KO_FILE) 57 | }else{ 58 | spe=.getspeices(species) 59 | tmp<-keggLink("pathway",spe) 60 | tmp<-substr(tmp,9,13) 61 | names(tmp)<-sub('.*:','',names(tmp)) 62 | tmp<-vec_to_df(tmp,name=c(keytype,"PATH")) 63 | if(keytype!="ENTREZID"){ 64 | tmp[,1]<-idconvert(species,keys=tmp[,1],fkeytype = "ENTREZID",tkeytype = keytype) 65 | tmp<-na.omit(tmp) 66 | } 67 | KO_FILE=tmp 68 | } 69 | annot<-getann("KEGG") 70 | KO_FILE[,1]<-as.vector(KO_FILE[,1]) 71 | KO_FILE[,2]<-as.vector(KO_FILE[,2]) 72 | KO_FILE$Annot<-annot[KO_FILE[,2],"annotation"] 73 | return(KO_FILE) 74 | } 75 | #' Convert ID between ENTREZID to SYMBOL or other type ID based on bioconductor annotation package 76 | #' @param species you can check the support species by using showData() 77 | #' @param keys input genes 78 | #' @param fkeytype the gene type you want to convert 79 | #' @param tkeytype the gene type you want to get 80 | #' @export 81 | #' @author Kai Guo 82 | idconvert<-function(species,keys,fkeytype,tkeytype){ 83 | dbname<-.getdbname(species); 84 | suppressMessages(require(dbname,character.only = T)) 85 | dbname<-eval(parse(text=dbname)) 86 | mapIds(dbname,keys=as.vector(keys), 87 | column=tkeytype, 88 | keytype=fkeytype, 89 | multiVals="first") 90 | } 91 | .getdbname<-function(species="human"){ 92 | dbname=.getdb(species=species); 93 | if(is.null(dbname)){ 94 | cat("You must check if your request database is avaliable by using showData, 95 | If not you could make your database by using makeOwnGO and makeOwnKO 96 | and give a user defined database\n") 97 | stop("databse error!") 98 | } 99 | return(dbname) 100 | } 101 | .getdb<-function(species=species){ 102 | species=tryCatch(match.arg(species,c("anopheles","arabidopsis","bovine","celegans","canine","fly","zebrafish", 103 | "ecoli","ecsakai","chicken","human","mouse","rhesus","malaria","chipm","rat", 104 | "toxoplasma","streptomyces","pig","yeast","xenopus","warm")), 105 | error=function(cond){return("unsupported")}) 106 | if (species == "anopheles") { 107 | dbname <- "org.Ag.eg.db" 108 | } else if (species == "arabidopsis") { 109 | dbname <- "org.At.tair.db" 110 | } else if (species == "bovine") { 111 | dbname <- "org.Bt.eg.db" 112 | } else if (species == "canine") { 113 | dbname <- "org.Cf.eg.db" 114 | } else if (species == "worm" || species == "celegans") { 115 | dbname <- "org.Ce.eg.db" 116 | } else if (species == "chicken") { 117 | dbname <- "org.Gg.eg.db" 118 | } else if (species == "ecolik12") { 119 | dbname <- "org.EcK12.eg.db" 120 | } else if (species == "ecsakai") { 121 | dbname <- "org.EcSakai.eg.db" 122 | } else if (species == "fly") { 123 | dbname <- "org.Dm.eg.db" 124 | } else if (species == "human") { 125 | dbname <- "org.Hs.eg.db" 126 | } else if (species == "malaria") { 127 | dbname <- "org.Pf.plasmo.db" 128 | } else if (species == "chipm") { 129 | dbname <- "org.Pt.eg.db" 130 | }else if (species == "mouse") { 131 | dbname <- "org.Mm.eg.db" 132 | } else if (species == "pig") { 133 | dbname <- "org.Ss.eg.db" 134 | } else if (species == "rat") { 135 | dbname <- "org.Rn.eg.db" 136 | } else if (species == "rhesus") { 137 | dbname <- "org.Mmu.eg.db" 138 | } else if (species == "xenopus") { 139 | dbname <- "org.Xl.eg.db" 140 | } else if (species == "yeast") { 141 | dbname <- "org.Sc.sgd.db" 142 | } else if (species == "streptomyces") { 143 | dbname <- "org.Sco.eg.db" 144 | } else if (species == "zebrafish") { 145 | dbname <- "org.Dr.eg.db" 146 | } else if (species == "toxoplasma"){ 147 | dbname<- "org.Tgondii.eg.db" 148 | } else { 149 | dbname <- NULL 150 | } 151 | return(dbname) 152 | } 153 | .getspeices<-function(species="human"){ 154 | species=tryCatch(match.arg(species,c("anopheles","arabidopsis","bovine","celegans","canine","fly","zebrafish", 155 | "ecoli","ecsakai","chicken","human","mouse","rhesus","malaria","chipm","rat", 156 | "toxoplasma","sco","pig","yeast","xenopus","warm")), 157 | error=function(cond){return("unsupported")}) 158 | if (species == "anopheles") { 159 | species <- "aga" 160 | } else if (species == "arabidopsis") { 161 | species <- "ath" 162 | } else if (species == "bovine") { 163 | species <- "bta" 164 | } else if (species == "canine") { 165 | species <- "cfa" 166 | } else if (species == "chicken") { 167 | species <- "gga" 168 | } else if (species == "chipm") { 169 | species <- "ptr" 170 | } else if (species == "ecolik12") { 171 | species <- "eco" 172 | } else if (species == "ecsakai") { 173 | species <- "ecs" 174 | } else if (species == "fly") { 175 | species <- "dme" 176 | } else if (species == "human") { 177 | species <- "hsa" 178 | } else if (species == "malaria") { 179 | species <- "pfa" 180 | } else if (species == "mouse") { 181 | species <- "mmu" 182 | } else if (species == "pig") { 183 | species <- "ssc" 184 | } else if (species == "rat") { 185 | species <- "rno" 186 | } else if (species == "rhesus") { 187 | species <- "mcc" 188 | } else if (species == "worm" || species == "celegans") { 189 | species <- "cel" 190 | } else if (species == "xenopus") { 191 | species <- "xla" 192 | } else if (species == "yeast") { 193 | species <- "sce" 194 | } else if (species =="streptomyces"){ 195 | species <- "sco" 196 | } else if (species == "zebrafish") { 197 | species <- "dre" 198 | } else { 199 | species <- NULL 200 | } 201 | return(species) 202 | } 203 | #' show avaliable data based on bioconductor annotation package 204 | #' @export 205 | #' @author Kai Guo 206 | showData<-function(){ 207 | species=c("anopheles","arabidopsis","bovine","celegans","canine","fly","zebrafish", 208 | "ecoli","ecsakai","chicken","human","mouse","rhesus","malaria","chipm","rat", 209 | "toxoplasma","sco","pig","yeast","xenopus") 210 | dbname=c("org.Ag.eg.db","org.At.tair.db","org.Bt.eg.db","org.Ce.eg.db","org.Cf.eg.db","org.Dm.eg.db", 211 | "org.Dr.eg.db","org.EcK12.eg.db","org.EcSakai.eg.db","org.Gg.eg.db","org.Hs.eg.db","org.Mm.eg.db", 212 | "org.Mmu.eg.db","org.Pf.plasmo.db","org.Pt.eg.db","org.Rn.eg.db","org.Sc.sgd.db","org.Sco.eg.db", 213 | "org.Ss.eg.db","org.Tgondii.eg.db","org.Xl.eg.db") 214 | dbdata<-data.frame(dbname=dbname,species=species) 215 | dbdata 216 | } 217 | 218 | vec_to_df<-function(x,name){ 219 | dd<-data.frame(names(x),x) 220 | colnames(dd)<-name 221 | return(dd) 222 | } 223 | -------------------------------------------------------------------------------- /R/enrich.R: -------------------------------------------------------------------------------- 1 | #' Enrichment analysis for any type of annotation data 2 | #' @importFrom dplyr filter_ 3 | #' @importFrom magrittr %>% 4 | #' @param df DGE files (DESeq2 result files) or vector contains gene names 5 | #' @param annot annotation file for all genes 6 | #' @param annot.info Term of all annotation 7 | #' @param filename output filename 8 | #' @param gene.cutoff DGE singificant cutoff value 9 | #' @param padj.method p value adjust method 10 | #' @param keepRich keep terms with high rich factor 11 | #' @export 12 | #' @author Kai Guo 13 | enrich<-function(df,annot,annot.info=NULL,minSize=1,maxSize=500,keepRich=TRUE,filename=NULL,gene.cutoff=0.01,padj.method="BH"){ 14 | suppressMessages(require(tidyr)) 15 | ao2gene<-sf(annot) 16 | ao2gene_num<-name_table(ao2gene) 17 | gene2ao<-sf(annot[,c(2,1)]) 18 | if(is.data.frame(df)){ 19 | IGE<-rownames(df)[df$padj%filter_(~Significant<=maxSize) 48 | if(keepRich==FALSE){ 49 | resultFis<-resultFis%>%filter_(~Significant>=minSize) 50 | }else{ 51 | resultFis<-resultFis%>%filter_(~Significant>=minSize|(~Annotated/~Significant)==1) 52 | } 53 | if(!is.null(filename)){ 54 | write.table(resultFis,file=paste(filename,".txt",sep=""),sep="\t",quote=F,row.names=F) 55 | } 56 | return(resultFis) 57 | } 58 | #' Display enrichment result By using barchart 59 | #' @param resultFis Ennrichment analysis result data.frame 60 | #' @param top Number of Terms you want to display 61 | #' @param pvalue.cutoff the cut-off value for selecting Term 62 | #' @param padj.cutoff the padj cut-off value for selecting Term 63 | #' @param low color used for small value 64 | #' @param high color used for large value 65 | #' @param fontsize.x fontsize for x axis 66 | #' @param fontsize.y fontsize for y axis 67 | #' @param horiz show horiz or not (default: FALSE) 68 | #' @param filename output filename 69 | #' @param width width for output file 70 | #' @param height height for output file 71 | #' @export 72 | #' @author Kai Guo 73 | enrichbar<-function(resultFis,top=50,pvalue.cutoff=0.05,padj.cutoff=NULL, 74 | low="lightpink",high="red", 75 | order=FALSE,horiz=FALSE,fontsize.x=10,fontsize.y=10, 76 | fontsize.text=3,angle=75,usePadj=TRUE,filename=NULL, 77 | width=10,height=8){ 78 | require(ggplot2) 79 | if(!is.null(padj.cutoff)){ 80 | resultFis<-resultFis[resultFis$Padj=top){ 85 | resultFis<-resultFis[1:top,] 86 | } 87 | if(max(resultFis$Significant/(resultFis$Annotated+0.1))<=1){ 88 | yheight=max(resultFis$Significant/resultFis$Annotated)+0.1 89 | }else{ 90 | yheight=1 91 | } 92 | if(order==TRUE){ 93 | resultFis$rich<-as.numeric(resultFis$Significant)/as.numeric(resultFis$Annotated) 94 | resultFis$Term<-factor(resultFis$Term,levels=resultFis$Term[order(resultFis$rich)]) 95 | } 96 | if(usePadj==FALSE){ 97 | p<-ggplot(resultFis,aes(x=Term,y=round(as.numeric(Significant/Annotated),2)))+geom_bar(stat="identity",aes(fill=-log10(as.numeric(Pvalue)))) 98 | p<-p+scale_fill_gradient(low=low,high=high)+theme_light() 99 | if(horiz==TRUE){ 100 | p<-p+theme(axis.text.y=element_text(face="bold",size=fontsize.y),axis.text.x=element_text(face="bold",color="black",size=fontsize.x,angle=angle))+labs(fill="-log10(Pvalue)") 101 | p<-p+coord_flip() 102 | p<-p+geom_text(aes(label=Significant),hjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 103 | }else{ 104 | p<-p+theme(axis.text.y=element_text(face="bold",size=fontsize.y),axis.text.x=element_text(face="bold",color="black",size=fontsize.x,angle=angle,vjust=1,hjust=1))+labs(fill="-log10(Pvalue)") 105 | p<-p+geom_text(aes(label=Significant),vjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 106 | } 107 | print(p) 108 | }else{ 109 | p<-ggplot(resultFis,aes(x=Term,y=round(as.numeric(Significant/Annotated),2)))+geom_bar(stat="identity",aes(fill=-log10(as.numeric(Padj)))) 110 | p<-p+scale_fill_gradient2(low=low,high=high)+theme_light() 111 | if(horiz==TRUE){ 112 | p<-p+theme(axis.text.y=element_text(face="bold",size=fontsize.y),axis.text.x=element_text(face="bold",color="black",size=fontsize.x,angle=angle))+labs(fill="-log10(Pvalue)") 113 | p<-p+coord_flip() 114 | p<-p+geom_text(aes(label=Significant),hjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 115 | }else{ 116 | p<-p+theme(axis.text.y=element_text(face="bold",size=fontsize.y),axis.text.x=element_text(face="bold",color="black",size=fontsize.x,angle=angle,vjust=1,hjust=1))+labs(fill="-log10(Pvalue)") 117 | p<-p+geom_text(aes(label=Significant),vjust=-0.3,size=fontsize.text)+xlab("Annotation")+ylab("Rich Factor")+ylim(0,yheight) 118 | } 119 | print(p) 120 | } 121 | if(!is.null(filename)){ 122 | ggsave(p,file=paste(filename,"barplot.pdf",sep="_"),width=width,height=height) 123 | } 124 | } 125 | #' Display enrichment result By using dotchart 126 | #' @param resultFis: Ennrichment analysis result data.frame 127 | #' @param top: Number of Terms you want to display 128 | #' @param pvalue.cutoff: the cut-off value for selecting Term 129 | #' @param padj.cutoff: the padj cut-off value for selecting Term 130 | #' @param low color used for small value 131 | #' @param high color used for large value 132 | #' @param alpha alpha-transparency scales 133 | #' @param fontsize.x fontsize for x axis 134 | #' @param fontsize.y fontsize for y axis 135 | #' @param filename output filename 136 | #' @param width width for output file 137 | #' @param height height for output file 138 | #' @export 139 | #' @author Kai Guo 140 | enrichdot<-function(resultFis,top=50,pvalue.cutoff=0.05,order=FALSE, 141 | low="lightpink",high="red",alpha=0.7, 142 | padj.cutoff=NULL,fontsize.x=10,fontsize.y=10, 143 | usePadj=TRUE,filename=NULL,width=10,height=8){ 144 | library(ggplot2) 145 | if(!is.null(padj.cutoff)){ 146 | resultFis<-resultFis[resultFis$Padj=top){ 151 | dd<-resultFis[1:top,] 152 | }else{ 153 | dd<-resultFis 154 | } 155 | if(nrow(dd)>=1){ 156 | dd$rich<-dd$Significant/dd$Annotated 157 | if(order==TRUE){ 158 | dd$Term<-factor(dd$Term,levels=dd$Term[order(dd$rich)]) 159 | } 160 | if(usePadj==FALSE){ 161 | p<-ggplot(dd,aes(x=rich,y=Term))+geom_point(aes(size=Significant,color=-log10(Pvalue)),alpha=alpha)+ 162 | theme(axis.text.y=element_text(face="bold",size=fontsize.y),axis.text.x=element_text(face="bold",color="black",size=fontsize.x))+ 163 | scale_colour_gradient(low=low,high=high)+theme_minimal()+ylab("Pathway name")+ 164 | xlab("Rich factor")+labs(size="Gene number")+guides(color=guide_colourbar(order = 1),size=guide_legend(order = 2)) 165 | print(p) 166 | }else{ 167 | p<-ggplot(dd,aes(x=rich,y=Term))+geom_point(aes(size=Significant,color=-log10(Padj)),alpha=alpha)+ 168 | theme(axis.text.y=element_text(face="bold",size=fontsize.y),axis.text.x=element_text(face="bold",color="black",size=fontsize.x))+ 169 | scale_colour_gradient(low=low,high=high)+theme_minimal()+ylab("Pathway name")+ 170 | xlab("Rich factor")+labs(size="Gene number")+guides(color=guide_colourbar(order = 1),size=guide_legend(order = 2)) 171 | print(p) 172 | } 173 | if(!is.null(filename)){ 174 | ggsave(p,file=paste(filename,"dotplot.pdf",sep="_"),width=width,height=height) 175 | } 176 | }else{ 177 | cat("No Pathway enrichment results were found!\n") 178 | } 179 | 180 | } 181 | --------------------------------------------------------------------------------