StereoMolGraph#
- class stereomolgraph.StereoMolGraph(mol_graph: None | MolGraph = None)#
Bases:
MolGraphMolGraphwith the ability to store stereochemistry information for atoms and bonds.Two graphs compare equal, if they are isomorphic and have the same stereochemistry.
- get_atom_stereo(atom: int) None | AtomStereo#
Returns the stereo information of the atom if it exists else None. Raises a ValueError if the atom is not in the graph.
- Parameters:
atom (
int) – atomdefault – Default value if no stereo information is found, defaults to None
- Return type:
None|AtomStereo- Returns:
Stereo information of atom
- set_atom_stereo(atom_stereo: AtomStereo)#
Adds stereo information to the graph
- Parameters:
atom – Atoms to be used for chiral information
stereo – Chiral information
- delete_atom_stereo(atom: int)#
Deletes stereo information from the graph
- Parameters:
atom (
int) – Atom to be used for stereo information
- get_bond_stereo(bond: Iterable[int]) None | BondStereo#
Gets the stereo information of the bond or None if it does not exist. Raises a ValueError if the bond s not in the graph.
- Parameters:
bond (
Iterable[int]) – Bond- Return type:
None|BondStereo- Returns:
stereo information of bond
- set_bond_stereo(bond_stereo: BondStereo)#
Stets the stereo information of the bond
- Parameters:
bond – Bond
bond_stereo (
BondStereo) – Stereo information of the bond
- delete_bond_stereo(bond: Iterable[int])#
Deletes the stereo information of the bond
- Parameters:
bond (
Iterable[int]) – Bond
- remove_atom(atom: int)#
Removes an atom from the graph and deletes all chiral information associated with it
- Parameters:
atom (
int) – Atom
- copy(frozen: bool = False) Self#
- Return type:
Self- Returns:
returns a copy of self
- relabel_atoms(mapping: dict[int, int], copy: bool = True) Self#
Relabels the atoms of the graph and the chiral information accordingly
- Parameters:
mapping (
dict[int,int]) – Mapping of old atom ids to new atom idscopy (
bool) – If the graph should be copied before relabeling, defaults to True (default:True)
- Return type:
Self- Returns:
Returns the relabeled graph
- subgraph(atoms: Iterable[int]) Self#
Returns a subgraph of the graph with the given atoms and the chiral information accordingly
- Parameters:
atoms (
Iterable[int]) – Atoms to be used for the subgraph- Return type:
Self- Returns:
Subgraph
- enantiomer() Self#
Creates the enantiomer of the StereoMolGraph by inversion of all atom stereocenters. The result can be identical to the molecule itself if no enantiomer exists.
- Return type:
Self- Returns:
Enantiomer
- classmethod from_rdmol(rdmol: Mol, use_atom_map_number: bool = False, stereo_complete: bool = False) Self#
Creates a StereoMolGraph from an RDKit Mol object. All hydrogens have to be explicit. Stereo information is conserved for tetrahedral atoms and double bonds.
- Parameters:
rdmol (
Mol) – RDKit Mol objectuse_atom_map_number (
bool) – If the atom map number should be used instead of the atom index, Default: False (default:False)stereo_complete (
bool) – If True, we assume that the stereochemistry in the RDKit Mol is complete and all non chiral tetrahedral centers are set to an arbitrary configuration instead of None. (default:False)
- Return type:
Self- Returns:
StereoMolGraph
- add_atom(atom: int, atom_type: int | str, **attr: Any)#
Adds atom to the MolGraph
- Parameters:
atom (
int) – Atom IDatom_type (
int|str) – Atom Type
- add_bond(atom1: int, atom2: int, **attr: Any)#
Adds bond between Atom1 and Atom2.
- Parameters:
atom1 (
int) – Atom1atom2 (
int) – Atom2
- property atom_types: tuple[int, ...]#
- Returns:
Returns all atom types in the MolGraph
- property atoms: Collection[int]#
- Returns:
Returns all atoms of the molecule
- property atoms_with_attributes: Mapping[int, dict[str, Any]]#
- Returns:
Returns all atoms in the MolGraph with their attributes
- bonded_to(atom: int) frozenset[int]#
Returns the atoms connected to the atom.
- Parameters:
atom (
int) – Id of the atom.- Return type:
frozenset[int]- Returns:
tuple of atoms connected to the atom.
- property bonds: Collection[frozenset[int]]#
- Returns:
Returns all bonds in the MolGraph
- bonds_from_bond_order_matrix(matrix: ndarray, threshold: float = 0.5, include_bond_order: bool = False)#
Adds bonds the the graph based on bond orders from a matrix
- Parameters:
matrix (
ndarray) – Bond order Matrixthreshold (
float) – Threshold for bonds to be included as edges, defaults to 0.5 (default:0.5)include_bond_order (
bool) – If bond orders should be included as edge attributes, defaults to False (default:False)
- property bonds_with_attributes: Mapping[frozenset[int], dict[str, Any]]#
- Returns:
Returns all bonds in the MolGraph with their attributes
- classmethod compose(mol_graphs: Iterable[MolGraph]) Self#
Creates a MolGraph object from a list of MolGraph objects.
Duplicate nodes or edges are overwritten, such that the resulting graph only contains one node or edge with that name. Duplicate attributes of duplicate nodes, edges and the stereochemistry are also overwritten in order of iteration.
- Parameters:
mol_graphs (
Iterable[MolGraph]) – list of MolGraph objects- Return type:
Self- Returns:
Returns MolGraph
- connected_components() list[set[int]]#
- Return type:
list[set[int]]- Returns:
Returns the connected components of the graph
- connectivity_matrix() ndarray[tuple[int, int], dtype[int8]]#
Returns a connectivity matrix of the graph as a list of lists. Order is the same as in self.atoms() 1 if nodes are connected, 0 if not.
- Return type:
ndarray[tuple[int,int],dtype[int8]]- Returns:
Connectivity matrix as list of lists
- delete_atom_attribute(atom: int, attr: str)#
Deletes the Attribute of the Atom Raises KeyError if attribute is not present. Raises KeyError if atom is not in graph.
- Parameters:
atom (
int) – Atom IDattr (
str) – Attribute
- Raises:
ValueError – The attribute “atom_type” can not be deleted
- delete_bond_attribute(atom1: int, atom2: int, attr: str)#
Deletes the Attribute of the bond between Atom1 and Atom2
- Parameters:
atom1 (
int)atom2 (
int) – Atom1attr (
str) – Attribute
- freeze() Self#
Freeze the graph, making it immutable and hashable.
A frozen graph can be used in sets and as dict keys. Mutation methods will raise
TypeError. Usecopy()to get a mutable copy.- Return type:
Self- Returns:
self (for chaining)
- classmethod from_atom_types_and_bond_order_matrix(atom_types: Sequence[int | str], matrix: ndarray, threshold: float = 0.5, include_bond_order: bool = False)#
- Parameters:
atom_types (
Sequence[int|str]) – list of atom types as integers or symbols, must correspond to the matrixmatrix (
ndarray) – np.matrix of bond orders or connectivities ([0..1])threshold (
float) – Threshold for bonds to be included as edges, defaults to 0.5 (default:0.5)include_bond_order (
bool) – If bond orders should be included as edge attributes, defaults to False (default:False)
- Returns:
Returns MolGraph
- property frozen: bool#
Whether the graph is currently frozen (immutable).
- get_atom_attribute(atom: int, attr: str) Any | None#
Returns the value of the attribute of the atom or None if the atom does not have this attribute. Raises KeyError if atom is not in graph.
- Parameters:
atom (
int) – Atomattr (
str) – Attribute
- Raises:
KeyError – Atom not in graph
- Return type:
Optional[Any]- Returns:
Returns the value of the attribute of the atom
- get_atom_attributes(atom: int, attributes: Iterable[str] | None = None) Mapping[str, Any]#
Returns the attributes of the atom. If no attributes are given, all attributes are returned. Raises KeyError if atom is not in graph.
- Parameters:
atom (
int) – Atomattributes (
Optional[Iterable[str]]) – Specific attributes to return (default:None)
- Return type:
Mapping[str,Any]- Returns:
Returns all or just the chosen attributes of the atom
- get_atom_type(atom: int) int#
Returns the atom type of the atom. Raises KeyError if atom is not in graph.
- Parameters:
atom (
int) – Atom- Raises:
KeyError – Atom not in graph
- Return type:
int- Returns:
Returns the atom type of the atom
- get_bond_attribute(atom1: int, atom2: int, attr: str) Any#
Returns the value of the attribute of the bond between Atom1 and Atom2. Raises KeyError if bond is not in graph.
- Parameters:
atom1 (
int) – Atom1atom2 (
int) – Atom2attr (
str) – Attribute
- Return type:
Any- Returns:
Returns the value of the attribute of the bond between Atom1 and Atom2
- get_bond_attributes(atom1: int, atom2: int, attributes: Iterable[str] | None = None) Mapping[str, Any]#
- Parameters:
atom1 (
int) – Atom1atom2 (
int) – Atom2attributes (
Optional[Iterable[str]]) – Specific attributes to return (default:None)
- Return type:
Mapping[str,Any]- Returns:
Returns chosen attributes of the bond between Atom1 and Atom2
- has_atom(atom: int) bool#
Returns True if the molecules contains an atom with this id.
- Parameters:
atom (
int) – Atom- Return type:
bool- Returns:
value
- has_bond(atom1: int, atom2: int) bool#
Returns True if bond is in MolGraph.
- Parameters:
atom1 (
int) – Atom1atom2 (
int) – Atom2
- Return type:
bool- Returns:
If the bond is in MolGraph
- property n_atoms: int#
- Returns:
Returns number of atoms in the MolGraph
- property neighbors: Mapping[int, set[int]]#
- Returns:
Returns all neighbors of the atoms in the MolGraph
- node_connected_component(atom: int) set[int]#
- Parameters:
atom (
int) – atom id- Return type:
set[int]- Returns:
Returns the connected component that includes atom_id
- remove_bond(atom1: int, atom2: int)#
Removes bond between Atom1 and Atom2.
- Parameters:
atom1 (
int) – Atom1atom2 (
int) – Atom2
- set_atom_attribute(atom: int, attr: str, value: Any)#
sets the Value of the Attribute on Atom. Raises KeyError if atom is not in graph.
- Parameters:
atom (
int) – Atomattr (
str) – Attributevalue (
Any) – Value
- Raises:
KeyError – Atom not in graph
ValueError – The attribute “atom_type” can only have values of type Element
- set_bond_attribute(atom1: int, atom2: int, attr: str, value: Any)#
sets the Attribute of the bond between Atom1 and Atom2. The Attribute “bond_order” can only have numerical values. Raises KeyError if bond is not in graph.
- Parameters:
atom1 (
int) – Atom1atom2 (
int) – Atom2attr (
str) – Attributevalue (
Any) – Value
- classmethod from_geometry_and_bond_order_matrix(geo: Geometry, matrix: ndarray, threshold: float = 0.5, include_bond_order: bool = False) Self#
Creates a CiralMolGraph object from a Geometry and a bond order matrix
- Parameters:
geo (
Geometry) – Geometrymatrix (
ndarray) – Bond order matrixthreshold (
float) – Threshold for bonds to be included as edges, defaults to 0.5 (default:0.5)include_bond_order (
bool) – If bond orders should be included as edge attributes, defaults to False (default:False)
- Return type:
Self- Returns:
Returns MolGraph
- classmethod from_geometry(geo: ~stereomolgraph.coords.Geometry, switching_function: ~stereomolgraph.coords.BondsFromDistance = <stereomolgraph.coords.BondsFromDistance object>) Self#
Creates a StereoMolGraph object from a Geometry and a switching function. Uses the Default switching function if none are given.
- Parameters:
geo (
Geometry) – Geometryswitching_function (
BondsFromDistance) – Function to determine if two atoms are connected (default:<stereomolgraph.coords.BondsFromDistance object at 0x7859b188fa80>)
- Return type:
Self- Returns:
StereoMolGraph of molecule
- is_stereo_valid() bool#
Checks if the bonds required to have the defined stereochemistry are present in the graph.
- Return type:
bool- Returns:
True if the stereochemistry is valid