-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_gene_distribution.R
92 lines (55 loc) · 1.8 KB
/
read_gene_distribution.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env Rscript
# This script takes a BAM file and a TranscriptDB sqlite file and plots
# the distribution of reads across an "average" gene.
library("GenomicFeatures")
library("GenomicRanges")
library("Rsamtools")
options(stringsAsFactors = FALSE);
args <- commandArgs(trailingOnly=TRUE)
bam <- args[1]
transcripts <- args[2]
#for testing
bam <- "/mnt/esc/chip_export_sorted_nodups.bam"
db.file <- "/mnt/data/mmusculus_ensembl_20110927.sqlite"
genes <- loadFeatures(db.file)
genes <-as.list(genes)
# load the bam into a GappedAlignments object ?
aln <- readGappedAlignments(bam)
# aln has chr, exonList doesn't.
rname(aln) <- gsub("chr","",rname(aln))
# and uses MT not M
rname(aln) <- gsub("M","MT",rname(aln))
# This will give you an RLE along each chr.
cov <- coverage(aln)
get.cov <- function(x){
start <- as.integer(x["tx_start"]) - 20000
end <- as.integer(x["tx_end"]) + 20000
chr <- x["tx_chrom"]
return(cov[[as.character(chr)]][start:end])
}
test<-apply(genes[[1]], 1, get.cov)
what <- c("rname", "pos", "length")
which <- RangesList(space=genes[[1]][,"tx_chrom"], start=genes[[1]][,"tx_start"], end=genes[[1]][,"tx_end"])
# This doesn't do what they think it does.
counter <- function(gnModel)
{
hits <- countOverlaps(aln, gnModel)
counts <- countOverlaps(gnModel, aln[hits==1])
names(counts) <- names(gnModel)
counts
}
counts <- counter(exonList[[1]])
save(counts, file=file.path(outputDir, "counts.rda"))
### faffing about #####
# get transcripts
transcripts <- transcripts(genes)
# get transcripts by gene
transcriptList <- transcriptsBy(genes, by="gene")
# get exons
exons <- exons(genes)
# get exons by transcripts
exonList <- exonsBy(genes, by="gene")
# get introns by genei
ntronList <- intronsByTranscript(genes)
transcriptsByOverlap
exonsByOverlap