Importation de données associées à un numéro CAS donné à partir du site Web NIST WebBook en R

StackOverflow https://stackoverflow.com//questions/20004779

  •  20-12-2019
  •  | 
  •  

Question

Je voudrais récupérer des informations associées à un numéro de registre CAS donné (Service de résumé chimique NR) à partir du site Web NIST WebBook en R, à l'aide de l'API fourni.

E.g. pour CAS NR. "19431-79-9" (Caryophylladienol II), http:// WebBook. nist.gov/cgi/cbook.cgi?id=19431-79-9&itive=si&mask=2000#gas-chrom Je suis arrivé aussi loin que

casno = "19431-79-9"
casno2 = gsub("-", "", casno)
raw=readLines(paste('http://webbook.nist.gov/cgi/cbook.cgi?ID=',casno,'&Units=SI&Mask=2000#Gas-Chrom', sep=""))

# mass spec, empty here, but not e.g. for casno2="630035" 
casno2="630035"
jcampfile = readLines(paste("http://webbook.nist.gov/cgi/cbook.cgi?JCAMP=C",casno2,"&Index=0&Type=Mass",sep=""))
if (jcampfile[[1]]=="##TITLE=Spectrum not found.") jcampfile=NA              

casno2 = gsub("-", "", casno)
# molecular stucture
molfile2d=readLines(paste("http://webbook.nist.gov/cgi/cbook.cgi?Str2File=C",casno2,sep=""))
if (molfile2d==character(0)) molfile2d=NA
molfile3d=readLines(paste("http://webbook.nist.gov/cgi/cbook.cgi?Str3File=C",casno2,sep=""))
if (molfile3d==character(0)) molfile3d=NA

des bits suivants de la sortie brute, j'aimerais alors extraire les variables et listes suivantes:

"name=\" Top \">Caryophylladienol II</a></h1>" 
-> name="Caryophylladienol II"

"Formula</a>:</strong> C<sub>15</sub>H<sub>24</sub>O</li>\n \n \n<li><strong>" 
-> formula="C15H24O"

"Molecular weight</a>:</strong> 220.3505</li>\n \n \n<li>" 
-> MW=220.3505

"IUPAC Standard InChI:</strong>\n \n<br /><table>\n<tr><td>\n<ul style=\" list-style-type: circle;\">\n<li><tt>InChI=1S/C15H24O/c1-10-6-8-14(16)11(2)5-7-13-12(10)9-15(13,3)4/h12-14,16H,1-2,5-9H2,3-4H3/t12?,13?,14-/m1/s1</tt></li>\n" 
-> InChI="InChI=1S/C15H24O/c1-10-6-8-14(16)11(2)5-7-13-12(10)9-15(13,3)4/h12-14,16H,1-2,5-9H2,3-4H3/t12?,13?,14-/m1/s1" 

"IUPAC Standard InChIKey:</strong>\n<tt>CIIYOYPOMGIECX-JXQTWKCFSA-N</tt>" 
-> InChiKey="CIIYOYPOMGIECX-JXQTWKCFSA-N"

"Stereoisomers:....<strong>
-> stereoisomers=XXX (list of stereoisomers)

"Other names:...\n"
-> synonyms=XXX (list of synonyms)

"Normal alkane RI..."
-> list of measured RIs plus on which column they were measured
e.g. here RIs=c(1637,1631,1627,1656,1615,1638,1628,1602,1611,1635,1622,1622,1627); columns=c("HP-5 MS","DB-5","RTX-1","Col-Elite 5MS","DB-5","DB-5","DB-5","DB-1","DB-5","CP Sil 5 CB","BP-1","RTX-1","DB-5")

Toute réflexion sur la façon dont je ferais le meilleur de ce dernier type d'analyse? Idéalement, cela devrait alors tous être emballé dans une fonction qui prend une liste de CAS NRS comme entrée, les annote à l'aide d'informations à partir du webbook NIST et les écrit dans un fichier texte. Mais pas besoin de l'avoir tellement poli - quelque chose de me faire commencerait d'aider vraiment!

Edit: J'ai essayé d'analyser le fichier HTML à l'aide de HTMLTreeParse dans Package XML, mais je ne réussis pas bien. Quelqu'un aurait-il un peu plus d'expérience avec cette fonction pour pouvoir m'aider un peu par hasard?

Edit: J'ai trouvé une solution pour importer les données de Mathematica, voir https://mathematica.stackexchange.com/questions/37091/look-up-info-associated-with-a-given-cas-chemical-Identifier -From-the-nist-webbo . Si quelqu'un aurait la compétence pour porter ce code à r, laissez-moi savoir!

