2
$\begingroup$

Background Information: In Mathematica 12, SDF Import automatically triggers the generation of Molecule[] representations. SDF files contain both structural information as well as metadata, and a valid SDF need not contain the structural information.

Problem: Mathematica fails to import valid Metadata, if structure information is missing. This is out of spec for the SDF format.

Demonstration: First, retrieve an SDF file containing two substance records from PubChem

retrieveSDF[sids_List] := With[
  {url = URLBuild[
     {"https://pubchem.ncbi.nlm.nih.gov/rest/pug/substance/sid/",
      StringRiffle[sids, ","], (*comma-separated SIDs*)
      "/record/sdf"}]},
  URLExecute[url, {}, "Text"]]

ImportString[retrieveSDF[{50608295, 3272}], "SDF"]

output from ImportString This is totally understandable---substance 50608295 doesn't contain any structure information, so it is asking too much to generate a Molecule[].

But it's record does contain valid Metadata. Writing the SDF to disk and Import[]-ing only the "Metadata" data elements returns a failure.

Export["example.sdf", retrieveSDF[{50608295, 3272}], "Text"]
Import["example.sdf", {"Metadata"}]

output from Import of Metadata only

(despite the fact that valid Metadata is in fact available in the example.sdf file---seen by Import["example.sdf", "Text"] or just opening)

Audience participation question: Can anyone try this on Mathematica <12 ? SDF handling has been around since version 7, but I suspect that the use of the new Molecule[] functionality in version 12 has broken this.

Update: @BobTheChemist reports that it behaves properly (returns Metadata) in Mathematica 11.3, so I've upgraded the question to the "bug" tag)

$\endgroup$
  • 1
    $\begingroup$ V11.3 yields Metadata for the record. It reports None instead of $Failed for the ImportString command. $\endgroup$ – bobthechemist Dec 11 '19 at 18:19
  • 1
    $\begingroup$ I can fix this for 12.1. $\endgroup$ – Jason B. Dec 11 '19 at 21:19
2
$\begingroup$

One of the support functions called by Import is checking if the molecule has no atoms, and if so then it returns $Failed. This is just because the developer was short-sighted and didn't see a use for a molecule-less SDF file.

This is fixed in the current sources for the next release. For version 12.0 we can do a "surgical strike" on the existing code to fix the issue.

Molecule;(*this symbol will autoload the package*)
DownValues[Chemistry`Private`MoleculeFileParseDump`getMoleculeFromSDF] = ReplaceAll[
    DownValues @ Chemistry`Private`MoleculeFileParseDump`getMoleculeFromSDF,
    HoldPattern[If[Less[_["atomCount", _], 1], ___]] :> Null
]

Evaluating the above should fix the problem.

$\endgroup$
  • $\begingroup$ Confirming that this fixes the issue in version 12.0: generic Import[] returns Molecule[]s with zero atoms having the correct MetaInformation; Import[] of Metadata only correctly returns an Association. $\endgroup$ – Joshua Schrier Dec 12 '19 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.