-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene_annotations.R
333 lines (325 loc) · 14.3 KB
/
gene_annotations.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
###### make_annotation
## make annotation will return a set of GRanges accordingly to the
## parameters given to the function. Each GRange represents a single gene.
## In case more than one transcript is annotated for a gene, all transcripts are
## merged in order to obtain the largest region possible associated to
## that gene. In case a gene has non overlapping transcripts associated, the
## gene is removed.
## Arguments:
## - "annotation" can be either "singleChr" or "genome"
## - "singleChrName" must specify chromosome name (in case "singleChr" is selected)
## - "annotationFeature" a character can be either "TSS", "GB", "TES"
## > "TSS" returns the TSS region defined by the coordinates defined in
## promoter_limits
## > "TES" returns the TES region defined by the coordinates defined in
## promoter_limits
## > "GB" returns the region between the TSS and the TES
## > "tx" returns the whole transcript, extended in the promoter and termination
## regions, according to promoter_limits and termination_limits definitions
## - "promoter_limits" a numeric of length 2, defines the upstream and
## downstream distances from the annotated transcription-start-site
## - "termination_limits" a numeric of length 2, defines the upstream and
## downstream distances from the annotated transcription-end-site
## - "txdb" a TxDb object
make_annotation <- function(annotation, singleChrName, annotationFeature,
promoter_limits=c(50, 300), termination_limits=c(1000, 4000),
txdb=TxDb.Mmusculus.UCSC.mm9.knownGene) {
promoter_start <- promoter_limits[1]
promoter_end <- promoter_limits[2]
termination_start <- termination_limits[1]
termination_end <- termination_limits[2]
message('Creating annotation...')
reduceLevels <- function(grange) {
grange@seqnames <- droplevels(grange@seqnames)
grange@seqinfo <- grange@seqinfo[levels(grange@seqnames)]
grange }
# library(TxDb.Mmusculus.UCSC.mm9.knownGene)
# txdb <- TxDb.Mmusculus.UCSC.mm9.knownGene
tx <- transcripts(txdb)
names(tx) <- select(txdb, keys = tx$tx_name
, keytype = "TXNAME", columns = "GENEID")$GENEID
if( annotation == 'singleChr' )
{######## single chromosome
tx <- reduceLevels(tx[seqnames(tx)==singleChrName])
}
# remove NA named genes
tx <- tx[!is.na(names(tx))]
######## select a unique feature per each gene
tx <- reduce(split(tx, names(tx)))
# remove genes which still have more than one transcript after reducing...
tx <- unlist(tx[elementNROWS(tx)==1])
annotationGR <- tx
######## create either gene body or promoter annotation
annotationGR <- switch(annotationFeature
# for GB, reduce and unlist the GRList split by the name of the gene
, 'GB'={
# remove the promoter region from the gene body
#### PLUS
start(annotationGR[strand(tx)=='+']) <- # min(TTS+prom,TES)
apply(cbind(
start(tx[strand(tx)=='+'])+(promoter_end+1)
, end(tx[strand(tx)=='+'])+1
), 1, min)
end(annotationGR[strand(tx)=='+']) <- # max(TTS+prom,TES-term)
apply(cbind(
start(tx[strand(tx)=='+'])+(promoter_end+1)
, end(tx[strand(tx)=='+'])-termination_start
), 1, max)
#### MINUS
start(annotationGR[strand(tx)=='-']) <-
apply(cbind(
end(tx[strand(tx)=='-'])-promoter_end-1
, start(tx[strand(tx)=='-'])+termination_start
), 1, min)
end(annotationGR[strand(tx)=='-']) <-
apply(cbind(
end(tx[strand(tx)=='-'])-(promoter_end+1)
, start(tx[strand(tx)=='-'])-1
), 1, max)
trim(annotationGR)
}
, 'TSS'={
# keep only the promoter region (plus strand)
start(annotationGR[strand(tx)=='+']) <-
start(tx[strand(tx)=='+'])-promoter_start
end(annotationGR[strand(tx)=='+']) <-
apply(cbind(
start(tx[strand(tx)=='+'])+promoter_end
, end(tx[strand(tx)=='+'])
), 1, min)
# keep only the promoter region (minus strand)
end(annotationGR[strand(tx)=='-']) <-
end(tx[strand(tx)=='-'])+promoter_start
start(annotationGR[strand(tx)=='-']) <-
apply(cbind(
end(tx[strand(tx)=='-'])-promoter_end
, start(tx[strand(tx)=='-'])
), 1, max)
trim(annotationGR)
}
, 'TES'={
# keep only the promoter region (plus strand)
end(annotationGR[strand(tx)=='+']) <-
end(tx[strand(tx)=='+'])+termination_end
start(annotationGR[strand(tx)=='+']) <-
apply(cbind(
start(tx[strand(tx)=='+'])+promoter_end+2
, end(tx[strand(tx)=='+'])-(termination_start-1)
), 1, max)
# keep only the promoter region (minus strand)
start(annotationGR[strand(tx)=='-']) <-
start(tx[strand(tx)=='-'])-termination_end
end(annotationGR[strand(tx)=='-']) <-
apply(cbind(
end(tx[strand(tx)=='-'])-promoter_end-2
, start(tx[strand(tx)=='-'])+(termination_start-1)
), 1, min)
trim(annotationGR)
}, 'tx'={
# keep only the promoter region (plus strand)
start(annotationGR[strand(tx)=='+']) <-
start(tx[strand(tx)=='+'])-promoter_start
end(annotationGR[strand(tx)=='+']) <-
end(tx[strand(tx)=='+'])+termination_end
# keep only the promoter region (minus strand)
end(annotationGR[strand(tx)=='-']) <-
end(tx[strand(tx)=='-'])+promoter_start
start(annotationGR[strand(tx)=='-']) <-
start(tx[strand(tx)=='-'])-termination_end
trim(annotationGR)
}
)
return(annotationGR)
}
###### make_transcripts_annotation (modify)
## make annotation will return a set of GRanges accordingly to the
## parameters given to the function. Each GRange represents a single gene.
## In case more than one transcript is annotated for a gene, all transcripts are
## merged in order to obtain the largest region possible associated to
## that gene. In case a gene has non overlapping transcripts associated, the
## gene is removed.
## Arguments:
## - "annotation" can be either "singleChr" or "genome"
## - "singleChrName" must specify chromosome name (in case "singleChr" is selected)
## - "annotationFeature" a character can be either "TSS", "GB", "TES"
## > "TSS" returns the TSS region defined by the coordinates defined in
## promoter_limits
## > "TES" returns the TES region defined by the coordinates defined in
## promoter_limits
## > "GB" return the region between the TSS and the TES
## - "promoter_limits" a numeric of length 2, defines the upstream and
## downstream distances from the annotated transcription-start-site
## - "termination_limits" a numeric of length 2, defines the upstream and
## downstream distances from the annotated transcription-end-site
## - "txdb" a TxDb object
make_transcripts_annotation <- function(txdb) {
print('Creating the annotation...')
################
## define 5'UTR, exons, introns, 3'UTR
#################
myGenetx <- reduce(transcriptsBy(txdb,'gene'))
myGenetx <- unlist(myGenetx[elementNROWS(myGenetx)==1])
myGenetxPlus <- myGenetx[strand(myGenetx)=='+']
myGenetxMinus <- myGenetx[strand(myGenetx)=='-']
unambiguousTx <- names(myGenetx)
####################
## 5'UTR - exons ######
###################
print("1/6 : 5'UTR/exons...")
fiveUtrGR <- fiveUTRsByTranscript(txdb, use.names=TRUE)
fiveUtrMatch <- select(txdb, keys = names(fiveUtrGR), keytype = "TXNAME", columns = "GENEID")
#### annotate the 5utr db (plus strand)
myFiveUtrDB <- fiveUtrMatch[fiveUtrMatch$GENEID%in%names(myGenetxPlus),]
myFiveUtrDB$start <- sapply(start(fiveUtrGR[myFiveUtrDB$TXNAME]),min)
myFiveUtrDB$exons <- sapply(start(fiveUtrGR[myFiveUtrDB$TXNAME]),length)
myFiveUtrDB$txstart <- start(myGenetxPlus[myFiveUtrDB$GENEID])
# select the 5'UTR that match the start of the annotation
myFiveUtrDB <- myFiveUtrDB[myFiveUtrDB$start == myFiveUtrDB$txstart,]
# remove the identical 5'UTR annotations
myFiveUtrDB <- myFiveUtrDB[!duplicated(myFiveUtrDB[,-1]),]
# keep only the most complete annotation
myFiveUtrDB <- do.call('rbind',lapply(split(myFiveUtrDB,myFiveUtrDB$GENEID), function(x) x[which.max(x$exons),]))
# create the GRanges
myFiveUtrGRplus <- fiveUtrGR[myFiveUtrDB$TXNAME]
names(myFiveUtrGRplus) <- myFiveUtrDB$GENEID
#### annotate the 5utr db (minus strand)
myFiveUtrDB <- fiveUtrMatch[fiveUtrMatch$GENEID%in%names(myGenetxMinus),]
myFiveUtrDB$start <- sapply(end(fiveUtrGR[myFiveUtrDB$TXNAME]),max)
myFiveUtrDB$exons <- sapply(end(fiveUtrGR[myFiveUtrDB$TXNAME]),length)
myFiveUtrDB$txstart <- end(myGenetxMinus[myFiveUtrDB$GENEID])
# select the 5'UTR that match the start of the annotation
myFiveUtrDB <- myFiveUtrDB[myFiveUtrDB$start == myFiveUtrDB$txstart,]
# remove the identical 5'UTR annotations
myFiveUtrDB <- myFiveUtrDB[!duplicated(myFiveUtrDB[,-1]),]
# keep only the most complete annotation
myFiveUtrDB <- do.call('rbind',lapply(split(myFiveUtrDB,myFiveUtrDB$GENEID), function(x) x[which.max(x$exons),]))
# create the GRanges
myFiveUtrGRminus <- fiveUtrGR[myFiveUtrDB$TXNAME]
names(myFiveUtrGRminus) <- myFiveUtrDB$GENEID
#### concatenate
myFiveUtrGRExons <- c(myFiveUtrGRplus, myFiveUtrGRminus)
#######################
## 5'UTR - introns ######
#####################
print("2/6 : 5'UTR/introns...")
myFiveUtrGRIntrons <- psetdiff(unlist(range(myFiveUtrGRExons)),myFiveUtrGRExons)
myFiveUtrGRIntrons <- myFiveUtrGRIntrons[elementNROWS(myFiveUtrGRIntrons)>0]
####################
## 3'UTR - exons ######
###################
print("3/6 : 3'UTR/exons...")
threeUtrGR <- threeUTRsByTranscript(txdb, use.names=TRUE)
threeUtrMatch <- select(txdb, keys = names(threeUtrGR), keytype = "TXNAME", columns = "GENEID")
#### annotate the 5utr db (plus strand)
myThreeUtrDB <- threeUtrMatch[threeUtrMatch$GENEID%in%names(myGenetxPlus),]
myThreeUtrDB$end <- sapply(end(threeUtrGR[myThreeUtrDB$TXNAME]),max)
myThreeUtrDB$exons <- sapply(end(threeUtrGR[myThreeUtrDB$TXNAME]),length)
myThreeUtrDB$txend <- end(myGenetxPlus[myThreeUtrDB$GENEID])
# select the 5'UTR that match the end of the annotation
myThreeUtrDB <- myThreeUtrDB[myThreeUtrDB$end == myThreeUtrDB$txend,]
# remove the identical 5'UTR annotations
myThreeUtrDB <- myThreeUtrDB[!duplicated(myThreeUtrDB[,-1]),]
# keep only the most complete annotation
myThreeUtrDB <- do.call('rbind',lapply(split(myThreeUtrDB,myThreeUtrDB$GENEID), function(x) x[which.max(x$exons),]))
# create the GRanges
myThreeUtrGRplus <- threeUtrGR[myThreeUtrDB$TXNAME]
names(myThreeUtrGRplus) <- myThreeUtrDB$GENEID
#### annotate the 5utr db (minus strand)
myThreeUtrDB <- threeUtrMatch[threeUtrMatch$GENEID%in%names(myGenetxMinus),]
myThreeUtrDB$start <- sapply(start(threeUtrGR[myThreeUtrDB$TXNAME]),min)
myThreeUtrDB$exons <- sapply(start(threeUtrGR[myThreeUtrDB$TXNAME]),length)
myThreeUtrDB$txend <- start(myGenetxMinus[myThreeUtrDB$GENEID])
# select the 5'UTR that match the start of the annotation
myThreeUtrDB <- myThreeUtrDB[myThreeUtrDB$start == myThreeUtrDB$txend,]
# remove the identical 5'UTR annotations
myThreeUtrDB <- myThreeUtrDB[!duplicated(myThreeUtrDB[,-1]),]
# keep only the most complete annotation
myThreeUtrDB <- do.call('rbind',lapply(split(myThreeUtrDB,myThreeUtrDB$GENEID), function(x) x[which.max(x$exons),]))
# create the GRanges
myThreeUtrGRminus <- threeUtrGR[myThreeUtrDB$TXNAME]
names(myThreeUtrGRminus) <- myThreeUtrDB$GENEID
#### concatenate
myThreeUtrGRExons <- c(myThreeUtrGRplus, myThreeUtrGRminus)
#######################
## 3'UTR - introns ######
#####################
print("4/6 : 3'UTR/introns...")
myThreeUtrGRIntrons <- psetdiff(unlist(range(myThreeUtrGRExons)),myThreeUtrGRExons)
myThreeUtrGRIntrons <- myThreeUtrGRIntrons[elementNROWS(myThreeUtrGRIntrons)>0]
######################
## Coding - regions ######
#######################
myCodingRegions <- myGenetx
myFiveUtrGenes <- names(myFiveUtrGRExons)
myCodingRegions[myFiveUtrGenes] <- psetdiff(myCodingRegions[myFiveUtrGenes],unlist(range(myFiveUtrGRExons)))
myThreeUtrGenes <- names(myThreeUtrGRExons)
myCodingRegions[myThreeUtrGenes] <- psetdiff(myCodingRegions[myThreeUtrGenes],unlist(range(myThreeUtrGRExons)))
####################
## Coding - exons ######
###################
print('5/6 : Creating Coding/exons...')
myExonsGR <- disjoin(exonsBy(txdb,'gene'))[names(myCodingRegions)]
myCDSExonsGR <- pintersect(myExonsGR,myCodingRegions,drop.nohit.ranges=TRUE)
######################
## Coding - introns ######
#######################
print('6/6 : Creating Coding/introns...')
myCDSIntronsGR <- psetdiff(unlist(range(myCDSExonsGR)),myCDSExonsGR)
myCDSIntronsGR <- myCDSIntronsGR[elementNROWS(myCDSIntronsGR)>0]
############
## Gather ######
#############
print('Creating empty GRangesList... (This step could be long, up to 2-3 minutes)')
emptyGRList <- GRangesList(sapply(unambiguousTx, function(x) GRanges()))
# user system elapsed
# 139.860 0.000 139.869
#+ 31.696 0.420 32.126
threeUTRintrons <- threeUTRexons <- CDSintrons <- CDSexons <- fiveUTRintrons <- fiveUTRexons <- emptyGRList
fiveUTRexons[names(myFiveUtrGRExons)] <- myFiveUtrGRExons
fiveUTRintrons[names(myFiveUtrGRIntrons)] <- myFiveUtrGRIntrons
CDSexons[names(myCDSExonsGR)] <- myCDSExonsGR
CDSintrons[names(myCDSIntronsGR)] <- myCDSIntronsGR
threeUTRexons[names(myThreeUtrGRExons)] <- myThreeUtrGRExons
threeUTRintrons[names(myThreeUtrGRIntrons)] <- myThreeUtrGRIntrons
txAnnotation <- list(
"fiveUTRexons"=fiveUTRexons,
"fiveUTRintrons"=fiveUTRintrons,
"CDSexons"=CDSexons,
"CDSintrons"=CDSintrons,
"threeUTRexons"=threeUTRexons,
"threeUTRintrons"=threeUTRintrons
)
print('Done.')
return(txAnnotation)
}
###### gene length
## returns the length of the genomic region region associated to each gene,
## as defined in "make_annotation" function
## Arguments:
## - "annotation" can be either "singleChr" or "genome"
## - "singleChrName" must specify chromosome name (in case "singleChr" is selected)
gene_length <- function(annotation, singleChrName) {
message('Creating annotation...')
reduceLevels <- function(grange) {
grange@seqnames <- droplevels(grange@seqnames)
grange@seqinfo <- grange@seqinfo[levels(grange@seqnames)]
grange }
library(TxDb.Mmusculus.UCSC.mm9.knownGene)
txdb <- TxDb.Mmusculus.UCSC.mm9.knownGene
tx <- transcripts(txdb)
names(tx) <- select(txdb, keys = tx$tx_name
, keytype = "TXNAME", columns = "GENEID")$GENEID
if( annotation == 'singleChr' )
{######## single chromosome
tx <- reduceLevels(tx[seqnames(tx)==singleChrName])
}
# remove NA named genes
tx <- tx[!is.na(names(tx))]
######## select a unique feature per each gene
tx <- reduce(split(tx, names(tx)))
# remove genes which still have more than one transcript after reducing...
tx <- unlist(tx[elementNROWS(tx)==1])
gl <- width(tx)
names(gl) <- names(tx)
return(gl)
}