<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Another approach to embedded molecular data in org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2016/03/28/Another-approach-to-embedded-molecular-data-in-org-mode</link>
      <pubDate>Mon, 28 Mar 2016 11:25:16 EDT</pubDate>
      <category><![CDATA[chemistry]]></category>
      <category><![CDATA[orgmode]]></category>
      <category><![CDATA[emacs]]></category>
      <guid isPermaLink="false">c-Frb_EpIvnXnKeo9vGWBKuL3Ak=</guid>
      <description>Another approach to embedded molecular data in org-mode</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-1"&gt;1. Summary thoughts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-2"&gt;2. Appendix of molecules&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-2-1"&gt;2.1. Water&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-2-2"&gt;2.2. Caffeine&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;style&gt;.molecule {background-color:LightSkyBlue;}&lt;/style&gt;
&lt;p&gt;
In the &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2016/03/26/A-molecule-link-for-org-mode/"&gt;last post&lt;/a&gt; we examined a molecule link to a src-block defining a molecule in some format. We blurred the distinction between program and data there. Here we re-separate them to try out some different ideas. We will use an &lt;a href="http://orgmode.org/manual/Special-blocks.html"&gt;org-mode special block&lt;/a&gt; to contain the "data" which is a molecular representation in some format. Then, we will use open-babel to convert the format to various other formats to explore using the data.
&lt;/p&gt;

&lt;p&gt;
Here is a methane molecule (with 4 implicit hydrogens in the SMILES format). We put it in a named special block in org-mode, and even put a header on it to indicate the format and a display name!
&lt;/p&gt;

&lt;div class="molecule" id="methane-smiles"&gt;
&lt;p&gt;
C
&lt;/p&gt;

&lt;/div&gt;

&lt;p&gt;
We can use the SMILES representation block as input to a new command that converts it to the CML format, with coordinates. We use a simple shell command here and pass the contents of the molecule in as a variable. That is nice because in SMILES methane is represented by a single "C", and this CML is &lt;i&gt;much&lt;/i&gt; more verbose.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh" id="methane-cml"&gt;&lt;span style="color: #006FE0;"&gt;echo&lt;/span&gt; $&lt;span style="color: #BA36A5;"&gt;input&lt;/span&gt; | obabel -ismi -o cml --gen3d
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;molecule xmlns="http://www.xml-cml.org/schema"&amp;gt;
 &amp;lt;atomArray&amp;gt;
  &amp;lt;atom id="a1" elementType="C" x3="1.047517" y3="-0.064442" z3="0.060284"/&amp;gt;
  &amp;lt;atom id="a2" elementType="H" x3="2.139937" y3="-0.064341" z3="0.059898"/&amp;gt;
  &amp;lt;atom id="a3" elementType="H" x3="0.683568" y3="-0.799429" z3="-0.661322"/&amp;gt;
  &amp;lt;atom id="a4" elementType="H" x3="0.683566" y3="0.927794" z3="-0.216100"/&amp;gt;
  &amp;lt;atom id="a5" elementType="H" x3="0.683669" y3="-0.321317" z3="1.056822"/&amp;gt;
 &amp;lt;/atomArray&amp;gt;
 &amp;lt;bondArray&amp;gt;
  &amp;lt;bond atomRefs2="a1 a2" order="1"/&amp;gt;
  &amp;lt;bond atomRefs2="a1 a3" order="1"/&amp;gt;
  &amp;lt;bond atomRefs2="a1 a4" order="1"/&amp;gt;
  &amp;lt;bond atomRefs2="a1 a5" order="1"/&amp;gt;
 &amp;lt;/bondArray&amp;gt;
