#!/usr/bin/env Rscript # Combine per-sample Salmon quant.sf files into one transcript count matrix. # Column names and order come from the sample column of the design file. # Values are estimated counts (NumReads), not TPM. # The id column is the transcript id. # The name column is the common gene name from the GTF. # Input sample design. DESIGN_FILE = "design.csv" # Directory of per-sample Salmon quantifications. COUNT_DIR = "counts" # GTF used to map transcript id to gene name. GTF_FILE = "gtf/GRCh38.chr16.annotation.gtf" # Output count matrix. COUNT_FILE = "csv/transcript_counts.csv" # Install the optparse package automatically. if(!require(optparse, quietly=T)){ install.packages("optparse", repos="http://cran.us.r-project.org") library(optparse) } # Command line options. option_list = list( make_option(c("-d", "--design_file"), action="store", default=DESIGN_FILE, dest="design_file", type="character", help="input sample design [%default]"), make_option(c("-c", "--count_dir"), action="store", default=COUNT_DIR, dest="count_dir", type="character", help="directory of per-sample Salmon quantifications [%default]"), make_option(c("-g", "--gtf_file"), action="store", default=GTF_FILE, dest="gtf_file", type="character", help="GTF used to map transcript id to gene name [%default]"), make_option(c("-o", "--output"), action="store", default=COUNT_FILE, dest="count_file", type="character", help="output transcript count matrix [%default]") ) # Parse the options. opt = parse_args(OptionParser(option_list=option_list)) # Set the values from the options. design_file = opt$design_file count_dir = opt$count_dir gtf_file = opt$gtf_file count_file = opt$count_file cat("#\n") cat("# Combine Salmon transcript counts into one matrix\n") cat("#\n") cat("# https://bioconductor.org/packages/release/bioc/html/tximport.html\n") cat("# Pass the -h flag to get help on parameters\n") suppressPackageStartupMessages(library(tximport)) # transcript_id -> gene_name from the annotation read_names <- function(gtf) { cat("# Loading gene names from:", gtf, "\n") lines <- readLines(gtf) lines <- lines[grepl("\ttranscript\t", lines)] tx_id <- sub('.*transcript_id "([^"]+)".*', "\\1", lines) gene <- sub('.*gene_name "([^"]+)".*', "\\1", lines) setNames(gene, tx_id) } # Salmon NumReads for each transcript and sample in the design. combine_counts <- function(design, dir, gtf) { cat("# Loading design from:", design, "\n") samples <- read.csv(design, stringsAsFactors = FALSE)$sample files <- file.path(dir, samples, "quant.sf") names(files) <- samples for (path in files) cat("# Loading counts from:", path, "\n") # txOut keeps one row per transcript. countsFromAbundance "no" keeps NumReads. txi <- tximport(files, type = "salmon", txOut = TRUE, countsFromAbundance = "no") mat <- txi$counts data.frame( id = rownames(mat), name = unname(read_names(gtf)[rownames(mat)]), mat, check.names = FALSE, stringsAsFactors = FALSE ) } counts <- combine_counts(design_file, count_dir, gtf_file) dir.create(dirname(count_file), showWarnings = FALSE, recursive = TRUE) cat("# Saving count matrix to:", count_file, "\n") write.csv(counts, count_file, row.names = FALSE, quote = FALSE)