RNA-seq下游数据分析-ballgown到报告。 以Rscript为主,对接PGx RNA-seq choppy现有pipeline,到生成RNA-seq分析报告所需的rds和csv文件。
r
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

159 lines
5.5KB

  1. #!/usr/bin/env Rscript
  2. ###Copyright 2019 Ying Yu from Fudan-PGx group
  3. # example:
  4. # Rscript RNAseq_6_enrichfunc.R -o /home/yuying/rnaseqreport_test -i ballgown_geneexp_log2fpkm_floor0p01_c3r58395_2019-04-29.txt -g group1.txt -p organoid
  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. ##import file
  36. out_dir<-paste(gsub("/$","",opt$out_dir),"/",sep="")
  37. gene<-read.csv(opt$input,header=T,stringsAsFactors=F)
  38. ##########################
  39. #########ID convert#######
  40. ##########################
  41. if(length(grep("ID_convert_table.rds",dir()))>0){
  42. idconvert<-readRDS("ID_convert_table.rds")
  43. }else{
  44. stop("Cannot find ID_convert_table.rds in the working folder. Exit!", call.=FALSE)
  45. }
  46. if(opt$type_gene_id=="EnsemblGID"){
  47. gene$EntrezID<-idconvert$EntrezID[match(gene[,1],idconvert$EnsemblID)]
  48. if(length(which(is.na(gene$EntrezID)))==nrow(gene)){
  49. stop("Cannot convert Ensembl gene ID to Entrez gene ID. Exit!", call.=FALSE)
  50. }
  51. }
  52. if(opt$type_gene_id=="GeneSymbol"){
  53. gene$EntrezID<-idconvert$EntrezID[match(gene[,1],idconvert$GeneSymbol)]
  54. if(length(which(is.na(gene$EntrezID)))==nrow(gene)){
  55. stop("Cannot convert GeneSymbol to Entrez gene ID. Exit!", call.=FALSE)
  56. }
  57. }
  58. if(opt$type_gene_id=="EntrezID"){
  59. gene$EntrezID<-gene[,1]
  60. }
  61. ##########################
  62. #########Enrich GO#######
  63. ##########################
  64. groupn<-unique(gene[,2])
  65. if(length(groupn)==0){
  66. message("Warning: no group infomation. Function enrichment will be conducted as one group.")
  67. }else{
  68. message(paste("A number of ", length(groupn)," group(s) is detected. Function enrichment will be conducted in ",length(groupn), " group(s).",sep=""))
  69. }
  70. ####
  71. egoall<-c()
  72. ekeggall<-c()
  73. if(length(groupn)==0){
  74. g1<-gene$EntrezID
  75. g1<-g1[!g1==""]
  76. #conduct enrichment
  77. ego<-data.frame(enrichGO(g1, 'org.Hs.eg.db', ont = 'ALL', pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  78. ekg<- data.frame( enrichKEGG(g1, organism = "hsa", keyType = "kegg", pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  79. #modify output
  80. if(!nrow(ego)==0){
  81. ego1<-cbind(groupn[i],ego)
  82. colnames(ego1)[1]<-c("versus")
  83. egoall<-rbind(egoall,ego1)
  84. }
  85. if(!nrow(ekg)==0){
  86. ekg1<-cbind(groupn[i],ekg)
  87. colnames(ekg1)[1]<-c("versus")
  88. ekeggall<-rbind(ekeggall,ekg1)
  89. }
  90. }else{
  91. for (i in 1:length(groupn)){
  92. g1<-gene$EntrezID[gene[,2]==groupn[i]]
  93. g1<-g1[!g1==""]
  94. #conduct enrichment
  95. ego<-data.frame(enrichGO(g1, 'org.Hs.eg.db', ont = 'ALL', pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  96. ekg<- data.frame( enrichKEGG(g1, organism = "hsa", keyType = "kegg", pvalueCutoff = opt$pvalueCutoff, pAdjustMethod = opt$pAdjustMethod, qvalueCutoff = opt$qvalueCutoff))
  97. if(!nrow(ego)==0){
  98. ego1<-cbind(groupn[i],ego)
  99. colnames(ego1)[1]<-c("versus")
  100. egoall<-rbind(egoall,ego1)
  101. }
  102. if(!nrow(ekg)==0){
  103. ekg1<-cbind(groupn[i],ekg)
  104. colnames(ekg1)[1]<-c("versus")
  105. ekeggall<-rbind(ekeggall,ekg1)
  106. }
  107. }
  108. }
  109. #write output
  110. if(nrow(egoall)==0){
  111. message("No significant GO term is identified.")
  112. }else{
  113. message(paste(nrow(egoall),"significant GO term(s) is(are) identified."))
  114. rownames(egoall)<-c(1:nrow(egoall))
  115. egoall$pvalue<-signif(egoall$pvalue,4)
  116. egoall$p.adjust<-signif(egoall$p.adjust,4)
  117. egoall$qvalue<-signif(egoall$qvalue,4)
  118. write.csv(egoall,paste(out_dir,opt$project_code,"_GOenrich.csv",sep=""))
  119. }
  120. if(nrow(ekeggall)==0){
  121. message("No significant KEGG pathway is identified.")
  122. }else{
  123. message(paste(nrow(ekeggall),"significant KEGG pathway(s) is(are) identified."))
  124. rownames(ekeggall)<-c(1:nrow(ekeggall))
  125. ekeggall$pvalue<-signif(ekeggall$pvalue,4)
  126. ekeggall$p.adjust<-signif(ekeggall$p.adjust,4)
  127. ekeggall$qvalue<-signif(ekeggall$qvalue,4)
  128. write.csv(ekeggall,paste(out_dir,opt$project_code,"_KEGGenrich.csv",sep=""))
  129. }
  130. ########