RNA-seq下游数据分析-ballgown到报告。 以Rscript为主,对接PGx RNA-seq choppy现有pipeline,到生成RNA-seq分析报告所需的rds和csv文件。
r
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

179 行
6.2KB

  1. #!/usr/bin/env Rscript
  2. ###Copyright 2019 Ying Yu from Fudan-PGx group
  3. # example:
  4. # Rscript RNAseq_6_enrichfunc.R -i rnaseq_degs_acrossgroups.csv
  5. suppressPackageStartupMessages(library("optparse"))
  6. suppressPackageStartupMessages(library("clusterProfiler"))
  7. # specify our desired options in a list
  8. # by default OptionParser will add an help option equivalent to
  9. # make_option(c("-h", "--help"), action="store_true", default=FALSE,
  10. # help="Show this help message and exit")
  11. # input input list , rds, from * to *
  12. option_list <- list(
  13. make_option(c("-o", "--out_dir"), type="character",default="./",
  14. help="The output directory [default ./]"),
  15. make_option(c("-i", "--input"),type="character", default=NULL,
  16. help="The input DEG list in csv format. The first column: gene; second column: group. Required! "),
  17. make_option(c("-e", "--type_gene_id"),type="character", default="EnsemblGID",
  18. help="The type of gene symbol. Could be either of EnsemblGID/EntrezID/GeneSymbol [default: EnsemblGID]"),
  19. make_option(c("-f", "--pvalueCutoff"), type="double",default=0.05,metavar="number",
  20. help="Cutoff value of p value. [default: 0.05]"),
  21. make_option(c("-m", "--pAdjustMethod"), type="character",default="BH",
  22. help="Method of adjust p value. One of \"holm\", \"hochberg\", \"hommel\", \"bonferroni\", \"BH\", \"BY\", \"fdr\", \"none\". [default: BH]"),
  23. make_option(c("-q", "--qvalueCutoff"), type="double",default=0.2,metavar="number",
  24. help="Cutoff value of q value. [default: 0.2]"),
  25. make_option(c("-p", "--project_code"), type="character",default="rnaseq",
  26. help="Project code, which is used as prefix of output file. [default: rnaseq]")
  27. )
  28. # get command line options, if help option encountered print help and exit,
  29. # otherwise if options not found on command line then set defaults,
  30. opt <- parse_args(OptionParser(option_list=option_list))
  31. if (is.null(opt$input)){
  32. print_help(opt_parser)
  33. stop("At least one argument must be supplied (input file).", call.=FALSE)
  34. }
  35. message("Need to connected to the Internet.")
  36. ##import file
  37. out_dir<-paste(gsub("/$","",opt$out_dir),"/",sep="")
  38. gene<-read.csv(opt$input,header=T,stringsAsFactors=F)
  39. ##########################
  40. #########ID convert#######
  41. ##########################
  42. message("Begin ID conversion.")
  43. if(length(grep("ID_convert_table.rds",dir()))>0){
  44. idconvert<-readRDS("ID_convert_table.rds")
  45. }else{
  46. stop("Cannot find ID_convert_table.rds in the working folder. Exit!", call.=FALSE)
  47. }
  48. if(opt$type_gene_id=="EnsemblGID"){
  49. gene$EntrezID<-idconvert$EntrezID[match(gene[,1],idconvert$EnsemblID)]
  50. if(length(which(is.na(gene$EntrezID)))==nrow(gene)){
  51. stop("Cannot convert Ensembl gene ID to Entrez gene ID. Exit!", call.=FALSE)
  52. }
  53. }
  54. if(opt$type_gene_id=="GeneSymbol"){
  55. gene$EntrezID<-idconvert$EntrezID[match(gene[,1],idconvert$GeneSymbol)]
  56. if(length(which(is.na(gene$EntrezID)))==nrow(gene)){
  57. stop("Cannot convert GeneSymbol to Entrez gene ID. Exit!", call.=FALSE)
  58. }
  59. }
  60. if(opt$type_gene_id=="EntrezID"){
  61. gene$EntrezID<-gene[,1]
  62. }
  63. message("Finish ID conversion.")
  64. ##########################
  65. #########Enrich GO#######
  66. ##########################
  67. groupn<-unique(gene[,2])
  68. if(length(groupn)==0){
  69. message("Warning: no group infomation. Function enrichment will be conducted as one group.")
  70. }else{
  71. message(paste("A number of ", length(groupn)," group(s) is detected. Function enrichment will be conducted in ",length(groupn), " group(s).",sep=""))
  72. }
  73. ####
  74. egoall<-c()
  75. ekeggall<-c()
  76. if(length(groupn)==0){
  77. g1<-gene$EntrezID
  78. g1<-g1[!g1==""]
  79. #conduct enrichment
  80. message("Contucting enrichment analysis on GO terms...")
  81. ego<-data.frame(enrichGO(g1, 'org.Hs.eg.db', ont = 'ALL', pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  82. message("Contucting enrichment analysis on KEGG pathways...")
  83. ekg<- data.frame( enrichKEGG(g1, organism = "hsa", keyType = "kegg", pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  84. #modify output
  85. if(!nrow(ego)==0){
  86. ego1<-cbind(groupn[i],ego)
  87. colnames(ego1)[1]<-c("versus")
  88. egoall<-rbind(egoall,ego1)
  89. }
  90. if(!nrow(ekg)==0){
  91. ekg1<-cbind(groupn[i],ekg)
  92. colnames(ekg1)[1]<-c("versus")
  93. ekeggall<-rbind(ekeggall,ekg1)
  94. }
  95. }else{
  96. for (i in 1:length(groupn)){
  97. message(paste("Group ", groupn[i],sep=""))
  98. g1<-gene$EntrezID[gene[,2]==groupn[i]]
  99. g1<-g1[!g1==""]
  100. #conduct enrichment
  101. message("Contucting enrichment analysis on GO terms...")
  102. ego<-data.frame(enrichGO(g1, 'org.Hs.eg.db', ont = 'ALL', pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  103. message("Contucting enrichment analysis on KEGG pathways...")
  104. ekg<- data.frame( enrichKEGG(g1, organism = "hsa", keyType = "kegg", pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  105. if(!nrow(ego)==0){
  106. ego1<-cbind(groupn[i],ego)
  107. colnames(ego1)[1]<-c("versus")
  108. egoall<-rbind(egoall,ego1)
  109. message(paste(nrow(ego),"significant GO term(s) is(are) identified."))
  110. }else{
  111. message("No significant GO term is identified.")
  112. }
  113. if(!nrow(ekg)==0){
  114. ekg1<-cbind(groupn[i],ekg)
  115. colnames(ekg1)[1]<-c("versus")
  116. ekeggall<-rbind(ekeggall,ekg1)
  117. message(paste(nrow(ekg),"significant KEGG pathway(s) is(are) identified."))
  118. }else{
  119. message("No significant KEGG pathway is identified.")
  120. }
  121. }
  122. }
  123. message("Wrinting output...")
  124. #write output
  125. if(nrow(egoall)==0){
  126. message("No significant GO term is identified across all tested groups.")
  127. }else{
  128. message(paste(nrow(egoall),"significant GO term(s) is(are) identified across all tested groups."))
  129. rownames(egoall)<-c(1:nrow(egoall))
  130. egoall$pvalue<-signif(egoall$pvalue,4)
  131. egoall$p.adjust<-signif(egoall$p.adjust,4)
  132. egoall$qvalue<-signif(egoall$qvalue,4)
  133. write.csv(egoall,paste(out_dir,opt$project_code,"_GOenrich.csv",sep=""))
  134. }
  135. if(nrow(ekeggall)==0){
  136. message("No significant KEGG pathway is identified across all tested groups.")
  137. }else{
  138. message(paste(nrow(ekeggall),"significant KEGG pathway(s) is(are) identified across all tested groups."))
  139. rownames(ekeggall)<-c(1:nrow(ekeggall))
  140. ekeggall$pvalue<-signif(ekeggall$pvalue,4)
  141. ekeggall$p.adjust<-signif(ekeggall$p.adjust,4)
  142. ekeggall$qvalue<-signif(ekeggall$qvalue,4)
  143. write.csv(ekeggall,paste(out_dir,opt$project_code,"_KEGGenrich.csv",sep=""))
  144. }
  145. ########