Était-ce utile?

La solution

pour la première chaîne d'URL dans votre question, essayez

casno = "19431-79-9"
url <- paste('http://webbook.nist.gov/cgi/cbook.cgi?ID=',casno,'&Units=SI&Mask=2000#Gas-Chrom', sep="")
doc <- htmlParse(url)

name <- xpathSApply(doc, "//a[@id='Top']", xmlValue)
name
[1] "Caryophylladienol II"

Prenez toutes les listes avec un titre en gras (une sortie tronquée pour l'affichage)

x <- xpathSApply(doc, "//li/strong/..", xmlValue)
x

[1] "Formula: C15H24O" 
[2] "Molecular weight: 220.3505" 
[3] "IUPAC Standard InChI:\n\n\nInChI=1S/C15H24O/c1-10-6-8-14(16)11(2)5-7-13-12(10)9-15(13,3)4/h12-14,16H,1-2,5-9H2, ...
[4] "IUPAC Standard InChIKey:\nCIIYOYPOMGIECX-JXQTWKCFSA-N" 
[5] "CAS Registry Number: 19431-79-9"  
[6] "Chemical structure: \nThis structure is also available as a 2d Mol file\n
[7] "Species with the same structure:\nCaryophylla-4(14), 8(15)-dien-5-ol\n\n"
[8] "Stereoisomers:\nCaryophylladienol I\nCaryophylla-3(15),7(14)-dien-6-ol\n«alpha»-Caryophylladienol\nExo methylene ...
[9] "Other names:\nCaryophylla-4(14),8(15)-dien-5«alpha»-ol;\nCaryophylla-2(12),6(13)-dien-5-«alpha»-ol;\nCaryophylla ...
[10] "Information on this page:\nGas Chromatography\nReferences\nNotes / Error Report\n\n"
[11] "Options:\nSwitch to calorie-based units\n\n" 

Si vous n'écrivez que dans un fichier, vous pouvez corriger la liste délimitée dans l'élément 8 (remplacer les nouvelles lignes avec point-virgule) et supprimer les nouvelles lignes restantes.

x <- gsub(":\n", ": ", x) 
x[8] <- gsub("\n+", ";", x[8])
x <- gsub("\n", "", x)
x <- gsub("Download the identifier in a file.", "", x)

Utilisez readhtmltable pour les tables

y <-readHTMLTable(doc, stringsAsFactors=FALSE)

Puis comptez les lignes pour trouver la table correcte et obtenir des valeurs

sapply(y, nrow)
NULL NULL NULL NULL NULL NULL 
   1    1    5   13    6    1 

y[[4]][,2:3]
    Active phase     I
1        HP-5 MS 1637.
2        DB-5 MS 1631.
3          RTX-1 1627.
4  Col-Elite 5MS 1656.
5           DB-5 1615.
...

ri <- paste0(gsub(".", "", y[[4]][,3], fixed=TRUE), "=", y[[4]][,2], collapse=";")
ri
[1] "1637=HP-5 MS;1631=DB-5 MS;1627=RTX-1;1656=Col-Elite 5MS;1615=DB-5;1638=DB-5;1628=DB-5;1602=DB-1;1611=DB-5;1635=CP Sil 5 CB;1622=BP-1;1622=RTX-1;1627=DB-5"

Enfin, combiner et écrire dans un fichier

cas <- c(paste("Name:", name), x[c(1:5,7:9)], paste("RI:", ri) )
write( cas, file="cas.out")

Il existe d'autres moyens de saisir les valeurs dans des listes non ordonnées, par exemple, d'obtenir tous les stéréoisomères comme un vecteur ...

stereo <- xpathSApply(doc, "//li/strong[text()='Stereoisomers:']/../ul/li/a", xmlValue)
 [1] "Caryophylladienol I"                       "Caryophylla-3(15),7(14)-dien-6-ol"         "«alpha»-Caryophylladienol"                
 [4] "Exo methylene isomer of Caryophyllenol I"  "«beta»-Caryophylla-4(14),8(15)-dien-5-ol"  "Caryophylla-4(12),8(13)-dien-5-«beta»-ol" 
 [7] "Caryophylla-4,8-dien-5-ol"                 "Caryophylla-4(12),8(13) diene 5 «beta»-ol" "Caryophyla-4(14),8(15)-dien-5-ol"         
[10] "Caryophylla-4(12).8(13)-diene-5«beta»-ol"  "2(12),6(13)-Caryophylladien-5-ol"

puis écrivez plusieurs lignes dans un fichier à la place.

paste("Stereoisomer:", stereo)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top