RDKit
Open-source cheminformatics and machine learning.
BitOps.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2003-2012 greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #include <RDGeneral/export.h>
11 #ifndef __RD_BITOPS_H__
12 #define __RD_BITOPS_H__
13 /*! \file BitOps.h
14 
15  \brief Contains general bit-comparison and similarity operations.
16 
17  The notation used to document the similarity metrics is:
18  - \c V1_n: number of bits in vector 1
19  - \c V1_o: number of on bits in vector 1
20  - <tt>(V1&V2)_o</tt>: number of on bits in the intersection of vectors 1 and
21  2
22 
23  */
24 
25 #include "BitVects.h"
26 #include <string>
27 
28 //! general purpose wrapper for calculating the similarity between two bvs
29 //! that may be of unequal size (will automatically fold as appropriate)
30 template <typename T>
31 double SimilarityWrapper(const T& bv1, const T& bv2,
32  double (*metric)(const T&, const T&),
33  bool returnDistance = false) {
34  double res = 0.0;
35  if (bv1.getNumBits() > bv2.getNumBits()) {
36  T* bv1tmp = FoldFingerprint(bv1, bv1.getNumBits() / bv2.getNumBits());
37  res = metric(*bv1tmp, bv2);
38  delete bv1tmp;
39  } else if (bv2.getNumBits() > bv1.getNumBits()) {
40  T* bv2tmp = FoldFingerprint(bv2, bv2.getNumBits() / bv1.getNumBits());
41  res = metric(bv1, *bv2tmp);
42  delete bv2tmp;
43  } else {
44  res = metric(bv1, bv2);
45  }
46  if (returnDistance) res = 1.0 - res;
47  return res;
48 }
49 //! \overload
50 template <typename T>
51 double SimilarityWrapper(const T& bv1, const T& bv2, double a, double b,
52  double (*metric)(const T&, const T&, double, double),
53  bool returnDistance = false) {
54  double res = 0.0;
55  if (bv1.getNumBits() > bv2.getNumBits()) {
56  T* bv1tmp = FoldFingerprint(bv1, bv1.getNumBits() / bv2.getNumBits());
57  res = metric(*bv1tmp, bv2, a, b);
58  delete bv1tmp;
59  } else if (bv2.getNumBits() > bv1.getNumBits()) {
60  T* bv2tmp = FoldFingerprint(bv2, bv2.getNumBits() / bv1.getNumBits());
61  res = metric(bv1, *bv2tmp, a, b);
62  delete bv2tmp;
63  } else {
64  res = metric(bv1, bv2, a, b);
65  }
66  if (returnDistance) res = 1.0 - res;
67  return res;
68 }
69 
70 RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const char* probe, const char* ref);
71 RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const std::string& probe, const std::string& ref);
73  const ExplicitBitVect& ref);
74 
75 template <typename T1>
76 RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const T1& probe, const std::string& pkl);
77 
78 template <typename T1>
79 RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const T1& probe, const T1& ref);
80 
81 //! returns the number of on bits in common between two bit vectors
82 /*!
83  \return (bv1&bv2)_o
84 */
85 template <typename T1, typename T2>
86 RDKIT_DATASTRUCTS_EXPORT int NumOnBitsInCommon(const T1& bv1, const T2& bv2);
87 
89 
90 //! returns the Tanimoto similarity between two bit vects
91 /*!
92  \return <tt>(bv1&bv2)_o / [bv1_o + bv2_o - (bv1&bv2)_o]</tt>
93 */
94 template <typename T1, typename T2>
95 RDKIT_DATASTRUCTS_EXPORT double TanimotoSimilarity(const T1& bv1, const T2& bv2);
96 
97 //! returns the Cosine similarity between two bit vects
98 /*!
99  \return <tt>(bv1&bv2)_o / sqrt(bv1_o + bv2_o)</tt>
100 */
101 template <typename T1, typename T2>
102 RDKIT_DATASTRUCTS_EXPORT double CosineSimilarity(const T1& bv1, const T2& bv2);
103 
104 //! returns the Kulczynski similarity between two bit vects
105 /*!
106  \return <tt>(bv1&bv2)_o * [bv1_o + bv2_o] / [2 * bv1_o * bv2_o]</tt>
107 */
108 template <typename T1, typename T2>
109 RDKIT_DATASTRUCTS_EXPORT double KulczynskiSimilarity(const T1& bv1, const T2& bv2);
110 
111 //! returns the Dice similarity between two bit vects
112 /*!
113  \return <tt>2*(bv1&bv2)_o / [bv1_o + bv2_o]</tt>
114 */
115 template <typename T1, typename T2>
116 RDKIT_DATASTRUCTS_EXPORT double DiceSimilarity(const T1& bv1, const T2& bv2);
117 
118 //! returns the Tversky similarity between two bit vects
119 /*!
120  \return <tt>(bv1&bv2)_o / [a*bv1_o + b*bv2_o + (1 - a - b)*(bv1&bv2)_o]</tt>
121 
122  Notes:
123  # 0 <= a,b <= 1
124  # Tversky(a=1,b=1) = Tanimoto
125  # Tversky(a=1/2,b=1/2) = Dice
126 
127 */
128 template <typename T1, typename T2>
129 RDKIT_DATASTRUCTS_EXPORT double TverskySimilarity(const T1& bv1, const T2& bv2, double a, double b);
130 
131 //! returns the Sokal similarity between two bit vects
132 /*!
133  \return <tt>(bv1&bv2)_o / [2*bv1_o + 2*bv2_o - 3*(bv1&bv2)_o]</tt>
134 */
135 template <typename T1, typename T2>
136 RDKIT_DATASTRUCTS_EXPORT double SokalSimilarity(const T1& bv1, const T2& bv2);
137 
138 //! returns the McConnaughey similarity between two bit vects
139 /*!
140  \return <tt>[(bv1&bv2)_o * (bv1_o + bv2_o) - (bv1_o * bv2_o)] / (bv1_o *
141  bv2_o)</tt>
142 */
143 template <typename T1, typename T2>
144 RDKIT_DATASTRUCTS_EXPORT double McConnaugheySimilarity(const T1& bv1, const T2& bv2);
145 
146 //! returns the Asymmetric similarity between two bit vects
147 /*!
148  \return <tt>(bv1&bv2)_o / min(bv1_o,bv2_o)</tt>
149 */
150 template <typename T1, typename T2>
151 RDKIT_DATASTRUCTS_EXPORT double AsymmetricSimilarity(const T1& bv1, const T2& bv2);
152 
153 //! returns the Braun-Blanquet similarity between two bit vects
154 /*!
155  \return <tt>(bv1&bv2)_o / max(bv1_o,bv2_o)</tt>
156 */
157 template <typename T1, typename T2>
158 RDKIT_DATASTRUCTS_EXPORT double BraunBlanquetSimilarity(const T1& bv1, const T2& bv2);
159 
160 //! returns the Russel similarity between two bit vects
161 /*!
162  \return <tt>(bv1&bv2)_o / bv1_o</tt>
163 
164  <b>Note:</b> that this operation is non-commutative:
165  RusselSimilarity(bv1,bv2) != RusselSimilarity(bv2,bv1)
166 
167 */
168 template <typename T1, typename T2>
169 RDKIT_DATASTRUCTS_EXPORT double RusselSimilarity(const T1& bv1, const T2& bv2);
170 
171 //! returns the Rogot-Goldberg similarity between two bit vects
172 /*!
173  \return <tt>(bv1&bv2)_o / (bv1_o + bv2_o)
174  + (bv1_n - bv1_o - bv2_o + (bv1&bv2)_o) / (2*bv1_n - bv1_o - bv2_o) </tt>
175 */
176 template <typename T1, typename T2>
177 RDKIT_DATASTRUCTS_EXPORT double RogotGoldbergSimilarity(const T1& bv1, const T2& bv2);
178 
179 //! returns the on bit similarity between two bit vects
180 /*!
181  \return <tt>(bv1&bv2)_o / (bv1|bv2)_o </tt>
182 */
183 template <typename T1, typename T2>
184 RDKIT_DATASTRUCTS_EXPORT double OnBitSimilarity(const T1& bv1, const T2& bv2);
185 
186 //! returns the number of common bits (on and off) between two bit vects
187 /*!
188  \return <tt>bv1_n - (bv1^bv2)_o</tt>
189 */
190 template <typename T1, typename T2>
191 RDKIT_DATASTRUCTS_EXPORT int NumBitsInCommon(const T1& bv1, const T2& bv2);
192 
194 
195 //! returns the common-bit similarity (on and off) between two bit vects
196 //! This is also called Manhattan similarity.
197 /*!
198  \return <tt>[bv1_n - (bv1^bv2)_o] / bv1_n</tt>
199 */
200 template <typename T1, typename T2>
201 RDKIT_DATASTRUCTS_EXPORT double AllBitSimilarity(const T1& bv1, const T2& bv2);
202 
203 //! returns an IntVect with indices of all on bits in common between two bit
204 // vects
205 template <typename T1, typename T2>
206 RDKIT_DATASTRUCTS_EXPORT IntVect OnBitsInCommon(const T1& bv1, const T2& bv2);
207 
208 //! returns an IntVect with indices of all off bits in common between two bit
209 // vects
210 template <typename T1, typename T2>
211 RDKIT_DATASTRUCTS_EXPORT IntVect OffBitsInCommon(const T1& bv1, const T2& bv2);
212 
213 //! returns the on-bit projected similarities between two bit vects
214 /*!
215  \return two values, as a DoubleVect:
216  - <tt>(bv1&bv2)_o / bv1_o</tt>
217  - <tt>(bv1&bv2)_o / bv2_o</tt>
218 */
219 template <typename T1, typename T2>
220 RDKIT_DATASTRUCTS_EXPORT DoubleVect OnBitProjSimilarity(const T1& bv1, const T2& bv2);
221 
222 //! returns the on-bit projected similarities between two bit vects
223 /*!
224  \return two values, as a DoubleVect:
225  - <tt>[bv1_n - (bv1|bv2)_o] / [bv1_n - bv1_o]</tt>
226  - <tt>[bv2_n - (bv1|bv2)_o] / [bv2_n - bv2_o]</tt>
227 
228  <b>Note:</b> <tt>bv1_n = bv2_n</tt>
229 
230 */
231 template <typename T1, typename T2>
232 RDKIT_DATASTRUCTS_EXPORT DoubleVect OffBitProjSimilarity(const T1& bv1, const T2& bv2);
233 
234 //! folds a bit vector \c factor times and returns the result
235 /*!
236  \param bv1 the vector to be folded
237  \param factor (optional) the number of times to fold it
238 
239  \return a pointer to the folded fingerprint, which is
240  <tt>bv1_n/factor</tt> long.
241 
242  <b>Note:</b> The caller is responsible for <tt>delete</tt>ing the result.
243  */
244 template <typename T1>
245 RDKIT_DATASTRUCTS_EXPORT T1* FoldFingerprint(const T1& bv1, unsigned int factor = 2);
246 
247 //! returns a text representation of a bit vector (a string of 0s and 1s)
248 /*!
249  \param bv1 the vector to use
250 
251  \return an std::string
252 
253  */
254 template <typename T1>
255 RDKIT_DATASTRUCTS_EXPORT std::string BitVectToText(const T1& bv1);
256 
257 //! returns a hex representation of a bit vector compatible with Andrew Dalke's
258 // FPS format
259 /*!
260  \param bv1 the vector to use
261 
262  \return an std::string
263 
264  */
265 template <typename T1>
266 RDKIT_DATASTRUCTS_EXPORT std::string BitVectToFPSText(const T1& bv1);
267 
268 //! returns a binary string representation of a bit vector (an array of bytes)
269 /*!
270  \param bv1 the vector to use
271 
272  \return an std::string
273 
274  */
275 template <typename T1>
276 RDKIT_DATASTRUCTS_EXPORT std::string BitVectToBinaryText(const T1& bv1);
277 
278 //! updates a bit vector from Andrew Dalke's FPS format
279 /*!
280  \param bv1 the vector to use
281  \param fps the FPS hex string
282 
283 
284  */
285 template <typename T1>
286 RDKIT_DATASTRUCTS_EXPORT void UpdateBitVectFromFPSText(T1& bv1, const std::string& fps);
287 
288 //! updates a bit vector from a binary string representation of a bit vector (an
289 // array of bytes)
290 /*!
291  \param bv1 the vector to use
292  \param fps the binary string
293 
294 
295  */
296 template <typename T1>
297 RDKIT_DATASTRUCTS_EXPORT void UpdateBitVectFromBinaryText(T1& bv1, const std::string& fps);
298 
299 // FIX: docs and tests please
300 
301 RDKIT_DATASTRUCTS_EXPORT unsigned int CalcBitmapPopcount(const unsigned char* bv1, unsigned int nBytes);
302 
303 RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTanimoto(const unsigned char* bv1, const unsigned char* bv2,
304  unsigned int nBytes);
305 RDKIT_DATASTRUCTS_EXPORT double CalcBitmapDice(const unsigned char* bv1, const unsigned char* bv2,
306  unsigned int nBytes);
307 RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTversky(const unsigned char* bv1, const unsigned char* bv2,
308  unsigned int nBytes, double ca, double cb);
309 RDKIT_DATASTRUCTS_EXPORT bool CalcBitmapAllProbeBitsMatch(const unsigned char* probe,
310  const unsigned char* ref, unsigned int nBytes);
311 #endif
RDKIT_DATASTRUCTS_EXPORT DoubleVect OnBitProjSimilarity(const T1 &bv1, const T2 &bv2)
returns the on-bit projected similarities between two bit vects
RDKIT_DATASTRUCTS_EXPORT double CalcBitmapDice(const unsigned char *bv1, const unsigned char *bv2, unsigned int nBytes)
RDKIT_DATASTRUCTS_EXPORT double KulczynskiSimilarity(const T1 &bv1, const T2 &bv2)
returns the Kulczynski similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double OnBitSimilarity(const T1 &bv1, const T2 &bv2)
returns the on bit similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT void UpdateBitVectFromFPSText(T1 &bv1, const std::string &fps)
updates a bit vector from Andrew Dalke&#39;s FPS format
RDKIT_DATASTRUCTS_EXPORT int NumBitsInCommon(const T1 &bv1, const T2 &bv2)
returns the number of common bits (on and off) between two bit vects
Pulls in all the BitVect classes.
RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTversky(const unsigned char *bv1, const unsigned char *bv2, unsigned int nBytes, double ca, double cb)
RDKIT_DATASTRUCTS_EXPORT double SokalSimilarity(const T1 &bv1, const T2 &bv2)
returns the Sokal similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT DoubleVect OffBitProjSimilarity(const T1 &bv1, const T2 &bv2)
returns the on-bit projected similarities between two bit vects
RDKIT_DATASTRUCTS_EXPORT double McConnaugheySimilarity(const T1 &bv1, const T2 &bv2)
returns the McConnaughey similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT IntVect OffBitsInCommon(const T1 &bv1, const T2 &bv2)
returns an IntVect with indices of all off bits in common between two bit
RDKIT_DATASTRUCTS_EXPORT T1 * FoldFingerprint(const T1 &bv1, unsigned int factor=2)
folds a bit vector factor times and returns the result
RDKIT_DATASTRUCTS_EXPORT std::string BitVectToFPSText(const T1 &bv1)
returns a hex representation of a bit vector compatible with Andrew Dalke&#39;s
RDKIT_DATASTRUCTS_EXPORT void UpdateBitVectFromBinaryText(T1 &bv1, const std::string &fps)
updates a bit vector from a binary string representation of a bit vector (an
RDKIT_DATASTRUCTS_EXPORT int NumOnBitsInCommon(const T1 &bv1, const T2 &bv2)
returns the number of on bits in common between two bit vectors
RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const char *probe, const char *ref)
#define RDKIT_DATASTRUCTS_EXPORT
Definition: export.h:112
RDKIT_DATASTRUCTS_EXPORT double AllBitSimilarity(const T1 &bv1, const T2 &bv2)
RDKIT_DATASTRUCTS_EXPORT double RusselSimilarity(const T1 &bv1, const T2 &bv2)
returns the Russel similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double TanimotoSimilarity(const T1 &bv1, const T2 &bv2)
returns the Tanimoto similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT unsigned int CalcBitmapPopcount(const unsigned char *bv1, unsigned int nBytes)
RDKIT_DATASTRUCTS_EXPORT std::string BitVectToBinaryText(const T1 &bv1)
returns a binary string representation of a bit vector (an array of bytes)
RDKIT_DATASTRUCTS_EXPORT double DiceSimilarity(const T1 &bv1, const T2 &bv2)
returns the Dice similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double BraunBlanquetSimilarity(const T1 &bv1, const T2 &bv2)
returns the Braun-Blanquet similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double TverskySimilarity(const T1 &bv1, const T2 &bv2, double a, double b)
returns the Tversky similarity between two bit vects
std::vector< double > DoubleVect
Definition: BitVect.h:19
double SimilarityWrapper(const T &bv1, const T &bv2, double(*metric)(const T &, const T &), bool returnDistance=false)
Definition: BitOps.h:31
RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTanimoto(const unsigned char *bv1, const unsigned char *bv2, unsigned int nBytes)
RDKIT_DATASTRUCTS_EXPORT std::string BitVectToText(const T1 &bv1)
returns a text representation of a bit vector (a string of 0s and 1s)
RDKIT_DATASTRUCTS_EXPORT double RogotGoldbergSimilarity(const T1 &bv1, const T2 &bv2)
returns the Rogot-Goldberg similarity between two bit vects
std::vector< int > IntVect
Definition: BitVect.h:17
a class for bit vectors that are densely occupied
RDKIT_DATASTRUCTS_EXPORT double AsymmetricSimilarity(const T1 &bv1, const T2 &bv2)
returns the Asymmetric similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT IntVect OnBitsInCommon(const T1 &bv1, const T2 &bv2)
returns an IntVect with indices of all on bits in common between two bit
RDKIT_DATASTRUCTS_EXPORT double CosineSimilarity(const T1 &bv1, const T2 &bv2)
returns the Cosine similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT bool CalcBitmapAllProbeBitsMatch(const unsigned char *probe, const unsigned char *ref, unsigned int nBytes)