&amp;lt;/molecule&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
We can also use the CML output as input to a command that generates an SVG image, again, passing the CML in via a variable in the header.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh"&gt;&lt;span style="color: #006FE0;"&gt;echo&lt;/span&gt; $&lt;span style="color: #BA36A5;"&gt;cml&lt;/span&gt; | obabel -icml -o svg
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2016-03-28-Another-approach-to-embedded-molecular-data-in-org-mode/methane.svg"&gt; 
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
With our previous &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2016/03/26/A-molecule-link-for-org-mode/"&gt;molecule link&lt;/a&gt; we can refer to these in our text now as &lt;a href="#methane-smiles"&gt;methane-smiles&lt;/a&gt; and &lt;a href="#methane-cml"&gt;methane-cml&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
So far it all looks good. Let us do something new. We will use the SMILES representation to create an &lt;a href="https://wiki.fysik.dtu.dk/ase/ase/atoms.html"&gt;ase.atoms&lt;/a&gt; object in Python. First, we create an xyz format that &lt;code&gt;ase&lt;/code&gt; can read. Rather than clutter up our document with the output, we silence it.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh" id="methane-xyz"&gt;&lt;span style="color: #006FE0;"&gt;echo&lt;/span&gt; $&lt;span style="color: #BA36A5;"&gt;input&lt;/span&gt; | obabel -ismi -o xyz --gen3d
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Now, we can use the string generated in a Python file to generate a tempfile (or you could have saved the result above to a file and just read it in here). I was too lazy to make the file link to the image myself, so I setup a :file header and just print the result to stdout in this block. Although all we do here is create a new image, this demonstrates you can use data from a MOLECULE block and pass it into a Python script where other kinds of calculations might occur.
&lt;/p&gt;


&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;from&lt;/span&gt; ase.io &lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; read, write

&lt;span style="color: #0000FF;"&gt;from&lt;/span&gt; tempfile &lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; mkstemp
&lt;span style="color: #BA36A5;"&gt;fd&lt;/span&gt;, &lt;span style="color: #BA36A5;"&gt;fname&lt;/span&gt; = mkstemp(suffix=&lt;span style="color: #008000;"&gt;".xyz"&lt;/span&gt;)
&lt;span style="color: #0000FF;"&gt;with&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;open&lt;/span&gt;(fname, &lt;span style="color: #008000;"&gt;'w'&lt;/span&gt;) &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; f:
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   f.write(xyz)

&lt;span style="color: #BA36A5;"&gt;atoms&lt;/span&gt; = read(fname)
write(&lt;span style="color: #008000;"&gt;'-'&lt;/span&gt;, atoms, &lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;=&lt;span style="color: #008000;"&gt;"png"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2016-03-28-Another-approach-to-embedded-molecular-data-in-org-mode/methane.png"&gt; 
&lt;/p&gt;
&lt;/div&gt;


&lt;p&gt;
The last point to discuss is discoverability. It would be helpful if we could use a program to "extract" molecular information about the molecules we use in our work. Here is a block that will map over the MOLECULE blocks and summarize what is found with a common format (SMILES again). We generate a table of clickable links to each molecule found in the documents. There is a small appendix in this document containing &lt;a href="#h2o"&gt;h2o&lt;/a&gt; and &lt;a href="#caffeine"&gt;caffeine&lt;/a&gt; that will show in this table.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;mlc-to-smiles&lt;/span&gt; (blk)
  &lt;span style="color: #036A07;"&gt;"Convert a molecule BLK to smiles format using openbabel."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((headers (&lt;span style="color: #0000FF;"&gt;-flatten&lt;/span&gt;
                   (mapcar 'org-babel-parse-header-arguments
                           (org-element-property &lt;span style="color: #006FE0;"&gt;:header&lt;/span&gt; blk))))
         (format (cdr (assoc &lt;span style="color: #006FE0;"&gt;:format&lt;/span&gt; headers)))
         (content (buffer-substring-no-properties
                   (org-element-property &lt;span style="color: #006FE0;"&gt;:contents-begin&lt;/span&gt; blk)
                   (org-element-property &lt;span style="color: #006FE0;"&gt;:contents-end&lt;/span&gt; blk)))
         (tempfile (make-temp-file &lt;span style="color: #008000;"&gt;"obabel-"&lt;/span&gt;)))
    (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; tempfile
      (insert content))

    &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;convert to smiles. This outputs a smiles string and the file it was&lt;/span&gt;
    &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;generated from. I don't know how to suppress the file, so we use awk to&lt;/span&gt;
    &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;just get the SMILEs strings. It is not pretty. I know.&lt;/span&gt;
    (&lt;span style="color: #0000FF;"&gt;prog1&lt;/span&gt;
        (s-trim (shell-command-to-string
                 (format  &lt;span style="color: #008000;"&gt;"obabel %s %s -osmi 2&amp;gt; /dev/null | awk '{print $1}'"&lt;/span&gt;
                          (format &lt;span style="color: #008000;"&gt;"-i%s"&lt;/span&gt; format) tempfile)))
      (delete-file tempfile))))


&lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Generate the table of molecules&lt;/span&gt;
(append '((&lt;span style="color: #008000;"&gt;"Display name"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"Name"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"format"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"SMILES representation"&lt;/span&gt;))
        '(hline)
        (org-element-map (org-element-parse-buffer) 'special-block
          (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (sb)
            (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (string= &lt;span style="color: #008000;"&gt;"MOLECULE"&lt;/span&gt; (org-element-property &lt;span style="color: #006FE0;"&gt;:type&lt;/span&gt; sb))
              (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((headers (&lt;span style="color: #0000FF;"&gt;-flatten&lt;/span&gt;
                              (mapcar 'org-babel-parse-header-arguments
                                      (org-element-property &lt;span style="color: #006FE0;"&gt;:header&lt;/span&gt; sb)))))

                (list
                 (format &lt;span style="color: #008000;"&gt;"[[molecule:%s][%s]]"&lt;/span&gt; (org-element-property &lt;span style="color: #006FE0;"&gt;:name&lt;/span&gt; sb)
                         (cdr (assoc &lt;span style="color: #006FE0;"&gt;:display-name&lt;/span&gt; headers)))
                 (org-element-property &lt;span style="color: #006FE0;"&gt;:name&lt;/span&gt; sb)
                 (cdr (assoc &lt;span style="color: #006FE0;"&gt;:format&lt;/span&gt; headers))
                 (mlc-to-smiles sb)))))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th scope="col" class="left"&gt;Display name&lt;/th&gt;
&lt;th scope="col" class="left"&gt;Name&lt;/th&gt;
&lt;th scope="col" class="left"&gt;format&lt;/th&gt;
&lt;th scope="col" class="left"&gt;SMILES representation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;&lt;a href="#methane-smiles"&gt;methane-smiles&lt;/a&gt;&lt;/td&gt;
&lt;td class="left"&gt;methane-smiles&lt;/td&gt;
&lt;td class="left"&gt;smiles&lt;/td&gt;
&lt;td class="left"&gt;C&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="left"&gt;&lt;a href="#h2o"&gt;h2o&lt;/a&gt;&lt;/td&gt;
&lt;td class="left"&gt;h2o&lt;/td&gt;
&lt;td class="left"&gt;cml&lt;/td&gt;
&lt;td class="left"&gt;OO&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="left"&gt;&lt;a href="#caffeine"&gt;caffeine&lt;/a&gt;&lt;/td&gt;
&lt;td class="left"&gt;caffeine&lt;/td&gt;
&lt;td class="left"&gt;xyz&lt;/td&gt;
&lt;td class="left"&gt;Cn1cnc2n(C)c(=O)n(C)c(=O)c12&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
That seems pretty discoverable to me. We not only can discover the molecules in this post, but can pretty easily convert them to other formats (SMILES) in this case. Since we can run any code we want on them, we could just as well import them to a database, or do subsequent calculations on them.
&lt;/p&gt;

&lt;p&gt;
The MOLECULE block is not standard, and I have only demonstrated here that it is suitable for this purpose. But, it looks like we could extend it and deal with a variety of formats. We can use headers to add metadata, format, etc&amp;#x2026; Some features I find missing are similar to those in code blocks where we can type C-c ' to edit them in special modes, and the nice syntax highlighting that often comes with that.
&lt;/p&gt;

&lt;p&gt;
It might be helpful to make the export of MOLECULE blocks nicer looking and more functional. The default export, for example doesn't put an id attribute in the block. First, we rewrite an org-function to add the id attribute to the exported blocks so our molecule links will work.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-html-special-block&lt;/span&gt; (special-block contents info)
  &lt;span style="color: #036A07;"&gt;"Transcode a SPECIAL-BLOCK element from Org to HTML.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;CONTENTS holds the contents of the block.  INFO is a plist&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;holding contextual information."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((block-type (downcase
                      (org-element-property &lt;span style="color: #006FE0;"&gt;:type&lt;/span&gt; special-block)))
         (contents (&lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; contents &lt;span style="color: #008000;"&gt;""&lt;/span&gt;))
         (html5-fancy (&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (org-html-html5-p info)
                           (plist-get info &lt;span style="color: #006FE0;"&gt;:html-html5-fancy&lt;/span&gt;)
                           (member block-type org-html-html5-elements)))
         (attributes (org-export-read-attribute &lt;span style="color: #006FE0;"&gt;:attr_html&lt;/span&gt; special-block)))
    (&lt;span style="color: #0000FF;"&gt;unless&lt;/span&gt; html5-fancy
      (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((class (plist-get attributes &lt;span style="color: #006FE0;"&gt;:class&lt;/span&gt;)))
        (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; attributes (plist-put attributes &lt;span style="color: #006FE0;"&gt;:class&lt;/span&gt;
                                    (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; class (concat class &lt;span style="color: #008000;"&gt;" "&lt;/span&gt; block-type)
                                      block-type)))
        (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (org-element-property &lt;span style="color: #006FE0;"&gt;:name&lt;/span&gt; special-block)
          (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; attributes (plist-put
                            attributes &lt;span style="color: #006FE0;"&gt;:id&lt;/span&gt;
                            (org-element-property &lt;span style="color: #006FE0;"&gt;:name&lt;/span&gt; special-block))))))
    (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; attributes (org-html--make-attribute-string attributes))
    (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (not (equal attributes &lt;span style="color: #008000;"&gt;""&lt;/span&gt;))
      (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; attributes (concat &lt;span style="color: #008000;"&gt;" "&lt;/span&gt; attributes)))
    (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; html5-fancy
        (format &lt;span style="color: #008000;"&gt;"&amp;lt;%s%s&amp;gt;\n%s&amp;lt;/%s&amp;gt;"&lt;/span&gt; block-type attributes
                contents block-type)
      (format &lt;span style="color: #008000;"&gt;"&amp;lt;div%s&amp;gt;\n%s\n&amp;lt;/div&amp;gt;"&lt;/span&gt; attributes contents))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
org-html-special-block
&lt;/pre&gt;

&lt;p&gt;
It would be nice to add some additional information around the block, e.g. that it is a molecule, maybe some tooltip about the format, etc&amp;#x2026;, but we leave that to another day. These should probably be handled specially with a dedicated export function. You will note that MOLECULE blocks don't export too well, they should probably be wrapped in &amp;lt;pre&amp;gt; for HTML export. We will at least make them stand out with this bit of css magic.
&lt;/p&gt;

&lt;pre class="example"&gt;
#+HTML_HEAD_EXTRA:  &amp;lt;style&amp;gt;.molecule {background-color:LightSkyBlue;}&amp;lt;/style&amp;gt;
&lt;/pre&gt;



&lt;div id="outline-container-sec-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Summary thoughts&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
This looks pretty promising as a way to embed molecular data into org-files so that the data is reusable and discoverable. If there is metadata that cannot go into the MOLECULE format we can put it in headers instead. This seems like it could be useful.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-sec-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; Appendix of molecules&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;/div&gt;&lt;div id="outline-container-sec-2-1" class="outline-3"&gt;
&lt;h3 id="sec-2-1"&gt;&lt;span class="section-number-3"&gt;2.1&lt;/span&gt; Water&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-1"&gt;
&lt;p&gt;
Here is water in the CML format.
&lt;/p&gt;

&lt;div class="molecule" id="h2o"&gt;
&lt;p&gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;molecule xmlns="&lt;a href="http://www.xml-cml.org/schema"&gt;http://www.xml-cml.org/schema&lt;/a&gt;"&amp;gt;
 &amp;lt;atomArray&amp;gt;
  &amp;lt;atom id="a1" elementType="O"/&amp;gt;
  &amp;lt;atom id="a2" elementType="O"/&amp;gt;
 &amp;lt;/atomArray&amp;gt;
 &amp;lt;bondArray&amp;gt;
  &amp;lt;bond atomRefs2="a1 a2" order="1"/&amp;gt;
 &amp;lt;/bondArray&amp;gt;
&amp;lt;/molecule&amp;gt;
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-sec-2-2" class="outline-3"&gt;
&lt;h3 id="sec-2-2"&gt;&lt;span class="section-number-3"&gt;2.2&lt;/span&gt; Caffeine&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-2"&gt;
&lt;p&gt;
This is a simple xyz format of caffeine.
&lt;/p&gt;

&lt;div class="molecule" id="caffeine"&gt;
&lt;p&gt;
24
&lt;/p&gt;

&lt;p&gt;
C          1.02887       -0.01688       -0.03460
N          2.46332        0.11699       -0.03522
C          3.33799       -0.94083       -0.03530
N          4.59156       -0.53767       -0.03594
C          4.50847        0.82120       -0.03623
N          5.57252        1.69104       -0.03687
C          6.93040        1.17620       -0.03898
C          5.33446        3.06602       -0.03685
O          6.26078        3.88171       -0.03594
N          3.98960        3.48254       -0.03830
C          3.70813        4.90531       -0.04199
C          2.87287        2.63769       -0.03747
O          1.71502        3.04777       -0.03830
C          3.21603        1.25723       -0.03610
H          0.54478        0.95872       -0.03440
H          0.73663       -0.56946        0.86233
H          0.73584       -0.56959       -0.93118
H          3.00815       -1.97242       -0.03493
H          7.67209        1.97927       -0.03815
H          7.07929        0.56516       -0.93486
H          7.08112        0.56135        0.85404
H          4.61163        5.51902       -0.04152
H          3.11230        5.15092        0.84340
H          3.11643        5.14660       -0.93127
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2016 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2016/03/28/Another-approach-to-embedded-molecular-data-in-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>A molecule link for org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2016/03/26/A-molecule-link-for-org-mode</link>
      <pubDate>Sat, 26 Mar 2016 15:28:17 EDT</pubDate>
      <category><![CDATA[chemistry]]></category>
      <category><![CDATA[orgmode]]></category>
      <category><![CDATA[emacs]]></category>
      <guid isPermaLink="false">U1RoQRECYwAfObskx8qSl1Vly28=</guid>
      <description>A molecule link for org-mode</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-1"&gt;1. Appendix of molecules&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-2"&gt;2. smiles major mode&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Here I am exploring some ideas on compact and functional representations of molecules in org-mode. We will use some functionality from OpenBabel (&lt;a href="https://openbabel.org/docs/dev/index.html"&gt;https://openbabel.org/docs/dev/index.html&lt;/a&gt; ) for conversion of formats.
&lt;/p&gt;

&lt;p&gt;
One approach we could use is the &lt;a href="https://en.wikipedia.org/wiki/Simplified_molecular-input_line-entry_system"&gt;SMILES&lt;/a&gt; representation. OpenBabel provides tools to convert SMILES to a visualization like this. Let's check out an old favorite: caffeine.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh"&gt;obabel -:&lt;span style="color: #008000;"&gt;"Cn1cnc2n(C)c(=O)n(C)c(=O)c12"&lt;/span&gt; -osvg
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2016-03-26-A-molecule-link-for-org-mode/out.svg"&gt; 
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
We can imagine the SMILES string is a program, and use an org-mode src block to contain it.  It isn't quite a program, as it is more like data, but we can make the block executable if we define how to "execute" the block, and for that we will just have obabel generate the svg representation of the molecule. Here is our execute function. It simply generates the svg to stdout. We can use a :file header to capture it in a file.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-babel-execute:smiles&lt;/span&gt; (body params)
  (shell-command-to-string
   (format &lt;span style="color: #008000;"&gt;"obabel -:\"%s\" -osvg 2&amp;gt; /dev/null"&lt;/span&gt; body)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
org-babel-execute:smiles
&lt;/pre&gt;

&lt;p&gt;
You can find a smiles block in &lt;a href="#sec-1"&gt;Appendix of molecules&lt;/a&gt; that was adapted from &lt;a href="http://www.daylight.com/dayhtml_tutorials/languages/smiles/smiles_examples.html"&gt;here&lt;/a&gt; .
&lt;/p&gt;

&lt;p&gt;
Now, we need a link to refer to our molecule. We want the follow action to jump to our src block which should have a name. We will have it export as the name of the block linked to the molecule definition. This should work fine for definitions in the document. It is not robust to link to molecules in other org-files in the export. That would require those files to be exported too. For now we just define an HTML export.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;molecule-jump&lt;/span&gt; (name)
  (org-mark-ring-push)
  (org-open-link-from-string (format &lt;span style="color: #008000;"&gt;"[[%s]]"&lt;/span&gt; path)))

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;molecule-export&lt;/span&gt; (path desc backend)
  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((name (&lt;span style="color: #0000FF;"&gt;save-window-excursion&lt;/span&gt;
                (molecule-jump path)
                (org-element-property &lt;span style="color: #006FE0;"&gt;:name&lt;/span&gt; (org-element-context)))))
    (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
     ((eq 'html backend)
      (format &lt;span style="color: #008000;"&gt;"&amp;lt;a href=\"#%s\"&amp;gt;%s&amp;lt;/a&amp;gt;"&lt;/span&gt; name name)))))

(org-add-link-type
 &lt;span style="color: #008000;"&gt;"molecule"&lt;/span&gt;
 'molecule-jump
 'molecule-export)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Now we link to &lt;a href="#LSD"&gt;LSD&lt;/a&gt; and &lt;a href="#ethanol"&gt;ethanol&lt;/a&gt; that allows us to navigate to the definition. We can also refer to a molecule in another file like &lt;a href="#ethanol"&gt;ethanol&lt;/a&gt;. The links are clickable, and should jump to the molecule definition. On export to HTML they will be links to the definition.
&lt;/p&gt;

&lt;p&gt;
Our link provides some limited functionality. We can provide more by making the follow action open a menu for example. Instead, we created a major mode &lt;a href="#sec-2"&gt;here&lt;/a&gt;. It provides a function to convert smiles to CML. It is readily extensible to do other conversions.
&lt;/p&gt;

&lt;p&gt;
One of the reasons we want to have molecules as "data" is so we can find them in our papers. Here is an example of that. We defined two molecules in the Appendix, and we find them here.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(org-element-map (org-element-parse-buffer)
    'src-block
  (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (src)
    (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (string= &lt;span style="color: #008000;"&gt;"smiles"&lt;/span&gt; (org-element-property &lt;span style="color: #006FE0;"&gt;:language&lt;/span&gt; src))
      (org-element-property &lt;span style="color: #006FE0;"&gt;:name&lt;/span&gt; src))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;LSD&lt;/td&gt;
&lt;td class="left"&gt;ethanol&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
There is still a lot to do to make this really functional. For example, we might want to use the molecules to write reactions. We might also want more advanced conversion or lookup functions, and more export options. It might be desirable to have tooltips on the links to see the molecules too. No doubt one might want to fine-tune the way the blocks run, so that options could be passed as header args.
Maybe I will work on that another day.
&lt;/p&gt;

&lt;div id="outline-container-sec-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;a id="ID-1CD759B4-E276-4990-982C-E98CCE5B0517" name="ID-1CD759B4-E276-4990-982C-E98CCE5B0517"&gt;&lt;/a&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Appendix of molecules&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;

&lt;p&gt;
Here is an example smiles block.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;
&lt;label class="org-src-name"&gt;A lysergic acid diethylamide molecule&lt;/label&gt;
&lt;pre class="src src-smiles" id="LSD"&gt;CCN(CC)C(=O)[C@H]1CN(C)[C@@H]2Cc3c[nH]c4cccc(C2=C1)c34
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2016-03-26-A-molecule-link-for-org-mode/lsd.svg"&gt; 
&lt;/p&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;label class="org-src-name"&gt;An ethanol molecule.&lt;/label&gt;
&lt;pre class="src src-smiles" id="ethanol"&gt;CCO
&lt;/pre&gt;
&lt;/div&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2016-03-26-A-molecule-link-for-org-mode/ethanol.svg"&gt; 
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-sec-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;&lt;a id="ID-7978044B-CB2F-4AB3-8142-34A28B3DB201" name="ID-7978044B-CB2F-4AB3-8142-34A28B3DB201"&gt;&lt;/a&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; smiles major mode&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;p&gt;
It would be nice to have a language mode to do special edits of SMILES src blocks. This mode does very little but provide a function that converts SMILES to CML using obabel and open it in a buffer. We redirect stderr to /dev/null to avoid seeing the messages from obabel. We also provide another function that opens a browser to names of the molecule.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;require&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;easymenu&lt;/span&gt;)

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;smiles-cml&lt;/span&gt; ()
  &lt;span style="color: #036A07;"&gt;"Convert the smiles string in the buffer to CML."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;interactive&lt;/span&gt;)
  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((smiles (buffer-string)))
    (switch-to-buffer (get-buffer-create &lt;span style="color: #008000;"&gt;"SMILES-CML"&lt;/span&gt;))
    (erase-buffer)
    (insert
     (shell-command-to-string
      (format &lt;span style="color: #008000;"&gt;"obabel -:\"%s\" -ocml 2&amp;gt; /dev/null"&lt;/span&gt;
              smiles)))
    (goto-char (point-min))
    (xml-mode)))

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;smiles-names&lt;/span&gt; ()
  (&lt;span style="color: #0000FF;"&gt;interactive&lt;/span&gt;)
  (browse-url
   (format &lt;span style="color: #008000;"&gt;"http://cactus.nci.nih.gov/chemical/structure/%s/names"&lt;/span&gt;
           (buffer-string))))

