Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get synonyms #20

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"

[compat]
HTTP = "0.8, 0.9"
HTTP = "0.8, 0.9, 1.9.8"
LightXML = "0.8, 0.9"
julia = "1"

Expand Down
2 changes: 1 addition & 1 deletion src/PubChemCrawler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module PubChemCrawler
using HTTP
using LightXML

export atomregex, parse_formula, get_cid, get_for_cids, query_substructure, query_substructure_pug
export atomregex, parse_formula, get_cid, get_for_cids, query_substructure, query_substructure_pug, get_synonyms

include("utils.jl")
include("query.jl")
Expand Down
28 changes: 28 additions & 0 deletions src/query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,31 @@ function get_for_cids(cids;
end

get_for_cids(cid::Int; kwargs...) = get_for_cids([cid]; kwargs...)


"""
synonyms = get_synonyms(name="glucose")
synonyms = get_synonyms(smiles="C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O")
synonyms = get_synonyms(cid=5793)

Return a list of substance or compound synonyms.
"""
function get_synonyms(; name=nothing, cid=nothing, smiles=nothing,
kwargs...)::Vector{String}
# inputs
input = "compound/"
if name !== nothing
(smiles === nothing && cid === nothing) || throw(ArgumentError("only one of name, cid, or smiles can be specified"))
input *= "name/$(HTTP.escapeuri(name))/"
elseif cid !== nothing
(smiles === nothing) || throw(ArgumentError("only one of name, cid, or smiles can be specified"))
input *= "cid/$cid/"
elseif smiles !== nothing
input *= "smiles/$smiles/"
else
throw(ArgumentError("one of name, cid, or smiles must be specified"))
end
url = prolog * input * "synonyms/TXT"
r = HTTP.request("GET", url; kwargs...)
return split(chomp(String(r.body)), "\n")
end
Binary file modified test/http_record/CAS_as_json.bson
Binary file not shown.
Binary file modified test/http_record/CAS_as_txt.bson
Binary file not shown.
Binary file modified test/http_record/aspirin_cid_from_name.bson
Binary file not shown.
Binary file modified test/http_record/aspirin_cid_from_smiles.bson
Binary file not shown.
Binary file modified test/http_record/aspirin_smiles_from_cid.bson
Binary file not shown.
Binary file modified test/http_record/asprin_sdf_3d.bson
Binary file not shown.
Binary file modified test/http_record/cGMP_cid.bson
Binary file not shown.
Binary file modified test/http_record/estriol_cid.bson
Binary file not shown.
Binary file modified test/http_record/estriol_substructure.bson
Binary file not shown.
Binary file modified test/http_record/estriol_substructure_formulas.bson
Binary file not shown.
Binary file added test/http_record/estriol_synonyms_from_cid.bson
Binary file not shown.
Binary file added test/http_record/estriol_synonyms_from_name.bson
Binary file not shown.
Binary file not shown.
Binary file modified test/http_record/smarts.bson
Binary file not shown.
Binary file modified test/http_record/smarts_pug.bson
Binary file not shown.
Binary file modified test/http_record/sodium_acetate_parent.bson
Binary file not shown.
16 changes: 13 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const allrecordings = [joinpath("http_record", file) for file in [
"cGMP_cid.bson",
"aspirin_cid_from_name.bson",
"CAS_as_json.bson",
"CAS_as_txt.bson"
"CAS_as_txt.bson",
"estriol_synonyms_from_name.bson",
"estriol_synonyms_from_cid.bson",
"estriol_synonyms_from_smiles.bson"
]]
const get_recordings = !all(isfile, allrecordings)

Expand All @@ -46,11 +49,11 @@ BrokenRecord.configure!(; path="http_record")
@test 27125 ∈ df.CID # check that estetrol has estriol as a substructure
sleep(2.0 * get_recordings)
df13 = CSV.File(playback(() -> query_substructure(;smarts="[r13]Br", output="CSV"), "smarts.bson")) |> DataFrame # brominated 13-atom ring structures
@test 153064026 ∈ df13.CID
@test 118303825 ∈ df13.CID
# The recommended approach for substructure searches is `query_substructure_pug`
sleep(5.0 * get_recordings)
cids13 = playback(() -> query_substructure_pug(;smarts="[r13]Br", poll_interval=10*get_recordings), "smarts_pug.bson") # brominated 13-atom ring structures, via PUG interface
@test 153064026 ∈ cids13
@test 118303825 ∈ cids13

# properties
sleep(5.0 * get_recordings)
Expand Down Expand Up @@ -84,4 +87,11 @@ BrokenRecord.configure!(; path="http_record")
@test dct[:InformationList][:Information][1][:RN] == ["40732-48-7", "7665-99-8"]
sleep(5.0 * get_recordings)
@test chomp(String(playback(() -> get_for_cids(cids[1]; xrefs="RN,", output="TXT"), "CAS_as_txt.bson"))) == "40732-48-7\n7665-99-8"

#synonyms
@test "Trimesta" ∈ playback(() -> get_synonyms(name="estriol"), "estriol_synonyms_from_name.bson")
sleep(5.0 * get_recordings)
@test "Trimesta" ∈ playback(() -> get_synonyms(cid=cid_estriol), "estriol_synonyms_from_cid.bson")
sleep(5.0 * get_recordings)
@test "Trimesta" ∈ playback(() -> get_synonyms(smiles="C[C@]12CC[C@H]3[C@H]([C@@H]1C[C@H]([C@@H]2O)O)CCC4=C3C=CC(=C4)O"), "estriol_synonyms_from_smiles.bson")
end