gicrisf / tochemfig

Make Emacs write chemfig code from molfile or SMILES.
MIT License
9 stars 0 forks source link

+title: tochemfig.el

+author: Giovanni Crisalfi

/Manipulating molecules with Emacs/.

=tochemfig= is an Emacs interface to =mol2chemfig=.

[[./default-demo.gif]]

+begin_quote

=mol2chemfig= is a Python program that generates TeX graphics of chemical structures provided in molfile or SMILES format. Its output is written in the syntax of the chemfig package, which in turn is based on TiKZ.

+end_quote

Thus, both LaTeX/TiKZ and Python/mol2chemfig are required in order to compile =tochemfig='s output.

This package only requires access to =mol2chemfig= through the shell. This means you can set up a working Python 3 environment and launch =mol2chemfigPy3= or keep using the original =mol2chemfig= in Python 2. You could use the LUA web client too, if you are more comfortable with it, but of course that way you would depend on internet access.

Most linux distros come with a working Python 3 version already installed. To keep things simple, you could install =mol2chemfigPy3= with this command:

+begin_src bash

pip install -U mol2chemfigPy3

+end_src

+begin_src

CN1C=NC2=C1C(=O)N(C(=O)N2C)C

+end_src

You can turn this string into chemfig's code by running =M-x chemfig-input-direct=.

Gif

The minibuffer will request an input: copy-paste the SMILES, press =RET= and Emacs will do the magic, inserting the final result right into your buffer.

+begin_export latex

\chemfig{ % 1 -[:42]N% 2 -[:96]% 3 =[:24]N% 4 -[:312]% 5 =[:240]% 6 ( -[:168]\phantom{N}% -> 2 ) -[:300]% 7 ( =[:240]O% 8 ) -N% 9 ( -[:300]% 14 ) -[:60]% 10 ( =O% 11 ) -[:120]N% 12 ( -[:180]% -> 5 ) -[:60]% 13 }

+end_export

If we export the LaTeX document as a PDF, we get the following result

+DOWNLOADED: screenshot @ 2022-11-08 00:39:41

+CAPTION: Caffeine rendered by LaTeX/TiKZ

[[file:Quickstart/2022-11-08_00-39-41_screenshot.png]]

which is fine, but maybe we're used to see the xanthines with a slight rotation, with the imidazole ring on the right and parallel to the ground.

+DOWNLOADED: screenshot @ 2022-11-08 00:50:01

+Caption: Caffeine rendered by LaTeX/TiKZ, flipped and rotated

[[file:Quickstart/2022-11-08_00-50-01_screenshot.png]]

To obtain this result, we can rotate the molecule by an angle of -30° and flip it horizontally, all with a single act: running =M-X chemfig-rotate= on the same input (as long as the "direct" input is set as default).

+begin_src emacs-lisp

(setq tochemfig-default-input 'direct')

+end_src

If you're not familiar with changing the default variables in your config file, read the usage section of this file.

On Doom Emacs, you could install directly from the Github repo:

+begin_src emacs-lisp :tangle packages.el :noweb yes

(package! tochemfig :recipe (:host github :repo "gicrisf/tochemfig"))

+end_src

On vanilla Emacs, you can do the same using =use-package= or =straight=. An example with =straight=:

+begin_src emacs-lisp

(straight-use-package '(tochemfig :type git :host github :repo "gicrisf/tochemfig"))

+end_src

  1. To use =tochemfig-custom-raw=, you can write the flags for the shell command. This method may not be as comfortable as the other options, but it is immediate if you are accustomed to the underlying CLI interface.
  2. Other =tochemfig-= functions allow you to overwrite a single parameter with different settings than the defaults. For example, =tochemfig-input-direct= changes the input mode to allow pasting a SMILES directly into the minibuffer.

    [[./custom-one-edit-demo.gif]]

The last case is better explained through a contextualized, applied example. Assuming you always get your molecules from an external file and set "file" as the default input, what do you do if you need to input a SMILES directly into the minibuffer? You could use =tochemfig-custom=, but that may be too time consuming if you only need to make a single change from the defaults. Instead, use =tochemfig-input-direct= to change the input mode temporarily. Similarly, if you want to keep all default settings but remove whitespaces when rendering, use =tochemfig-terse=. The same goes for any other parameter in the list.

It's important to note that in this library the same defaults of the original =mol2chemfig= have been kept to avoid confusion among users. To set your custom defaults, simply reset the default command by specifying new defaults in your configuration file.

+begin_src emacs-lisp

;; We can change the default simply resetting the default var. ;; The default input expected is a mol file; (setq tochemfig-default-input 'direct')

;; The default option provides absolute angles; (setq tochemfig-default-relative-angles t)

;; This option improves the aesthetics of double and triple bonds; (setq tochemfig-default-fancy-bonds t)

;; We can wrap the code in \chemfig latex command; (setq tochemfig-default-wrap-chemfig t)

+end_src

Launching =tochemfig-default= you ask Emacs to execute the command by the defaults without losing time asking anything and you must specify only the molecule you want to draw. If you usually use files as sources, maybe you will prefer to leave =tochemfig-default-input= as 'file'.

The other functions overwrite a specific argument over the default. Maybe sometimes you prefer throwing the SMILES of a molecule directly in the minibuffer, so, although your default is set to 'file', you can very easily by calling =tochemfig-input-direct=.

This way, every other preference is leaved untouched (and so you can keep the fancy bonds you usually expect from your outputs).

The defaults have impact on any function you launch except for =tochemfig-custom-raw=. Yes, =tochemfif-custom= is influenced too, because the first configuration is based on the actual defaults. You can change any value before the rendering and checking wheter a parameter is already selected or not.

To have a complete view on all the features provided, install the package and launch =M-x tochemfig=.

[ ] Wrapper for org mode (export block as LaTeX).

In questa libreria devo limitarmi SOLO a creare un'interfaccia per LaTeX

E quindi ricalcare SOLO le funzioni previste da mol2chemfig

[ ] Add a drawer for collecting metadata about the generated molecule

This could give me the opportunity to "re-render" the same molecule on place

Ma il drawer è una prerogativa di Org mode!

Meglio inserire queste funzioni in una libreria a parte

Potrebbe chiamarsi chemorg; chemutils;

(vedi wikinforg for wikinfo, anche per come ha implementato il drawer)

The drawer should collect:

- The name of the molecule (if given)

- The name of the file (if it's from file)

- A list of flag

This way I could edit the chemfig code, then re-render with other options

Per esempio, potrei scrivere la versione coi carbonio espliciti (più leggibile)

Alterare le funzioni che mi interessano, poi ri-renderizzare con carboni impliciti

Can I convert chemfig to SMILES?

L'ideale sarebbe produrre sul posto l'immagine

Sia per la maggiore integrazione in org-mode e quindi più facile esportazione in:

- HTML

- ODT (ecc.)

Sia perché così potrei visualizzare il risultato delle modifiche in tempi ridotti

libchemfig potrebbe collezionare molecole

Ma a che pro, quando c'è pubchem? Poi perché collezionarle in chemfig?

Tanto vale collezionarle in SMILES, no?

Eh, ma potrei conservarle come submols già utilizzabili, proprio importare le submols

[[https://ko-fi.com/V7V425BFU][https://ko-fi.com/img/githubbutton_sm.svg]]