.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/9-showing_custom_bonds.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_9-showing_custom_bonds.py: Showing custom bonds using stk ============================== This example demonstrates how to add shapes into the chemiscope output such that custom bonds that would not automatically be assigned can be rendered. This is done by using `stk `_ to generate and analyse molecules, which comes with topology/bonding information by default (using the cheminformatic software rdkit). We use `stko `_ to calculate some rudimentary properties of `stk` molecules. `stko` can be installed with ``pip install stko``. .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python import stk import stko from rdkit.Chem import AllChem as rdkit import chemiscope .. GENERATED FROM PYTHON SOURCE LINES 25-28 Generate a list of stk BuildingBlocks (representation of a molecule) with properties. This also includes working with rdkit, which comes installed with stk. .. GENERATED FROM PYTHON SOURCE LINES 29-128 .. code-block:: Python rdkitmol = rdkit.MolFromSmiles("Cc1ccccc1") rdkitmol = rdkit.AddHs(rdkitmol) rdkit.Kekulize(rdkitmol) params = rdkit.ETKDGv3() params.randomSeed = 0xF00D rdkit.EmbedMolecule(rdkitmol, params) structures = [ # A building block. stk.BuildingBlock(smiles="NCCN"), # A mostly optimised cage molecule. stk.ConstructedMolecule( topology_graph=stk.cage.FourPlusSix( building_blocks=( stk.BuildingBlock( smiles="NCCN", functional_groups=[stk.PrimaryAminoFactory()], ), stk.BuildingBlock( smiles="O=CC(C=O)C=O", functional_groups=[stk.AldehydeFactory()], ), ), optimizer=stk.MCHammer(), ), ), # A metal-organic cage. stk.ConstructedMolecule( stk.cage.M2L4Lantern( building_blocks=( stk.BuildingBlock( smiles="[Pd+2]", functional_groups=( stk.SingleAtom(stk.Pd(0, charge=2)) for i in range(4) ), position_matrix=[[0.0, 0.0, 0.0]], ), stk.BuildingBlock( smiles=("C1=NC=CC(C2=CC=CC(C3=C" "C=NC=C3)=C2)=C1"), functional_groups=[ stk.SmartsFunctionalGroupFactory( smarts="[#6]~[#7X2]~[#6]", bonders=(1,), deleters=(), ), ], ), ), # Ensure that bonds between the # GenericFunctionalGroups of the ligand and the # SingleAtom functional groups of the metal are # dative. reaction_factory=stk.DativeReactionFactory( stk.GenericReactionFactory( bond_orders={ frozenset( { stk.GenericFunctionalGroup, stk.SingleAtom, } ): 9, }, ), ), ), ), # A host guest molecule. stk.ConstructedMolecule( topology_graph=stk.host_guest.Complex( host=stk.BuildingBlock.init_from_molecule( stk.ConstructedMolecule( topology_graph=stk.cage.FourPlusSix( building_blocks=( stk.BuildingBlock( smiles="NC1CCCCC1N", functional_groups=[ stk.PrimaryAminoFactory(), ], ), stk.BuildingBlock( smiles="O=Cc1cc(C=O)cc(C=O)c1", functional_groups=[stk.AldehydeFactory()], ), ), optimizer=stk.MCHammer(), ), ) ), guests=stk.host_guest.Guest( building_block=stk.BuildingBlock("[Br][Br]"), ), ), ), # From rdkit. stk.BuildingBlock.init_from_rdkit_mol(rdkitmol), ] .. GENERATED FROM PYTHON SOURCE LINES 129-131 Write their properties using any method, here we show using stko: https://stko-docs.readthedocs.io/en/latest/ .. GENERATED FROM PYTHON SOURCE LINES 132-143 .. code-block:: Python energy = stko.UFFEnergy() shape_calc = stko.ShapeCalculator() properties = { "uffenergy": [energy.get_energy(molecule) for molecule in structures], "aspheriticty": [ shape_calc.get_results(molecule).get_asphericity() for molecule in structures ], } .. GENERATED FROM PYTHON SOURCE LINES 144-145 Get the stk bonding information and convert them into shapes. .. GENERATED FROM PYTHON SOURCE LINES 146-156 .. code-block:: Python shape_dict = chemiscope.convert_stk_bonds_as_shapes( frames=structures, bond_color="#fc5500", bond_radius=0.12, ) # Write the shape string for settings to turn them on automatically. shape_string = ",".join(shape_dict.keys()) .. GENERATED FROM PYTHON SOURCE LINES 157-158 A chemiscope widget showing the result without the added bonding. .. GENERATED FROM PYTHON SOURCE LINES 159-163 .. code-block:: Python chemiscope.show(frames=structures, properties=properties) .. chemiscope:: _datasets/fig_9-showing_custom_bonds_015.json.gz :mode: default .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 164-165 Writing to a json.gz file, again without added bonding. .. GENERATED FROM PYTHON SOURCE LINES 166-176 .. code-block:: Python chemiscope.write_input( path="noshape_example.json.gz", frames=structures, properties=properties, meta=dict(name="Missing bonds by automation."), settings=chemiscope.quick_settings(x="aspheriticty", y="uffenergy", color=""), ) .. GENERATED FROM PYTHON SOURCE LINES 177-178 Now with added bonding information. .. GENERATED FROM PYTHON SOURCE LINES 179-197 .. code-block:: Python chemiscope.show( frames=structures, properties=properties, shapes=shape_dict, settings=chemiscope.quick_settings( x="aspheriticty", y="uffenergy", color="", structure_settings={ "shape": shape_string, "atoms": True, "bonds": False, "spaceFilling": False, }, ), ) .. chemiscope:: _datasets/fig_9-showing_custom_bonds_016.json.gz :mode: default .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 198-199 Write to json file with added shapes. .. GENERATED FROM PYTHON SOURCE LINES 200-219 .. code-block:: Python chemiscope.write_input( path="shape_example.json.gz", frames=structures, properties=properties, meta=dict(name="Added all stk bonds."), settings=chemiscope.quick_settings( x="aspheriticty", y="uffenergy", color="", structure_settings={ "shape": shape_string, "atoms": True, "bonds": False, "spaceFilling": False, }, ), shapes=shape_dict, ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.001 seconds) .. _sphx_glr_download_examples_9-showing_custom_bonds.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 9-showing_custom_bonds.ipynb <9-showing_custom_bonds.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 9-showing_custom_bonds.py <9-showing_custom_bonds.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 9-showing_custom_bonds.zip <9-showing_custom_bonds.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_