library(AnnotationHub)
library(ensembldb)
#To avoid prompting from annotation hub
setAnnotationHubOption("CACHE", "./AnnotationHub")
setAnnotationHubOption("ASK", FALSE)
# Connect to AnnotationHub
ah <- AnnotationHub()Genomic annotations for functional analyses
Approximate time: 30 minutes
Learning Objectives
- Discuss the available genomic annotation databases and the different types if information stored
- Compare and contrast the tools available for accessing genomic annotation databases
Genomic annotations
The analysis of next-generation sequencing results requires associating genes, transcripts, proteins, etc. with functional or regulatory information. To perform functional analysis on gene lists, we often need to obtain gene identifiers that are compatible with the tools we wish to use and this is not always trivial.
Databases
We retrieve information on the processes, pathways, etc. (for which a gene is involved in) from the necessary database where the information is stored. The database you choose will be dependent on what type of information you are trying to obtain. Examples of databases that are often queried, include:
General databases
Offer comprehensive information on genome features, feature coordinates, homology, variant information, phenotypes, protein domain/family information, associated biological processes/pathways, associated microRNAs, etc.:
- Ensembl (use Ensembl gene IDs)
- NCBI (use Entrez gene IDs)
- UCSC
- EMBL-EBI
Annotation-specific databases
Provide annotations related to a specific topic:
- Gene Ontology (GO): database of gene ontology biological processes, cellular components and molecular functions - based on Ensembl or Entrez gene IDs or official gene symbols
- KEGG: database of biological pathways - based on Entrez gene IDs
- MSigDB: database of gene sets
- Reactome: database of biological pathways
- Human Phenotype Ontology: database of genes associated with human disease
- CORUM: database of protein complexes for human, mouse, rat
- …
Tools for accessing databases
Within R, there are many popular packages used for gene/transcript-level annotation. These packages provide tools that take the list of genes you provide and retrieve information for each gene using one or more of the databases listed above.
Annotation tools: for accessing/querying annotations from a specific databases
| Tool | Description | Pros | Cons |
|---|---|---|---|
| org.Xx.eg.db | Query gene feature information for the organism of interest | gene ID conversion, biotype and coordinate information | only latest genome build available |
| EnsDb.Xx.vxx | Transcript and gene-level information directly fetched from Ensembl API (similar to TxDb, but with filtering ability and versioned by Ensembl release) | easy functions to extract features, direct filtering | Not the most up-to-date annotations, more difficult to use than some packages |
| TxDb.Xx.UCSC.hgxx.knownGene | UCSC database for transcript and gene-level information or can create own TxDb from an SQLite database file using the GenomicFeatures package | feature information, easy functions to extract features | only available current and recent genome builds - can create your own, less up-to-date with annotations than Ensembl |
| annotables | Gene-level feature information immediately available for the human and model organisms | super quick and easy gene ID conversion, biotype and coordinate information | static resource, not updated regularly |
| biomaRt | An R package version of the Ensembl BioMart online tool | all Ensembl database information available, all organisms on Ensembl, wealth of information |
AnnotationHub
AnnotationHub is a wonderful resource for accessing genomic data or querying large collection of whole genome resources, including ENSEMBL, UCSC, ENCODE, Broad Institute, KEGG, NIH Pathway Interaction Database, etc. All of this information is stored and easily accessible by directly connecting to the database.
To get started with AnnotationHub, we first load the library and connect to the database:
To see the types of information stored inside our database, we can just type the name of the object:
# Explore the AnnotationHub object
ahAnnotationHub with 71082 records
# snapshotDate(): 2023-10-23
# $dataprovider: Ensembl, BroadInstitute, UCSC, ftp://ftp.ncbi.nlm.nih.gov/g...
# $species: Homo sapiens, Mus musculus, Drosophila melanogaster, Rattus norv...
# $rdataclass: GRanges, TwoBitFile, BigWigFile, EnsDb, Rle, OrgDb, SQLiteFil...
# additional mcols(): taxonomyid, genome, description,
# coordinate_1_based, maintainer, rdatadateadded, preparerclass, tags,
# rdatapath, sourceurl, sourcetype
# retrieve records with, e.g., 'object[["AH5012"]]'
title
AH5012 | Chromosome Band
AH5013 | STS Markers
AH5014 | FISH Clones
AH5015 | Recomb Rate
AH5016 | ENCODE Pilot
... ...
AH116479 | Ensembl 111 EnsDb for Xiphophorus maculatus
AH116480 | Ensembl 111 EnsDb for Xenopus tropicalis
AH116481 | Ensembl 111 EnsDb for Zonotrichia albicollis
AH116482 | Ensembl 111 EnsDb for Zalophus californianus
AH116483 | Ensembl 111 EnsDb for Zosterops lateralis melanops
If you would like to see more information about any of the classes of data you can extract that information as well. For example, if you wanted to determine all species information available, you could explore that within the AnnotationHub object:
# Explore all species information available
unique(ah$species) %>% head()[1] "Homo sapiens" "Vicugna pacos" "Dasypus novemcinctus"
[4] "Otolemur garnettii" "Papio hamadryas" "Papio anubis"
In addition to species information, there is also additional information about the type of Data Objects and the Data Providers:
# Explore the types of Data Objects available
unique(ah$rdataclass) %>% head()[1] "GRanges" "data.frame" "Inparanoid8Db" "TwoBitFile"
[5] "ChainFile" "SQLiteConnection"
# Explore the Data Providers
unique(ah$dataprovider) %>% head()[1] "UCSC" "Ensembl" "RefNet" "Inparanoid8" "NHLBI"
[6] "ChEA"
Now that we know the types of information available from AnnotationHub we can query it for the information we want using the query() function. Let’s say we would like to return the Ensembl EnsDb information for Human. To return the records available, we need to use the terms as they are output from the ah object to extract the desired data.
# Query AnnotationHub
human_ens <- query(ah, c("Homo sapiens", "EnsDb"))The query retrieves all hits for the EnsDb objects, and you will see that they are listed by the release number. The most current release for GRCh38 is Ensembl 109 and AnnotationHub offers that as an option to use. However, if you look at options for older releases, for Homo sapiens it only goes back as far as Ensembl 87. This is fine if you are using GRCh38, however if you were using an older genome build like hg19/GRCh37, you would need to load the EnsDb package if available for that release or you might need to build your own with ensembldb.
human_ensAnnotationHub with 26 records
# snapshotDate(): 2023-10-23
# $dataprovider: Ensembl
# $species: Homo sapiens
# $rdataclass: EnsDb
# additional mcols(): taxonomyid, genome, description,
# coordinate_1_based, maintainer, rdatadateadded, preparerclass, tags,
# rdatapath, sourceurl, sourcetype
# retrieve records with, e.g., 'object[["AH53211"]]'
title
AH53211 | Ensembl 87 EnsDb for Homo Sapiens
AH53715 | Ensembl 88 EnsDb for Homo Sapiens
AH56681 | Ensembl 89 EnsDb for Homo Sapiens
AH57757 | Ensembl 90 EnsDb for Homo Sapiens
AH60773 | Ensembl 91 EnsDb for Homo Sapiens
... ...
AH104864 | Ensembl 107 EnsDb for Homo sapiens
AH109336 | Ensembl 108 EnsDb for Homo sapiens
AH109606 | Ensembl 109 EnsDb for Homo sapiens
AH113665 | Ensembl 110 EnsDb for Homo sapiens
AH116291 | Ensembl 111 EnsDb for Homo sapiens
If we are looking for the latest Ensembl release, so that the annotations are the most up-to-date, we can use the AnnotationHub ID to subset the object:
# Extract annotations of interest
human_ens <- human_ens[[length(human_ens)]]Now we can use ensembldb functions to extract the information at the gene, transcript, or exon levels. We are interested in the gene-level annotations, so we can extract that information as follows:
# Extract gene-level information
genes(human_ens, return.type = "data.frame") %>% head()But note that it is just as easy to get the transcript- or exon-level information:
# Extract transcript-level information
transcripts(human_ens, return.type = "data.frame") %>% head()# Extract exon-level information
exons(human_ens, return.type = "data.frame") %>% head()To obtain an annotation data frame using AnnotationHub, we’ll use the genes() function, but only keep selected columns and filter out rows to keep those corresponding to our gene identifiers in our results file:
# Create a gene-level dataframe
annotations_ahb <- genes(human_ens, return.type = "data.frame") %>%
dplyr::select(gene_id, gene_name, entrezid, gene_biotype, description) %>%
dplyr::filter(gene_id %in% res_tableCont_tb$gene)This dataframe looks like it should be fine as it is, but we look a little closer we will notice that the column containing Entrez identifiers is a list, and in fact there are many Ensembl identifiers that map to more than one Entrez identifier!
# Wait a second, we don't have one-to-one mappings!
class(annotations_ahb$entrezid)[1] "list"
which(map(annotations_ahb$entrezid, length) > 1)ENSG00000290825 ENSG00000237613 ENSG00000177133 ENSG00000239149 ENSG00000229571
1 4 148 315 330
ENSG00000158747 ENSG00000233008 ENSG00000137962 ENSG00000187733 ENSG00000277610
465 1482 1630 1720 1981
ENSG00000263513 ENSG00000232527 ENSG00000288905 ENSG00000117262 ENSG00000188092
2018 2022 2038 2058 2119
ENSG00000291158 ENSG00000143384 ENSG00000163156 ENSG00000162747 ENSG00000120341
2169 2213 2245 2628 2830
ENSG00000230124 ENSG00000199396 ENSG00000199270 ENSG00000201925 ENSG00000203668
2871 3474 3475 3476 3643
ENSG00000242147 ENSG00000230417 ENSG00000166272 ENSG00000171714 ENSG00000281880
3808 4561 4922 5685 5737
ENSG00000229183 ENSG00000254911 ENSG00000245552 ENSG00000109927 ENSG00000149557
6040 6727 6759 7066 7125
ENSG00000151067 ENSG00000111671 ENSG00000213809 ENSG00000111215 ENSG00000212127
7252 7373 7521 7539 7544
ENSG00000255374 ENSG00000178882 ENSG00000215483 ENSG00000176124 ENSG00000186047
7553 9099 9515 9656 9664
ENSG00000206588 ENSG00000214900 ENSG00000258537 ENSG00000257365 ENSG00000223403
10381 10518 10549 10745 11223
ENSG00000258710 ENSG00000183629 ENSG00000207432 ENSG00000261247 ENSG00000178115
11366 11438 11474 11475 11494
ENSG00000207430 ENSG00000137843 ENSG00000237289 ENSG00000159289 ENSG00000167195
11497 11637 11764 12253 12297
ENSG00000278662 ENSG00000230373 ENSG00000290824 ENSG00000290355 ENSG00000069764
12465 12532 12809 12810 13220
ENSG00000224712 ENSG00000166780 ENSG00000185164 ENSG00000184110 ENSG00000181625
13222 13244 13289 13469 13511
ENSG00000213648 ENSG00000169627 ENSG00000132207 ENSG00000261052 ENSG00000090857
13512 13557 13558 13560 14083
ENSG00000157335 ENSG00000157429 ENSG00000196436 ENSG00000260788 ENSG00000260279
14084 14114 14169 14294 14438
ENSG00000197912 ENSG00000286190 ENSG00000171916 ENSG00000154016 ENSG00000233098
14443 14655 14997 15022 15101
ENSG00000126861 ENSG00000274808 ENSG00000274512 ENSG00000273709 ENSG00000263715
15293 15416 15459 15679 15783
ENSG00000120088 ENSG00000228696 ENSG00000238083 ENSG00000108379 ENSG00000267452
15785 15795 15800 15805 15970
ENSG00000287664 ENSG00000136488 ENSG00000227036 ENSG00000175711 ENSG00000173213
16084 16185 16312 16659 16670
ENSG00000266256 ENSG00000188629 ENSG00000213999 ENSG00000205076 ENSG00000178934
17327 17781 18194 18640 18641
ENSG00000213904 ENSG00000231924 ENSG00000160336 ENSG00000204577 ENSG00000267710
18801 18811 19328 19364 19475
ENSG00000228262 ENSG00000115239 ENSG00000228590 ENSG00000186854 ENSG00000291150
19994 20169 20219 20525 20627
ENSG00000143429 ENSG00000144015 ENSG00000186281 ENSG00000015568 ENSG00000144063
20628 20667 20673 20851 20858
ENSG00000290614 ENSG00000136698 ENSG00000236107 ENSG00000213160 ENSG00000237298
21079 21081 21331 21357 21489
ENSG00000185176 ENSG00000205611 ENSG00000088298 ENSG00000168746 ENSG00000215440
22175 22570 22668 22809 23015
ENSG00000275496 ENSG00000278903 ENSG00000291050 ENSG00000274391 ENSG00000229425
23153 23154 23188 23204 23230
ENSG00000156273 ENSG00000205670 ENSG00000222018 ENSG00000243627 ENSG00000221398
23301 23388 23389 23391 23394
ENSG00000159216 ENSG00000184012 ENSG00000160200 ENSG00000160201 ENSG00000160202
23399 23488 23522 23523 23526
ENSG00000188660 ENSG00000234289 ENSG00000160223 ENSG00000288811 ENSG00000128383
23529 23533 23550 23707 24281
ENSG00000197182 ENSG00000144455 ENSG00000231304 ENSG00000186448 ENSG00000283154
24462 24568 24775 25011 26228
ENSG00000250302 ENSG00000245293 ENSG00000251600 ENSG00000151611 ENSG00000198948
27173 27623 27870 27890 28057
ENSG00000281720 ENSG00000260596 ENSG00000205572 ENSG00000205571 ENSG00000172062
28221 28222 28784 28785 28799
ENSG00000145736 ENSG00000134986 ENSG00000120709 ENSG00000247199 ENSG00000249738
28802 29171 29457 29679 29815
ENSG00000278970 ENSG00000112679 ENSG00000145979 ENSG00000228223 ENSG00000241370
30085 30117 30295 30467 30655
ENSG00000146112 ENSG00000244355 ENSG00000244731 ENSG00000204314 ENSG00000237541
30672 30754 30783 30793 30815
ENSG00000204209 ENSG00000124713 ENSG00000146147 ENSG00000065615 ENSG00000111850
30860 31036 31195 31388 31419
ENSG00000112541 ENSG00000290751 ENSG00000155026 ENSG00000105889 ENSG00000196683
32110 32147 32305 32436 32443
ENSG00000152926 ENSG00000291154 ENSG00000291153 ENSG00000262461 ENSG00000274570
32909 32978 32980 33009 33011
ENSG00000178809 ENSG00000186645 ENSG00000105793 ENSG00000004866 ENSG00000224897
33077 33120 33205 33590 33645
ENSG00000230549 ENSG00000236125 ENSG00000215372 ENSG00000255251 ENSG00000285765
34222 34223 34225 34227 34228
ENSG00000285950 ENSG00000255378 ENSG00000186562 ENSG00000237038 ENSG00000225327
34235 34236 34237 34239 34240
ENSG00000254866 ENSG00000145002 ENSG00000076554 ENSG00000229140 ENSG00000181135
34326 34337 34970 35386 35507
ENSG00000181404 ENSG00000180071 ENSG00000287838 ENSG00000283378 ENSG00000260691
35587 36005 36013 36072 36149
ENSG00000196873 ENSG00000234394 ENSG00000158169 ENSG00000225194 ENSG00000234323
36151 36154 36426 36440 36567
ENSG00000286112 ENSG00000233016 ENSG00000273730 ENSG00000277626 ENSG00000277641
36848 37010 37096 37123 37124
ENSG00000288357 ENSG00000288487 ENSG00000288206 ENSG00000233192 ENSG00000257473
37153 37154 37155 37264 37284
ENSG00000227046 ENSG00000223793 ENSG00000206206 ENSG00000231823 ENSG00000206301
37288 37302 37307 37324 37344
ENSG00000231617 ENSG00000225103 ENSG00000206279 ENSG00000231526 ENSG00000229396
37349 37367 37372 37391 37396
ENSG00000268009 ENSG00000269791 ENSG00000236362 ENSG00000189064 ENSG00000179304
37823 37824 37878 37879 37916
ENSG00000268350 ENSG00000158301 ENSG00000269226 ENSG00000101883 ENSG00000267978
37918 38260 38308 38443 38658
ENSG00000221867 ENSG00000268902 ENSG00000197172 ENSG00000235961 ENSG00000063587
38696 38697 38701 38708 38713
ENSG00000224533 ENSG00000099715 ENSG00000114374 ENSG00000291016
38796 38811 38826 38841
So what do we do here? And why do we have this problem? An answer from the Ensembl Help Desk is that this occurs when we cannot choose a perfect match; ie when we have two good matches, but one does not appear to match with a better percentage than the other. In that case, we assign both matches. What we will do is choose to keep the first identifier for these multiple mapping cases.
annotations_ahb$entrezid <- map(annotations_ahb$entrezid,1) %>% unlist()Let’s take a look and see how many of our Ensembl identifiers have an associated gene symbol, and how many of them are unique:
sum(is.na(annotations_ahb$gene_name))[1] 0
sum(duplicated(annotations_ahb$gene_name))[1] 12290
Let’s identify the non-duplicated genes and only keep the ones that are not duplicated:
# Determine the indices for the non-duplicated genes
non_duplicates_idx <- which(duplicated(annotations_ahb$gene_name) == FALSE)
# How many rows does annotations_ahb have?
annotations_ahb %>% nrow()[1] 38844
# Return only the non-duplicated genes using indices
annotations_ahb <- annotations_ahb[non_duplicates_idx, ]
# How many rows are we left with after removing?
annotations_ahb %>% nrow()[1] 26554
Finally, it would be good to know what proportion of the Ensembl identifiers map to an Entrez identifier:
# Determine how many of the Entrez column entries are NA
which(is.na(annotations_ahb$entrezid)) %>% length()[1] 7279
For the different steps in the functional analysis, we require Ensembl and Entrez IDs. We will use the gene annotations that we generated previously to merge with our differential expression results.
## Merge the AnnotationHub dataframe with the results
res_ids_ahb <- inner_join(res_tableCont_tb, annotations_ahb, by=c("gene"="gene_id")) Using AnnotationHub to create a tx2gene file
If you wish to easily translate between genes and transcripts (this depends on how the count matrix was computed), it would be wise to create a ‘transcript to gene’ translation file (or tx2gene file). In our case, we already have one tx2gene table generated by our pipeline, but sometimes you may not have access to an tx2gene file already, so it is useful to learn how to create such a file To create it, we would need to use a combination of the methods above and merge two dataframes together. For example:
## DO NOT RUN THIS CODE
# Create a transcript dataframe
txdb <- transcripts(human_ens, return.type = "data.frame") %>%
dplyr::select(tx_id, gene_id)
txdb <- txdb[grep("ENST", txdb$tx_id),]
# Create a gene-level dataframe
genedb <- genes(human_ens, return.type = "data.frame") %>%
dplyr::select(gene_id, gene_name)
# Merge the two dataframes together
annotations <- inner_join(txdb, genedb)Annotables package
The annotables package is a super easy annotation package to use. It is not updated frequently, so it’s not great for getting the most up-to-date information for the current builds and does not have information for other organisms than human and mouse, but is a quick way to get annotation information, specially for older builds.
library(annotables)
grch38We can see that the grch37 object already contains all the information we want in a super easy way. Let’s annotate the results of our shrunken DEA for our Control vs Vampirium comparison:
## Re-run this code if you are unsure that you have the right table
res_tableCont <- lfcShrink(dds, coef = "condition_control_vs_vampirium")
res_tableCont_tb <- res_tableCont %>%
data.frame() %>%
rownames_to_column(var="gene") %>%
as_tibble()## Return the IDs for the gene symbols in the DE results
ids <- grch38 %>% dplyr::filter(ensgene %in% rownames(res_tableCont))
## Merge the IDs with the results
res_ids <- inner_join(res_tableCont_tb, ids, by=c("gene"="ensgene"))
head(res_ids)Our data is now ready to use for functional analysis! We have all the ids necessary to proceed.
Exercise 1
- Create a new
res_idsobject using theannotablespackage with the human build grch37. NOTE call itres_ids_grch37! - What are the differences between the
res_id_ahbobject and theres_ids_grch37?
Exercise 2
Annotate the results of your DEA for Garlicum vs Vampirium with grch38.