RNA-seq下游数据分析-ballgown到报告。 以Rscript为主,对接PGx RNA-seq choppy现有pipeline,到生成RNA-seq分析报告所需的rds和csv文件。
r
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 line
6.4KB

  1. #!/usr/bin/env Rscript
  2. ###Copyright 2019 Ying Yu from Fudan-PGx group
  3. # example:
  4. # Rscript RNAseq_5_pwGSEA.R -o /home/yuying/rnaseqreport_test -i example_geneexp_log2fpkm_floor0p01_c13r58395_2019-04-30.txt -g group13_2.txt
  5. suppressPackageStartupMessages(library("optparse"))
  6. suppressPackageStartupMessages(library("fgsea"))
  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 expression files. 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("-g", "--sample_group"),type="character", default=NULL,
  20. help="File for sample group infomation.The input file containing sample name and group infomation. note colname must be like: sample group1 group2... Required! "),
  21. make_option(c("-q", "--padjvalueCutoff"), type="double",default=0.2,metavar="number",
  22. help="Cutoff value of adjusted p value. [default: 0.2]"),
  23. make_option(c("-p", "--project_code"), type="character",default="rnaseq",
  24. help="Project code, which is used as prefix of output file. [default: rnaseq]")
  25. )
  26. # get command line options, if help option encountered print help and exit,
  27. # otherwise if options not found on command line then set defaults,
  28. opt <- parse_args(OptionParser(option_list=option_list))
  29. if (is.null(opt$input)){
  30. print_help(opt_parser)
  31. stop("At least one argument must be supplied (input file).", call.=FALSE)
  32. }
  33. if (is.null(opt$sample_group)){
  34. stop("At least one argument must be supplied (input group infomation for DEG analysis).", call.=FALSE)
  35. }
  36. ##import file
  37. out_dir<-paste(gsub("/$","",opt$out_dir),"/",sep="")
  38. logexpr<-read.table(opt$input,header=T,stringsAsFactors=F,row.names=1)
  39. #check exp file is log scale
  40. if(max(logexpr[,1])-min(logexpr[,1])>100){
  41. stop("DEG anlaysis should be conducted based on expression profile on log scale. Please run log2 first", call.=FALSE)
  42. }
  43. ##import sample group file and check
  44. sample_group<-read.table(opt$sample_group,sep="\t",header=T)
  45. if(length(grep("group",colnames(sample_group)))==0){
  46. stop("No group is identified in sample_group file. Make sure the head of sample_group file is like sample, group1, group2.")
  47. }
  48. #c2: curated gene sets (rdata file)
  49. load("./human_c2_v5p2.rdata")
  50. #c5: GO gene sets (rdata file)
  51. load("./human_c5_v5p2.rdata")
  52. ##########################
  53. #########ID convert#######
  54. ##########################
  55. message("Begin ID conversion.")
  56. if(length(grep("ID_convert_table.rds",dir()))>0){
  57. idconvert<-readRDS("./ID_convert_table.rds")
  58. }else{
  59. stop("Cannot find ID_convert_table.rds in the working folder. Exit!", call.=FALSE)
  60. }
  61. if(opt$type_gene_id=="EnsemblGID"){
  62. gene_entrez<-idconvert$EntrezID[match(rownames(logexpr),idconvert$EnsemblID)]
  63. if(length(which(is.na(gene_entrez)))==nrow(logexpr)){
  64. stop("Cannot convert Ensembl gene ID to Entrez gene ID. Exit!", call.=FALSE)
  65. }else{
  66. logexpr1<-logexpr[!gene_entrez=="",]
  67. gene_entrez1<-gene_entrez[!gene_entrez==""]
  68. logexpr.entrez<-apply(logexpr1,2,function(x){unlist(tapply(x,as.factor(gene_entrez1),mean))})
  69. }
  70. }
  71. if(opt$type_gene_id=="GeneSymbol"){
  72. gene_entrez<-idconvert$EntrezID[match(rownames(logexpr),idconvert$GeneSymbol)]
  73. if(length(which(is.na(gene_entrez)))==nrow(logexpr)){
  74. stop("Cannot convert Ensembl gene ID to Entrez gene ID. Exit!", call.=FALSE)
  75. }else{
  76. logexpr1<-logexpr[!gene_entrez=="",]
  77. gene_entrez1<-gene_entrez[!gene_entrez==""]
  78. logexpr.entrez<-apply(logexpr1,2,function(x){unlist(tapply(x,as.factor(gene_entrez1),mean))})
  79. }
  80. }
  81. if(opt$type_gene_id=="EntrezID"){
  82. logexpr.entrez<-logexpr
  83. }
  84. message("Finish ID conversion.")
  85. ######################
  86. ######## GSEA ########
  87. ######################
  88. groupn<-grep("group",colnames(sample_group))
  89. c5sigall<-c()
  90. c2sigall<-c()
  91. for ( i in groupn){
  92. compgroup<-combn(unique(sample_group[,i]), 2)
  93. for ( j in 1:ncol(compgroup)){
  94. nam<-paste(compgroup[1,j],"vs",compgroup[2,j],sep="")
  95. versus<-paste(compgroup[1,j],"vs",compgroup[2,j],sep=" ")
  96. groupA<-logexpr.entrez[,as.character(sample_group$sample[sample_group[,i] %in% compgroup[1,j]])]
  97. groupB<-logexpr.entrez[,as.character(sample_group$sample[sample_group[,i] %in% compgroup[2,j]])]
  98. logfc<-rowMeans(groupA)-rowMeans(groupB)
  99. logfc<-logfc[order(-logfc)]
  100. #GSEA in GO term
  101. fgseaRes.c5 <- fgsea(Hs.c5, logfc, minSize=15, maxSize = 500, nperm=1000)
  102. c5sig<-fgseaRes.c5[fgseaRes.c5$padj<opt$padjvalueCutoff,]
  103. c5sig<-c5sig[order(c5sig$pval),]
  104. c5sig<-data.frame(c5sig)
  105. c5sig$leadingEdge<-sapply(c5sig$leadingEdge,function(x){paste0(unlist(x),collapse=", ")})
  106. if(nrow(c5sig)==0){
  107. message(paste("No significant GO term is identified in group ",nam,".",sep=""))
  108. }else{
  109. message(paste(nrow(c5sig)," significant GO term(s) is(are) identified in group ",nam,".",sep=""))
  110. c5sigall<-rbind(c5sigall,cbind(versus,c5sig))
  111. }
  112. #GSEA in curated gene sets
  113. fgseaRes.c2 <- fgsea(Hs.c2, logfc, minSize=15, maxSize = 500, nperm=1000)
  114. c2sig<-fgseaRes.c2[fgseaRes.c2$padj<opt$padjvalueCutoff,]
  115. c2sig<-c2sig[order(c2sig$pval),]
  116. c2sig<-data.frame(c2sig)
  117. c2sig$leadingEdge<-sapply(c2sig$leadingEdge,function(x){paste0(unlist(x),collapse=", ")})
  118. if(nrow(c2sig)==0){
  119. message(paste("No significant curated gene sets is identified in group ",nam,".",sep=""))
  120. }else{
  121. message(paste(nrow(c2sig)," significant curated gene sets are identified in group ",nam,".",sep=""))
  122. c2sigall<-rbind(c2sigall,cbind(versus,c2sig))
  123. }
  124. }
  125. }
  126. if(nrow(c5sigall)==0){
  127. message("No significant GO term is identified.")
  128. }else{
  129. c5sigall$pval<-signif(c5sigall$pval,4)
  130. c5sigall$padj<-signif(c5sigall$padj,4)
  131. c5sigall$ES<-signif(c5sigall$ES,4)
  132. c5sigall$NES<-signif(c5sigall$NES,4)
  133. rownames(c5sigall)<-c(1:nrow(c5sigall))
  134. write.csv(c5sigall,paste(out_dir,opt$project_code,"_gsea_go.csv",sep=""))
  135. }
  136. if(nrow(c2sigall)==0){
  137. message("No significant GO term is identified.")
  138. }else{
  139. c2sigall$pval<-signif(c2sigall$pval,4)
  140. c2sigall$padj<-signif(c2sigall$padj,4)
  141. c2sigall$ES<-signif(c2sigall$ES,4)
  142. c2sigall$NES<-signif(c2sigall$NES,4)
  143. rownames(c2sigall)<-c(1:nrow(c2sigall))
  144. write.csv(c2sigall,paste(out_dir,opt$project_code,"_gsea_curatedgenesets.csv",sep=""))
  145. }