// // 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 #ifndef RD_MHFPFPS_H #define RD_MHFPFPS_H #include #include #include #include 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: 2048. \param seed a random seed. Default: 42. */ 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 FromStringArray(const std::vector& 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 FromArray(const std::vector& 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 10 * 3 shingles when a radius of 3 is chosen. \param radius the maximum radius of the substructure that is generated at each atom. Default: 3. \param rings whether the rings (SSSR) are extrected from the molecule and added to the shingling. Given the molecule "C1CCCCCC1C(=O)C", "C1CCCCCC1" would be added to the shingling. Default: true. \param isomeric whether the SMILES added to the shingling are isomeric. Default: false. \param kekulize whether the SMILES added to the shingling are kekulized. Default: true. 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: 1. \returns the shingling of a molecule. */ std::vector 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 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: 3. \param rings whether the rings (SSSR) are extrected from the molecule and added to the shingling. Given the molecule "C1CCCCCC1C(=O)C", "C1CCCCCC1" would be added to the shingling. Default: true. \param isomeric whether the SMILES added to the shingling are isomeric. Default: false. \param kekulize whether the SMILES added to the shingling are kekulized. Default: true. 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: 1. \returns the MHFP fingerprint. */ std::vector Encode(ROMol& mol, unsigned char radius = 3, bool rings = true, bool isomeric = false, bool kekulize = false, unsigned char min_radius = 1); //! \overload std::vector> Encode(std::vector& mols, unsigned char radius = 3, bool rings = true, bool isomeric = false, bool kekulize = false, unsigned char min_radius = 1); //! \overload std::vector 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> Encode(std::vector& 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: 3. \param rings whether the rings (SSSR) are extrected from the molecule and added to the shingling. Given the molecule "C1CCCCCC1C(=O)C", "C1CCCCCC1" would be added to the shingling. Default: true. \param isomeric whether the SMILES added to the shingling are isomeric. Default: false. \param kekulize whether the SMILES added to the shingling are kekulized. Default: true. 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: 1. \param length the length into which the fingerprint is folded. Default: 2048. \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 EncodeSECFP( std::vector& 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 EncodeSECFP( std::vector& 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 Jaccard / Tanimoto distance between two MHFP fingerprints. \param a an MHFP fingerprint vector. \param b an MHFP fingerprint vector. \returns the Jaccard / Tanimoto distance between the two fingerprints. */ static double Distance(const std::vector& a, const std::vector& b) { size_t matches = 0; for (size_t i = 0; i < a.size(); i++) { if (a[i] == b[i]) { matches++; } } return matches / (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& 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 HashShingling(std::vector vec) { std::vector 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 perms_a_; std::vector perms_b_; }; } // namespace MHFPFingerprints } // namespace RDKit #endif