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
|
//
// Copyright (c) 2014, 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.
//
#include <GraphMol/RDKitBase.h>
#include <DataStructs/ExplicitBitVect.h>
#include <DataStructs/BitOps.h>
#include <GraphMol/ChemReactions/Reaction.h>
#include <GraphMol/Fingerprints/Fingerprints.h>
#include <GraphMol/Fingerprints/AtomPairs.h>
#include "GraphMol/ChemReactions/ReactionFingerprints.h"
#include <GraphMol/Fingerprints/MACCS.h>
#include <GraphMol/Fingerprints/MorganFingerprints.h>
#include <DataStructs/SparseIntVect.h>
#include <GraphMol/ChemReactions/ReactionUtils.h>
namespace {
RDKit::SparseIntVect<std::uint32_t> *generateFingerprint(
RDKit::ROMol &mol, unsigned int fpSize, RDKit::FingerprintType t) {
mol.updatePropertyCache(false);
RDKit::SparseIntVect<std::uint32_t> *res;
switch (t) {
case RDKit::AtomPairFP: {
RDKit::SparseIntVect<std::int32_t> *tmp1 =
RDKit::AtomPairs::getHashedAtomPairFingerprint(mol, fpSize);
res = new RDKit::SparseIntVect<std::uint32_t>(fpSize);
for (auto val : tmp1->getNonzeroElements()) {
res->setVal(static_cast<std::uint32_t>(val.first), val.second);
}
delete tmp1;
} break;
case RDKit::TopologicalTorsion: {
RDKit::SparseIntVect<boost::int64_t> *tmp2 =
RDKit::AtomPairs::getHashedTopologicalTorsionFingerprint(mol, fpSize);
res = new RDKit::SparseIntVect<std::uint32_t>(fpSize);
for (auto val : tmp2->getNonzeroElements()) {
res->setVal(static_cast<std::uint32_t>(val.first), val.second);
}
delete tmp2;
} break;
case RDKit::MorganFP: {
if (!mol.getRingInfo()->isInitialized()) {
mol.updatePropertyCache();
RDKit::MolOps::findSSSR(mol);
}
res = RDKit::MorganFingerprints::getHashedFingerprint(mol, 2, fpSize);
} break;
default:
std::stringstream err;
err << ">> unsupported fingerprint type" << std::endl;
throw RDKit::ChemicalReactionException(err.str());
}
return res;
}
ExplicitBitVect *generateFingerprintAsBitVect(RDKit::ROMol &mol,
unsigned int fpSize,
RDKit::FingerprintType t) {
mol.updatePropertyCache(false);
ExplicitBitVect *res;
switch (t) {
case RDKit::AtomPairFP:
res =
RDKit::AtomPairs::getHashedAtomPairFingerprintAsBitVect(mol, fpSize);
break;
case RDKit::RDKitFP:
res = RDKit::RDKFingerprintMol(mol, 1, 7, fpSize);
break;
case RDKit::TopologicalTorsion:
res = RDKit::AtomPairs::getHashedTopologicalTorsionFingerprintAsBitVect(
mol, fpSize);
break;
case RDKit::MorganFP: {
if (!mol.getRingInfo()->isInitialized()) {
mol.updatePropertyCache();
RDKit::MolOps::findSSSR(mol);
}
res = RDKit::MorganFingerprints::getFingerprintAsBitVect(mol, 2, fpSize);
} break;
case RDKit::PatternFP:
res = RDKit::PatternFingerprintMol(mol, fpSize);
break;
default:
std::stringstream err;
err << ">> unsupported fingerprint type" << std::endl;
throw RDKit::ChemicalReactionException(err.str());
}
return res;
}
} // namespace
namespace RDKit {
const ReactionFingerprintParams DefaultStructuralFPParams(true, 0.2, 1, 1, 4096,
PatternFP);
const ReactionFingerprintParams DefaultDifferenceFPParams(true, 0.0, 10, 1,
2048, AtomPairFP);
SparseIntVect<std::uint32_t> *generateFingerprintChemReactionAsCountVect(
const ChemicalReaction &rxn, unsigned int fpSize, FingerprintType t,
ReactionMoleculeType mt) {
PRECONDITION(fpSize != 0, "fpSize==0");
auto *result = new SparseIntVect<std::uint32_t>(fpSize);
auto begin = getStartIterator(rxn, mt);
auto end = getEndIterator(rxn, mt);
for (; begin != end; ++begin) {
SparseIntVect<std::uint32_t> *tmp = generateFingerprint(**begin, fpSize, t);
(*result) += *tmp;
delete tmp;
}
return result;
}
ExplicitBitVect *generateFingerprintChemReactionAsBitVect(
const ChemicalReaction &rxn, unsigned int fpSize, FingerprintType t,
ReactionMoleculeType mt) {
PRECONDITION(fpSize != 0, "fpSize==0");
auto *result = new ExplicitBitVect(fpSize);
auto begin = getStartIterator(rxn, mt);
auto end = getEndIterator(rxn, mt);
for (; begin != end; ++begin) {
ExplicitBitVect *tmp = generateFingerprintAsBitVect(**begin, fpSize, t);
(*result) |= *tmp;
delete tmp;
}
return result;
}
// caller owns the result, it must be deleted
ExplicitBitVect *StructuralFingerprintChemReaction(
const ChemicalReaction &rxn, const ReactionFingerprintParams ¶ms) {
PRECONDITION(params.fpSize != 0, "fpSize==0");
unsigned int fpSize_final = params.fpSize / 2;
if (params.includeAgents) {
unsigned agent_fp_size = params.fpSize / 3;
if (params.bitRatioAgents < 1.0) {
agent_fp_size =
int(ceil(static_cast<double>(params.fpSize) * params.bitRatioAgents));
}
unsigned scaling = !(agent_fp_size % 2) ? agent_fp_size : agent_fp_size - 1;
fpSize_final = (params.fpSize - scaling) / 2;
}
unsigned int fpSize_agent = params.fpSize - 2 * fpSize_final;
ExplicitBitVect *reactantFP = generateFingerprintChemReactionAsBitVect(
rxn, fpSize_final, params.fpType, Reactant);
ExplicitBitVect *productFP = generateFingerprintChemReactionAsBitVect(
rxn, fpSize_final, params.fpType, Product);
auto *res = new ExplicitBitVect;
/* concatenate the two bitvectors */
(*res) = *reactantFP + *productFP;
if (fpSize_agent > 0) {
ExplicitBitVect *agentFP = generateFingerprintChemReactionAsBitVect(
rxn, fpSize_agent, params.fpType, Agent);
(*res) += *agentFP;
delete agentFP;
}
delete reactantFP;
delete productFP;
return res;
}
SparseIntVect<std::uint32_t> *DifferenceFingerprintChemReaction(
const ChemicalReaction &rxn, const ReactionFingerprintParams ¶ms) {
PRECONDITION(params.fpSize != 0, "fpSize==0");
PRECONDITION(params.fpType > 0 && params.fpType < 4,
"Fingerprinttype not supported");
SparseIntVect<std::uint32_t> *reactantFP =
generateFingerprintChemReactionAsCountVect(rxn, params.fpSize,
params.fpType, Reactant);
SparseIntVect<std::uint32_t> *productFP =
generateFingerprintChemReactionAsCountVect(rxn, params.fpSize,
params.fpType, Product);
auto *res = new SparseIntVect<std::uint32_t>;
if (params.includeAgents) {
SparseIntVect<std::uint32_t> *agentFP =
generateFingerprintChemReactionAsCountVect(rxn, params.fpSize,
params.fpType, Agent);
(*productFP) *= params.nonAgentWeight;
(*reactantFP) *= params.nonAgentWeight;
(*agentFP) *= params.agentWeight;
(*res) = *productFP - *reactantFP + *agentFP;
delete agentFP;
}
/* subtract product FP from reactant FP */
else {
(*res) = *productFP - *reactantFP;
}
delete reactantFP;
delete productFP;
return res;
}
} // namespace RDKit
|