Skip to content

Commit

Permalink
Merge pull request #8 from fernandogelin/master
Browse files Browse the repository at this point in the history
Change functions to use HTTP.jl package instead of deprecated Requests and HttpCommon.
  • Loading branch information
mirestrepo authored May 16, 2018
2 parents f9c918c + aeb98e2 commit 5066442
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 63 deletions.
5 changes: 2 additions & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
julia 0.6
EzXML 0.5
JSON
Requests
Gumbo 0.4.0
HttpCommon 0.3.0
Gumbo 0.4.0
HTTP
29 changes: 17 additions & 12 deletions src/eutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export

import EzXML
import JSON
import Requests
import HTTP

const baseURL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"

Expand All @@ -61,7 +61,7 @@ Parameters: db, version, retmode.
"""
function einfo(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.get(string(baseURL, "einfo.fcgi"), query=params)
return HTTP.request("GET", string(baseURL, "einfo.fcgi"), query=params)
end

"""
Expand All @@ -74,7 +74,7 @@ retmode, sort, field, datetype, reldate, mindate, maxdate.
"""
function esearch(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
res = Requests.get(string(baseURL, "esearch.fcgi"), query=params)
res = HTTP.request("GET", string(baseURL, "esearch.fcgi"), query=params)
if get(params, :usehistory, "") == "y"
set_context!(ctx, res)
end
Expand All @@ -90,7 +90,8 @@ Parameters: db, id, WebEnv.
"""
function epost(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
res = Requests.post(string(baseURL, "epost.fcgi"), data=params)
body = HTTP.escapeuri(params)
res = HTTP.request("POST", string(baseURL, "epost.fcgi"), query=body)
set_context!(ctx, res)
return res
end
Expand All @@ -104,7 +105,8 @@ Parameters: db, id, query_key, WebEnv, retstart, retmax, retmode, version.
"""
function esummary(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.post(string(baseURL, "esummary.fcgi"), data=params)
body = HTTP.escapeuri(params)
return HTTP.request("POST", string(baseURL, "esummary.fcgi"), query=body)
end

"""
Expand All @@ -117,7 +119,8 @@ strand, seq_start, seq_stop, complexity.
"""
function efetch(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.post(string(baseURL, "efetch.fcgi"), data=params)
body = HTTP.escapeuri(params)
return HTTP.request("POST", string(baseURL, "efetch.fcgi"), query=body)
end

"""
Expand All @@ -130,7 +133,8 @@ datetype, reldate, mindate, maxdate.
"""
function elink(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.post(string(baseURL, "elink.fcgi"), data=params)
body = HTTP.escapeuri(params)
return HTTP.request("POST", string(baseURL, "elink.fcgi"), query=body)
end

"""
Expand All @@ -142,7 +146,7 @@ Parameters: term.
"""
function egquery(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.get(string(baseURL, "egquery.fcgi"), query=params)
return HTTP.request("GET", string(baseURL, "egquery.fcgi"), query=params)
end

"""
Expand All @@ -154,7 +158,7 @@ Parameters: db, term.
"""
function espell(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.get(string(baseURL, "espell.fcgi"), query=params)
return HTTP.request("GET", string(baseURL, "espell.fcgi"), query=params)
end

"""
Expand All @@ -166,7 +170,7 @@ Parameters: db, rettype, bdata.
"""
function ecitmatch(ctx::Associative=empty_context(); params...)
params = process_parameters(params, ctx)
return Requests.get(string(baseURL, "ecitmatch.cgi"), query=params)
return HTTP.request("GET", string(baseURL, "ecitmatch.cgi"), query=params)
end

# Create an empty context.
Expand All @@ -181,8 +185,9 @@ function set_context!(ctx, res)
end

# extract WebEnv and query_key from the response
contenttype = res.headers["Content-Type"]
data = String(res.data)
contenttype = Dict(res.headers)["Content-Type"]
data = String(res.body)

if startswith(contenttype, "text/xml")
doc = EzXML.parsexml(data)
ctx[:WebEnv] = EzXML.nodecontent(findfirst(doc, "//WebEnv"))
Expand Down
43 changes: 23 additions & 20 deletions src/umls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export get_tgt,
get_semantic_types

using Gumbo
using Requests
using HttpCommon
import HTTP
import JSON

#------------- Endpoints -----------------------
const uri = "https://utslogin.nlm.nih.gov"
Expand All @@ -53,7 +53,7 @@ end
"""
time_to_last_save(file)
Get how many hours since last save
Get how many hours since last save
"""
function time_to_last_save(file)
#unix time is GMT
Expand Down Expand Up @@ -86,14 +86,14 @@ end
"""
get_tgt(; force_new::Bool = false, kwargs...)
Retrieve a ticket granting ticket (TGT) using
Retrieve a ticket granting ticket (TGT) using
1. UTS username and password OR
2. apikey
A tgt is valid for 8 hours. Therefore, look for UTS_TGT.txt in the local
directory to see if it has been recently stored. One can force getting a
new ticket by passing keyword argument `force_new=true`
directory to see if it has been recently stored. One can force getting a
new ticket by passing keyword argument `force_new=true`
####Examples
Expand All @@ -107,6 +107,7 @@ tgt = get_tgt(apikey = "mykey")
"""
function get_tgt(; force_new::Bool = false, kwargs...)
params = Dict(kwargs)
body = HTTP.escapeuri(params)
auth_endpoint = "/cas/v1/tickets/"

if !haskey(params, :apikey)
Expand All @@ -118,17 +119,17 @@ function get_tgt(; force_new::Bool = false, kwargs...)
end

# Check if there is a valid ticket on disk
tgt_file = "UTS_TGT.txt"
tgt_file = "UTS_TGT.txt"
if tgt_exists(tgt_file=tgt_file) && !force_new
info("UTS: Reading TGT from file")
info("UTS: Reading TGT from file")
return readline(tgt_file)
end

info("UTS: Requesting new TGT")
headers = Dict("Content-type"=> "application/x-www-form-urlencoded",
"Accept"=> "text/plain", "User-Agent"=>"julia" )
r = post(uri*auth_endpoint,data=params,headers=headers)
ascii_r = String(r.data)
r = HTTP.request("POST", uri*auth_endpoint, body=body, headers=headers)
ascii_r = String(r.body)

doc = parsehtml(ascii_r)
#for now - harcoded
Expand All @@ -154,15 +155,17 @@ Retrieve a single-use Service Ticket using TGT
"""
function get_ticket(tgt)
params = Dict("service"=> service)
h = Dict("Content-type"=> "application/x-www-form-urlencoded",
body = HTTP.escapeuri(params)

headers = Dict("Content-type"=> "application/x-www-form-urlencoded",
"Accept"=> "text/plain", "User-Agent"=>"JuliaBioServices" )
r = HttpCommon.Response(503)
r = HTTP.Response(503)
try
r = post(tgt; data=params, headers=h)
r = HTTP.request("POST", tgt; body=body, headers=headers)
catch
isdefined(r, :code) ? error("UMLS GET error: ", r.code) : error("UMLS COULD NOT GET")
end
return String(r.data)
return String(r.body)
end

#------------------Search ---------------------
Expand Down Expand Up @@ -224,13 +227,13 @@ function search_umls(tgt, query; version::String="current", timeout=1)
query["ticket"]= ticket
query["pageNumber"]= string(page)

r = get(rest_uri*content_endpoint, query=query, timeout=timeout)
r = HTTP.request("GET", rest_uri*content_endpoint, query=query, timeout=timeout)

if r.status != 200
error("Bad HTTP status $(r.status)")
end

json_response = Requests.json(r)
json_response = JSON.parse(String(r.body))
# println("No Results ", length(json_response["result"]["results"]))
if json_response["result"]["results"][1]["ui"]=="NONE"
break
Expand Down Expand Up @@ -260,7 +263,7 @@ end
"""
get_cui(tgt,cui)
Retrieve information (name, semantic types, number of atoms, etc) for a known CUI
Retrieve information (name, semantic types, number of atoms, etc) for a known CUI
from latest UMLS version or a specific release.
Returns UTS json response
Expand All @@ -285,14 +288,14 @@ function get_cui(tgt,cui; version="current")
rethrow(err)
end

r = HttpCommon.Response(503)
r = HTTP.Response(503)
try
r = get( rest_uri*content_endpoint,query=Dict("ticket"=> ticket))
r = HTTP.request("GET", rest_uri*content_endpoint,query=Dict("ticket"=> ticket))
catch
isdefined(r, :code) ? error("UMLS GET error: ", r.code) : error("UMLS COULD NOT GET")
end

return Requests.json(r)
return JSON.parse(String(r.body))
end


Expand Down
58 changes: 35 additions & 23 deletions test/eutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
@testset "einfo" begin
res = einfo(db="pubmed")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
@test nodename(elements(root(parsexml(res.body)))[1]) != "ERROR"
end

@testset "esearch" begin
res = esearch(db="pubmed", term="asthma")
res = esearch(db="pubmed", term="""(Asthma[MeSH Major Topic]) AND
("1/1/2018"[Date - Publication] :
"3000"[Date - Publication])""")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
@test nodename(elements(root(parsexml(res.body)))[1]) != "ERROR"
end

@testset "epost" begin
ctx = Dict()
res = epost(ctx, db="protein", id="NP_005537")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
@test haskey(ctx, :WebEnv)
@test haskey(ctx, :query_key)
end
Expand All @@ -28,13 +32,15 @@
# esummary doesn't seem to support accession numbers
res = esummary(db="protein", id="15718680,157427902,119703751")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
@test nodename(elements(root(parsexml(res.body)))[1]) != "ERROR"

res = esummary(db="protein", id=["15718680", "157427902", "119703751"])
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
@test nodename(elements(root(parsexml(res.body)))[1]) != "ERROR"

# esearch then esummary
query = "asthma[mesh] AND leukotrienes[mesh] AND 2009[pdat]"
Expand All @@ -54,8 +60,8 @@
@testset "efetch" begin
res = efetch(db="nuccore", id="NM_001178.5", retmode="xml", idtype="acc")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)

# epost then efetch
ctx = Dict()
Expand All @@ -68,22 +74,28 @@
@testset "elink" begin
res = elink(dbfrom="protein", db="gene", id="NM_001178.5")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
end

@testset "egquery" begin
res = egquery(term="asthma")
res = egquery(term="""(Asthma[MeSH Major Topic]) AND
("1/1/2018"[Date - Publication] :
"3000"[Date - Publication])""")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
end

@testset "espell" begin
res = espell(db="pubmed", term="athma")
@test res.status == 200
@test startswith(res.headers["Content-Type"], "text/xml")
@test isa(parsexml(res.data), EzXML.Document)
res = espell(db="pmc", term="fiberblast cell grwth")
@test res.status == 200
@test startswith(Dict(res.headers)["Content-Type"], "text/xml")
@test isa(parsexml(res.body), EzXML.Document)
doc = parsexml(res.body)
spelled_query = elements(root(doc))[4]
replaced_spell = nodecontent(elements(spelled_query)[4])
@test replaced_spell == "growth"
end

@testset "ecitmatch" begin
Expand All @@ -96,4 +108,4 @@
# requires retmode="xml".
# @show res.headers["Content-Type"]
end
end
end
9 changes: 4 additions & 5 deletions test/umls.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@


@testset "UMLS" begin
user= ""
user = ""
psswd = ""
apikey = ""

try
user = ENV["UMLS_USER"]
psswd = ENV["UMLS_PSSWD"]
Expand All @@ -17,10 +17,9 @@
term = "obesity"
query = Dict("string"=>term, "searchType"=>"exact" )


tgt = get_tgt(force_new=true, username=user, password=psswd)
#do it again to test reading from file
tgt = get_tgt(username=user, password=psswd)
tgt = get_tgt(username=user, password=psswd)
#test apikey
tgt = get_tgt(force_new=true, apikey=apikey)

Expand All @@ -33,4 +32,4 @@
@test sm[1] == "Disease or Syndrome"
end
end
end
end

0 comments on commit 5066442

Please sign in to comment.