Graph Molecules

class manim_chemistry.twoD.graph_molecule.GraphMolecule(vertices_dict: dict, edges_dict: dict, label: bool = False, numeric_label: bool = False, label_color: str = ManimColor('#000000'), *args, **kwargs)[source]

Bases: Graph, AbstractMolecule

Represents a molecule like a Graph from Manim.

Examples

Example: GraphMoleculeFromFile

from manim import *

from manim_chemistry import *

class GraphMoleculeFromFile(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_file(
            "../examples/molecule_files/mol_files/acetone_2d.mol"
        )
        self.wait()
        self.play(Write(molecule))
        self.wait()
from manim_chemistry import *

class GraphMoleculeFromFile(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_file(
            "../examples/molecule_files/mol_files/acetone_2d.mol"
        )
        self.wait()
        self.play(Write(molecule))
        self.wait()

Example: GraphMoleculeFromFileWithHydrogens

from manim import *

from manim_chemistry import *

class GraphMoleculeFromFileWithHydrogens(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_file(
            "../examples/molecule_files/mol_files/acetone_2d.mol",
            ignore_hydrogens=False
        )
        self.wait()
        self.play(Write(molecule))
        self.wait()
from manim_chemistry import *

class GraphMoleculeFromFileWithHydrogens(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_file(
            "../examples/molecule_files/mol_files/acetone_2d.mol",
            ignore_hydrogens=False
        )
        self.wait()
        self.play(Write(molecule))
        self.wait()

Example: GraphMoleculeFromPubChem

from manim import *

from manim_chemistry import *

class GraphMoleculeFromPubChem(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_pubchem(name="acetone")
        self.wait()
        self.play(Write(molecule))
        self.wait()
from manim_chemistry import *

class GraphMoleculeFromPubChem(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_pubchem(name="acetone")
        self.wait()
        self.play(Write(molecule))
        self.wait()

Example: GraphMoleculeFromPubChemThreeD

from manim import *

from manim_chemistry import *

class GraphMoleculeFromPubChemThreeD(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_pubchem(
            name="acetone",
            three_d=True,
            ignore_hydrogens=False
        )
        self.wait()
        self.play(Write(molecule))
        self.wait()
from manim_chemistry import *

class GraphMoleculeFromPubChemThreeD(Scene):
    def construct(self):
        molecule = GraphMolecule.molecule_from_pubchem(
            name="acetone",
            three_d=True,
            ignore_hydrogens=False
        )
        self.wait()
        self.play(Write(molecule))
        self.wait()

Recursive depht-first search to find connected atoms. Warning: Only works properly for non cyclic structures. If you use an atom from a cycle as the starting point, it will take the whole cycle.

find_atom_position_by_index(atom_index: int) array[source]

Returns the position of a single atom given its index.

Example: ` molecule = GraphMolecule.molecule_from_file("examples/molecule_files/mol_files/dimethylpropane.mol") print(molecule.find_atom_position_by_index(1)) >>> array([ 0.9397, -0.7497,  0.    ]) `

Args:

atom_index (int): Index of the atom inside the VDict.

Returns:

np.array: Array with the [x, y, z] coordinates of the atom.

find_atoms_position_by_index(atoms_index_list: list) list[source]

Returns the position of multiple atoms given their indices.

Example: ` molecule = GraphMolecule.molecule_from_file("examples/molecule_files/mol_files/dimethylpropane.mol") print(molecule.find_atoms_position_by_index([1,2,3])) >>> [array([ 0.0713, -0.0263,  0.    ]), array([-1.2754,  0.3464,  0.    ]), array([0.9674, 1.2186, 0.    ])] ` Args:

atoms_index_list (list): List of atoms indices to be gotten.

Returns:

list: List of the atoms positions.

find_bond_center_by_index(bond_index: tuple) array[source]

Returns the [x, y, z] coordinates of a bond given a bond tuple. The bund tuple corresponds to the indices of the atoms in the bond.

Example: ` molecule = GraphMolecule.molecule_from_file("examples/molecule_files/mol_files/dimethylpropane.mol") print(molecule.find_bond_center_by_index((1, 2)) >>> array([0.51935, 0.59615, 0.     ]) `

Args:

bond_index (tuple): index of the bond

Returns:

np.array: [x, y, z] coordinates of bond center.

find_bonds_center_by_index(bonds_tuples_list: list) list[source]

Returns the position of multiple bonds given their indices.

Example: ` molecule = GraphMolecule.molecule_from_file("examples/molecule_files/mol_files/dimethylpropane.mol") print(molecule.find_bonds_center_by_tuple([1,2,3])) >>> [array([ 0.0713, -0.0263,  0.    ]), array([-1.2754,  0.3464,  0.    ]), array([0.9674, 1.2186, 0.    ])] ` Args:

bondss_index_list (list): List of bonds indices to be gotten.

Returns:

list: List of the bonds positions.

find_position_along_bond_axis(bond_tuple: int, position_buff: float) array[source]

Returns a position along the bond axis given a bond index and depending on a position_buff. A value of 1 will return one end of the bond. A value of -1 will return the other end. All values in between return positions at some point of the middle of the bond, being 0 the center. Values bigger or lower that 1 and -1 will return positions outside the bond.

Args:

bond_tuple (int): Tuple of the bond position_buff (float): Position buff

Returns:

np.array: [x, y, z] coordinates of the final position selected.

get_bonds_from_atoms_indices(connected_atoms: list, excluded_atom: int = 0) list[source]

Given a list of atoms, returns the bonds related to those atoms.

get_connected_atoms(from_atom_index: int, to_atom_index: int) list[source]

Given two atoms index, returns the connected atoms to the second atom after the bond between the first atom and the second one.

Example: Index: 0 1 2 3 4 5 Atoms: C-O-C-N-C-H This function applied to atoms 2 (C) and 3 (N), would return [3, 4, 5], the indices of atoms N, C and H.

classmethod mc_molecule_to_atoms_and_bonds(mc_molecule: MCMolecule) Tuple[Dict, Dict][source]

Transforms the structure of a mc_molecule to a (vertices, edges) tuple with the following structure: - Vertices: {<atom_index>: MCAtom} - Edges: {(<from_atom_index>, <to_atom_index>): MCBond}

Args:

mc_molecule (MCMolecule): The origin MCMolecule

Returns:

Tuple[Dict, Dict]: See above.