GithubHelp home page GithubHelp logo

ropensci / webchem Goto Github PK

View Code? Open in Web Editor NEW
160.0 19.0 40.0 4.03 MB

Chemical Information from the Web

Home Page: https://docs.ropensci.org/webchem

License: Other

R 100.00%
rstats ropensci chemical-information webscraping cas-number chemspider identifier r r-package

webchem's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webchem's Issues

Add extractor functions

Some functions return huge (messy) lists.
Add extractor functions to extract and clean desired parts (e.g. solubility, inchikey, etc...).

  • cas()
  • smiles()
  • inchikey()

ppdb fails

e.g. 120-93-4 (because of dups in ppdb_idx)
1327-53-3 (because of encoding error)

Add unit tests

Perhaps on a separate branch to avoid conflicts with CRAN?!

ci_query('Tetracyclin', type = 'name') FAILS

> ci_query('Tetracyclin', type = 'name')
http://chem.sis.nlm.nih.gov/chemidplus/name/Tetracyclin
Error in html_table(xml_find_all(ttt, "//h2[contains(., 'Toxicity')]/following-sibling::div//table"))[[1]] : 
  subscript out of bounds

allanwood(): return a named vector of inchikeys for isomers

Currently:

> alanwood("S-Metolachlor", type = "commonname")$inchikey

# More then one link found! Returning first.
# 
# Querying s-metolachlor.html
# [1] "major component (S)-isomer:WVQBLGZPHOPPFO-LBPRGKRZSA-Nminor component (R)-isomer:WVQBLGZPHOPPFO-GFCCVEGCSA-N"

Should be

> alanwood("S-Metolachlor", type = "commonname")$inchikey

# More then one link found! Returning first.
# 
# Querying s-metolachlor.html
# [1] "major component (S)-isomer" - WVQBLGZPHOPPFO-LBPRGKRZSA-N
[2] "minor component (R)-isomer" - WVQBLGZPHOPPFO-GFCCVEGCSA-N

Make an user query

How is using it?
Why?
What is useful?
what is missing?
what can be enhanced?

Add examples to all fnx for bulk processing

e.g.

require(webchem)
compounds <- c('Triclosan', 'Formaldehyde')

# query all compounds, return first hit (-> named vector)
sapply(compounds, 
       function(x) cir_query(identifier = x , representation = 'cas', first = TRUE))
# query all compounds, return all hits (-> named list)
sapply(compounds, 
       function(x) cir_query(identifier = x , representation = 'cas'))

# format as data.frame
ll <- sapply(compounds, 
             function(x) cir_query(identifier = x , representation = 'cas'))
data.frame(compound = rep(names(ll), sapply(ll, length)),
           cas = unlist(ll))
  • allanwood
  • chemspider
  • cir
  • cts
  • etox
  • pan
  • physprop
  • ppdb
  • pubchem
  • chemid
  • wikidata

xml2 ?

Switch to xml2 package?

Add timeout

via

  h <- try(GET(qurl, timeout(2)))
  if (inherits(h, "try-error")) {
    warning('Problem with web service encountered... Returning NA.')
    return(NA)
  } else {
    tt <- read_xml(content(h, as = 'raw'))
  }

alanwood: return activity subgroup

Currently:

> alanwood("S-Metolachlor", type = "commonname")$activity
[1] "herbicides (chloroacetanilide herbicides)"

desired behaviour:

$activity  [1] "herbicides"
$activity_subgroup [1] "chloroacetanilide herbicides"

Notes for implementation:

  • subgroups are within brackets
  • multiple subgroups are divided by semi-colon (;)

aw_query inchikey

split multiple inchikeys:

BFPGVJIMBRLFIR-GUCBCRIZSA-Nidentifier for no stereochemistry:ZXQYGBMAQZUVMI-UHFFFAOYSA-N

alanwood() multiplies entries in the case of isomer mixtures

For example in the case of S-Metolachlor

http://www.alanwood.net/pesticides/s-metolachlor.html

the HTML snippet for the InChiKey is

<tr valign="baseline">
<th id="r11">InChIKey:</th>
<td headers="r11">major component (<i>S</i>)-isomer:<br>WVQBLGZPHOPPFO-LBPRGKRZSA-N<br>minor component (<i>R</i>)-isomer:<br>WVQBLGZPHOPPFO-GFCCVEGCSA-N</td>
</tr>

and we get

test <- webchem::alanwood("S-Metolachlor", type = "commonname")
## Querying s-metolachlor.htmls-metolachlor.html
test$inchikey
## [1] "major component (S)-isomer:WVQBLGZPHOPPFO-LBPRGKRZSA-Nminor component (R)-isomer:WVQBLGZPHOPPFO-GFCCVEGCSA-N"
## [2] "major component (S)-isomer:WVQBLGZPHOPPFO-LBPRGKRZSA-Nminor component (R)-isomer:WVQBLGZPHOPPFO-GFCCVEGCSA-N"

