-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractGeneInfoTable.R
More file actions
156 lines (132 loc) · 5.13 KB
/
extractGeneInfoTable.R
File metadata and controls
156 lines (132 loc) · 5.13 KB
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
#!/usr/bin/env Rscript
# This script generates a table of gene information (NCBI, GeneCards, KEGG, Uniprot, Marrvel).
# Call Syntax : Rscript extractGeneInfoTable.R <species> <geneIDArray> <domainName>
# Input : species (e.g. hsa, mmu)
# : geneArray (string of ENTREZ IDs, e.g. "3098,6120")
# : domainName (e.g. bcdw.org)
# Output : An HTML table containing links to KEGG, GeneCards, NCBI, Ensembl, Uniprot, Marrvel
suppressPackageStartupMessages({
library(jsonlite)
library(dplyr)
library(xtable)
library(plyr)
})
# ----------------------------- Helpers --------------------------------------
build_geneinfo_url <- function(domain, species, geneArray) {
GeneIDType <- "ENTREZID"
USE_NCBI_GENE_INFO <- 0
ViewType <- "json"
IncHTML <- 1
species_enc <- URLencode(species, reserved = TRUE)
geneArray_enc <- URLencode(geneArray, reserved = TRUE)
paste0(
"https://", domain,
"/dev/geneid/geneid_proc_selcol_GET.php", # remove dev for production code
"?species=", species_enc,
"&GeneListStr=", geneArray_enc,
"&GeneIDType=", GeneIDType,
"&USE_NCBI_GENE_INFO=", USE_NCBI_GENE_INFO,
"&View=", ViewType,
"&IncHTML=", IncHTML
)
}
getGeneInfoTable <- function(orgStr, geneArray, domainName) {
# resolve a relative path for images based on current working directory
currDir <- paste0("/", basename(getwd()))
url_str_gene_php <- build_geneinfo_url(domainName, orgStr, geneArray)
GeneAllInfo <- tryCatch(
{
fromJSON(url_str_gene_php, simplifyVector = TRUE)
},
error = function(e) {
write("ERROR: Unable to fetch or parse JSON from geneid service.", stderr())
quit(status = 1)
}
)
required_cols <- c(
"SYMBOL",
"HTML_KEGG",
"HTML_SYMBOL",
"HTML_ENTREZID",
"HTML_ENSEMBL",
"HTML_UNIPROT",
"HTML_MARRVEL"
)
if (!all(required_cols %in% names(GeneAllInfo))) {
write("ERROR: JSON response missing expected gene info columns.", stderr())
quit(status = 1)
}
GeneAllInfo_forhtml <- unique(GeneAllInfo[required_cols])
# collapse UNIPROT entries
newdf <- ddply(
GeneAllInfo_forhtml,
.(SYMBOL, HTML_KEGG, HTML_SYMBOL, HTML_ENTREZID, HTML_ENSEMBL, HTML_MARRVEL),
.fun = summarize,
HTML_UNIPROT_COLLAPSED = paste0(unique(HTML_UNIPROT), collapse = ", "),
.progress = "none",
.inform = FALSE,
.drop = TRUE,
.parallel = FALSE,
.paropts = NULL
)
colnames(newdf)[colnames(newdf) == "HTML_UNIPROT_COLLAPSED"] <- "HTML_UNIPROT"
GeneAllInfo_forhtml <- newdf
# collapse ENSEMBL entries
newdf <- ddply(
GeneAllInfo_forhtml,
.(SYMBOL, HTML_KEGG, HTML_SYMBOL, HTML_ENTREZID, HTML_UNIPROT, HTML_MARRVEL),
.fun = summarize,
HTML_ENSEMBL_COLLAPSED = paste0(unique(HTML_ENSEMBL), collapse = ", "),
.progress = "none",
.inform = FALSE,
.drop = TRUE,
.parallel = FALSE,
.paropts = NULL
)
colnames(newdf)[colnames(newdf) == "HTML_ENSEMBL_COLLAPSED"] <- "HTML_ENSEMBL"
GeneAllInfo_forhtml <- newdf
# build display table with logo images in header
n <- nrow(GeneAllInfo_forhtml)
outdf <- data.frame(matrix(ncol = 7, nrow = n), stringsAsFactors = FALSE)
symbolStr <- "Symbol"
keggStr <- paste0("<img src=\"", currDir, "/images/kegg4.gif\" alt=\"KEGG\" width=\"60\">")
geneCardsStr <- paste0("<img src=\"", currDir, "/images/logo_genecards.png\" alt=\"Gene Cards\" width=\"60\">")
ncbiStr <- paste0("<img src=\"", currDir, "/images/NCBILogo.gif\" alt=\"NCBI\" width=\"60\">")
ensemblStr <- paste0("<img src=\"", currDir, "/images/ensembl_logo.png\" alt=\"Ensembl\" width=\"60\">")
uniprotStr <- paste0("<img src=\"", currDir, "/images/Uniprot.png\" alt=\"Uniprot\" style=\"background-color:gray;padding:20px;\" width=\"60\">")
marrvelStr <- paste0("<img src=\"", currDir, "/images/marrvel.png\" alt=\"Marrvel\" width=\"60\">")
colnames(outdf) <- c(symbolStr, keggStr, geneCardsStr, ncbiStr, ensemblStr, uniprotStr, marrvelStr)
for (i in seq_len(n)) {
outdf[i, ] <- c(
GeneAllInfo_forhtml$SYMBOL[i],
GeneAllInfo_forhtml$HTML_KEGG[i],
GeneAllInfo_forhtml$HTML_SYMBOL[i],
GeneAllInfo_forhtml$HTML_ENTREZID[i],
GeneAllInfo_forhtml$HTML_ENSEMBL[i],
GeneAllInfo_forhtml$HTML_UNIPROT[i],
GeneAllInfo_forhtml$HTML_MARRVEL[i]
)
}
# print as HTML table with id='Table1' (same as original usage)
# sanitize.text.function = identity so that HTML links are not escaped
html <- capture.output(
print(
xtable(outdf, caption = NULL),
type = "html",
include.rownames = FALSE,
sanitize.text.function = function(x) x,
html.table.attributes = "class='styled-table' id='Table1'"
)
)
cat(paste(html, collapse = "\n"))
}
# ----------------------------- Main ----------------------------------------
args <- commandArgs(TRUE)
if (length(args) != 3) {
write("ERROR: Expected 3 arguments: <species> <geneIDArray> <domainName>", stderr())
quit(status = 1)
}
species <- args[1]
geneArray <- args[2]
domainName <- args[3]
getGeneInfoTable(species, geneArray, domainName)