6
$\begingroup$

So, I am developing a GUI for a python computational chemistry software (with PyQt5). The Qt part of the GUI will handle running the python software (calling the proper functions with proper arguments etc. i.e. will replace the need to write python code to use the software). However, the GUI also needs to be able to draw molecules and feed that information to the software. (The software by default can take SMILES, xyz, mol2 as input.)

So I essentially need a molecule drawing software that can be called from python GUI and will provide information about the drawn molecule back to the GUI. (Think something like the pubchem sketcher, where you click a button to open a sketcher and when you close it, the molecule gets passed to pubchem). Bonus points if the sketcher is 3D, like Avogadro or GaussView. I can also do with 2D sketchers like ChemDraw or Marvin if 3D is unavailable.

I know that Avogadro is supposed to have a python interface, but documentation is so sparse, that I have not been able to understand how to run it. So, is there a molecule builder or drawer software that can be interfaced to python easily? I need the molecule drawer to be callable or executable from within python. I am looking for molecule sketching softwares where the users can draw their molecules in the GUI by hand, I do not mean visualisation softwares!

$\endgroup$
3
  • 1
    $\begingroup$ Could add a more detailed answer if you like, but Pymol seems like it would fit your needs well. $\endgroup$
    – Tyberius
    Commented Oct 16, 2022 at 15:58
  • $\begingroup$ @Tyberius Pymol seems a bit too large and complicated (i.e. loads of features that I don't really need). I just need to draw 3D molecules and then pass it off to the python script. $\endgroup$
    – S R Maiti
    Commented Oct 17, 2022 at 10:26
  • 1
    $\begingroup$ BTW, there's an Avogadro forum and of course the GitHub issue tracker. I'm more than happy to discuss interfacing Avogadro with other programs .. should be fairly easy IMHO. $\endgroup$ Commented Feb 11, 2023 at 4:28

2 Answers 2

6
$\begingroup$

You can use RDkit to draw 2D structures of molecules.
You can use RDkit + py3Dmol to draw 3d structures of molecules.

You can install them with pip like this:

pip install rdkit
pip install py3Dmol

3D structures

import py3Dmol
from rdkit import Chem
from rdkit.Chem import AllChem

def MolTo3DView(mol, size=(300, 300), style="stick", surface=False, opacity=0.5):
    """Draw molecule in 3D"""
    assert style in ('line', 'stick', 'sphere', 'carton')
    mblock = Chem.MolToMolBlock(mol)
    viewer = py3Dmol.view(width=size[0], height=size[1])
    viewer.addModel(mblock, 'mol')
    viewer.setStyle({style:{}})
    if surface:
        viewer.addSurface(py3Dmol.SAS, {'opacity': opacity})
    viewer.zoomTo()
    return viewer

def smi2conf(smiles):
    '''Convert SMILES to rdkit.Mol with 3D coordinates'''
    mol = Chem.MolFromSmiles(smiles)
    if mol is not None:
        mol = Chem.AddHs(mol)
        AllChem.EmbedMolecule(mol)
        AllChem.MMFFOptimizeMolecule(mol, maxIters=200)
        return mol
    else:
        return None

smi = 'O=C(O)C(O)C(O)C(=O)O'
conf = smi2conf(smi)
# This will open an interactive image viewer
viewer = MolTo3DView(conf, size=(600, 300), style='sphere')
viewer.show()

3d_structure

2D structures

Here is a sample code to draw 2D diagram of tartaric acid:

from rdkit import Chem
from rdkit.Chem import Draw

input_smiles = 'O=C(O)C(O)C(O)C(=O)O'
mol = Chem.MolFromSmiles(input_smiles)
Draw.MolToImage(mol)

without_numbering

You can also specify options such as numbering the individual atoms like this:

from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import rdMolDraw2D

input_smiles = 'O=C(O)C(O)C(O)C(=O)O'
mol = Chem.MolFromSmiles(input_smiles)
dos = Draw.MolDrawOptions()
dos.addAtomIndices=True
Draw.MolToImage(mol, options= dos)

with_numbering

$\endgroup$
6
  • 1
    $\begingroup$ +1 and welcome to our new community! Thanks for contributing this answer and we hope to see much more of you in the future!!! $\endgroup$ Commented Feb 9, 2023 at 14:27
  • 1
    $\begingroup$ Thanks for the warm welcome @NikeDattani. Will definitely contribute more ! I just started reviving my love for chemistry with the help of python (which I love as well). $\endgroup$ Commented Feb 9, 2023 at 14:30
  • 1
    $\begingroup$ Thanks for the answer, but it is unrelated to the question. I was looking for molecule sketchers, where users can draw molecules, not how to visualise molecules from python. If you know 3D or 2D sketcher softwares that would be helpful to me. I already know about rdkit's visualisation. $\endgroup$
    – S R Maiti
    Commented Feb 9, 2023 at 15:04
  • $\begingroup$ I think the original version of the question might not have made it clear enough that the molecule has to be "sketched". The question asked about drawing, which means that import Draw from rdkit.Chem would seem like a reasonable solution. Were the suggestions in Shern Ren Tee's answer helpful? $\endgroup$ Commented Feb 10, 2023 at 2:58
  • $\begingroup$ @NikeDattani I used the word "sketcher" multiple times in the OP, and said that the drawing needs to be fed back into the python. I also gave the example of pubchem sketcher, avogadro and gaussview as similar examples. I think it is very clear even without the last edit. I will check the suggestions from the other soon. $\endgroup$
    – S R Maiti
    Commented Feb 10, 2023 at 10:01
1
$\begingroup$

You may be interested in the venerable JSME, used by web applications like WebMO and the Automated Topology Builder, or SketChem which is now in development.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .