RDCode: RDKit::MolOps Namespace Reference

# RDKit::MolOps Namespace Reference

Groups a variety of molecular query and transformation operations. More...

## Functions
int | countAtomElec (const Atom *at)
| return the number of electrons available on an atom to donate for aromaticity
int | getFormalCharge (const ROMol &mol)
| sums up all atomic formal charges and returns the result
bool | atomHasConjugatedBond (const Atom *at)
| returns whether or not the given Atom is involved in a conjugated bond
unsigned int | getMolFrags (const ROMol &mol, INT_VECT &mapping)
| find fragments (disconnected components of the molecular graph)
unsigned int | getMolFrags (const ROMol &mol, VECT_INT_VECT &frags)
| find fragments (disconnected components of the molecular graph)
std::vector< boost::shared_ptr < ROMol > > | getMolFrags (const ROMol &mol, bool sanitizeFrags=true, INT_VECT *frags=0)
| splits a molecule into its component fragments
void | findSpanningTree (const ROMol &mol, INT_VECT &mst)
| finds a molecule's minimium spanning tree (MST)
DiscrimTuple | computeDiscriminators (const ROMol &mol, bool useBO=true, bool force=false)
| calculates a set of molecular discriminators from the distance matrix
DiscrimTuple | computeDiscriminators (double *distMat, int nb, int na)
| Same as MolOps::computeDiscriminators(const ROMol &mol), except that this directly uses the user-supplied distance matrix.
double | computeBalabanJ (const ROMol &mol, bool useBO=true, bool force=false, const std::vector< int > *bondPath=0, bool cacheIt=true)
| calculates Balaban's J index for the molecule
Dealing with hydrogens
ROMol * | addHs (const ROMol &mol, bool explicitOnly=false, bool addCoords=false)
| returns a copy of a molecule with hydrogens added in as explicit Atoms
ROMol * | removeHs (const ROMol &mol, bool implicitOnly=false, bool updateExplicitCount=false, bool sanitize=true)
| returns a copy of a molecule with hydrogens removed
ROMol * | mergeQueryHs (const ROMol &mol)
Sanitization
void | sanitizeMol (RWMol &mol)
| carries out a collection of tasks for cleaning up a molecule and ensuring that it makes "chemical sense"
int | setAromaticity (RWMol &mol)
| Sets up the aromaticity for a molecule.
void | cleanUp (RWMol &mol)
| Designed to be called by the sanitizer to handle special cases before anything is done.
void | adjustHs (RWMol &mol)
| adjust the number of implicit and explicit Hs for special cases
void | Kekulize (RWMol &mol, bool markAtomsBonds=true, unsigned int maxBackTracks=100)
| Kekulizes the molecule.
void | setConjugation (ROMol &mol)
| flags the molecule's conjugated bonds
void | setHybridization (ROMol &mol)
| calculates and sets the hybridization of all a molecule's Stoms
Ring finding and SSSR
int | findSSSR (const ROMol &mol, VECT_INT_VECT &res)
| finds a molecule's Smallest Set of Smallest Rings
int | findSSSR (const ROMol &mol, VECT_INT_VECT *res=0)
int | symmetrizeSSSR (ROMol &mol, VECT_INT_VECT &res)
| symmetrize the molecule's Smallest Set of Smallest Rings
int | symmetrizeSSSR (ROMol &mol)
Shortest paths and other matrices
double * | getAdjacencyMatrix (const ROMol &mol, bool useBO=false, int emptyVal=0, bool force=false, const char *propNamePrefix=0)
| returns a molecule's adjacency matrix
double * | getDistanceMat (const ROMol &mol, bool useBO=false, bool useAtomWts=false, bool force=false, const char *propNamePrefix=0)
| Computes the molecule's topological distance matrix.
double * | getDistanceMat (const ROMol &mol, const std::vector< int > &activeAtoms, const std::vector< const Bond * > &bonds, bool useBO=false, bool useAtomWts=false)
| Computes the molecule's topological distance matrix.
INT_LIST | getShortestPath (const ROMol &mol, int aid1, int aid2)
| Find the shortest path between two atoms.
Canonicalization
void | rankAtoms (const ROMol &mol, INT_VECT &ranks, bool breakTies=true, VECT_INT_VECT *rankHistory=0)
| assign a canonical ordering to a molecule's atoms
Stereochemistry
void | cleanupChirality (RWMol &mol)
| removes bogus chirality markers (those on non-sp3 centers):
void | assignChiralTypesFrom3D (ROMol &mol, int confId=-1, bool replaceExistingTags=true)
| Uses a conformer to assign ChiralType to a molecule's atoms.
void | assignStereochemistry (ROMol &mol, bool cleanIt=false, bool force=false)
| Assign stereochemistry tags to atoms (i.e. R/S) and bonds (i.e. Z/E).
static void | assignAtomChiralCodes (ROMol &mol, bool cleanIt=false, bool force=false)
static void | assignBondStereoCodes (ROMol &mol, bool cleanIt=false, bool force=false)
void | findPotentialStereoBonds (ROMol &mol, bool cleanIt=false)
| finds bonds that could be cis/trans in a molecule and mark them as Bond::STEREONONE

