Skip to content

Commit 9bbd3d4

Browse files
tp2750fchichorro
andauthored
Get synonyms (#30)
Fetch alternative names for the same substance Ref: https://pubchem.ncbi.nlm.nih.gov/docs/pug-rest\#section\=Synonyms --------- Co-authored-by: Filipe Chichorro <[email protected]>
1 parent 11473b5 commit 9bbd3d4

22 files changed

+40
-2
lines changed

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ query_substructure_pug
108108
query_substructure
109109
get_for_cids
110110
pug
111+
get_synonyms
111112
```
112113

113114
### Utilities

src/PubChemCrawler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module PubChemCrawler
33
using HTTP
44
using LightXML
55

6-
export atomregex, parse_formula, get_cid, get_cids, get_for_cids, query_substructure, query_substructure_pug, pug
6+
export atomregex, parse_formula, get_cid, get_cids, get_for_cids, query_substructure, query_substructure_pug, pug, get_synonyms
77

88
include("utils.jl")
99
include("query.jl")

src/query.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,30 @@ function pug(args...; silent = true, escape_args = true, return_text = false, st
261261
b = return_text ? chomp(String(r.body)) : r.body
262262
return b
263263
end
264+
265+
"""
266+
synonyms = get_synonyms(name="glucose")
267+
synonyms = get_synonyms(smiles="C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O")
268+
synonyms = get_synonyms(cid=5793)
269+
270+
Return a list of substance or compound synonyms.
271+
"""
272+
function get_synonyms(; name=nothing, cid=nothing, smiles=nothing,
273+
kwargs...)::Vector{String}
274+
# inputs
275+
input = "compound/"
276+
if name !== nothing
277+
(smiles === nothing && cid === nothing) || throw(ArgumentError("only one of name, cid, or smiles can be specified"))
278+
input *= "name/$(HTTP.escapeuri(name))/"
279+
elseif cid !== nothing
280+
(smiles === nothing) || throw(ArgumentError("only one of name, cid, or smiles can be specified"))
281+
input *= "cid/$cid/"
282+
elseif smiles !== nothing
283+
input *= "smiles/$smiles/"
284+
else
285+
throw(ArgumentError("one of name, cid, or smiles must be specified"))
286+
end
287+
url = prolog * input * "synonyms/TXT"
288+
r = HTTP.request("GET", url; kwargs...)
289+
return split(chomp(String(r.body)), "\n")
290+
end

test/http_record/CAS_as_json.bson

36 Bytes
Binary file not shown.

test/http_record/CAS_as_txt.bson

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

test/http_record/aspirin_cids.bson

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)