1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
//
// Copyright (c) 2009-2010, Novartis Institutes for BioMedical Research Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Novartis Institutes for BioMedical Research Inc.
// nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Created by Greg Landrum, July 2008
//
//
#include <GraphMol/RDKitBase.h>
#include <GraphMol/Fingerprints/MorganFingerprints.h>
#include <RDGeneral/hash/hash.hpp>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <boost/dynamic_bitset.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/foreach.hpp>
#include <algorithm>
#include <GraphMol/Fingerprints/FingerprintUtil.h>
namespace {
class ss_matcher {
public:
ss_matcher(){};
ss_matcher(const std::string &pattern) {
RDKit::RWMol *p = RDKit::SmartsToMol(pattern);
TEST_ASSERT(p);
m_matcher.reset(p);
};
// const RDKit::ROMOL_SPTR &getMatcher() const { return m_matcher; };
const RDKit::ROMol *getMatcher() const { return m_matcher.get(); };
private:
RDKit::ROMOL_SPTR m_matcher;
};
} // namespace
namespace RDKit {
namespace MorganFingerprints {
uint32_t updateElement(SparseIntVect<uint32_t> &v, unsigned int elem,
bool counting) {
uint32_t bit = elem % v.getLength();
if (counting) {
v.setVal(bit, v.getVal(bit) + 1);
} else {
v.setVal(bit, 1);
}
return bit;
}
uint32_t updateElement(ExplicitBitVect &v, unsigned int elem,
bool counting = false) {
RDUNUSED_PARAM(counting);
uint32_t bit = elem % v.getNumBits();
v.setBit(bit);
return bit;
}
template <typename T>
void calcFingerprint(const ROMol &mol, unsigned int radius,
std::vector<uint32_t> *invariants,
const std::vector<uint32_t> *fromAtoms, bool useChirality,
bool useBondTypes, bool useCounts,
bool onlyNonzeroInvariants, BitInfoMap *atomsSettingBits,
T &res) {
const ROMol *lmol = &mol;
std::unique_ptr<ROMol> tmol;
if (useChirality && !mol.hasProp(common_properties::_StereochemDone)) {
tmol = std::unique_ptr<ROMol>(new ROMol(mol));
MolOps::assignStereochemistry(*tmol);
lmol = tmol.get();
}
unsigned int nAtoms = lmol->getNumAtoms();
bool owner = false;
if (!invariants) {
invariants = new std::vector<uint32_t>(nAtoms);
owner = true;
getConnectivityInvariants(*lmol, *invariants);
}
// Make a copy of the invariants:
std::vector<uint32_t> invariantCpy(nAtoms);
std::copy(invariants->begin(), invariants->end(), invariantCpy.begin());
// add the round 0 invariants to the result:
for (unsigned int i = 0; i < nAtoms; ++i) {
if (!fromAtoms || std::find(fromAtoms->begin(), fromAtoms->end(), i) !=
fromAtoms->end()) {
if (!onlyNonzeroInvariants || (*invariants)[i]) {
uint32_t bit = updateElement(res, (*invariants)[i], useCounts);
if (atomsSettingBits)
(*atomsSettingBits)[bit].push_back(std::make_pair(i, 0));
}
}
}
boost::dynamic_bitset<> chiralAtoms(nAtoms);
// these are the neighborhoods that have already been added
// to the fingerprint
std::vector<boost::dynamic_bitset<>> neighborhoods;
// these are the environments around each atom:
std::vector<boost::dynamic_bitset<>> atomNeighborhoods(
nAtoms, boost::dynamic_bitset<>(mol.getNumBonds()));
boost::dynamic_bitset<> deadAtoms(nAtoms);
boost::dynamic_bitset<> includeAtoms(nAtoms);
if (fromAtoms) {
BOOST_FOREACH (uint32_t idx, *fromAtoms) { includeAtoms.set(idx, 1); }
} else {
includeAtoms.set();
}
std::vector<unsigned int> atomOrder(nAtoms);
if (onlyNonzeroInvariants) {
std::vector<std::pair<int32_t, uint32_t>> ordering;
for (unsigned int i = 0; i < nAtoms; ++i) {
if (!(*invariants)[i])
ordering.push_back(std::make_pair(1, i));
else
ordering.push_back(std::make_pair(0, i));
}
std::sort(ordering.begin(), ordering.end());
for (unsigned int i = 0; i < nAtoms; ++i) {
atomOrder[i] = ordering[i].second;
}
} else {
for (unsigned int i = 0; i < nAtoms; ++i) {
atomOrder[i] = i;
}
}
// now do our subsequent rounds:
for (unsigned int layer = 0; layer < radius; ++layer) {
std::vector<uint32_t> roundInvariants(nAtoms);
std::vector<boost::dynamic_bitset<>> roundAtomNeighborhoods =
atomNeighborhoods;
std::vector<AccumTuple> neighborhoodsThisRound;
BOOST_FOREACH (unsigned int atomIdx, atomOrder) {
if (!deadAtoms[atomIdx]) {
const Atom *tAtom = lmol->getAtomWithIdx(atomIdx);
if (!tAtom->getDegree()) {
deadAtoms.set(atomIdx, 1);
continue;
}
std::vector<std::pair<int32_t, uint32_t>> nbrs;
ROMol::OEDGE_ITER beg, end;
boost::tie(beg, end) = lmol->getAtomBonds(tAtom);
while (beg != end) {
const Bond *bond = mol[*beg];
roundAtomNeighborhoods[atomIdx][bond->getIdx()] = 1;
unsigned int oIdx = bond->getOtherAtomIdx(atomIdx);
roundAtomNeighborhoods[atomIdx] |= atomNeighborhoods[oIdx];
int32_t bt = 1;
if (useBondTypes) {
if (!useChirality || bond->getBondType() != Bond::DOUBLE ||
bond->getStereo() == Bond::STEREONONE) {
bt = static_cast<int32_t>(bond->getBondType());
} else {
const int32_t stereoOffset = 100;
const int32_t bondTypeOffset = 10;
bt = stereoOffset +
bondTypeOffset * static_cast<int32_t>(bond->getBondType()) +
static_cast<int32_t>(bond->getStereo());
}
}
nbrs.push_back(std::make_pair(bt, (*invariants)[oIdx]));
++beg;
}
// sort the neighbor list:
std::sort(nbrs.begin(), nbrs.end());
// and now calculate the new invariant and test if the atom is newly
// "chiral"
boost::uint32_t invar = layer;
gboost::hash_combine(invar, (*invariants)[atomIdx]);
bool looksChiral = (tAtom->getChiralTag() != Atom::CHI_UNSPECIFIED);
for (std::vector<std::pair<int32_t, uint32_t>>::const_iterator it =
nbrs.begin();
it != nbrs.end(); ++it) {
// add the contribution to the new invariant:
gboost::hash_combine(invar, *it);
// std::cerr<<" "<<atomIdx<<": "<<it->first<<" "<<it->second<<" ->
// "<<invar<<std::endl;
// update our "chirality":
if (useChirality && looksChiral && chiralAtoms[atomIdx]) {
if (it->first != static_cast<int32_t>(Bond::SINGLE)) {
looksChiral = false;
} else if (it != nbrs.begin() && it->second == (it - 1)->second) {
looksChiral = false;
}
}
}
if (useChirality && looksChiral) {
chiralAtoms[atomIdx] = 1;
// add an extra value to the invariant to reflect chirality:
std::string cip = "";
tAtom->getPropIfPresent(common_properties::_CIPCode, cip);
if (cip == "R") {
gboost::hash_combine(invar, 3);
} else if (cip == "S") {
gboost::hash_combine(invar, 2);
} else {
gboost::hash_combine(invar, 1);
}
}
roundInvariants[atomIdx] = static_cast<uint32_t>(invar);
neighborhoodsThisRound.push_back(
boost::make_tuple(roundAtomNeighborhoods[atomIdx],
static_cast<uint32_t>(invar), atomIdx));
if (std::find(neighborhoods.begin(), neighborhoods.end(),
roundAtomNeighborhoods[atomIdx]) != neighborhoods.end()) {
// we have seen this exact environment before, this atom
// is now out of consideration:
deadAtoms[atomIdx] = 1;
// std::cerr<<" atom: "<< atomIdx <<" is dead."<<std::endl;
}
}
}
std::sort(neighborhoodsThisRound.begin(), neighborhoodsThisRound.end());
for (std::vector<AccumTuple>::const_iterator iter =
neighborhoodsThisRound.begin();
iter != neighborhoodsThisRound.end(); ++iter) {
// if we haven't seen this exact environment before, update the
// fingerprint:
if (std::find(neighborhoods.begin(), neighborhoods.end(),
iter->get<0>()) == neighborhoods.end()) {
if (!onlyNonzeroInvariants || invariantCpy[iter->get<2>()]) {
if (includeAtoms[iter->get<2>()]) {
uint32_t bit = updateElement(res, iter->get<1>(), useCounts);
if (atomsSettingBits)
(*atomsSettingBits)[bit].push_back(
std::make_pair(iter->get<2>(), layer + 1));
}
if (!fromAtoms || std::find(fromAtoms->begin(), fromAtoms->end(),
iter->get<2>()) != fromAtoms->end()) {
neighborhoods.push_back(iter->get<0>());
}
}
// std::cerr<<" layer: "<<layer<<" atom: "<<iter->get<2>()<<" "
// <<iter->get<0>()<< " " << iter->get<1>() << " " <<
// deadAtoms[iter->get<2>()]<<std::endl;
} else {
// we have seen this exact environment before, this atom
// is now out of consideration:
// std::cerr<<" atom: "<< iter->get<2>()<<" is dead."<<std::endl;
deadAtoms[iter->get<2>()] = 1;
}
}
// the invariants from this round become the global invariants:
std::copy(roundInvariants.begin(), roundInvariants.end(),
invariants->begin());
atomNeighborhoods = roundAtomNeighborhoods;
}
if (owner) delete invariants;
}
SparseIntVect<uint32_t> *getFingerprint(const ROMol &mol, unsigned int radius,
std::vector<uint32_t> *invariants,
const std::vector<uint32_t> *fromAtoms,
bool useChirality, bool useBondTypes,
bool useCounts,
bool onlyNonzeroInvariants,
BitInfoMap *atomsSettingBits) {
SparseIntVect<uint32_t> *res;
res = new SparseIntVect<uint32_t>(std::numeric_limits<uint32_t>::max());
calcFingerprint(mol, radius, invariants, fromAtoms, useChirality,
useBondTypes, useCounts, onlyNonzeroInvariants,
atomsSettingBits, *res);
return res;
}
SparseIntVect<uint32_t> *getHashedFingerprint(
const ROMol &mol, unsigned int radius, unsigned int nBits,
std::vector<uint32_t> *invariants, const std::vector<uint32_t> *fromAtoms,
bool useChirality, bool useBondTypes, bool onlyNonzeroInvariants,
BitInfoMap *atomsSettingBits) {
SparseIntVect<uint32_t> *res;
res = new SparseIntVect<uint32_t>(nBits);
calcFingerprint(mol, radius, invariants, fromAtoms, useChirality,
useBondTypes, true, onlyNonzeroInvariants, atomsSettingBits,
*res);
return res;
}
ExplicitBitVect *getFingerprintAsBitVect(const ROMol &mol, unsigned int radius,
unsigned int nBits,
std::vector<uint32_t> *invariants,
const std::vector<uint32_t> *fromAtoms,
bool useChirality, bool useBondTypes,
bool onlyNonzeroInvariants,
BitInfoMap *atomsSettingBits) {
auto *res = new ExplicitBitVect(nBits);
calcFingerprint(mol, radius, invariants, fromAtoms, useChirality,
useBondTypes, false, onlyNonzeroInvariants, atomsSettingBits,
*res);
return res;
}
} // end of namespace MorganFingerprints
} // end of namespace RDKit
|