run-featurecounts.R 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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, "--featureType", help="a character string or a vector of character strings giving the feature type or types used to select rows in the GTF annotation which will be used for read summarization", type="character", default="exon")
  10. p <- add_argument(p, "--attrType", help="a character string giving the attribute type in the GTF annotation which will be used to group features (eg. exons) into meta-features", type="character", default="gene_id")
  11. p <- add_argument(p, "--isPairedEnd", help="indicating whether libraries contain paired-end reads or not", type="logical", default=TRUE)
  12. p <- add_argument(p, "--output", help="output prefix", type="character")
  13. # Parse the command line arguments
  14. argv <- parse_args(p)
  15. library(Rsubread)
  16. library(limma)
  17. library(edgeR)
  18. bamFile <- argv$bam
  19. gtfFile <- argv$gtf
  20. nthreads <- 1
  21. outFilePref <- argv$output
  22. outStatsFilePath <- paste(outFilePref, '.log', sep = '');
  23. outCountsFilePath <- paste(outFilePref, '.count', sep = '');
  24. fCountsList = featureCounts(bamFile, annot.ext=gtfFile, isGTFAnnotationFile=TRUE, nthreads=nthreads, GTF.featureType=argv$featureType, GTF.attrType=argv$attrType, isPairedEnd=argv$isPairedEnd)
  25. dgeList = DGEList(counts=fCountsList$counts, genes=fCountsList$annotation)
  26. fpkm = rpkm(dgeList, dgeList$genes$Length)
  27. tpm = exp(log(fpkm) - log(sum(fpkm)) + log(1e6))
  28. write.table(fCountsList$stat, outStatsFilePath, sep="\t", col.names=FALSE, row.names=FALSE, quote=FALSE)
  29. featureCounts = cbind(fCountsList$annotation[,1], fCountsList$counts, fpkm, tpm)
  30. colnames(featureCounts) = c('gene_id', 'counts', 'fpkm','tpm')
  31. write.table(featureCounts, outCountsFilePath, sep="\t", col.names=TRUE, row.names=FALSE, quote=FALSE)