run-featurecounts.R 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env Rscript
  2. # parse parameter ---------------------------------------------------------
  3. library(argparser, quietly=TRUE)
  4. # Create a parser
  5. p <- arg_parser("run featureCounts and calculate FPKM/TPM")
  6. # Add command line arguments
  7. p <- add_argument(p, "--bam", help="input: bam file", type="character")
  8. p <- add_argument(p, "--gtf", help="input: gtf file", type="character")
  9. p <- add_argument(p, "--output", help="output prefix", type="character")
  10. # Parse the command line arguments
  11. argv <- parse_args(p)
  12. library(Rsubread)
  13. library(limma)
  14. library(edgeR)
  15. bamFile <- argv$bam
  16. gtfFile <- argv$gtf
  17. nthreads <- 1
  18. outFilePref <- argv$output
  19. outStatsFilePath <- paste(outFilePref, '.log', sep = '');
  20. outCountsFilePath <- paste(outFilePref, '.count', sep = '');
  21. fCountsList = featureCounts(bamFile, annot.ext=gtfFile, isGTFAnnotationFile=TRUE, nthreads=nthreads, isPairedEnd=TRUE)
  22. dgeList = DGEList(counts=fCountsList$counts, genes=fCountsList$annotation)
  23. fpkm = rpkm(dgeList, dgeList$genes$Length)
  24. tpm = exp(log(fpkm) - log(sum(fpkm)) + log(1e6))
  25. write.table(fCountsList$stat, outStatsFilePath, sep="\t", col.names=FALSE, row.names=FALSE, quote=FALSE)
  26. featureCounts = cbind(fCountsList$annotation[,1], fCountsList$counts, fpkm, tpm)
  27. colnames(featureCounts) = c('gene_id', 'counts', 'fpkm','tpm')
  28. write.table(featureCounts, outCountsFilePath, sep="\t", col.names=TRUE, row.names=FALSE, quote=FALSE)