Re: [Rdkit-discuss] Display Molecules within IPython Console

2020-09-01 Thread Scalfani, Vincent
Hi Greg,

Draw.ShowMol(m) works perfectly and even works in a loop to view multiple 
molecules (one at a time). thanks!

Hi Ivan, thanks for the terminal graphics code example. I think the ASCII art 
structures are fun and useful. I've used that in Open Babel before to quickly 
view molecules. Such an ASCII structure view method in RDKit would be great and 
I would be interested in trying it.

Vin


From: Ivan Tubert-Brohman 
Sent: Tuesday, September 1, 2020 9:29 AM
To: Scalfani, Vincent 
Cc: rdkit-discuss@lists.sourceforge.net 
Subject: [EXTERNAL] Re: [Rdkit-discuss] Display Molecules within IPython Console

Hi Vin,

If you are running the IPython console on a terminal emulator that supports 
graphics, you could display the molecule by printing out the necessary terminal 
escape codes followed by the image buffer. The solution is terminal-specific; 
here's an example that works using the Kitty terminal:

from base64 import standard_b64encode
import io

def serialize_gr_command(cmd, payload=None):
   cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
   ans = []
   w = ans.append
   w(b'\033_G'), w(cmd.encode('ascii'))
   if payload:
  w(b';')
  w(payload)
   w(b'\033\\')
   return b''.join(ans)

def write_chunked(cmd, data):
   data = standard_b64encode(data)
   while data:
  chunk, data = data[:4096], data[4096:]
  m = 1 if data else 0
  cmd['m'] = m
  sys.stdout.buffer.write(serialize_gr_command(cmd, chunk))
  sys.stdout.flush()
  cmd.clear()

def cat_mol(mol):
img = Chem.Draw.MolToImage(mol)
buf = io.BytesIO()
img.save(buf, format='png')
write_chunked({'a': 'T', 'f': 100}, buf.getvalue())

And then you can do something like this:
[image.png]

I'm sure the above could be adapted to work with iterm2 or other terminals that 
support graphics, but that's left as an exercise to the reader.

An alternative I often use when graphics are not available is to render the 
structure as ASCII art, but that may be an acquired taste. The above would look 
like this:

[image.png]

I'll leave the code that does that for some other day.

Best,
Ivan

On Tue, Sep 1, 2020 at 9:57 AM Scalfani, Vincent 
mailto:vfscalf...@ua.edu>> wrote:
Hello,

Is it possible to display a molecule image directly in an IPython console (not 
a Jupyter Notebook)? Or maybe I need to send the image file directly to my 
image viewer? I would like to be able to quickly view the molecules without 
using a Jupyter Notebook or having to save the PNGs. For example:

In [6]: from rdkit import Chem
   ...: from rdkit.Chem.Draw import IPythonConsole
   ...: from rdkit.Chem import Draw

In [7]: m = Chem.MolFromSmiles('c1ncncc1C(=O)[O-]')

In [8]: m
Out[8]: 

In [9]: Chem.Draw.MolToImage(m)
Out[9]: 


Thanks for your help.

Vin


---

Vincent F. Scalfani

The University of Alabama

<mailto:vfscalf...@ua.edu>


___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net<mailto:Rdkit-discuss@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss


Re: [Rdkit-discuss] Display Molecules within IPython Console

2020-09-01 Thread Ivan Tubert-Brohman
Hi Vin,

If you are running the IPython console on a terminal emulator that supports
graphics, you could display the molecule by printing out the necessary
terminal escape codes followed by the image buffer. The solution is
terminal-specific; here's an example that works using the Kitty terminal:

from base64 import standard_b64encode
import io

def serialize_gr_command(cmd, payload=None):
   cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
   ans = []
   w = ans.append
   w(b'\033_G'), w(cmd.encode('ascii'))
   if payload:
  w(b';')
  w(payload)
   w(b'\033\\')
   return b''.join(ans)

def write_chunked(cmd, data):
   data = standard_b64encode(data)
   while data:
  chunk, data = data[:4096], data[4096:]
  m = 1 if data else 0
  cmd['m'] = m
  sys.stdout.buffer.write(serialize_gr_command(cmd, chunk))
  sys.stdout.flush()
  cmd.clear()

def cat_mol(mol):
img = Chem.Draw.MolToImage(mol)
buf = io.BytesIO()
img.save(buf, format='png')
write_chunked({'a': 'T', 'f': 100}, buf.getvalue())

And then you can do something like this:
[image: image.png]

I'm sure the above could be adapted to work with iterm2 or other terminals
that support graphics, but that's left as an exercise to the reader.

An alternative I often use when graphics are not available is to render the
structure as ASCII art, but that may be an acquired taste. The above would
look like this:

[image: image.png]

I'll leave the code that does that for some other day.

Best,
Ivan

On Tue, Sep 1, 2020 at 9:57 AM Scalfani, Vincent  wrote:

> Hello,
>
> Is it possible to display a molecule image directly in an IPython console
> (not a Jupyter Notebook)? Or maybe I need to send the image file directly
> to my image viewer? I would like to be able to quickly view the molecules
> without using a Jupyter Notebook or having to save the PNGs. For example:
>
> In [6]: from rdkit import Chem
>...: from rdkit.Chem.Draw import IPythonConsole
>...: from rdkit.Chem import Draw
>
>
> In [7]: m = Chem.MolFromSmiles('c1ncncc1C(=O)[O-]')
>
>
> In [8]: m
>
> Out[8]: 
>
> In [9]: Chem.Draw.MolToImage(m)
>
> Out[9]:  0x...>
>
>
> Thanks for your help.
>
> Vin
>
> ---
>
> Vincent F. Scalfani
> The University of Alabama
>
> 
>
> ___
> Rdkit-discuss mailing list
> Rdkit-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss


Re: [Rdkit-discuss] Display Molecules within IPython Console

2020-09-01 Thread Greg Landrum
Hi Vin,

I added something a long time ago which still seems to work: Draw.ShowMol:

In [3]: from rdkit.Chem import Draw


In [4]: Draw.ShowMol(m)



That, for me at least, opens a window with an image of the molecule. It's
using tkinter, so it should work on all three supported operating systems.

Unfortunately, at least on my machine, I have to close the window before
the ipython prompt becomes active again.

-greg


On Tue, Sep 1, 2020 at 3:59 PM Scalfani, Vincent  wrote:

> Hello,
>
> Is it possible to display a molecule image directly in an IPython console
> (not a Jupyter Notebook)? Or maybe I need to send the image file directly
> to my image viewer? I would like to be able to quickly view the molecules
> without using a Jupyter Notebook or having to save the PNGs. For example:
>
> In [6]: from rdkit import Chem
>...: from rdkit.Chem.Draw import IPythonConsole
>...: from rdkit.Chem import Draw
>
>
> In [7]: m = Chem.MolFromSmiles('c1ncncc1C(=O)[O-]')
>
>
> In [8]: m
>
> Out[8]: 
>
> In [9]: Chem.Draw.MolToImage(m)
>
> Out[9]:  0x...>
>
>
> Thanks for your help.
>
> Vin
>
> ---
>
> Vincent F. Scalfani
> The University of Alabama
>
> 
>
> ___
> Rdkit-discuss mailing list
> Rdkit-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss