diff --git a/src/naming_conventions.jl b/src/naming_conventions.jl index f1fbda2..8548c9e 100644 --- a/src/naming_conventions.jl +++ b/src/naming_conventions.jl @@ -99,3 +99,53 @@ function query_uniprot_accession(id) end return nothing end + +""" + jobID = GPCRAnalysis.map_uniprot_submit(ids, from, to="UniProtKB") + +Submit a list of `ids` to the Uniprot ID mapping service, to convert from ID convention `from` to `to`. +The jobID can be used to check the status (`map_uniprot_status`) and retrieve the results (`map_uniprot_retrieve`). + +# Examples +```jldoctest` +julia> jobID = GPCRAnalysis.map_uniprot_submit(["ENSMUSG00000067064", "ENSMUSG00000057464"], "Ensembl"); +``` +""" +function map_uniprot_submit(ids::AbstractString, from::AbstractString, to::AbstractString="UniProtKB") + resp = HTTP.post("https://rest.uniprot.org/idmapping/run", [], + Dict("from" => from, "to" => "UniProtKB", "ids" => ids)) + if resp.status == 200 + return JSON3.read(String(resp.body))["jobId"] + end + return nothing +end +map_uniprot_submit(ids::AbstractVector, from::AbstractString) = map_uniprot_submit(join(ids, ','), from) + +""" + status = GPCRAnalysis.map_uniprot_status(jobId) + +Check the status of a Uniprot ID mapping job. Returns `true` if the results are +ready. Otherwise, returns the status object. +""" +function map_uniprot_status(jobId) + resp = HTTP.get("https://rest.uniprot.org/idmapping/status/$jobId", ["Accept" => "application/json"]) + if resp.status == 200 + status = JSON3.read(String(HTTP.decode(resp))) + haskey(status, "results") && return true + return status + end + return nothing +end + +""" + result = GPCRAnalysis.map_uniprot_retrieve(jobId) + +Retrieve the results of a Uniprot ID mapping job. +""" +function map_uniprot_retrieve(jobId) + resp = HTTP.get("https://rest.uniprot.org/idmapping/stream/$jobId", ["Accept" => "application/json"]) + if resp.status == 200 + return JSON3.read(String(HTTP.decode(resp))) + end + return nothing +end diff --git a/test/runtests.jl b/test/runtests.jl index 1db1fa7..6d7e088 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -296,6 +296,18 @@ using Test if !isdefined(@__MODULE__, :skip_download) || !skip_download @testset "Uniprot" begin @test query_uniprot_accession("T2R38_MOUSE") == "Q7TQA6" + + jobID = GPCRAnalysis.map_uniprot_submit(["ENSMUSG00000067064", "ENSMUSG00000057464"], "Ensembl") + tstart = time() + sleep(1) + status = false + while status != true && time() < tstart + 20 + sleep(1) + status = GPCRAnalysis.map_uniprot_status(jobID) + end + results = GPCRAnalysis.map_uniprot_retrieve(jobID)[:results] + resultsdict = Dict(obj["from"] => obj["to"] for obj in results) + @test resultsdict["ENSMUSG00000067064"] == "Q8VGU4" end @testset "EBI and NCBI" begin