-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniProtKBAC2EnsemblID.R
44 lines (37 loc) · 1.61 KB
/
UniProtKBAC2EnsemblID.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
UniProtKBAC2EnsemblID <- function(UniProtKBAC.CSV,
Wait = 5,
To = "Ensembl") {
suppressPackageStartupMessages(library("httr"))
Response <- POST("https://rest.uniprot.org/idmapping/run",
body = list(ids = UniProtKBAC.CSV, from = "UniProtKB_AC-ID", to = To),
encode = "form",
accept_json())
# Id Mapping API is not supported for mapping results with "mapped to" IDs more than 500,000
jobId <- content(Response, as = "parsed", encoding = "UTF-8")$jobId
JobOngoing <- TRUE
while (JobOngoing) {
SystemCommand <- paste("curl -i https://rest.uniprot.org/idmapping/status/", jobId, sep = "")
Response <- system(SystemCommand, intern = TRUE)
if ("{\"jobStatus\":\"FINISHED\"}" %in% Response) {
JobOngoing <- FALSE
}
else {
Sys.sleep(Wait) # in seconds
}
}
SystemCommand <- paste("curl -s https://rest.uniprot.org/idmapping/stream/", jobId, sep = "")
Result <- system(SystemCommand, intern = TRUE)
return(ParseUniProtREST(Result))
}
ParseUniProtREST <- function(Result) {
Result1 <- gsub("\\{\"results\":\\[\\{", "", Result)
Result2 <- gsub("\"\\}\\]\\}", "", Result1)
Result3 <- unlist(strsplit(Result2, split = "\"\\},\\{"))
Table <- matrix("", nrow = length(Result3), ncol = 2)
colnames(Table) <- c("uniprotsptrembl", "ensembl_gene_id")
for (i in 1 : length(Result3)) {
StringVector <- unlist(strsplit(Result3[i], split = "\"from\":\"|\",\"to\":\"|\\."))
Table[i,] <- c(StringVector[2], StringVector[3])
}
return(Table)
}