NIVANorge / chemspiderapi

R functionalites for ChemSpider's new API services
https://developer.rsc.org/compounds-v1/apis
MIT License
5 stars 2 forks source link

apikey error message #6

Closed giovp closed 4 years ago

giovp commented 4 years ago

Hi,

I've followed vignette etc. for setting up conversion of InChIKey to SMILES, but keep getting this error:

ChemSpider Response Error Details: "401: Unauthorized. Check you have supplied the correct API key and that you have sent it as an HTTP Header called 'apikey'.".

Code

library(chemspiderapi)
library(tidyverse)
library(keyring)
file <- read_tsv("./all_compounds.tsv", col_names = FALSE)
colnames(file) <- "InChIKey"
red_file <- file %>% head(3)

#store api key
apikey <- keyring::key_get(service = "ChemSpider API key", username = Sys.getenv("giovp"))

#make conversion slow

inputFormat <- "InChIKey"
outputFormat <- "SMILES"
slowly_post_convert <- slowly(post_convert, rate = rate_delay(pause = 4), quiet = FALSE)

tidyverse_demo <- red_file %>% 
  mutate(queryId = map_chr(InChIKey, ~ slowly_post_convert(input = .x, inputFormat = inputFormat, outputFormat = outputFormat, apikey = apikey)))

red_file

> # A tibble: 3 x 1
   InChIKey                   
   <chr>                      
 1 IAZDPXIOMUYVGZ-UHFFFAOYSA-N
 2 DWJXYEABWRJFSP-XOBRGWDASA-N
 3 AQGNHMOJWBZFQQ-UHFFFAOYSA-N

the api key I added with keyring is the one provided by chemspider (33 characters). Do I also have to add the api secret ? Also, for chemspider, I requested a demoAPI key, not the compound key (first option). Shall I request for the other one?

thank you, giovanni

RaoulWolf commented 4 years ago

Hi @giovp,

Before using the functionalities of the package you will need the compound API key, which will be a 32 character string. The API secret does not have to be provided to the package.

Unfortunately, the ChemSpider API does not support the direct conversion from InChIKey to SMILES. However, you can go for a two-step processes, first converting your InChIKeys to InChI strings, then converting the InChI strings to SMILES.

The possible conversion options are also named in the help file that you can access via help(post_convert).

Please let me know if this works for you!

giovp commented 4 years ago

Hi @RaoulWolf ,

thanks a lot for the prompt reply, I did a test run and it works. Couple of more questions on the matter:

Thank you again for the help, and btw the documentation+package is really great, for somebody who has zero knowledge on the topic it was very intuitive and practical. Thank you! Giovanni

RaoulWolf commented 4 years ago

The 4 second example is based on one of the common query restrictions for ChemSpider's API services. You always have an upper monthly limit (for example 1000 queries per month, depending on your subscription), but also a limit on how many requests per minute can be submitted (for example 15 queries per minute). This minute-limitation is achieved by introducing the rate limits as shown in the vignette; for 15 queries per minute this translates to the 4 second sleep time per query. Those limitations are based on your API subscription, and if in doubt I suggest contacting ChemSpider directly.

The 1000 compounds you mention probably are in the context of monthly queries? Or do you mean in a particular function of the package?

Thanks for the kind words 😃 Glad you find it useful!

giovp commented 4 years ago

Thanks a lot, I indeed have reached the maximum already 😅

ChemSpider Response Error Details: "429: Too Many Requests. Send fewer requests, or use rate-limiting to slow them down, then try again.".

yes, those 1k are probably the monthly query max.

Thanks again, I'll try a workaround with chemspider.

RaoulWolf commented 4 years ago

Alright, I'll close the issue now.

If there are any other troubles, feel free to open a new issue!