I would like to retrieve information associated with a given CAS registry number (Chemical Abstracts Service nr) from the NIST webbook web site in Mathematica, using the provided API (e.g. for geraniol, http://webbook.nist.gov/cgi/cbook.cgi?ID=106-24-1&Units=SI&Mask=2000#Gas-Chrom) (note I use the NCI CADD cactus server to convert InChiKeys to molecular structures if NIST doesn't list the molecular structure).
E.g. for a few example CAS chemical identifiers
casnrs = {"106-24-1", "28115-92-6", "281-17-4", "28122-24-9", "28122-25-0", \
"28122-28-3", "281-23-2", "28123-26-4", "28123-68-4", "28123-70-8"};
I would like to download all associated information listed at the NIST webbook site. What is the best method to go about doing this in Mathematica?
Edit: with the help of some of the posts below I came to the following solution: (below is also an alternative method based on importing the website as XML)
Import molecular structures
getmolstrcactus[casnr_, threed_] :=
Module[{str, notes, inchikey},
If[threed == True, str = "?get3d=true"; notes = "Cactus/Corina MM",
str = ""; notes = "Cactus"];
Quiet[Check[{Import[
"http://cactus.nci.nih.gov/chemical/structure/" <> casnr <>
"/sdf" <> str, "MOL"], "Cactus/Corina MM"}
, inchikey =
StringReplace[
ImportString[
URLFetch["http://webbook.nist.gov/cgi/cbook.cgi",
"Parameters" -> {"ID" -> casnr, "UNITS" -> "SI",
"MASK" -> "2000"}], {"HTML", "Data"}][[2, 4]],
"IUPAC Standard InChIKey: " -> ""];
Check[{Import[
"http://cactus.nci.nih.gov/chemical/structure/" <> inchikey <>
"/sdf" <> str, "MOL"], "Cactus/Corina MM"},
{"NA", "NA"}]]]];
import2Dmolstr[casnr_] :=
Module[{},
raw = Import[
"http://webbook.nist.gov/cgi/cbook.cgi?Str2File=C" <> casnr,
"String"];
If[StringLength[raw] != 183,
{Quiet[ImportString[raw, "MOL"]], "NIST"},
getmolstrcactus[casnr, False]]];
AbsoluteTiming[import2Dmolstr /@ casnrs // TableForm]
import3Dmolstr[casnr_] := Module[{},
Quiet[Check[{Import[
"http://webbook.nist.gov/cgi/cbook.cgi?Str3File=C" <> casnr,
"MOL"], "NIST/MOPAC6/GAUSSIAN94 PM3"},
getmolstrcactus[casnr, True]]]];
AbsoluteTiming[import3Dmolstr /@ casnrs // TableForm]
Import infrared and/or electron ionization mass spectra
importspectrum[casnr_, type_] := Quiet[Module[{}, If[type == "Mass",
Check[
ImportString[
StringReplace[
Import["http://webbook.nist.gov/cgi/cbook.cgi?JCAMP=C" <>
StringReplace[casnr, "-" -> ""] <> "&Index=0&Type=" <>
type, "String"], {x : NumberString ~~ " " ~~
y : NumberString :> x <> "\n" <> y}], "JCAMPDX"][[1]],
"NA"], Check[
Import["http://webbook.nist.gov/cgi/cbook.cgi?JCAMP=C" <>
StringReplace[casnr, "-" -> ""] <> "&Index=0&Type=" <> "IR",
"JCAMPDX"][[1]], "NA"]]]];
importmassspectrum[casnr_] := importspectrum[casnr, "Mass"];
importIRspectrum[casnr_] := importspectrum[casnr, "IR"];
plotIRspec[casnr_] := Module[{}, spec = importIRspectrum[casnr];
Quiet[Check[
ListPlot[spec, Joined -> True, PlotRange -> All,
PlotStyle -> PointSize[0]], "NA"]]];
plotmassspec[casnr_] := Module[{}, spec = importmassspectrum[casnr];
Quiet[Check[
ListPlot[spec, Filling -> Axis, FillingStyle -> Blue,
PlotRange -> All, PlotStyle -> PointSize[0]], "NA"]]];
plotIRspec["7647-01-0"]
AbsoluteTiming[plotmassspec /@ casnrs // TableForm]
Import other info
removeHTML[str_] :=
StringSplit[StringReplace[str, "<" ~~ Except[">"] .. ~~ ">" -> ""],
"\n"];
stringclean[str_] :=
StringReplace[
str, {"»" -> "", "«" -> "", ";" -> "",
"alpha" -> "\[Alpha]", "beta" -> "\[Beta]", "gamma" -> "\[Gamma]",
"delta" -> "\[Delta]", "epsilon" -> "\[Epsilon]"}];
stringcheck[str_, part_] :=
Quiet[Check[
If[StringTake[str, StringLength[part]] == part,
StringReplace[str, part -> ""], "NA"], "NA"]];
CAScactusinfo[casnr_] :=
Quiet[Module[{formula, mw, inchi, inchikey, name, synonyms, source},
formula =
Check[Import[
"http://cactus.nci.nih.gov/chemical/structure/" <> casnr <>
"/formula", "String"], "NA"];
If[formula != "NA",
mw =
Import["http://cactus.nci.nih.gov/chemical/structure/" <>
casnr <> "/mw", "String"];
inchi =
StringSplit[
Import["http://cactus.nci.nih.gov/chemical/structure/" <>
casnr <> "/stdinchi", "String"], "InChI="][[1]];
inchikey =
StringSplit[
Import["http://cactus.nci.nih.gov/chemical/structure/" <>
casnr <> "/stdinchikey", "String"], "InChIKey="][[1]];
name =
Import["http://cactus.nci.nih.gov/chemical/structure/" <>
casnr <> "/iupac_name", "String"];
synonyms =
StringJoin[
Riffle[Import[
"http://cactus.nci.nih.gov/chemical/structure/" <> casnr <>
"/names", "List"], "|"]];
source = "NCI/CADD Cactus Chemical Identifier Resolver";,
{formula, mw, inchi, inchikey, name, synonyms, source} = {"NA",
"NA", "NA", "NA", "NA", "NA", "NA"};];
{formula, mw, inchi, inchikey, name, synonyms, source}]];
CASnistinfo[casnr_] :=
Module[{raw, data, parts, formula, mw, inchi, inchikey, cas, name,
synonyms, stereoisomers, source, cactusinfo},
raw = Check[
ImportString[
URLFetch["http://webbook.nist.gov/cgi/cbook.cgi",
"Parameters" -> {"ID" -> casnr, "UNITS" -> "SI",
"MASK" -> "2000"}], {"String"}], ""];
data = ImportString[raw, {"HTML", "Data"}];
If[And[data != {},
data != {" Standard Reference Data Program", " Data Gateway",
"Chemistry WebBook"}],
parts = {"Formula : ", "Molecular weight : ", "InChI=",
"IUPAC Standard InChIKey: ", "CAS Registry Number: ",
"Other names:", "Stereoisomers:"};
formula =
StringReplace[stringcheck[data[[2, 1]], parts[[1]]], " " -> ""];
mw = stringcheck[data[[2, 2]], parts[[2]]];
cas = casnr;(* stringcheck[data[[2,5]],parts[[5]]]; *)
name =
stringclean[
StringSplit[StringSplit[raw, "<title>"][[2]], "</title>"][[1]]];
synonyms =
Quiet[
Check[If[Length[StringPosition[raw, parts[[6]]]] != 0,
Quiet[stringclean[
StringJoin[
Riffle[removeHTML[
StringSplit[
StringSplit[raw, "strong>Other names:</strong>"][[2]],
"<strong>"][[1]]], "|"]]]], "NA"], "NA"]];
stereoisomers =
Quiet[Check[
If[Length[StringPosition[raw, parts[[7]]]] != 0,
Quiet[stringclean[
StringJoin[
Riffle[removeHTML[
StringSplit[
StringSplit[raw, "strong>Stereoisomers:</strong>"][[
2]], "<strong>"][[1]]], "|"]]]], "NA"], "NA"]];
inchi = Quiet[stringcheck[data[[2, 3, 1, 1]], parts[[3]]]];
inchikey = stringcheck[data[[2, 4]], parts[[4]]];
If[inchi == "NA", cactusinfo = CAScactusinfo[casnr];
inchi = cactusinfo[[3]];
inchikey = cactusinfo[[4]];
source =
"NIST Webbook and NCI/CADD Cactus Chemical Identifier \
Resolver";, source = "NIST Webbook";];
{casnr, formula, mw, inchi, inchikey, cas, name, synonyms,
stereoisomers, source},
{formula, mw, inchi, inchikey, name, synonyms, source} =
CAScactusinfo[casnr];
{casnr, formula, mw, inchi, inchikey, casnr, name, synonyms, "NA",
source}]];
AbsoluteTiming[CASnistinfo /@ casnrs // TableForm]
Import gas chromatography retention indices
(Note: here I partly parse the raw HTML, in the post below there is also a nice solution based on importing as XML)
CASnistRIs[casnr_] :=
Quiet[Module[{raw, raw2, fun, RItypes, nRItypes, data, dat, pos,
RItypecol, RIdata, inchikey},
Check[
raw = URLFetch[
"http://webbook.nist.gov/cgi/cbook.cgi?ID=" <> casnr <>
"&Units=SI&Mask=2000#Gas-Chrom"];
raw2 =
Drop[StringSplit[
StringSplit[
StringSplit[raw,
"<h2><a id=\"Gas-Chrom\" name=\"Gas-Chrom\">Gas \
Chromatography</a></h2>"][[2]],
"<h2><a id=\"Refs\" name=\"Refs\">References"][[1]],
"<h3>"], 1];
fun[s_] := StringSplit[StringSplit[s, "Type="][[2]], "#"][[1]];
RItypes = Map[fun, raw2];
nRItypes = Length[RItypes];
data = ImportString[raw, {"HTML", "Data"}];
pos = Position[data, "Column type"][[1, 1]];
RItypecol = {};
RIdata = ArrayFlatten[Table[dat = Drop[data[[pos + i - 1]], 1];
If[Dimensions[dat][[2]] == 5,
dat = Transpose[
Insert[Transpose[dat],
Table["NA", {Dimensions[dat][[1]]}], 3]];];
RItypecol =
Append[RItypecol,
Table[RItypes[[i]], {Dimensions[dat][[1]]}]];
dat, {i, 1, nRItypes}], 1];
RItypecol = Flatten[RItypecol];
RIdata = Transpose[Insert[Transpose[RIdata], RItypecol, 1]];
RIdata =
Transpose[
Insert[Transpose[RIdata],
Table[casnr, {Dimensions[RIdata][[1]]}], 1]];
data = ImportString[raw, {"HTML", "Data"}];
inchikey = stringcheck[data[[2, 4]], "IUPAC Standard InChIKey: "];
If[inchikey == "NA",
inchikey =
Quiet[Check[
StringSplit[
Import["http://cactus.nci.nih.gov/chemical/structure/" <>
casnr <> "/stdinchikey", "String"], "InChIKey="][[1]],
"NA"]];];
RIdata =
Transpose[
Insert[Transpose[RIdata],
Table[inchikey, {Dimensions[RIdata][[1]]}], 2]];
RIdata =
Insert[RIdata, {"CAS nr", "InChiKey", "RI type", "Column",
"Active phase", "Temperature (\[Degree]C)", "RI", "Reference",
"Temperature program"}, 1], "NA"]
]];
AbsoluteTiming[CASnistRIs["106-24-1"] // TableForm ]
ImportString[raw, {"HTML", "Data"}]
will be an easier starting point $\endgroup$raw
data. Would you please clarify for the chemically illiterate? Thanks. :) $\endgroup$ImportString
. $\endgroup$