(&lt;span style="color: #0000FF;"&gt;defvar&lt;/span&gt; &lt;span style="color: #BA36A5;"&gt;smiles-mode-map&lt;/span&gt;
  nil
  &lt;span style="color: #036A07;"&gt;"Keymap for smiles-mode."&lt;/span&gt;)

&lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;adapted from http://ergoemacs.org/emacs/elisp_menu_for_major_mode.html&lt;/span&gt;
(&lt;span style="color: #0000FF;"&gt;define-derived-mode&lt;/span&gt; &lt;span style="color: #006699;"&gt;smiles-mode&lt;/span&gt; fundamental-mode &lt;span style="color: #008000;"&gt;"smiles-mode"&lt;/span&gt;
  &lt;span style="color: #036A07;"&gt;"Major mode for SMILES code."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; buffer-invisibility-spec '(t)
        mode-name &lt;span style="color: #008000;"&gt;" &amp;#9786;"&lt;/span&gt;)

  (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (not smiles-mode-map)
    (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; smiles-mode-map (make-sparse-keymap)))
  (define-key smiles-mode-map (kbd &lt;span style="color: #008000;"&gt;"C-c C-c"&lt;/span&gt;) 'smiles-cml)
  (define-key smiles-mode-map (kbd &lt;span style="color: #008000;"&gt;"C-c C-n"&lt;/span&gt;) 'smiles-names)

  (define-key smiles-mode-map [menu-bar] (make-sparse-keymap))

  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((menuMap (make-sparse-keymap &lt;span style="color: #008000;"&gt;"SMILES"&lt;/span&gt;)))
    (define-key smiles-mode-map [menu-bar smiles] (cons &lt;span style="color: #008000;"&gt;"SMILES"&lt;/span&gt; menuMap))

    (define-key menuMap [cml]
      '(&lt;span style="color: #008000;"&gt;"CML"&lt;/span&gt; . smiles-cml))
    (define-key menuMap [names]
      '(&lt;span style="color: #008000;"&gt;"Names"&lt;/span&gt; . smiles-names))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
smiles-mode
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2016 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2016/03/26/A-molecule-link-for-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
