RNA-seq下游数据分析-ballgown到报告。 以Rscript为主,对接PGx RNA-seq choppy现有pipeline,到生成RNA-seq分析报告所需的rds和csv文件。
r
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

191 行
7.1KB

  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 in tab-delimited format 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. make_option(c("-d", "--ref_rdata_dir"), type="character",default="./",
  26. help="The directory of reference files: human_c2_v5p2.rdata, human_c5_v5p2.rdata and ID_convert_table.rds. [default: ./]")
  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. if (is.null(opt$sample_group)){
  36. stop("At least one argument must be supplied (input group infomation for DEG analysis).", call.=FALSE)
  37. }
  38. ##import file
  39. out_dir<-paste(gsub("/$","",opt$out_dir),"/",sep="")
  40. logexpr<-read.table(opt$input,header=T,stringsAsFactors=F,row.names=1,check.names=F)
  41. #check exp file is log scale
  42. if(max(logexpr[,1])-min(logexpr[,1])>100){
  43. stop("DEG anlaysis should be conducted based on expression profile on log scale. Please run log2 first", call.=FALSE)
  44. }
  45. ##import sample group file and check
  46. sample_group<-read.table(opt$sample_group,sep="\t",header=T)
  47. if(length(grep("group",colnames(sample_group)))==0){
  48. stop("No group is identified in sample_group file. Make sure the head of sample_group file is like sample, group1, group2.")
  49. }
  50. #refdir
  51. refdir<-paste(gsub("/$","",opt$ref_rdata_dir),"/",sep="")
  52. #c2: curated gene sets (rdata file)
  53. #c5: GO gene sets (rdata file)
  54. if(length(grep("human_c2_v5p2.rdata",dir(refdir)))>0){
  55. load(paste(refdir,"human_c2_v5p2.rdata",sep=""))
  56. }else{
  57. stop("Cannot find human_c2_v5p2.rds in the ref_rdata_dir. Exit!", call.=FALSE)
  58. }
  59. if(length(grep("human_c5_v5p2.rdata",dir(refdir)))>0){
  60. load(paste(refdir,"human_c5_v5p2.rdata",sep=""))
  61. }else{
  62. stop("Cannot find human_c5_v5p2.rds in the ref_rdata_dir. Exit!", call.=FALSE)
  63. }
  64. ##########################
  65. #########ID convert#######
  66. ##########################
  67. message("Begin ID conversion.")
  68. if(length(grep("ID_convert_table.rds",dir(refdir)))>0){
  69. idconvert<-readRDS(paste(refdir,"ID_convert_table.rds",sep=""))
  70. }else{
  71. stop("Cannot find ID_convert_table.rds in the working folder. Exit!", call.=FALSE)
  72. }
  73. if(opt$type_gene_id=="EnsemblGID"){
  74. gene_entrez<-idconvert$EntrezID[match(rownames(logexpr),idconvert$EnsemblID)]
  75. if(length(which(is.na(gene_entrez)))==nrow(logexpr)){
  76. stop("Cannot convert Ensembl gene ID to Entrez gene ID. Exit!", call.=FALSE)
  77. }else{
  78. logexpr1<-logexpr[!gene_entrez=="",]
  79. gene_entrez1<-gene_entrez[!gene_entrez==""]
  80. logexpr.entrez<-apply(logexpr1,2,function(x){unlist(tapply(x,as.factor(gene_entrez1),mean))})
  81. }
  82. }
  83. if(opt$type_gene_id=="GeneSymbol"){
  84. gene_entrez<-idconvert$EntrezID[match(rownames(logexpr),idconvert$GeneSymbol)]
  85. if(length(which(is.na(gene_entrez)))==nrow(logexpr)){
  86. stop("Cannot convert Ensembl gene ID to Entrez gene ID. Exit!", call.=FALSE)
  87. }else{
  88. logexpr1<-logexpr[!gene_entrez=="",]
  89. gene_entrez1<-gene_entrez[!gene_entrez==""]
  90. logexpr.entrez<-apply(logexpr1,2,function(x){unlist(tapply(x,as.factor(gene_entrez1),mean))})
  91. }
  92. }
  93. if(opt$type_gene_id=="EntrezID"){
  94. logexpr.entrez<-logexpr
  95. }
  96. message("Finish ID conversion.")
  97. ######################
  98. ######## GSEA ########
  99. ######################
  100. groupn<-grep("group",colnames(sample_group))
  101. c5sigall<-c()
  102. c2sigall<-c()
  103. for ( i in groupn){
  104. compgroup<-combn(unique(sample_group[,i]), 2)
  105. for ( j in 1:ncol(compgroup)){
  106. nam<-paste(compgroup[1,j],"vs",compgroup[2,j],sep="")
  107. versus<-paste(compgroup[1,j],"vs",compgroup[2,j],sep=" ")
  108. groupA<-logexpr.entrez[,as.character(sample_group$sample[sample_group[,i] %in% compgroup[1,j]])]
  109. groupB<-logexpr.entrez[,as.character(sample_group$sample[sample_group[,i] %in% compgroup[2,j]])]
  110. logfc<-rowMeans(groupA)-rowMeans(groupB)
  111. logfc<-logfc[order(-logfc)]
  112. #GSEA in GO term
  113. fgseaRes.c5 <- fgsea(Hs.c5, logfc, minSize=15, maxSize = 500, nperm=1000)
  114. c5sig<-fgseaRes.c5[fgseaRes.c5$padj<opt$padjvalueCutoff,]
  115. if(nrow(c5sig)==0){
  116. message(paste("No significant GO term is identified in group ",nam,".",sep=""))
  117. }else{
  118. message(paste(nrow(c5sig)," significant GO term(s) is(are) identified in group ",nam,".",sep=""))
  119. c5sig<-c5sig[order(c5sig$pval),]
  120. c5sig<-data.frame(c5sig)
  121. c5sig$leadingEdge<-sapply(c5sig$leadingEdge,function(x){paste0(unlist(x),collapse=", ")})
  122. c5sigall<-rbind(c5sigall,cbind(versus,c5sig))
  123. }
  124. #GSEA in curated gene sets
  125. fgseaRes.c2 <- fgsea(Hs.c2, logfc, minSize=15, maxSize = 500, nperm=1000)
  126. c2sig<-fgseaRes.c2[fgseaRes.c2$padj<opt$padjvalueCutoff,]
  127. if(nrow(c2sig)==0){
  128. message(paste("No significant curated gene sets is identified in group ",nam,".",sep=""))
  129. }else{
  130. message(paste(nrow(c2sig)," significant curated gene sets are identified in group ",nam,".",sep=""))
  131. c2sig<-c2sig[order(c2sig$pval),]
  132. c2sig<-data.frame(c2sig)
  133. c2sig$leadingEdge<-sapply(c2sig$leadingEdge,function(x){paste0(unlist(x),collapse=", ")})
  134. c2sigall<-rbind(c2sigall,cbind(versus,c2sig))
  135. }
  136. }
  137. }
  138. if(length(c5sigall)==0){
  139. message("No significant GO term is identified.")
  140. }else{
  141. c5sigall$pval<-signif(c5sigall$pval,4)
  142. c5sigall$padj<-signif(c5sigall$padj,4)
  143. c5sigall$ES<-signif(c5sigall$ES,4)
  144. c5sigall$NES<-signif(c5sigall$NES,4)
  145. rownames(c5sigall)<-c(1:nrow(c5sigall))
  146. write.csv(c5sigall,paste(out_dir,opt$project_code,"_gsea_go.csv",sep=""))
  147. }
  148. if(length(c2sigall)==0){
  149. message("No significant GO term is identified.")
  150. }else{
  151. c2sigall$pval<-signif(c2sigall$pval,4)
  152. c2sigall$padj<-signif(c2sigall$padj,4)
  153. c2sigall$ES<-signif(c2sigall$ES,4)
  154. c2sigall$NES<-signif(c2sigall$NES,4)
  155. rownames(c2sigall)<-c(1:nrow(c2sigall))
  156. write.csv(c2sigall,paste(out_dir,opt$project_code,"_gsea_curatedgenesets.csv",sep=""))
  157. }