RingMatchTableSet.h
Go to the documentation of this file.
//
// Copyright (C) 2014 Novartis Institutes for BioMedical Research
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <list>
#include <algorithm>
#include <math.h>
#include "SubstructMatchCustom.h"
namespace RDKit {
namespace FMCS {
class RingMatchTableSet {
class RingMatchTable {
FMCS::MatchTable MatchMatrix;
std::map<const INT_VECT*, unsigned> RingIndex;
public:
inline void clear() {
MatchMatrix.clear();
RingIndex.clear();
}
inline void resize(unsigned s1, unsigned s2) {
MatchMatrix.resize(s1, s2);
for (size_t i = 0; i < s1; i++)
for (size_t j = 0; j < s2; j++) MatchMatrix.set(i, j, false);
}
inline void makeRingIndex(const ROMol* mol2) {
unsigned i = 0;
// for each TARGET ring
const RingInfo::VECT_INT_VECT& rings2 = mol2->getRingInfo()->bondRings();
for (RingInfo::VECT_INT_VECT::const_iterator r2 = rings2.begin();
r2 != rings2.end(); r2++)
RingIndex[&*r2] = i++;
}
inline bool isEqual(unsigned i, const INT_VECT* r2) const {
return MatchMatrix.at(i, getRingIndex(r2));
}
inline void setMatch(unsigned i, const INT_VECT* r2) {
MatchMatrix.set(i, getRingIndex(r2), true);
}
private:
inline unsigned getRingIndex(const INT_VECT* r2) const {
std::map<const INT_VECT*, unsigned>::const_iterator j =
RingIndex.find(r2);
if (RingIndex.end() == j) throw - 1;
return j->second;
}
};
private:
std::vector<std::vector<size_t> >* QueryBondRingsIndeces;
std::map<const ROMol*, std::vector<std::vector<size_t> > >
TargetBondRingsIndecesSet; // by target molecules
std::map<const ROMol*, RingMatchTable> MatchMatrixSet; // by target molecules
std::map<const INT_VECT*, unsigned> QueryRingIndex;
public:
RingMatchTableSet() : QueryBondRingsIndeces(0) {}
inline void clear() {
if (QueryBondRingsIndeces) QueryBondRingsIndeces->clear();
TargetBondRingsIndecesSet.clear();
MatchMatrixSet.clear();
QueryRingIndex.clear();
}
inline bool isQueryBondInRing(unsigned bi) const {
return (*QueryBondRingsIndeces)[bi].empty();
}
inline const std::vector<size_t>& getQueryBondRings(unsigned bi) const {
return (*QueryBondRingsIndeces)[bi];
}
inline bool isTargetBondInRing(const ROMol* target, unsigned bi) const {
std::map<const ROMol*, std::vector<std::vector<size_t> > >::const_iterator
i = TargetBondRingsIndecesSet.find(target);
if (TargetBondRingsIndecesSet.end() == i) throw - 1; // never
return i->second[bi].empty();
}
inline const std::vector<size_t>& getTargetBondRings(const ROMol* target,
unsigned bi) const {
std::map<const ROMol*, std::vector<std::vector<size_t> > >::const_iterator
i = TargetBondRingsIndecesSet.find(target);
if (TargetBondRingsIndecesSet.end() == i) throw - 1; // never
return i->second[bi];
}
inline bool isEqual(const INT_VECT* r1, const INT_VECT* r2,
const ROMol* mol2) const {
const RingMatchTable& m = getTargetMatchMatrix(mol2);
unsigned i = getQueryRingIndex(r1);
return m.isEqual(i, r2);
}
void init(const ROMol* query) {
MatchMatrixSet.clear();
// fill out QueryRingIndex
unsigned i = 0;
const RingInfo::VECT_INT_VECT& rings = query->getRingInfo()->bondRings();
for (RingInfo::VECT_INT_VECT::const_iterator r = rings.begin();
r != rings.end(); r++)
QueryRingIndex[&*r] = i++;
TargetBondRingsIndecesSet.clear();
QueryBondRingsIndeces = &TargetBondRingsIndecesSet[query];
QueryBondRingsIndeces->resize(query->getNumBonds());
size_t ri = 0;
for (RingInfo::VECT_INT_VECT::const_iterator r = rings.begin();
r != rings.end(); r++, ri++)
for (INT_VECT::const_iterator bi = r->begin(); bi != r->end();
bi++) // all bonds in the ring
(*QueryBondRingsIndeces)[*bi].push_back(ri);
}
inline void addTargetBondRingsIndeces(const ROMol* mol2) {
std::vector<std::vector<size_t> >& m = TargetBondRingsIndecesSet[mol2];
m.resize(mol2->getNumBonds());
size_t ri = 0;
const RingInfo::VECT_INT_VECT& rings = mol2->getRingInfo()->bondRings();
for (RingInfo::VECT_INT_VECT::const_iterator r = rings.begin();
r != rings.end(); r++, ri++)
for (INT_VECT::const_iterator bi = r->begin(); bi != r->end();
bi++) // all bonds in the ring
m[*bi].push_back(ri);
}
void computeRingMatchTable(
const ROMol* query, const ROMol* targetMolecule,
const MCSParameters& parameters) { // call it for all targets
const RingInfo::VECT_INT_VECT& rings1 = query->getRingInfo()->bondRings();
const RingInfo::VECT_INT_VECT& rings2 =
targetMolecule->getRingInfo()->bondRings();
RingMatchTable& m =
addTargetMatchMatrix(targetMolecule, rings1.size(), rings2.size());
unsigned i = 0;
// for each query ring
for (RingInfo::VECT_INT_VECT::const_iterator r1 = rings1.begin();
r1 != rings1.end(); r1++, i++) {
FMCS::Graph graph1;
makeRingGraph(graph1, *r1,
query); // for each query ring bond ADD all atoms and bonds
// for each TARGET ring
for (RingInfo::VECT_INT_VECT::const_iterator r2 = rings2.begin();
r2 != rings2.end(); r2++) {
if (r1->size() != r2->size()) // rings are different
continue;
FMCS::Graph graph2;
makeRingGraph(
graph2, *r2,
targetMolecule); // for each TAG ring bond ADD all atoms and bonds
// check ring substruct match
MCSBondCompareParameters bp = parameters.BondCompareParameters;
bp.RingMatchesRingOnly = false;
bp.CompleteRingsOnly = false;
bool match =
#ifdef NEVER_xxx_PRECOMPUTED_TABLES_MATCH // not computed yet, because
// MatchTable computation usees this
// ring info table
FMCS::SubstructMatchCustomTable(graph2, graph1, tag->AtomMatchTable,
tag->BondMatchTable);
#else // noticable slowly:
FMCS::SubstructMatchCustom(
graph2, *targetMolecule, graph1, *query, parameters.AtomTyper,
parameters.BondTyper, NULL, parameters.AtomCompareParameters,
bp, NULL);
#endif
if (match) m.setMatch(i, &*r2);
}
}
}
private:
void makeRingGraph(FMCS::Graph& g, const INT_VECT& ring,
const ROMol* mol) const { // ADD all atoms and bonds
std::map<const Atom*, unsigned> atomMap;
for (size_t i = 0; i < ring.size(); i++) {
const Bond* bond = mol->getBondWithIdx(ring[i]);
const Atom* atom1 = bond->getBeginAtom();
const Atom* atom2 = bond->getEndAtom();
unsigned j1 = NotSet;
unsigned j2 = NotSet;
std::map<const Atom*, unsigned>::const_iterator ai;
ai = atomMap.find(atom1);
if (atomMap.end() != ai) j1 = ai->second;
ai = atomMap.find(atom2);
if (atomMap.end() != ai) j2 = ai->second;
if (NotSet == j1) {
j1 = g.m_vertices.size();
atomMap[atom1] = j1;
g.addAtom(atom1->getIdx());
}
if (NotSet == j2) {
j2 = g.m_vertices.size();
atomMap[atom2] = j2;
g.addAtom(atom2->getIdx());
}
g.addBond(ring[i], j1, j2);
}
}
inline unsigned getQueryRingIndex(const INT_VECT* r1) const {
std::map<const INT_VECT*, unsigned>::const_iterator i =
QueryRingIndex.find(r1);
if (QueryRingIndex.end() == i) throw - 1; // never
return i->second;
}
inline const RingMatchTable& getTargetMatchMatrix(const ROMol* mol2) const {
std::map<const ROMol*, RingMatchTable>::const_iterator mi =
MatchMatrixSet.find(mol2);
if (MatchMatrixSet.end() == mi) throw - 1; // never
return mi->second;
}
inline RingMatchTable& addTargetMatchMatrix(const ROMol* mol2, unsigned s1,
unsigned s2) {
RingMatchTable& m = MatchMatrixSet[mol2];
m.resize(s1, s2);
m.makeRingIndex(mol2);
return m;
}
};
}
} // namespace RDKit
RDKit::FMCS::SubstructMatchCustom
bool SubstructMatchCustom(const FMCS::Graph &target, const ROMol &mol, const FMCS::Graph &query, const ROMol &querySrc, MCSAtomCompareFunction atomCompare, MCSBondCompareFunction bondCompare, MCSFinalMatchCheckFunction finalCompare, const MCSAtomCompareParameters &acp, const MCSBondCompareParameters &bcp, void *user_data, match_V_t *match=0)
RDKit::FMCS::Graph::addAtom
void addAtom(unsigned atom)
Definition: Graph.h:27
RDKit::FMCS::RingMatchTableSet::isQueryBondInRing
bool isQueryBondInRing(unsigned bi) const
Definition: RingMatchTableSet.h:74
RDKit::FMCS::RingMatchTableSet::clear
void clear()
Definition: RingMatchTableSet.h:67
RDKit::MCSBondCompareParameters::RingMatchesRingOnly
bool RingMatchesRingOnly
Definition: FMCS.h:32
RDKit::Bond::getEndAtom
Atom * getEndAtom() const
returns a pointer to our end Atom
RDKit::MCSBondCompareParameters
Definition: FMCS.h:31
RDKit::FMCS::TArray2D< bool >
RDKit::FMCS::TArray2D::clear
void clear()
Definition: MatchTable.h:29
RDKit::RingInfo::bondRings
const VECT_INT_VECT & bondRings() const
returns our bond-rings vectors
Definition: RingInfo.h:126
RDKit::FMCS::TArray2D::set
void set(size_t row, size_t col, T val)
Definition: MatchTable.h:39
RDKit::ROMol::getNumBonds
unsigned int getNumBonds(bool onlyHeavy=1) const
returns our number of Bonds
RDKit::FMCS::RingMatchTableSet::computeRingMatchTable
void computeRingMatchTable(const ROMol *query, const ROMol *targetMolecule, const MCSParameters &parameters)
Definition: RingMatchTableSet.h:133
RDKit::MCSBondCompareParameters::CompleteRingsOnly
bool CompleteRingsOnly
Definition: FMCS.h:33
RDKit::ROMol
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:102
RDKit::MCSParameters::AtomTyper
MCSAtomCompareFunction AtomTyper
Definition: FMCS.h:104
RDKit::FMCS::Graph::addBond
void addBond(unsigned bond, unsigned beginAtom, unsigned endAtom)
Definition: Graph.h:31
RDKit::ROMol::getRingInfo
RingInfo * getRingInfo() const
Definition: ROMol.h:374
RDKit::FMCS::SubstructMatchCustomTable
bool SubstructMatchCustomTable(const FMCS::Graph &target, const ROMol &target_mol, const FMCS::Graph &query, const ROMol &querySrc, const MatchTable &atomMatchTable, const MatchTable &bondMatchTable, const MCSParameters *parameters=0, match_V_t *match=0)
RDKit::FMCS::RingMatchTableSet::getQueryBondRings
const std::vector< size_t > & getQueryBondRings(unsigned bi) const
Definition: RingMatchTableSet.h:77
RDKit::FMCS::RingMatchTableSet::RingMatchTableSet
RingMatchTableSet()
Definition: RingMatchTableSet.h:65
RDKit::MCSParameters::BondTyper
MCSBondCompareFunction BondTyper
Definition: FMCS.h:105
RDKit::FMCS::Graph
Definition: Graph.h:22
RDKit::Bond::getBeginAtom
Atom * getBeginAtom() const
returns a pointer to our begin Atom
RDKit::INT_VECT
std::vector< int > INT_VECT
Definition: types.h:146
RDKit::Atom::getIdx
unsigned int getIdx() const
returns our index within the ROMol
Definition: Atom.h:126
RDKit::ROMol::getBondWithIdx
Bond * getBondWithIdx(unsigned int idx)
returns a pointer to a particular Bond
RDKit::FMCS::RingMatchTableSet::getTargetBondRings
const std::vector< size_t > & getTargetBondRings(const ROMol *target, unsigned bi) const
Definition: RingMatchTableSet.h:87
RDKit
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
RDKit::FMCS::RingMatchTableSet
Definition: RingMatchTableSet.h:17
RDKit::FMCS::RingMatchTableSet::isTargetBondInRing
bool isTargetBondInRing(const ROMol *target, unsigned bi) const
Definition: RingMatchTableSet.h:81
SubstructMatchCustom.h
RDKit::MCSParameters::BondCompareParameters
MCSBondCompareParameters BondCompareParameters
Definition: FMCS.h:103
RDKit::Bond
class for representing a bond
Definition: Bond.h:46
RDKit::FMCS::RingMatchTableSet::init
void init(const ROMol *query)
Definition: RingMatchTableSet.h:102
RDKit::RingInfo::VECT_INT_VECT
std::vector< INT_VECT > VECT_INT_VECT
Definition: RingInfo.h:28
RDKit::FMCS::NotSet
const unsigned int NotSet
Definition: SubstructMatchCustom.h:20
RDKit::MCSParameters
Definition: FMCS.h:97
RDKit::FMCS::RingMatchTableSet::isEqual
bool isEqual(const INT_VECT *r1, const INT_VECT *r2, const ROMol *mol2) const
Definition: RingMatchTableSet.h:95
RDKit::FMCS::TArray2D::resize
void resize(size_t cy, size_t cx)
Definition: MatchTable.h:34
RDKit::FMCS::RingMatchTableSet::addTargetBondRingsIndeces
void addTargetBondRingsIndeces(const ROMol *mol2)
Definition: RingMatchTableSet.h:120
RDKit::Atom
The class for representing atoms.
Definition: Atom.h:67
RDKit::MCSParameters::AtomCompareParameters
MCSAtomCompareParameters AtomCompareParameters
Definition: FMCS.h:102
RDKit::FMCS::TArray2D::at
T at(size_t row, size_t col)
Definition: MatchTable.h:42