4
$\begingroup$

Mathematica has supported Chemdraw file (CDX) import since 12.1. However, there appear to be an error when side chains are defined in an abbreviated fashion (as is common in organic chemistry):

Consider the following example Chemdraw file:

url = "https://www.oecd-nea.org/ideal/structures/BuBTBP.cdx?fileKey=238";
Import[url]
%["MolecularFormulaString"]
MoleculePlot[%%]

Molecule imported

(*"C16H6*4N8"*)

molecule picture with atoms replaced by a star

Notice how the resulting molecule has some of the positions replaced with * characters.

If I instead download the file at that URL and open it in the Chemdraw application, I see the following:

screen snapshot of the molecule in chemdraw

(Note how the side chains are defined as implicit butyl groups

Selecting Copy As...>SMILES in ChemDraw puts the following string into the Clipboard, which I can paste in Mathematica to get the correct final molecule, molecular formula, etc:

chemdrawCopyAs = "CCCCC(N=N1)=C(CCCC)N=C1C2=NC(C3=NC(C4=NN=C(CCCC)C(CCCC)=N4)=CC=C3)=CC=C2";
Molecule[%]
%["MolecularFormulaString"]
MoleculePlot[%%]

imported Molecule from SMILES

(*"C32H42N8"*)

picture of the full molecule

$\endgroup$
6
  • $\begingroup$ CDX support is limited. What happens if you export from chem draw to MOL and import that? $\endgroup$
    – Jason B.
    Oct 13, 2023 at 22:35
  • $\begingroup$ @Jason B. : Exporting to a MOL file (using File>Export...) and then using Import to read the resulting MOL file does seem to work correctly. (I'm also hacking out a workaround where I use AppleScript to have ChemDraw Copy-As-SMILES and then read that from the clipboard.) $\endgroup$ Oct 13, 2023 at 22:42
  • 2
    $\begingroup$ Can you please report this as a bug, and include both the "CDX" and "MOL" files? $\endgroup$
    – Jason B.
    Oct 14, 2023 at 13:53
  • $\begingroup$ Aye aye. Submitted. $\endgroup$ Oct 16, 2023 at 16:18
  • $\begingroup$ Coming back to this. Is there any difference if you change the string "C4H9" to one of the forms listed in this file? It looks like "n-Bu" and "nButly" are both listed for this ligand. The CDX file only has the string "C4H9", without any information on the connectivity, so a lookup table of common abbreviations is used. $\endgroup$
    – Jason B.
    Oct 23, 2023 at 20:24

1 Answer 1

1
$\begingroup$

ChemDraw can interpret the correct representation, even if this is not explicitly represented in the CDX file. So the idea is to remotely control ChemDraw to get the information, and to do that we have to build three pieces of functionality:

  1. Drive ChemDraw with AppleScript to open a CDX file, select all of the molecules (there should only be one...), and Edit>Copy As...>SMILES

  2. Retrieve the results from the clipboard and use this to populate the entry in our record (with a sanity check against the stated molecular weight and molar mass)

  3. Automate downloading the CDX files from the URLs, putting the results into a temporary directory to process them

This type of system-level application scripting is going to be very operating system dependent, so the solution below will only work on a Mac; for the record I performed this with MacOS 13.5 and ChemDraw 22.2 (and Mathematica 13.3).

We start by creating a lightweight function wrapper around the osascript command line program which will let us run AppleScripts; this codes is copied directly from a Stack Exchange post on using AppleScript in Mathematica:

(* Code source: https://mathematica.stackexchange.com/questions/36764/how-to-import-a-numbers-spreadsheet/36772#36772 *)
   
   AppleScript["RunFile", file_] := Run["osascript " <> file] 
   
   AppleScript["RunScript", script_] := With[
     {file = ToFileName[$TemporaryDirectory, "script.txt"]}, 
     Export[file, script, "String"]; 
     AppleScript["RunFile", file] 
    ]

After a bit of fumbling around, I devised the following AppleScript which takes a file path (provided as the typical POSIX filepath string) as input and results in the SMILES string on the clipboard. Once you know how to specify keystroke commands in AppleScript, it is relatively straightforward. After getting the script to run in the ScriptEditor, I created the following lightweight function wrapper using a StringTemplate:

scriptTemplate[file_] := AbsoluteFileName[file] // StringTemplate["set p to \"``\"set f to POSIX file ptell application \"ChemDraw\"    open f  activate    tell application \"System Events\"      keystroke \"a\" using {command down}        keystroke \"c\" using {option down, command down}       keystroke \"w\" using {command down}    end tell    delay 0.5end tell"]

(I found that it worked more reliably if I inserted a short delay after the copy and paste operation.) We also need to retrieve the result from the clipboard, as described in this stackoverflow thread (including versions for Windows and Ubuntu; the version implemented below is for MacOS):

(* Code source: https://mathematica.stackexchange.com/a/130224/63709 *)
  Clear[getSMILESFromCDX] 
   getClipboard[___] := Import["!pbpaste", "Text"] 
   
   getSMILESFromCDX[file_] := getClipboard @ AppleScript["RunScript", scriptTemplate[file]] 
   
  (*demo*) 
   getSMILESFromCDX["~/Downloads/BuBTBP.cdx"]

(*"CCCCC(N=N1)=C(CCCC)N=C1C2=NC(C3=NC(C4=NN=C(CCCC)C(CCCC)=N4)=CC=C3)=CC=C2"*)

Finally, we want to be able to retrieve a file from an URL, analogous to Import. We will write a custom function using URLDownload returns a File to a temporary download directory):

importMolFromURL[url_] := Molecule @ getSMILESFromCDX @ URLDownload[url] 
   
(*demo*) 
url = "https://www.oecd-nea.org/ideal/structures/BuBTBP.cdx?fileKey=238"; 
importMolFromURL[url]

picture of successfully imported molecule

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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