Browse code

Update toReportDF with keytype

Updating toReportDF to work with annotation keys beyond Entrez IDs

Jason Hackney authored on 22/05/2019 16:04:31
Showing 4 changed files

... ...
@@ -2,8 +2,15 @@ annotate.genes <- function(keys, annotation.db, keytype = "ENTREZID",
2 2
     columns = list(EntrezId = "ENTREZID", Symbol = "SYMBOL", 
3 3
         GeneName = "GENENAME"))
4 4
 {
5
-    if(class(annotation.db) == "character")
6
-        annotation.db <- get(annPkgName(annotation.db))
5
+    if(class(annotation.db) == "character"){
6
+        if(exists(annPkgName(annotation.db, "db"))){
7
+            annotation.db <- get(annPkgName(annotation.db, "db"))
8
+        } else if(exists(annPkgName(annotation.db, "env"))){
9
+            annotation.db <- get(annPkgName(annotation.db, "env"))
10
+        } else{
11
+            stop("Can't find annotation database:", annotation.db)
12
+        }
13
+    }
7 14
     
8 15
     ## For each column, return the values for the keys of interest
9 16
     ## using the keys and keytype provided above.
... ...
@@ -2,6 +2,7 @@
2 2
 ##' 
3 3
 ##' @param ids character vector containing gene identifiers
4 4
 ##' @param annotation.db character string containing annotation database that matches with gene identifiers
5
+##' @param keytype character string containing what kind of keys were passed in.
5 6
 check.eg.ids <- function(ids, annotation.db = 'org.Hs.eg'){
6 7
     ann.map <- getAnnMap("SYMBOL", annotation.db)
7 8
     check <- unlist(mget(ids, ann.map, ifnotfound=NA))
... ...
@@ -13,6 +14,16 @@ check.eg.ids <- function(ids, annotation.db = 'org.Hs.eg'){
13 14
 }
14 15
 
15 16
 check.ids <- function(ids, annotation.db = org.Hs.eg.db, keytype = "ENTREZID"){
17
+    if(class(annotation.db) == "character"){
18
+        if(exists(annPkgName(annotation.db, "db"))){
19
+            annotation.db <- get(annPkgName(annotation.db, "db"))
20
+        } else if(exists(annPkgName(annotation.db, "env"))){
21
+            annotation.db <- get(annPkgName(annotation.db, "env"))
22
+        } else{
23
+            stop("Can't find annotation database:", annotation.db)
24
+        }
25
+    }
26
+    
16 27
     allIds <- keys(annotation.db, keytype = keytype)
17 28
     check <- sum(ids %in% allIds)
18 29
     if(check == 0){
... ...
@@ -77,7 +77,8 @@ setMethod("toReportDF",
77 77
     signature = signature(object = "DESeqResults"),
78 78
     definition = function(object, htmlReport, DataSet = NULL, 
79 79
         annotation.db = NULL, pvalueCutoff = 0.01, lfc = 0, n = 500,
80
-        sort.by = "pvalue", make.plots = FALSE, ..., name = NULL)
80
+        sort.by = "pvalue", make.plots = FALSE, keytype = "ENTREZID", ..., 
81
+        name = NULL)
81 82
     {
82 83
         resTab <- as.data.frame(object[
83 84
             which(object$padj < pvalueCutoff & 
... ...
@@ -93,15 +94,17 @@ setMethod("toReportDF",
93 94
         if(n < nrow(resTab))
94 95
             resTab <- resTab[1:n, ]
95 96
         
96
-        ann.map.available <- tryCatch(getAnnMap("SYMBOL", annotation.db), 
97
-            error=function(e){ return(FALSE) })
97
+        ann.db.available <- !is.null(annotation.db) && 
98
+            (exists(annPkgName(annotation.db, "db")) || 
99
+                exists(annPkgName(annotation.db, "env")))
98 100
 
99
-        if (inherits(ann.map.available, "AnnDbBimap")){
101
+        if (ann.db.available){
100 102
             ## Check valid Entrez ids are passed in
101
-            check.eg.ids(rownames(resTab), annotation.db)
103
+            check.ids(rownames(resTab), annotation.db, keytype = keytype)
102 104
 
103
-            fdata <- ReportingTools:::annotate.genes(rownames(resTab), annotation.db,
104
-                keytype = "ENTREZID", columns = list(EntrezId = "ENTREZID", 
105
+            fdata <- ReportingTools:::annotate.genes(rownames(resTab), 
106
+                annotation.db, keytype = keytype, 
107
+                columns = list(EntrezId = "ENTREZID", 
105 108
                     Symbol = "SYMBOL", GeneName = "GENENAME"))
106 109
         } else {
107 110
             fdata <- data.frame(ID = rownames(resTab), stringsAsFactors = FALSE)
... ...
@@ -138,7 +141,7 @@ setMethod("toReportDF",
138 141
     definition = function(object, htmlReport, 
139 142
         contrast = NULL, resultName = NULL, annotation.db = NULL,
140 143
         pvalueCutoff = 0.01, lfc = 0, n = 500, sort.by = "pvalue", 
141
-        make.plots = FALSE, ...)
144
+        make.plots = FALSE, keytype = "ENTREZID", ...)
142 145
     {
143 146
         if (!"results" %in% mcols(mcols(object))$type) {
144 147
             stop("No results found in DESeqDataSet, please run DESeq first.")
... ...
@@ -159,15 +162,16 @@ setMethod("toReportDF",
159 162
         if(n < nrow(resTab))
160 163
             resTab <- resTab[1:n, ]
161 164
         
162
-        ann.map.available <- tryCatch(getAnnMap("SYMBOL", annotation.db), 
163
-            error=function(e){ return(FALSE) })
164
-
165
-        if (inherits(ann.map.available, "AnnDbBimap")){
165
+        ann.db.available <- !is.null(annotation.db) && 
166
+            (exists(annPkgName(annotation.db, "db")) || 
167
+                exists(annPkgName(annotation.db, "env")))
168
+                
169
+        if (ann.db.available){
166 170
             ## Check valid Entrez ids are passed in
167
-            check.eg.ids(rownames(resTab), annotation.db)
171
+            check.ids(rownames(resTab), annotation.db, keytype = keytype)
168 172
 
169 173
             fdata <- annotate.genes(rownames(resTab), annotation.db,
170
-                keytype = "ENTREZID", columns = list(EntrezId = "ENTREZID", 
174
+                keytype = keytype, columns = list(EntrezId = "ENTREZID", 
171 175
                     Symbol = "SYMBOL", GeneName = "GENENAME"))
172 176
         } else {
173 177
             fdata <- data.frame(ID = rownames(resTab), stringsAsFactors = FALSE)
... ...
@@ -25,4 +25,5 @@ test_AnnotationData <- function()
25 25
         "NA is returned for keys that have 1:many relationships")
26 26
     checkTrue(sum(ann.vals[,"Symbol"] == "B2M") == 2,
27 27
         "2 rows are returned for a duplicated key")
28
-}
29 28
\ No newline at end of file
29
+}
30
+