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
|
//
// 2019, Daniel Probst, Reymond Group @ University of Bern
//
// @@ 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.
//
/*! \file MHFP.h
*/
#include <RDGeneral/export.h>
#ifndef RD_MHFPFPS_H
#define RD_MHFPFPS_H
#include <string>
#include <vector>
#include <GraphMol/ROMol.h>
#include <DataStructs/ExplicitBitVect.h>
class SparseBitVect;
namespace RDKit {
namespace MHFPFingerprints {
const std::string mhfpFingerprintVersion = "1.0.0";
namespace FNV {
const uint32_t prime = 0x01000193;
const uint32_t seed = 0x811C9DC5;
//! A simple implementation of the Fowler–Noll–Vo hash function.
inline uint32_t hash(const std::string &str, uint32_t hash = seed) {
const unsigned char *ptr = (const unsigned char *)str.c_str();
size_t len = str.length();
while (len--) {
hash = (*ptr++ ^ hash) * prime;
}
return hash;
};
} // namespace FNV
class RDKIT_FINGERPRINTS_EXPORT MHFPEncoder {
public:
//! Constructor
/*!
\brief Construct a MHFPEncoder
The MHFPEncoder class is instantieted with a given number of permutations
and a seed. Fingerprints / minhashes created with a different number of
permutations or a different seed are not compatible.
\param n_permutations the number of permutations used to create hash
functions. This will be the dimensionality of the resulting vector.
Default: <tt>2048</tt>.
\param seed a random seed. Default: <tt>42</tt>.
*/
MHFPEncoder(unsigned int n_permutations = 2048, unsigned int seed = 42);
/*!
\brief Creates a MinHash from a vector of strings.
This method is exposed in order to enable advanced usage of this MHFP
implementation such as customizing the properties that are hashed in order
to create an MHFP instance. In theory, any number of values that can be
represented as strings can be minhashed. This method is called
by MHFPEncoder::Encode.
\param vec a vector containg strings (e.g. the smiles shingling of a
molecule).
\returns the MinHash of the input.
*/
std::vector<uint32_t> FromStringArray(const std::vector<std::string> &vec);
/*!
\brief Creates a MinHash from a list of unsigned integers.
This method is exposed in order to enable advanced usage of this MHFP
implementation such as MinHashing a sparse array generated by another
fingerprint (e.g. Morgan / ECFP).
\param vec a vector containg unsigned integers.
\returns the MinHash of the input.
*/
std::vector<uint32_t> FromArray(const std::vector<uint32_t> &vec);
/*!
\brief Creates a molecular shingling based on circular substructures.
A molecular shingling is a vector of SMILES that were extracted from and
represent a molecule. This method extracts substructures centered at each
atom of the molecule with different radii. A molecule with 10 atoms will
generate <tt>10 * 3</tt> shingles when a radius of <tt>3</tt> is chosen.
\param radius the maximum radius of the substructure that is generated at
each atom. Default: <tt>3</tt>.
\param rings whether the rings (SSSR) are extrected from the molecule and
added to the shingling. Given the molecule
<tt>"C1CCCCCC1C(=O)C"</tt>, <tt>"C1CCCCCC1"</tt> would be added
to the shingling. Default: <tt>true</tt>.
\param isomeric whether the SMILES added to the shingling are isomeric.
Default: <tt>false</tt>.
\param kekulize whether the SMILES added to the shingling are kekulized.
Default: <tt>true</tt>. NOTE that this will throw an exception if
the molecule cannot be kekulized.
\param min_radius the minimum radius that is used to extract n-grams.
Default: <tt>1</tt>.
\returns the shingling of a molecule.
*/
std::vector<std::string> CreateShingling(const ROMol &mol,
unsigned char radius = 3,
bool rings = true,
bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1);
//! \overload
std::vector<std::string> CreateShingling(const std::string &smiles,
unsigned char radius = 3,
bool rings = true,
bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1);
/*!
\brief Creates a MinHash vector from a molecule.
This methods is a wrapper around MHFPEncoder::CreateShingling and
MHFPEncoder::FromStringArray. When a vector of molecules or SMILES is passed
and RDKit was compiled with OpenMP, it is parallelized and will speed up by
a factor of the number of cores.
\param radius the maximum radius of the substructure that is generated at
each atom. Default: <tt>3</tt>.
\param rings whether the rings (SSSR) are extrected from the molecule and
added to the shingling. Given the molecule
<tt>"C1CCCCCC1C(=O)C"</tt>, <tt>"C1CCCCCC1"</tt> would be added
to the shingling. Default: <tt>true</tt>.
\param isomeric whether the SMILES added to the shingling are isomeric.
Default: <tt>false</tt>.
\param kekulize whether the SMILES added to the shingling are kekulized.
Default: <tt>true</tt>. NOTE that this will throw an exception if
the molecule cannot be kekulized.
\param min_radius the minimum radius that is used to extract n-grams.
Default: <tt>1</tt>.
\returns the MHFP fingerprint.
*/
std::vector<uint32_t> Encode(ROMol &mol, unsigned char radius = 3,
bool rings = true, bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1);
//! \overload
std::vector<std::vector<uint32_t>> Encode(std::vector<ROMol> &mols,
unsigned char radius = 3,
bool rings = true,
bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1);
//! \overload
std::vector<uint32_t> Encode(std::string &smiles, unsigned char radius = 3,
bool rings = true, bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1);
//! \overload
std::vector<std::vector<uint32_t>> Encode(std::vector<std::string> &smiles,
unsigned char radius = 3,
bool rings = true,
bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1);
/*!
\brief Creates a binary fingerprint based on circular sub-SMILES.
Creates a binary fingerprint similar to ECFP. However, instead of using
a Morgan-style hashing, circular n-grams (sub-SMILES) are created, hashed
directly and folded.
\param radius the maximum radius of the substructure that is generated at
each atom. Default: <tt>3</tt>.
\param rings whether the rings (SSSR) are extrected from the molecule and
added to the shingling. Given the molecule
<tt>"C1CCCCCC1C(=O)C"</tt>, <tt>"C1CCCCCC1"</tt> would be added
to the shingling. Default: <tt>true</tt>.
\param isomeric whether the SMILES added to the shingling are isomeric.
Default: <tt>false</tt>.
\param kekulize whether the SMILES added to the shingling are kekulized.
Default: <tt>true</tt>. NOTE that this will throw an exception if
the molecule cannot be kekulized.
\param min_radius the minimum radius that is used to extract n-grams.
Default: <tt>1</tt>.
\param length the length into which the fingerprint is folded.
Default: <tt>2048</tt>.
\returns the SECFP fingerprint.
*/
ExplicitBitVect EncodeSECFP(ROMol &mol, unsigned char radius = 3,
bool rings = true, bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1,
size_t length = 2048);
//! \overload
std::vector<ExplicitBitVect> EncodeSECFP(
std::vector<ROMol> &mols, unsigned char radius = 3, bool rings = true,
bool isomeric = false, bool kekulize = false,
unsigned char min_radius = 1, size_t length = 2048);
//! \overload
ExplicitBitVect EncodeSECFP(std::string &smiles, unsigned char radius = 3,
bool rings = true, bool isomeric = false,
bool kekulize = false,
unsigned char min_radius = 1,
size_t length = 2048);
//! \overload
std::vector<ExplicitBitVect> EncodeSECFP(
std::vector<std::string> &smiles, unsigned char radius = 3,
bool rings = true, bool isomeric = false, bool kekulize = false,
unsigned char min_radius = 1, size_t length = 2048);
/*!
\brief Calculates the Hamming distance between two MHFP
fingerprints.
\param a an MHFP fingerprint vector.
\param b an MHFP fingerprint vector.
\returns the Hamming distance between the two fingerprints.
*/
static double Distance(const std::vector<uint32_t> &a,
const std::vector<uint32_t> &b) {
size_t mismatches = 0;
for (size_t i = 0; i < a.size(); i++) {
if (a[i] != b[i]) {
mismatches++;
}
}
return mismatches / (double)a.size();
}
private:
//! The fastest mod implementation.
uint64_t FastMod(const uint64_t input, const uint64_t ceil) {
return input >= ceil ? input % ceil : input;
}
ExplicitBitVect Fold(const std::vector<uint32_t> &vec,
uint32_t length = 2048) {
ExplicitBitVect ebv(length);
for (size_t i = 0; i < vec.size(); i++) {
ebv.setBit(vec[i] % length);
}
return ebv;
}
std::vector<uint32_t> HashShingling(std::vector<std::string> vec) {
std::vector<uint32_t> result(vec.size());
for (size_t i = 0; i < vec.size(); i++) {
result[i] = FNV::hash(vec[i]);
}
return result;
}
unsigned int n_permutations_, seed_;
uint64_t prime_ = 2305843009213693951UL;
uint32_t max_hash_ = 4294967295;
std::vector<uint32_t> perms_a_;
std::vector<uint32_t> perms_b_;
};
} // namespace MHFPFingerprints
} // namespace RDKit
#endif
|