1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How can I plot a chemical structure with repeat units using Python?

Discussão em 'Python' iniciado por Stack, Setembro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to use Python to generate figures of the repeat units of a range of polymers, using the common notation of square brackets with a subscript "n". Here is an example from Wikipedia:

    [​IMG]

    I have found two widely distributed libraries that allow plotting chemical structures: openbabel with pybel, and rdkit. In the following, I will focus on rdkit, since it seemed to be more promissing, but, as long as it works, I would not care which library (for now ;).

    So, here's what I tried, basic premise:

    #!/usr/bin/env python

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.patches import Rectangle
    from rdkit import Chem
    from rdkit.Chem import Draw


    def plot_stereochemistries():
    """Generate and save plots of polyisoprene stereochemistries"""

    repeat_unit_14_cis = r"C\C=C(C)/C"

    mol_cis = Chem.MolFromSmiles(repeat_unit_14_cis)

    # Create a subplot
    fig, ax = plt.subplots(1, 1, figsize=(10, 10))

    # Plot the stereochemistry
    if mol_cis:
    img = Draw.MolToImage(mol_cis, size=(200, 200))
    ax.imshow(img)
    ax.set_title("Cis-1,4-polyisoprene")
    ax.axis("off")

    plt.tight_layout()
    plt.savefig("cis-1-4-polyisoprene.png")
    plt.show()


    if __name__ == "__main__":
    plot_stereochemistries()



    At this point, it's clear that rdkit would have no way of knowing that it should draw repeat unit. According to the RDKit Book, polymer SGroups are supported in the SMILES. However, a modification of the SMILES (r"C\C=C(C)/C" above) to any of the publicly available examples of sgroups in SMILES, such as from the Chemaxon documentation, r"CC(N)C=O |Sg:gen:0::,Sg:mon:1,2,4,0,3::,SgH:1:0|", does not result in a difference in visualisation with and without sgroup.

    The best bet currently is r"*C\C=C(C)/C*", but while this hints at being a repeat unit, it is not the square brackets with subscript I would like to see.

    How can I plot a chemical structure with repeat units in square brackets with a subscript using Python?

    Continue reading...

Compartilhe esta Página