## Detailed Description

Groups a variety of molecular query and transformation operations.

## Function Documentation

ROMol* RDKit::MolOps::addHs | ( | const ROMol & | mol,
|  | bool | explicitOnly = `false`,
|  | bool | addCoords = `false` |
| ) |  |  |

returns a copy of a molecule with hydrogens added in as explicit Atoms

Parameters:

  | mol | the molecule to add Hs to
  | explicitOnly | (optional) if this `true`, only explicit Hs will be added
  | addCoords | (optional) If this is true, estimates for the atomic coordinates of the added Hs will be used.

Returns:
  the new molecule

Notes:

- it makes no sense to use the `addCoords` option if the molecule's heavy atoms don't already have coordinates.
- the caller is responsible for `delete`ing the pointer this returns.

void RDKit::MolOps::adjustHs | ( | RWMol & | mol | ) |

adjust the number of implicit and explicit Hs for special cases

Currently this:

- modifies aromatic nitrogens so that, when appropriate, they have an explicit H marked (e.g. so that we get things like `"c1cc[nH]cc1"`

Parameters:

  | mol | the molecule of interest

Assumptions

- this is called after the molecule has been sanitized, aromaticity has been perceived, and the implicit valence of everything has been calculated.

static void RDKit::MolOps::assignAtomChiralCodes | ( | ROMol & | mol,
|  | bool | cleanIt = `false`,
|  | bool | force = `false` |
| ) |  |  | `[static]`

Deprecated:
  Use assignStereochemistry() instead

Definition at line 563 of file MolOps.h.

References assignStereochemistry().

static void RDKit::MolOps::assignBondStereoCodes | ( | ROMol & | mol,
|  | bool | cleanIt = `false`,
|  | bool | force = `false` |
| ) |  |  | `[static]`

Deprecated:
  Use assignStereochemistry() instead

Definition at line 567 of file MolOps.h.

References assignStereochemistry().

void RDKit::MolOps::assignChiralTypesFrom3D | ( | ROMol & | mol,
|  | int | confId = `-1`,
|  | bool | replaceExistingTags = `true` |
| ) |  |  |

Uses a conformer to assign ChiralType to a molecule's atoms.

Parameters:

  | mol | the molecule of interest
  | confId | the conformer to use
  | replaceExistingTags | if this flag is true, any existing atomic chiral tags will be replaced

If the conformer provided is not a 3D conformer, nothing will be done.

void RDKit::MolOps::assignStereochemistry | ( | ROMol & | mol,
|  | bool | cleanIt = `false`,
|  | bool | force = `false` |
| ) |  |  |

Assign stereochemistry tags to atoms (i.e. R/S) and bonds (i.e. Z/E).

Parameters:

  | mol | the molecule of interest
  | cleanIt | toggles removal of stereo flags from double bonds that can not have stereochemistry
  | force | forces the calculation to be repeated even if it has already been done

Notes:M

- Throughout we assume that we're working with a hydrogen-suppressed graph.

Referenced by assignAtomChiralCodes(), and assignBondStereoCodes().

bool RDKit::MolOps::atomHasConjugatedBond | ( | const Atom * | at | ) |

returns whether or not the given Atom is involved in a conjugated bond

void RDKit::MolOps::cleanUp | ( | RWMol & | mol | ) |

Designed to be called by the sanitizer to handle special cases before anything is done.

Currently this:

- modifies nitro groups, so that the nitrogen does not have a unreasonable valence of 5, as follows:
  - the nitrogen gets a positve charge
  - one of the oxygens gets a negative chage and the double bond to this oxygen is changed to a single bond The net result is that nitro groups can be counted on to be: `"[N+](=O)[O-]"`

Parameters:

  | mol | the molecule of interest

void RDKit::MolOps::cleanupChirality | ( | RWMol & | mol | ) |

removes bogus chirality markers (those on non-sp3 centers):

double RDKit::MolOps::computeBalabanJ | ( | const ROMol & | mol,
|  | bool | useBO = `true`,
|  | bool | force = `false`,
|  | const std::vector< int > * | bondPath = `0`,
|  | bool | cacheIt = `true` |
| ) |  |  |

calculates Balaban's J index for the molecule

Parameters:

  | mol | the molecule of interest
  | useBO | toggles inclusion of the bond order in the calculation (when false, we're not really calculating the J value)
  | force | forces the calculation (instead of using cached results)
  | bondPath | when included, only paths using bonds whose indices occur in this vector will be included in the calculation
  | cacheIt | If this is true, the calculated value will be cached as a property on the molecule

Returns:
  the J index

DiscrimTuple RDKit::MolOps::computeDiscriminators | ( | double * | distMat,
|  | int | nb,
|  | int | na |
| ) |  |  |

Same as MolOps::computeDiscriminators(const ROMol &mol), except that this directly uses the user-supplied distance matrix.

DiscrimTuple RDKit::MolOps::computeDiscriminators | ( | const ROMol & | mol,
|  | bool | useBO = `true`,
|  | bool | force = `false` |
| ) |  |  |

calculates a set of molecular discriminators from the distance matrix

Computes:

1. BalabanJ
2. the first eigenvalue of the distance matrix
3. the last but one eigenvalue of the distance matrix

Parameters:

  | mol | the molecule of interest
  | useBO | toggles inclusion of the bond order in the discriminators (when false, the discriminators are purely topological)
  | force | forces the calculation (instead of using cached results)

Returns:
  a `DiscrimTuple` with the results

int RDKit::MolOps::countAtomElec | ( | const Atom * | at | ) |

return the number of electrons available on an atom to donate for aromaticity

The result is determined using the default valency, number of lone pairs, number of bonds and the formal charge. Note that the atom may not donate all of these electrons to a ring for aromaticity (also used in Conjugation and hybridization code).

Parameters:

  | at | the atom of interest

Returns:
  the number of electrons

void RDKit::MolOps::findPotentialStereoBonds | ( | ROMol & | mol,
|  | bool | cleanIt = `false` |
| ) |  |  |

finds bonds that could be cis/trans in a molecule and mark them as Bond::STEREONONE

Parameters:

  | mol | the molecule of interest
  | cleanIt | toggles removal of stereo flags from double bonds that can not have stereochemistry

This function is usefuly in two situations

- when parsing a mol file; for the bonds marked here, coordinate informations on the neighbors can be used to indentify cis or trans states
- when writing a mol file; bonds that can be cis/trans but not marked as either need to be specially marked in the mol file

void RDKit::MolOps::findSpanningTree | ( | const ROMol & | mol,
|  | INT_VECT & | mst |
| ) |  |  |

finds a molecule's minimium spanning tree (MST)

Parameters:

  | mol | the molecule of interest
  | mst | used to return the MST as a vector of bond indices

int RDKit::MolOps::findSSSR | ( | const ROMol & | mol,
|  | VECT_INT_VECT * | res = `0` |
| ) |  |  |

int RDKit::MolOps::findSSSR | ( | const ROMol & | mol,
|  | VECT_INT_VECT & | res |
| ) |  |  |

finds a molecule's Smallest Set of Smallest Rings

Currently this implements a modified form of Figueras algorithm (JCICS - Vol. 36, No. 5, 1996, 986-991)

Parameters:

  | mol | the molecule of interest
  | res | used to return the vector of rings. Each entry is a vector with atom indices. This information is also stored in the molecule's RingInfo structure, so this argument is optional (see overload)

Returns:
  number of smallest rings found

Base algorithm:

- The original algorithm starts by finding representative degree 2 nodes.
- Representative because if a series of deg 2 nodes are found only one of them is picked.
- The smallest ring around each of them is found.
- The bonds that connect to this degree 2 node are them chopped off, yielding new deg two nodes
- The process is repeated on the new deg 2 nodes.
- If no deg 2 nodes are found, a deg 3 node is picked. The smallest ring with it is found. A bond from this is "carefully" (look in the paper) selected and chopped, yielding deg 2 nodes. The process is same as above once this is done.

Our Modifications:

- If available, more than one smallest ring around a representative deg 2 node will be computed and stored
- Typically 3 rings are found around a degree 3 node (when no deg 2s are available) and all the bond to that node are chopped.
- The extra rings that were found in this process are removed after all the nodes have been covered.

These changes were motivated by several factors:

- We believe the original algorithm fails to find the correct SSSR (finds the correct number of them but the wrong ones) on some sample mols
- Since SSSR may not be unique, a post-SSSR step to symmetrize may be done. The extra rings this process adds can be quite useful.

double* RDKit::MolOps::getAdjacencyMatrix | ( | const ROMol & | mol,
|  | bool | useBO = `false`,
|  | int | emptyVal = `0`,
|  | bool | force = `false`,
|  | const char * | propNamePrefix = `0` |
| ) |  |  |

returns a molecule's adjacency matrix

Parameters:

  | mol | the molecule of interest
  | useBO | toggles use of bond orders in the matrix
  | emptyVal | sets the empty value (for non-adjacent atoms)
  | force | forces calculation of the matrix, even if already computed
  | propNamePrefix | used to set the cached property name

Returns:
  the adjacency matrix.

Notes

- The result of this is cached in the molecule's local property dictionary, which will handle deallocation. Do the caller should not `delete` this pointer.

double* RDKit::MolOps::getDistanceMat | ( | const ROMol & | mol,
|  | const std::vector< int > & | activeAtoms,
|  | const std::vector< const Bond * > & | bonds,
|  | bool | useBO = `false`,
|  | bool | useAtomWts = `false` |
| ) |  |  |

Computes the molecule's topological distance matrix.

Uses the Floyd-Warshall all-pairs-shortest-paths algorithm.

Parameters:

  | mol | the molecule of interest
  | activeAtoms | only elements corresponding to these atom indices will be included in the calculation
  | bonds | only bonds found in this list will be included in the calculation
  | useBO | toggles use of bond orders in the matrix
  | useAtomWts | sets the diagonal elements of the result to 6.0/(atomic number) so that the matrix can be used to calculate Balaban J values. This does not affect the bond weights.

Returns:
  the distance matrix.

Notes

- The results of this call are not cached, the caller should `delete` this pointer.

double* RDKit::MolOps::getDistanceMat | ( | const ROMol & | mol,
|  | bool | useBO = `false`,
|  | bool | useAtomWts = `false`,
|  | bool | force = `false`,
|  | const char * | propNamePrefix = `0` |
| ) |  |  |

Computes the molecule's topological distance matrix.

Uses the Floyd-Warshall all-pairs-shortest-paths algorithm.

Parameters:

  | mol | the molecule of interest
  | useBO | toggles use of bond orders in the matrix
  | useAtomWts | sets the diagonal elements of the result to 6.0/(atomic number) so that the matrix can be used to calculate Balaban J values. This does not affect the bond weights.
  | force | forces calculation of the matrix, even if already computed
  | propNamePrefix | used to set the cached property name

Returns:
  the distance matrix.

Notes

- The result of this is cached in the molecule's local property dictionary, which will handle deallocation. Do the caller should not `delete` this pointer.

int RDKit::MolOps::getFormalCharge | ( | const ROMol & | mol | ) |

sums up all atomic formal charges and returns the result

std::vector<boost::shared_ptr<ROMol> > RDKit::MolOps::getMolFrags | ( | const ROMol & | mol,
|  | bool | sanitizeFrags = `true`,
|  | INT_VECT * | frags = `0` |
| ) |  |  |

splits a molecule into its component fragments

Parameters:

  | mol | the molecule of interest
  | sanitizeFrags | toggles sanitization of the fragments after they are built
  | frags | used to return the mapping of Atoms->fragments. if provided, `frags` will be `mol->getNumAtoms()` long on return and will contain the fragment assignment for each Atom

Returns:
  a vector of the fragments as smart pointers to ROMols

unsigned int RDKit::MolOps::getMolFrags | ( | const ROMol & | mol,
|  | VECT_INT_VECT & | frags |
| ) |  |  |

find fragments (disconnected components of the molecular graph)

Parameters:

  | mol | the molecule of interest
  | frags | used to return the Atoms in each fragment On return `mapping` will be `numFrags` long, and each entry will contain the indices of the Atoms in that fragment.

Returns:
  the number of fragments found.

unsigned int RDKit::MolOps::getMolFrags | ( | const ROMol & | mol,
|  | INT_VECT & | mapping |
| ) |  |  |

find fragments (disconnected components of the molecular graph)

Parameters:

  | mol | the molecule of interest
  | mapping | used to return the mapping of Atoms->fragments. On return `mapping` will be `mol->getNumAtoms()` long and will contain the fragment assignment for each Atom

Returns:
  the number of fragments found.

INT_LIST RDKit::MolOps::getShortestPath | ( | const ROMol & | mol,
|  | int | aid1,
|  | int | aid2 |
| ) |  |  |

Find the shortest path between two atoms.

Uses the Bellman-Ford algorithm

Parameters:

  | mol | molecule of interest
  | aid1 | index of the first atom
  | aid2 | index of the second atom

Returns:
  an std::list with the indices of the atoms along the shortest path

Notes:

- the starting and end atoms are not include in the path

void RDKit::MolOps::Kekulize | ( | RWMol & | mol,
|  | bool | markAtomsBonds = `true`,
|  | unsigned int | maxBackTracks = `100` |
| ) |  |  |

Kekulizes the molecule.

Parameters:

  | mol | the molecule of interest
  | markAtomsBonds | if this is set to true, `isAromatic` boolean settings on both the Bonds and Atoms are turned to false following the Kekulization, otherwise they are left alone in their original state.
  | maxBackTracks | the maximum number of attempts at back-tracking. The algorithm uses a back-tracking procedure to revist a previous setting of double bond if we hit a wall in the kekulization process

Notes:

- even if `markAtomsBonds` is `false` the `BondType` for all aromatic bonds will be changed from `RDKit::Bond::AROMATIC` to `RDKit::Bond::SINGLE` or RDKit::Bond::DOUBLE during Kekulization.

ROMol* RDKit::MolOps::mergeQueryHs | ( | const ROMol & | mol | ) |

returns a copy of a molecule with hydrogens removed and added as queries to the heavy atoms to which they are bound.

This is really intended to be used with molecules that contain QueryAtoms

Parameters:

  | mol | the molecule to remove Hs from

Returns:
  the new molecule

Notes:

- Atoms that do not already have hydrogen count queries will have one added, other H-related queries will not be touched. Examples:
  - C[H] -> [C;!H0]
  - [C;H1][H] -> [C;H1]
  - [C;H2][H] -> [C;H2]
- Hydrogens which aren't connected to a heavy atom will not be removed. This prevents molecules like `"[H][H]"` from having all atoms removed.
  - the caller is responsible for `delete`ing the pointer this returns.

void RDKit::MolOps::rankAtoms | ( | const ROMol & | mol,
|  | INT_VECT & | ranks,
|  | bool | breakTies = `true`,
|  | VECT_INT_VECT * | rankHistory = `0` |
| ) |  |  |

assign a canonical ordering to a molecule's atoms

The algorithm used here is a modification of the published Daylight canonical smiles algorithm (i.e. it uses atom invariants and products of primes).

Parameters:

  | mol | the molecule of interest
  | ranks | used to return the ranks
  | breakTies | toggles breaking of ties (see below)
  | rankHistory | used to return the rank history (see below)

Notes:

- Tie breaking should be done when it's important to have a full ordering of the atoms (e.g. when generating canonical traversal trees). If it's acceptable to have ties between symmetry-equivalent atoms (e.g. when generating CIP codes), tie breaking can/should be skipped.
  - if the `rankHistory` argument is provided, the evolution of the ranks of individual atoms will be tracked. The `rankHistory` pointer should be to a VECT_INT_VECT that has at least `mol.getNumAtoms()` elements.

ROMol* RDKit::MolOps::removeHs | ( | const ROMol & | mol,
|  | bool | implicitOnly = `false`,
|  | bool | updateExplicitCount = `false`,
|  | bool | sanitize = `true` |
| ) |  |  |

returns a copy of a molecule with hydrogens removed

Parameters:

  | mol | the molecule to remove Hs from
  | implicitOnly | (optional) if this `true`, only implicit Hs will be removed
  | updateExplicitCount | (optional) If this is `true`, when explicit Hs are removed from the graph, the heavy atom to which they are bound will have its counter of explicit Hs increased.
  | sanitize,: | (optional) If this is `true`, the final molecule will be sanitized

Returns:
  the new molecule

Notes:

- Hydrogens which aren't connected to a heavy atom will not be removed. This prevents molecules like `"[H][H]"` from having all atoms removed.
- Labelled hydrogen (e.g. atoms with atomic number=1, but mass > 1), will not be removed.

- the caller is responsible for `delete`ing the pointer this returns.

void RDKit::MolOps::sanitizeMol | ( | RWMol & | mol | ) |

carries out a collection of tasks for cleaning up a molecule and ensuring that it makes "chemical sense"

This functions calls the following in sequence

1. MolOps::cleanUp()
2. MolOps::Kekulize()
3. MolOps::setAromaticity()
4. MolOps::setConjugation()
5. MolOps::setHybridization()
6. MolOps::cleanupChirality()
7. MolOps::adjustHs()

Parameters:

  | mol | the RWMol to be cleaned

Notes:

- If there is a failure in the sanitization, a `SanitException` will be thrown.
- in general the user of this function should cast the molecule following this function to a ROMol, so that new atoms and bonds cannot be added to the molecule and screw up the sanitizing that has been done here

int RDKit::MolOps::setAromaticity | ( | RWMol & | mol | ) |

Sets up the aromaticity for a molecule.

This is what happens here:

1. find all the simple rings by calling the findSSSR function
2. loop over all the Atoms in each ring and mark them if they are candidates for aromaticity. A ring atom is a candidate if it can spare electrons to the ring and if it's from the first two rows of the periodic table.
3. ased on the candidate atoms, mark the rings to be either candidates or non-candidates. A ring is a candidate only if all its atoms are candidates
4. apply Hueckel rule to each of the candidate rings to check if the ring can be aromatic

Parameters:

  | mol | the RWMol of interest

Returns:
  1 on succes, 0 otherwise

Assumptions:

- Kekulization has been done (i.e. `MolOps::Kekulize()` has already been called)

void RDKit::MolOps::setConjugation | ( | ROMol & | mol | ) |

flags the molecule's conjugated bonds

void RDKit::MolOps::setHybridization | ( | ROMol & | mol | ) |

calculates and sets the hybridization of all a molecule's Stoms

int RDKit::MolOps::symmetrizeSSSR | ( | ROMol & | mol | ) |

int RDKit::MolOps::symmetrizeSSSR | ( | ROMol & | mol,
|  | VECT_INT_VECT & | res |
| ) |  |  |

symmetrize the molecule's Smallest Set of Smallest Rings

SSSR rings obatined from "findSSSR" can be non-unique in some case. For example, cubane has five SSSR rings, not six as one would hope.

This function adds additional rings to the SSSR list if necessary to make the list symmetric, e.g. all atoms in cubane will be part of the same number of SSSRs. This function choses these extra rings from the extra rings computed and discarded during findSSSR. The new ring are chosen such that:

- replacing a same sized ring in the SSSR list with an extra ring yields the same union of bond IDs as the orignal SSSR list

Parameters:

  | mol | - the molecule of interest
  | res | used to return the vector of rings. Each entry is a vector with atom indices. This information is also stored in the molecule's RingInfo structure, so this argument is optional (see overload)

Returns:
  the total number of rings = (new rings + old SSSRs)

Notes:

- if no SSSR rings are found on the molecule - MolOps::findSSSR() is called first