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.

77 lines
2.9KB

  1. #!/usr/bin/env Rscript
  2. ###Copyright 2019 Ying Yu from Fudan-PGx group
  3. # example:
  4. # Rscript RNAseq_2_pca.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("data.table"))
  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. option_list <- list(
  12. make_option(c("-o", "--out_dir"), type="character",default="./",
  13. help="The output directory [default ./]"),
  14. make_option(c("-i", "--input"),type="character", default=NULL,
  15. help="The input expression files. required!"),
  16. make_option(c("-g", "--sample_group"),type="character", default=NULL,
  17. 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... "),
  18. make_option(c("-p", "--project_code"), type="character",default="rnaseq",
  19. help="Project code, which is used as prefix of output file. [default: rnaseq]")
  20. )
  21. # get command line options, if help option encountered print help and exit,
  22. # otherwise if options not found on command line then set defaults,
  23. opt <- parse_args(OptionParser(option_list=option_list))
  24. if (is.null(opt$input)){
  25. print_help(opt_parser)
  26. stop("At least one argument must be supplied (input file).", call.=FALSE)
  27. }
  28. ##import exp file
  29. out_dir<-paste(gsub("/$","",opt$out_dir),"/",sep="")
  30. logexpr<-read.table(opt$input,header=T,stringsAsFactors=F,row.names=1,check.names=F)
  31. #check exp file is log scale
  32. if(max(logexpr[,1])-min(logexpr[,1])>100){
  33. stop("PCA anlaysis shoulc be conducted based on expression profile on log scale.", call.=FALSE)
  34. }
  35. #####################
  36. ##########PCA #######
  37. ##calculate pca
  38. pc.cr<-prcomp(t(logexpr),retx = TRUE)
  39. pca<-pc.cr$x
  40. pca<-data.frame(pca)
  41. pca$sample<-rownames(pca)
  42. pcanew<-pca
  43. message("PCA finished.")
  44. ####finished PCA
  45. ####add group infomaiton if imort
  46. if (is.null(opt$sample_group)){
  47. message("Warning: no group sample file. PCA will not be able to colored by group.")
  48. }else{
  49. sample_group<-read.table(opt$sample_group,sep="\t",header=T)
  50. if(length(grep("group",colnames(sample_group)))==0){
  51. message("No group is identified in sample_group file. Make sure the head of sample_group file is like sample, group1, group2.")
  52. }else{
  53. groupn<-grep("group",colnames(sample_group))
  54. for ( i in groupn){
  55. pcanew<- cbind(pcanew,sample_group[match(pca$sample,sample_group$sample),i])
  56. }
  57. colnames(pcanew)[c((ncol(pca)+1):ncol(pcanew))]<-colnames(sample_group)[groupn]
  58. }
  59. }
  60. #write output
  61. write.csv(pcanew,paste(out_dir,opt$project_code,"_pca.csv",sep=""))
  62. saveRDS(pcanew,paste(out_dir,opt$project_code,"_pca.rds",sep=""))
  63. ########