#!/usr/bin/env Rscript # Combine per-sample featureCounts tables into one count matrix. # Column names and order come from the sample column of the design file. # The name column is the common gene name from the GTF used for counting. # Input sample design. DESIGN_FILE = "design.csv" # Directory of per-sample featureCounts tables. COUNT_DIR = "counts" # GTF used for counting. Supplies the common gene name. GTF_FILE = "gtf/GRCh38.chr16.annotation.gtf" # Output count matrix. COUNT_FILE = "csv/gene_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 featureCounts tables [%default]"), make_option(c("-g", "--gtf_file"), action="store", default=GTF_FILE, dest="gtf_file", type="character", help="GTF used for counting; supplies the common gene name [%default]"), make_option(c("-o", "--output"), action="store", default=COUNT_FILE, dest="count_file", type="character", help="output 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 featureCounts tables into one matrix\n") cat("#\n") cat("# https://subread.sourceforge.net/featureCounts.html\n") cat("# Pass the -h flag to get help on parameters\n") suppressPackageStartupMessages(library(rtracklayer)) # featureCounts values for one sample, named by gene id. read_counts <- function(sample, dir) { path <- file.path(dir, paste0(sample, ".txt")) cat("# Loading counts from:", path, "\n") tab <- read.delim(path, comment.char = "#", check.names = FALSE) setNames(tab[[ncol(tab)]], tab$Geneid) } # gene_id to gene_name from the annotation. read_names <- function(gtf) { cat("# Loading gene names from:", gtf, "\n") ann <- import(gtf) genes <- ann[ann$type == "gene"] setNames(genes$gene_name, genes$gene_id) } # Count matrix with id, gene name, and one column per sample. combine_counts <- function(design, dir, gtf) { cat("# Loading design from:", design, "\n") samples <- read.csv(design, stringsAsFactors = FALSE)$sample cols <- lapply(samples, read_counts, dir = dir) genes <- names(cols[[1]]) mat <- vapply(cols, function(x) x[genes], numeric(length(genes))) colnames(mat) <- samples data.frame( id = genes, name = unname(read_names(gtf)[genes]), 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)