annotate 02_read_counts_tss/countsAroundTSS.R @ 0:071d2d36c828 draft default tip

Uploaded
author dktanwar
date Tue, 12 Sep 2017 14:14:32 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
1 ## How to execute this tool
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
2 # $Rscript my_r_tool.R --input input.csv --output output.csv
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
3
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
4 # Send R errors to stderr
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
5 options(show.error.messages = F, error = function(){cat(geterrmessage(), file = stderr()); q("no", 1, F)})
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
6
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
7 # Avoid crashing Galaxy with an UTF8 error on German LC settings
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
8 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
9
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
10 options(stringAsfactors = FALSE, useFancyQuotes = FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
11
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
12 # Check for packages requirement -----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
13 suppressMessages(source("https://bioconductor.org/biocLite.R"))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
14
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
15 list.of.packages <- c("GenomicRanges",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
16 "Rsamtools",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
17 "rtracklayer",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
18 "parallel",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
19 "foreach",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
20 "doParallel",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
21 "GenomicFeatures",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
22 "Gviz",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
23 "BSgenome.Hsapiens.UCSC.hg19",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
24 "org.Hs.eg.db",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
25 "BSgenome.Mmusculus.UCSC.mm10",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
26 "org.Mm.eg.db",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
27 "optparse",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
28 "getopt")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
29
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
30 new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
31 if(length(new.packages)) biocLite(new.packages)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
32
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
33 # packages required
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
34 tmp <- suppressMessages(lapply(list.of.packages, require, quietly =T,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
35 warn.conflicts = F, character.only = TRUE))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
36 rm(tmp)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
37
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
38
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
39 # Command line options ----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
40 # option_list <- list(
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
41 # make_option(c("-b", "--bamfile"), type="character", default=NULL,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
42 # help="bamfile for which read counts are to be calculated around TSS", metavar="character"),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
43 # make_option(c("-e", "--genome"), type="character", default = NULL,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
44 # help="genome: hg19/ mm10", metavar="character"),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
45 # make_option(c("-n", "--TSSn"), type="character", default = NULL,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
46 # help="region upstream TSS for which the read counts are to be calculated", metavar="character"),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
47 # make_option(c("-p", "--TSSp"), type="character", default = NULL,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
48 # help="region downstream TSS for which the read counts are to be calculated", metavar="character"),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
49 # make_option(c("-u", "--TxDbUCSC"), type="character", default = NULL,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
50 # help="Transcripts database: If you already have a sqlite Transcript database for hg19/ mm10, provide it, or do not enter anything. Software will create a database to use from UCSC.", metavar="character")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
51 # )
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
52 #
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
53 # opt_parser <- OptionParser(option_list=option_list)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
54 # opt <- parse_args(opt_parser)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
55
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
56 # Take in trailing command line arguments
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
57 args <- commandArgs(trailingOnly = TRUE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
58 # args <- c("ENCFF027UTM.bam", "hg19", 500, 500, "transcripts_mm10_UCSC.sqlite")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
59
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
60 # Get options using the spec as defined by the enclosed list
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
61 # Options are read from the default: commandArgs(TRUE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
62 option_specification = matrix(c(
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
63 'input1', 'i1', 2, 'character',
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
64 'input2', 'i2', 2, 'character',
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
65 'input3', 'i3', 2, 'integer',
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
66 'input4', 'i4', 2, 'integer',
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
67 'input5', 'i5', 2, 'character',
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
68 'output', 'o', 2, 'character'
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
69 ), byrow=TRUE, ncol=4);
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
70
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
71 # Parse options
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
72 options = getopt(option_specification)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
73
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
74 if (is.null(options$input1) | is.null(options$input2)){
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
75 stop("Please provide right input parameters. See help!", call.=FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
76 }
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
77
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
78
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
79 # opt$genome <- "mm10"
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
80 # opt$TxDbUCSC <- "transcripts_mm10_UCSC.sqlite"
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
81 # opt$bamfile <- "ENCFF027UTM.bam"
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
82 # opt$TSSn <- 500
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
83 # opt$TSSp <- 500
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
84
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
85 # Transcript database ----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
86 txdb <- NULL
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
87 if(is.null(options$input5)){
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
88 txdb <- makeTxDbFromUCSC(genome = options$input2, tablename="refGene")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
89 saveDb(txdb, file = paste("transcripts_", options$input2, "_UCSC.sqlite", sep = ""))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
90 } else{
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
91 txdb <- loadDb(options$input5)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
92 }
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
93
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
94 txs <- transcripts(txdb, columns = c("gene_id", "tx_id"))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
95 txs <- txs[sapply(split(seq_along(txs), sapply(txs$gene_id, "[", 1)),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
96 function(x) x[which.max(width(txs)[x])])]
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
97
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
98 geneID <- txs$"gene_id"
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
99 genes <- as.character(geneID)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
100
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
101
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
102 # Bamfiles and chromosomes
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
103 # bamfiles <- list.files(path = opt$bamdir, pattern = ".bam$", full.names = T)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
104 bamfiles <- options$input1
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
105
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
106 chrs <- scanBamHeader(bamfiles)[[1]]$targets
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
107
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
108
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
109 # TSS regions for calculationg counts ----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
110 tss <- suppressWarnings(GRanges(seqnames(txs),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
111 IRanges(start = ifelse(as.character(strand(txs)) == "+",
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
112 start(txs), end(txs)) - as.numeric(options$input3),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
113 width = as.numeric(options$input3) + as.numeric(options$input4)),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
114 strand = strand(txs), seqlengths = chrs))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
115
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
116
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
117 # Functions ----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
118 mylapply <- function(...) {
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
119 if(require(parallel) && .Platform$OS.type == "unix")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
120 mclapply(..., mc.cores = detectCores(), mc.preschedule = F)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
121 else
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
122 lapply(...)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
123 }
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
124
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
125 coverageForChr <- function(seqname, bamfile, chrs, fragmentSize=0, ...) {
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
126 message(", ", seqname, appendLF=FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
127 shift <- round(fragmentSize/2)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
128 param <- ScanBamParam(what=c("pos","qwidth","strand"),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
129 which=GRanges(seqname, IRanges(1, chrs[seqname])),
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
130 flag=scanBamFlag(isUnmappedQuery=FALSE))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
131 x <- scanBam(bamfile, ..., param=param)[[1]]
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
132 coverage(IRanges(ifelse(x[["strand"]]=="+", x[["pos"]]+shift,
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
133 x[["pos"]]+x[["qwidth"]]-shift), width=1), width=chrs[seqname])
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
134 }
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
135
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
136
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
137 countsForRegions <- function(bamfiles, regions, fragmentSize=0) {
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
138 names(regions) <- seq_along(regions)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
139 chrs <- seqlengths(regions)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
140 DF <- do.call(cbind, lapply(bamfiles, function(bf) {
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
141 message("counting alignments in ",basename(bf),appendLF=FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
142 cover <- mylapply(names(chrs), coverageForChr, bamfile=bf, chrs=chrs, fragmentSize=fragmentSize)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
143 names(cover) <- names(chrs)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
144 cover <- RleList(unlist(cover), compress=FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
145 vws <- Views(cover, as(regions,"RangesList"))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
146 vs <- unlist(viewSums(vws), use.names=FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
147 vs <- vs[match(names(regions), unlist(lapply(vws,names),use.names=FALSE))]
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
148 df <- DataFrame(vs)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
149 colnames(df) <- sub(".bam","",basename(bf))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
150 message(": done")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
151 df
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
152 }))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
153 }
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
154
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
155
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
156
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
157 # Counting reads ----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
158 cnts <- countsForRegions(bamfiles = bamfiles, regions = tss, fragmentSize = 150)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
159 cnts <- data.frame(Genes = genes, cnts, stringsAsFactors = F, check.names = F)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
160
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
161 # Annotating genes ----
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
162 anno <- NULL
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
163 if(options$input2 == "hg19"){
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
164 anno <- select(org.Hs.eg.db, as.character(cnts$Genes), c("SYMBOL", "GENENAME"))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
165 } else if (options$input2 == "mm10"){
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
166 anno <- select(org.Mm.eg.db, as.character(cnts$Genes), c("SYMBOL", "GENENAME"))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
167 } else {
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
168 stop("Please provide correct reference genome. See help!", call.=FALSE)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
169 }
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
170
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
171 cnts <- merge(anno, cnts, by.x = "ENTREZID", by.y = "Genes")
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
172
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
173 index <- which(is.na(cnts$SYMBOL))
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
174 cnts$SYMBOL[index] <- cnts$ENTREZID[index]
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
175
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
176 write.table(cnts, file = options$output, sep = "\t", row.names = F, quote = F)
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
177
071d2d36c828 Uploaded
dktanwar
parents:
diff changeset
178 sessionInfo()