which is duplicated (already the query as it seems).

check user input

e.g. queries should be of lenght 1.
See also issue #31

  • input of lenght one
  • functions to check type of input
    • CAS
    • inchikey
    • inchi
  • check input in functions
    • alanwood (cas)
    • chemid (rn, inchikey)
    • cir_query (rn, inchikey - only for given resolver)
    • cts (inchikey)
    • physprop
    • ppdb_query

add inchikey checks

e.g.

isinchikey=function(s) {      
    (s==toupper(s))&(nchar(s) == 27)&(substr(s,15,15)=="-")&(substr(s,26,26)=="-")
}

to check input for cts.

Suggestions from Tim Wenseleers

From Tom Wenseleers:

Hi Rajarshi or Eduard,
I just figured out how to download Kovats retention indices from Pubchem - in case you would be interested to include something like this in rpubchem or webchem :

library("xml2")
library("data.table")
compound.retention.index <- function(file=NULL) {
  pubchemid <- sub(".*/([0-9]+)/?.*", "\\1", file)
  compound <- read_xml(file)
  ns <- xml_ns(compound)
  information <- xml_find_all(compound, paste0(
    "//d1:TOCHeading[text()='Kovats Retention Index']",
    "/following-sibling::d1:Information"
  ), ns)
  indexes <- lapply(information, function(x) {
    name <- xml_text(xml_find_one(x, "./d1:Name", ns))
    values <- as.numeric(sapply(
      xml_find_all(x, "./*[contains(name(), 'NumValue')]", ns),
      xml_text))

    data.frame(pubchemid=pubchemid,
               column_class=name,
               kovats_ri=values)
  })

  return( do.call("rbind", indexes) )
}

cids <- c("5282108", "5282148", "91754124") # example pubchem cids
compounds <- paste0("https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/",cids,"/XML/?response_type=display")

# Kovats retention indices
rti <- do.call("rbind",
               lapply(compounds, compound.retention.index))
names(rti)
rti

Some time ago I also wrote some code to retrieve detailed retention index information, compound properties and EI mass spectra from the NIST Chemistry Webbook.
@Eduard - would you be interested by any chance to have some functions to interact with the NIST Chemistry Webbook in your webchem package?

best regards,
Tom

PS using the xml2 package in R tends to work better than the XML package, as the latter is known to have memory leaks, which can be really annoying for large annotation jobs

PS2 @Rajarshi - could be nice perhaps in your package if you could query also by inchikey, and that it would automatically recognize the input, and convert from inchikey to cid if desired:
library(webchem)
get_cid('GLZPCOQZEFWAFX-JXMROGBWSA-N')

"637566"

and

Hi Eduard,
Ha that's great! And yes I know how to use github - only thing is that I got quite a lot of teaching to do shortly, so it may be a little while then before I get to it then... I'll dig into my NIST functions too then.
Ha and to access all the info available in Pherobase (e.g. http://www.pherobase.com/database/nmr/nmr-detail-geraniol.php, http://www.pherobase.com/database/kovats/kovats-detail-geraniol.php, http://www.pherobase.com/database/compound/compounds-detail-geraniol.php, http://www.pherobase.com/database/floral-compounds/floral-taxa-compounds-detail-geraniol.php) I also have some functions - only thing is that for that one to enable generic queries (e.g. based on structure/InChiKey or name) I created a file with the relevant pointers and conversion from a given name or structure to a particular Pherobase page with retention index info, natural occurrece and behavioural function. 
(As pherobase itself does not implement a generic search function, it just uses Google for that)

best regards,
Tom

Vektorize

Check which function can handle input vectors and which not.
Add checks (e.g. in utils.R)

aw_query fails

aw_query('12071-83-9', type = 'cas')
Querying propineb.html
Error in if (grepl("isomer", inchikey)) { : argument is of length zero

standardize output

Eg. solubility should have the same name and unit through the results returned.

physprop fails on windows

See appveyor results:

1. Failure (at test-physprop.R#8): physprop returns correct results ------------
fl$cas not equal to "50-00-0"
1 string mismatches:
x[1]: "50-00-0"
y[1]: "�000050-00-0"

This happens because sub() doesn't work. Possible workarounds:

  • Hardcode encoding
  • ???

Allow vector input

This is related to issue #36.

Output would be a list of lists. Therefore we need the extractor functions.
And could skip the example with multiple inputs.

  • cts
  • physprop
  • cir
  • pubchem
  • etox
  • opsin
  • pan
  • wikidata
  • alanwood
  • chemspider
  • chemid

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.