knitr::opts_chunk$set(echo = TRUE)
suppressPackageStartupMessages(library(GenomicFeatures))
## Warning: package 'GenomicFeatures' was built under R version 4.3.3
## Warning: package 'GenomeInfoDb' was built under R version 4.3.3
suppressPackageStartupMessages(library(TxDb.Hsapiens.UCSC.hg19.knownGene))
suppressPackageStartupMessages(library(BSgenome.Hsapiens.UCSC.hg19))
## Warning: package 'Biostrings' was built under R version 4.3.3
suppressPackageStartupMessages(library(lubridate))
## Warning: package 'lubridate' was built under R version 4.3.3
There are three ways that users can obtain a TxDb object.
One way is to use the loadDb method to load the object directly from an appropriate .sqlite database file.
Here we are loading a previously created TxDb object based on UCSC known gene data. This database only contains a small subset of the possible annotations for human and is only included to demonstrate and test the functionality of the GenomicFeatures package as a demonstration.
samplefile <- system.file("extdata", "hg19_knownGene_sample.sqlite",
package="GenomicFeatures")
txdb <- loadDb(samplefile)
txdb
## TxDb object:
## # Db type: TxDb
## # Supporting package: GenomicFeatures
## # Data source: UCSC
## # Genome: hg19
## # Organism: Homo sapiens
## # UCSC Table: knownGene
## # Resource URL: http://genome.ucsc.edu/
## # Type of Gene ID: Entrez Gene ID
## # Full dataset: no
## # miRBase build ID: NA
## # transcript_nrow: 178
## # exon_nrow: 620
## # cds_nrow: 523
## # Db created by: GenomicFeatures package from Bioconductor
## # Creation time: 2014-10-08 10:31:15 -0700 (Wed, 08 Oct 2014)
## # GenomicFeatures version at creation time: 1.17.21
## # RSQLite version at creation time: 0.11.4
## # DBSCHEMAVERSION: 1.0
In this case, the TxDb object has been returned by the loadDb method.
More commonly however, we expect that users will just load a TxDb annotation package like this
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
hg19_txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene
hg19_txdb
## TxDb object:
## # Db type: TxDb
## # Supporting package: GenomicFeatures
## # Data source: UCSC
## # Genome: hg19
## # Organism: Homo sapiens
## # Taxonomy ID: 9606
## # UCSC Table: knownGene
## # Resource URL: http://genome.ucsc.edu/
## # Type of Gene ID: Entrez Gene ID
## # Full dataset: yes
## # miRBase build ID: GRCh37
## # transcript_nrow: 82960
## # exon_nrow: 289969
## # cds_nrow: 237533
## # Db created by: GenomicFeatures package from Bioconductor
## # Creation time: 2015-10-07 18:11:28 +0000 (Wed, 07 Oct 2015)
## # GenomicFeatures version at creation time: 1.21.30
## # RSQLite version at creation time: 1.0.0
## # DBSCHEMAVERSION: 1.1
Loading the package like this will also create a TxDb object, and by default that object will have the same name as the package itself.
Finally, the third way to obtain a TxDb object is to use one of the numerous tools defined in the txdbmaker package. txdbmaker provides a set of tools for making TxDb objects from genomic annotations from various sources (e.g. UCSC, Ensembl, and GFF files). See the vignette in the txdbmaker package for more information.
keys <- c("100033416", "100033417", "100033420") # Entrez gene IDs
columns(hg19_txdb)
## [1] "CDSCHROM" "CDSEND" "CDSID" "CDSNAME" "CDSSTART"
## [6] "CDSSTRAND" "EXONCHROM" "EXONEND" "EXONID" "EXONNAME"
## [11] "EXONRANK" "EXONSTART" "EXONSTRAND" "GENEID" "TXCHROM"
## [16] "TXEND" "TXID" "TXNAME" "TXSTART" "TXSTRAND"
## [21] "TXTYPE"
keytypes(hg19_txdb)
## [1] "CDSID" "CDSNAME" "EXONID" "EXONNAME" "GENEID" "TXID" "TXNAME"
select(hg19_txdb, keys = keys, columns="TXNAME", keytype="GENEID")
## 'select()' returned 1:1 mapping between keys and columns
## GENEID TXNAME
## 1 100033416 uc001yxl.4
## 2 100033417 uc001yxo.3
## 3 100033420 uc001yxr.3
Exercise: Use seqlevels to set only chromsome 15 to be active. BTW, the rest of this vignette will assume you have succeeded at this.
Solution:
seqlevels(hg19_txdb) <- "chr15"
seqlevels(hg19_txdb)
## [1] "chr15"
It is possible to filter the data that is returned from a TxDb object based on it’s chromosome. This can be a useful way to limit the things that are returned if you are only interested in studying a handful of chromosomes.
To determine which chromosomes are currently active, use the seqlevels method. For example:
head(seqlevels(hg19_txdb))
## [1] "chr15"
Will tell you all the chromosomes that are active for the TxDb.Hsapiens.UCSC.hg19.knownGene TxDb object (by default it will be all of them).
If you then wanted to only set Chromosome 1 to be active you could do it like this:
seqlevels(hg19_txdb) <- seqlevels0(hg19_txdb)
##Retrieving data using the select() method The TxDb objects inherit from AnnotationDb objects (just as the ChipDb and OrgDb objects do). One of the implications of this relationship is that these object ought to be used in similar ways to each other. Therefore we have written supporting columns, keytypes, keys and select methods for TxDb objects.
These methods can be a useful way of extracting data from a TxDb object. And they are used in the same way that they would be used to extract information about a ChipDb or an OrgDb object. Here is a simple example of how to find the UCSC transcript names that match with a set of gene IDs.
keys <- c("100033416", "100033417", "100033420")
columns(hg19_txdb)
## [1] "CDSCHROM" "CDSEND" "CDSID" "CDSNAME" "CDSSTART"
## [6] "CDSSTRAND" "EXONCHROM" "EXONEND" "EXONID" "EXONNAME"
## [11] "EXONRANK" "EXONSTART" "EXONSTRAND" "GENEID" "TXCHROM"
## [16] "TXEND" "TXID" "TXNAME" "TXSTART" "TXSTRAND"
## [21] "TXTYPE"
keytypes(hg19_txdb)
## [1] "CDSID" "CDSNAME" "EXONID" "EXONNAME" "GENEID" "TXID" "TXNAME"
select(hg19_txdb, keys = keys, columns="TXNAME", keytype="GENEID")
## 'select()' returned 1:1 mapping between keys and columns
## GENEID TXNAME
## 1 100033416 uc001yxl.4
## 2 100033417 uc001yxo.3
## 3 100033420 uc001yxr.3
Exercise: For the genes in the example above, find the chromosome and strand information that will go with each of the transcript names.
Solution:
columns(hg19_txdb)
## [1] "CDSCHROM" "CDSEND" "CDSID" "CDSNAME" "CDSSTART"
## [6] "CDSSTRAND" "EXONCHROM" "EXONEND" "EXONID" "EXONNAME"
## [11] "EXONRANK" "EXONSTART" "EXONSTRAND" "GENEID" "TXCHROM"
## [16] "TXEND" "TXID" "TXNAME" "TXSTART" "TXSTRAND"
## [21] "TXTYPE"
cols <- c("TXNAME", "TXSTRAND", "TXCHROM")
select(hg19_txdb, keys=keys, columns=cols, keytype="GENEID")
## 'select()' returned 1:1 mapping between keys and columns
## GENEID TXNAME TXCHROM TXSTRAND
## 1 100033416 uc001yxl.4 chr15 +
## 2 100033417 uc001yxo.3 chr15 +
## 3 100033420 uc001yxr.3 chr15 +
##Methods for returning GRanges objects Retrieving data with select is useful, but sometimes it is more convenient to extract the result as a GRanges object. This is often the case when you are doing counting or specialized overlap operations downstream. For these use cases there is another family of methods available.
Perhaps the most common operations for a TxDb object is to retrieve the genomic coordinates or ranges for exons, transcripts or coding sequences. The functions transcripts, exons, and cds return the coordinate information as a GRanges object.
As an example, all transcripts present in a TxDb object can be obtained as follows:
GR <- transcripts(hg19_txdb)
GR[1:3]
## GRanges object with 3 ranges and 2 metadata columns:
## seqnames ranges strand | tx_id tx_name
## <Rle> <IRanges> <Rle> | <integer> <character>
## [1] chr1 11874-14409 + | 1 uc001aaa.3
## [2] chr1 11874-14409 + | 2 uc010nxq.1
## [3] chr1 11874-14409 + | 3 uc010nxr.1
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome
The transcripts function returns a GRanges class object. You can learn a lot more about the manipulation of these objects by reading the GenomicRanges introductory vignette. The show method for a GRanges object will display the ranges, seqnames (a chromosome or a contig), and strand on the left side and then present related metadata on the right side.
The strand function is used to obtain the strand information from the transcripts. The sum of the Lengths of the Rle object that strand returns is equal to the length of the GRanges object.
tx_strand <- strand(GR)
tx_strand
## factor-Rle of length 82960 with 94 runs
## Lengths: 4073 3894 2642 2450 2179 2149 1474 ... 13 2 1 2 2 1
## Values : + - + - + - + ... + - + - + -
## Levels(3): + - *
sum(runLength(tx_strand))
## [1] 82960
length(GR)
## [1] 82960
In addition, the transcripts function can also be used to retrieve a subset of the transcripts available such as those on the +-strand of chromosome 1.
GR <- transcripts(hg19_txdb, filter=list(tx_chrom = "chr15", tx_strand = "+"))
length(GR)
## [1] 1732
unique(strand(GR))
## [1] +
## Levels: + - *
The exons and cds functions can also be used in a similar fashion to retrive genomic coordinates for exons and coding sequences.
The promoters function computes a GRanges object that spans the promoter region around the transcription start site for the transcripts in a TxDb object. The upstream and downstream arguments define the number of bases upstream and downstream from the transcription start site that make up the promoter region.
PR <- promoters(hg19_txdb, upstream=2000, downstream=400)
## Warning in valid.GenomicRanges.seqinfo(x, suggest.trim = TRUE): GRanges object contains 1 out-of-bound range located on sequence
## chrUn_gl000223. Note that ranges located on a sequence whose length is
## unknown (NA) or on a circular sequence are not considered out-of-bound
## (use seqlengths() and isCircular() to get the lengths and circularity
## flags of the underlying sequences). You can use trim() to trim these
## ranges. See ?`trim,GenomicRanges-method` for more information.
PR
## GRanges object with 82960 ranges and 2 metadata columns:
## seqnames ranges strand | tx_id tx_name
## <Rle> <IRanges> <Rle> | <integer> <character>
## uc001aaa.3 chr1 9874-12273 + | 1 uc001aaa.3
## uc010nxq.1 chr1 9874-12273 + | 2 uc010nxq.1
## uc010nxr.1 chr1 9874-12273 + | 3 uc010nxr.1
## uc001aal.1 chr1 67091-69490 + | 4 uc001aal.1
## uc001aaq.2 chr1 319084-321483 + | 5 uc001aaq.2
## ... ... ... ... . ... ...
## uc011mgu.1 chrUn_gl000237 2287-4686 - | 82956 uc011mgu.1
## uc011mgv.2 chrUn_gl000241 36476-38875 - | 82957 uc011mgv.2
## uc011mgw.1 chrUn_gl000243 9501-11900 + | 82958 uc011mgw.1
## uc022brq.1 chrUn_gl000243 11608-14007 + | 82959 uc022brq.1
## uc022brr.1 chrUn_gl000247 5417-7816 - | 82960 uc022brr.1
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome
A similar function (terminators) is provided to compute the terminator region around the transcription end site for the transcripts in a TxDb object.
Exercise: Use exons to retrieve all the exons from chromosome 15. How does the length of this compare to the value returned by transcripts? Solution:
EX <- exons(hg19_txdb)
EX[1:4]
## GRanges object with 4 ranges and 1 metadata column:
## seqnames ranges strand | exon_id
## <Rle> <IRanges> <Rle> | <integer>
## [1] chr1 11874-12227 + | 1
## [2] chr1 12595-12721 + | 2
## [3] chr1 12613-12721 + | 3
## [4] chr1 12646-12697 + | 4
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome
length(EX)
## [1] 289969
length(GR)
## [1] 1732
Often one is interested in how particular genomic features relate to each other, and not just their genomic positions. For example, it might be of interest to group transcripts by gene or to group exons by transcript. Such groupings are supported by the transcriptsBy, exonsBy, and cdsBy functions.
The following call can be used to group transcripts by genes:
GRList <- transcriptsBy(hg19_txdb, by = "gene")
length(GRList)
## [1] 23459
names(GRList)[10:13]
## [1] "10003" "100033413" "100033414" "100033415"
GRList[11:12]
## GRangesList object of length 2:
## $`100033413`
## GRanges object with 1 range and 2 metadata columns:
## seqnames ranges strand | tx_id tx_name
## <Rle> <IRanges> <Rle> | <integer> <character>
## [1] chr15 25296623-25296719 + | 53623 uc001yxg.3
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome
##
## $`100033414`
## GRanges object with 2 ranges and 2 metadata columns:
## seqnames ranges strand | tx_id tx_name
## <Rle> <IRanges> <Rle> | <integer> <character>
## [1] chr15 25299356-25299452 + | 53625 uc001yxi.3
## [2] chr15 25310172-25313030 + | 53632 uc001yxp.4
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome
The transcriptsBy function returns a GRangesList class object. As with GRanges objects, you can learn more about these objects by reading the GenomicRanges introductory vignette. The show method for a GRangesList object will display as a list of GRanges objects. And, at the bottom the seqinfo will be displayed once for the entire list.
For each of these three functions, there is a limited set of options that can be passed into the by argument to allow grouping. For the transcriptsBy function, you can group by gene, exon or cds, whereas for the exonsBy and cdsBy functions can only be grouped by transcript (tx) or gene.
So as a further example, to extract all the exons for each transcript you can call:
GRList <- exonsBy(hg19_txdb, by = "tx")
length(GRList)
## [1] 82960
names(GRList)[10:13]
## [1] "10" "11" "12" "13"
GRList[[12]]
## GRanges object with 3 ranges and 3 metadata columns:
## seqnames ranges strand | exon_id exon_name exon_rank
## <Rle> <IRanges> <Rle> | <integer> <character> <integer>
## [1] chr1 420206-420296 + | 20 <NA> 1
## [2] chr1 420992-421258 + | 21 <NA> 2
## [3] chr1 421396-421839 + | 22 <NA> 3
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome
As you can see, the GRangesList objects returned from each function contain genomic positions and identifiers grouped into a list-like object according to the type of feature specified in the by argument. The object returned can then be used by functions like findOverlaps to contextualize alignments from high-throughput sequencing.
The identifiers used to label the GRanges objects depend upon the data source used to create the TxDb object. So the list identifiers will not always be Entrez Gene IDs, as they were in the first example. Furthermore, some data sources do not provide a unique identifier for all features. In this situation, the group label will be a synthetic ID created by GenomicFeatures to keep the relations between features consistent in the database this was the case in the 2nd example. Even though the results will sometimes have to come back to you as synthetic IDs, you can still always retrieve the original IDs.
Exercise: Starting with the tx_ids that are the names of the GRList object we just made, use select to retrieve that matching transcript names. Remember that the list used a by argument = “tx”, so the list is grouped by transcript IDs.
Solution:
GRList <- exonsBy(hg19_txdb, by = "tx")
tx_ids <- names(GRList)
head(select(hg19_txdb, keys=tx_ids, columns="TXNAME", keytype="TXID"))
## 'select()' returned 1:1 mapping between keys and columns
## TXID TXNAME
## 1 1 uc001aaa.3
## 2 2 uc010nxq.1
## 3 3 uc010nxr.1
## 4 4 uc001aal.1
## 5 5 uc001aaq.2
## 6 6 uc001aar.2
Finally, the order of the results in a GRangesList object can vary with the way in which things were grouped. In most cases the grouped elements of the GRangesList object will be listed in the order that they occurred along the chromosome. However, when exons or CDS parts are grouped by transcript, they will instead be grouped according to their position along the transcript itself. This is important because alternative splicing can mean that the order along the transcript can be different from that along the chromosome.
##Predefined grouping functions The intronsByTranscript, fiveUTRsByTranscript and threeUTRsByTranscript are convenience functions that provide behavior equivalent to the grouping functions, but in prespecified form. These functions return a GRangesList object grouped by transcript for introns, 5’ UTR’s, and 3’ UTR’s, respectively. Below are examples of how you can call these methods.
length(intronsByTranscript(hg19_txdb))
## [1] 82960
length(fiveUTRsByTranscript(hg19_txdb))
## [1] 61495
length(threeUTRsByTranscript(hg19_txdb))
## [1] 60740
##Getting the actual sequence data The GenomicFeatures package also provides functions for converting from ranges to actual sequence (when paired with an appropriate BSgenome package).
suppressPackageStartupMessages(library(BSgenome.Hsapiens.UCSC.hg19))
genome <- BSgenome.Hsapiens.UCSC.hg19 # shorthand (for convenience)
tx_seqs1 <- extractTranscriptSeqs(genome, hg19_txdb, use.names=TRUE)
And, once these sequences have been extracted, you can translate them into proteins with translate:
suppressWarnings(translate(tx_seqs1))
## AAStringSet object of length 82960:
## width seq names
## [1] 550 LAVSLFFDLFFLFMCICCLLA...TPRRLHPAQLEILY*KHTVGF uc001aaa.3
## [2] 496 LAVSLFFDLFFLFMCICCLLA...PETFASCTARDPLLKAHCWFL uc010nxq.1
## [3] 531 LAVSLFFDLFFLFMCICCLLA...TPRRLHPAQLEILY*KHTVGF uc010nxr.1
## [4] 306 MVTEFIFLGLSDSQELQTFLF...DMKTAIRQLRKWDAHSSVKF* uc001aal.1
## [5] 10 YRPSS*LTMA uc001aaq.2
## ... ... ...
## [82956] 405 ASLGSLVSPAELLCSRLRGPG...FCLLLRRASMATDHCNLRLLR uc011mgu.1
## [82957] 245 LHF*SSPPWCCSGSHTRPEPA...TSL*KWENGFLGLK*PLY*MQ uc011mgv.2
## [82958] 10 W*ISAKVAKE uc011mgw.1
## [82959] 10 MQRWLVAL*A uc022brq.1
## [82960] 10 W*ISAKVAKE uc022brr.1
Exercise: But of course this is not a meaningful translation, because the call to extractTranscriptSeqs will have extracted all the transcribed regions of the genome regardless of whether or not they are translated. Look at the manual page for extractTranscriptSeqs and see how you can use cdsBy to only translate only the coding regions. Solution:
cds_seqs <- extractTranscriptSeqs(Hsapiens,
cdsBy(hg19_txdb, by="tx", use.names=TRUE))
translate(cds_seqs)
## AAStringSet object of length 63691:
## width seq names
## [1] 134 MSESINFSHNLGQLLSPPRCV...KGETQESVESRVLPGPRHRH* uc010nxq.1
## [2] 306 MVTEFIFLGLSDSQELQTFLF...DMKTAIRQLRKWDAHSSVKF* uc001aal.1
## [3] 390 MLLPPGSLSRPRTFSSQPLQT...CQQPQQAQLLPHSGPFRPNS* uc009vjk.2
## [4] 390 MLLPPGSLSRPRTFSSQPLQT...CQQPQQAQLLPHSGPFRPNS* uc001aau.3
## [5] 267 MAYLGPYPTSRQPPQMRLLPH...CQQPQQAQLLPHSGPFRPNS* uc021oeh.1
## ... ... ...
## [63687] 425 MALPTPSDSTLPAEARGRGRR...AASLEAPLSEEEYRALLEEL* uc031tgq.1
## [63688] 425 MALPTPSDSTLPAEARGRGRR...AASLEAPLSEEEYRALLEEL* uc031tgr.1
## [63689] 425 MALPTPSDSTLPAEARGRGRR...AASLEAPLSEEEYRALLEEL* uc031tgs.1
## [63690] 279 MGKGNEDSDLHCSSIQCSTDQ...HPFPGQEITETVSGSDEAKL* uc011mgh.2
## [63691] 280 MGKGNEDSDLHCSSIQCSTDQ...HPFPGQEITETVSGSDEAKL* uc011mgi.2