Giter Site home page Giter Site logo

nivanorge / chemspiderapi Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 2.0 446 KB

R functionalites for ChemSpider's new API services

Home Page: https://developer.rsc.org/compounds-v1/apis

License: MIT License

R 99.31% TeX 0.69%
chemspider r api-wrapper get post inchi inchikey smiles chemical-information cas-number

chemspiderapi's Introduction

chemspiderapi

R build status CodeCov test coverage

R functionalities for ChemSpider’s API services

ChemSpider has introduced a new API syntax in late 2018, and the old ChemSpider API syntax will be shut down at the end of November 2018. {chemspiderapi} provides R wrappers around the new API services from ChemSpider.

The aim of this package is to:

  1. Translate the new ChemSpider API services into R-friendly functions.
  2. Include thorough quality checking before the query is send, to avoid using up the query quota on, e.g., spelling errors.
  3. Implement the R functionality in a way that is suitable for both {base}- and {tidyverse}-style programming.

{chemspiderapi} relies on API keys to access ChemSpider’s API services. For this, we recommend storing the ChemSpider API key using the {keyring} package.

To limit the rate of API queries, we recommend using the {ratelimitr} package or purrr::slowly() within the {tidyverse} package collection.

To handle PNG images, the {magick} package is recommended.

We furthermore recommend the {memoise} package to “remember” the results of API queries (i.e., to not ruin the API allowance).

Additionally, the superb {webchem} package provides access to a lot of additional chemistry-related API services and is highly recommended.

Installation

R package

Install the package from GitHub (using the {remotes} package; automatically installed as part of {devtools}):

# install.packages("devtools")
remotes::install_github("NIVANorge/chemspiderapi")

The development version can be installed with:

# install.packages("devtools")
remotes::install_github("NIVANorge/chemspiderapi", ref = "dev")

Currently the only tested continuous integration environment for {chemspiderapi} is Linux. It should install smoothly on Windows 10 and mac OS machines as well. Please open an issue if you run into any troubles.

Dependencies

{chemspiderapi} relies on two essential dependencies. The {curl} package is used to handle the API queries and the {jsonlite} package is necessary to wrap and unwrap information for the queries.

If not already installed, these packages will be installed automatically when installing {chemspiderapi}. Should this result in trouble, the dependency packages can be installed manually:

install.packages(c("curl", "jsonlite"))

If {curl} or {jsonlite} are missing, (almost) all functions of {chemspiderapi} will fail and throw an error.

Coverage

As of 2021-01-05, the following functionalities are implemented (100% functionality with 100% annotation):

FILTERING

ChemSpider Compound API chemspiderapi Wrapper chemspiderapi Help File
filter-element-post post_element() yes
filter-formula-batch-post post_formula_batch() yes
filter-formula-batch-queryId-results-get get_formula_batch_queryId_results() yes
filter-formula-batch-queryId-status-get get_formula_batch_queryId_status() yes
filter-formula-post post_formula() yes
filter-inchi-post post_inchi() yes
filter-inchikey-post post_inchikey() yes
filter-intrinsicproperty-post post_intrinsicproperty() yes
filter-mass-batch-post post_mass_batch() yes
filter-mass-batch-queryId-results-get get_mass_batch_queryId_results() yes
filter-mass-batch-queryId-status-get get_mass_batch_queryId_status() yes
filter-mass-post post_mass() yes
filter-name-post post_name() yes
filter-queryId-results-get get_queryId_results() yes
filter-queryId-results-sdf-get get_queryId_results_sdf() yes
filter-queryId-status-get get_queryId_status() yes
filter-smiles-post post_smiles() yes

LOOKUPS

ChemSpider Compound API chemspiderapi Wrapper chemspiderapi Help File
lookups-datasources-get get_datasources() yes

RECORDS

ChemSpider Compound API chemspiderapi Wrapper chemspiderapi Help File
records-batch-post post_batch() yes
records-recordId-details-get get_recordId_details() yes
records-recordId-externalreferences-get get_recordId_externalreferences() yes
records-recordId-image-get get_recordId_image() yes
records-recordId-mol-get get_recordId_mol() yes

TOOLS

ChemSpider Compound API chemspiderapi Wrapper chemspiderapi Help File
tools-convert-post post_convert() yes
tools-validate-inchikey-post post_validate_inchikey() yes

Best practices for ChemSpider’s Compound APIs

This section will be updated with practical examples in the future.

The basic workflow order for the above FILTERING queries is:

  1. POST Query

  2. GET Status

  3. GET Results (after GET Status returns "Complete")

In practice, this means the following possible workflows can be implemented (from left to right):

POST Query GET Status GET Results
post_element() get_queryId_status() get_queryId_results()
post_formula() get_queryId_status() get_queryId_results()
post_formula_batch() get_formula_batch_queryId_status() get_formula_batch_queryId_results()
post_inchi() get_queryId_status() get_queryId_results()
post_inchikey() get_queryId_status() get_queryId_results()
post_intrinsicproperty() get_queryId_status() get_queryId_results()
post_mass() get_queryId_status() get_queryId_results()
post_mass_batch() get_mass_batch_queryId_status() get_mass_batch_queryId_results()
post_mass() get_queryId_status() get_queryId_results()
post_name() get_queryId_status() get_queryId_results()
post_smiles() get_queryId_status() get_queryId_results()

Typically, the result will be one or multiple ChemSpider IDs (recordId). They can be used as input into the above RECORDS queries, e.g., get_recordId_details().

Vignettes

As of 2021-01-05, the following five vignettes are available:

  • Storing and Accessing API Keys: A basic example on how to safely store and retrieve API keys using the keyring package.

  • Rate-Limiting API Queries: Multiple examples on how to slow down API queries. base, ratelimitr, and tidyverse workflows are provided.

  • Remembering API Queries: A basic example on how to “remember” the results of API queries using the memoise package. This can greatly reduce redundant API queries.

  • Saving MOL and SDF Files: Examples on how to save MOL and SDF files as returned by get_recordId_mol() and get_queryId_results_sdf().

  • Saving PNG Images: Examples on how to save PNG files, as returned by get_recordId_image(), using functionalities of the magick package.

Funding

This package was created at the Norwegian Institute for Water Research (NIVA) in conjunction with NIVA’s Computational Toxicology Program (NCTP) at NIVA’s Section for Ecotoxicology and Risk Assessment and funded by The Research Council of Norway (RCN), project 268294: Cumulative Hazard and Risk Assessment of Complex Mixtures and Multiple Stressors (MixRisk).

License

MIT © Raoul Wolf

chemspiderapi's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

chemspiderapi's Issues

Work flow vignette

A vignette should be created with work flow examples, also beyond the "normal" POST GET GET routine

Example workflow

The README needs a good workflow example, as well as an overview of possible workflows.

apikey error message

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.