RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
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)
30template <typename T>
31double 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) {
47 res = 1.0 - res;
48 }
49 return res;
50}
51//! \overload
52template <typename T>
53double SimilarityWrapper(const T &bv1, const T &bv2, double a, double b,
54 double (*metric)(const T &, const T &, double, double),
55 bool returnDistance = false) {
56 double res = 0.0;
57 if (bv1.getNumBits() > bv2.getNumBits()) {
58 T *bv1tmp = FoldFingerprint(bv1, bv1.getNumBits() / bv2.getNumBits());
59 res = metric(*bv1tmp, bv2, a, b);
60 delete bv1tmp;
61 } else if (bv2.getNumBits() > bv1.getNumBits()) {
62 T *bv2tmp = FoldFingerprint(bv2, bv2.getNumBits() / bv1.getNumBits());
63 res = metric(bv1, *bv2tmp, a, b);
64 delete bv2tmp;
65 } else {
66 res = metric(bv1, bv2, a, b);
67 }
68 if (returnDistance) {
69 res = 1.0 - res;
70 }
71 return res;
72}
73
75 const char *ref);
76RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const std::string &probe,
77 const std::string &ref);
79 const ExplicitBitVect &ref);
80
81template <typename T1>
83 const std::string &pkl);
84
85template <typename T1>
86RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const T1 &probe, const T1 &ref);
87
88//! returns the number of on bits in common between two bit vectors
89/*!
90 \return (bv1&bv2)_o
91*/
92template <typename T1, typename T2>
93RDKIT_DATASTRUCTS_EXPORT int NumOnBitsInCommon(const T1 &bv1, const T2 &bv2);
94
96 const ExplicitBitVect &bv2);
97
98//! returns the Tanimoto similarity between two bit vects
99/*!
100 \return <tt>(bv1&bv2)_o / [bv1_o + bv2_o - (bv1&bv2)_o]</tt>
101*/
102template <typename T1, typename T2>
104 const T2 &bv2);
105
106//! returns the Cosine similarity between two bit vects
107/*!
108 Also known as the Ochiai coefficient.
109 \return <tt>(bv1&bv2)_o / sqrt(bv1_o * bv2_o)</tt>
110*/
111template <typename T1, typename T2>
112RDKIT_DATASTRUCTS_EXPORT double CosineSimilarity(const T1 &bv1, const T2 &bv2);
113
114//! returns the Kulczynski similarity between two bit vects
115/*!
116 \return <tt>(bv1&bv2)_o * [bv1_o + bv2_o] / [2 * bv1_o * bv2_o]</tt>
117*/
118template <typename T1, typename T2>
120 const T2 &bv2);
121
122//! returns the Dice similarity between two bit vects
123/*!
124 \return <tt>2*(bv1&bv2)_o / [bv1_o + bv2_o]</tt>
125*/
126template <typename T1, typename T2>
127RDKIT_DATASTRUCTS_EXPORT double DiceSimilarity(const T1 &bv1, const T2 &bv2);
128
129//! returns the Tversky similarity between two bit vects
130/*!
131 \return <tt>(bv1&bv2)_o / [a*bv1_o + b*bv2_o + (1 - a - b)*(bv1&bv2)_o]</tt>
132
133 Notes:
134 # 0 <= a,b <= 1
135 # Tversky(a=1,b=1) = Tanimoto
136 # Tversky(a=1/2,b=1/2) = Dice
137
138*/
139template <typename T1, typename T2>
140RDKIT_DATASTRUCTS_EXPORT double TverskySimilarity(const T1 &bv1, const T2 &bv2,
141 double a, double b);
142
143//! returns the Sokal similarity between two bit vects
144/*!
145 \return <tt>(bv1&bv2)_o / [2*bv1_o + 2*bv2_o - 3*(bv1&bv2)_o]</tt>
146*/
147template <typename T1, typename T2>
148RDKIT_DATASTRUCTS_EXPORT double SokalSimilarity(const T1 &bv1, const T2 &bv2);
149
150//! returns the McConnaughey similarity between two bit vects
151/*!
152 \return <tt>[(bv1&bv2)_o * (bv1_o + bv2_o) - (bv1_o * bv2_o)] / (bv1_o *
153 bv2_o)</tt>
154*/
155template <typename T1, typename T2>
157 const T2 &bv2);
158
159//! returns the Asymmetric similarity between two bit vects
160/*!
161 Also known as the **Simpson similarity** or **Overlap coefficient**.
162 \return <tt>(bv1&bv2)_o / min(bv1_o,bv2_o)</tt>
163*/
164template <typename T1, typename T2>
166 const T2 &bv2);
167
168//! returns the Braun-Blanquet similarity between two bit vects
169/*!
170 \return <tt>(bv1&bv2)_o / max(bv1_o,bv2_o)</tt>
171*/
172template <typename T1, typename T2>
174 const T2 &bv2);
175
176//! returns the Russel similarity between two bit vects
177/*!
178 \return <tt>(bv1&bv2)_o / bv1_o</tt>
179
180 <b>Note:</b> that this operation is non-commutative:
181 RusselSimilarity(bv1,bv2) != RusselSimilarity(bv2,bv1)
182
183*/
184template <typename T1, typename T2>
185RDKIT_DATASTRUCTS_EXPORT double RusselSimilarity(const T1 &bv1, const T2 &bv2);
186
187//! returns the Rogot-Goldberg similarity between two bit vects
188/*!
189 \return <tt>(bv1&bv2)_o / (bv1_o + bv2_o)
190 + (bv1_n - bv1_o - bv2_o + (bv1&bv2)_o) / (2*bv1_n - bv1_o - bv2_o) </tt>
191*/
192template <typename T1, typename T2>
194 const T2 &bv2);
195
196//! returns the on bit similarity between two bit vects
197/*!
198 \return <tt>(bv1&bv2)_o / (bv1|bv2)_o </tt>
199*/
200template <typename T1, typename T2>
201RDKIT_DATASTRUCTS_EXPORT double OnBitSimilarity(const T1 &bv1, const T2 &bv2);
202
203//! returns the number of common bits (on and off) between two bit vects
204/*!
205 \return <tt>bv1_n - (bv1^bv2)_o</tt>
206*/
207template <typename T1, typename T2>
208RDKIT_DATASTRUCTS_EXPORT int NumBitsInCommon(const T1 &bv1, const T2 &bv2);
209
211 const ExplicitBitVect &bv2);
212
213//! returns the common-bit similarity (on and off) between two bit vects
214/*!
215 Also known as:
216 - **Simple Matching Coefficient**
217 - **Rand similarity**
218 - **Sokal–Michener similarity**
219
220 \return <tt>[bv1_n - (bv1^bv2)_o] / bv1_n</tt>
221*/
222template <typename T1, typename T2>
223RDKIT_DATASTRUCTS_EXPORT double AllBitSimilarity(const T1 &bv1, const T2 &bv2);
224
225//! returns an IntVect with indices of all on bits in common between two bit
226/// vects
227template <typename T1, typename T2>
228RDKIT_DATASTRUCTS_EXPORT IntVect OnBitsInCommon(const T1 &bv1, const T2 &bv2);
229
230//! returns an IntVect with indices of all off bits in common between two bit
231/// vects
232template <typename T1, typename T2>
234
235//! returns the on-bit projected similarities between two bit vects
236/*!
237 \return two values, as a DoubleVect:
238 - <tt>(bv1&bv2)_o / bv1_o</tt>
239 - <tt>(bv1&bv2)_o / bv2_o</tt>
240*/
241template <typename T1, typename T2>
243 const T2 &bv2);
244
245//! returns the on-bit projected similarities between two bit vects
246/*!
247 \return two values, as a DoubleVect:
248 - <tt>[bv1_n - (bv1|bv2)_o] / [bv1_n - bv1_o]</tt>
249 - <tt>[bv2_n - (bv1|bv2)_o] / [bv2_n - bv2_o]</tt>
250
251 <b>Note:</b> <tt>bv1_n = bv2_n</tt>
252
253*/
254template <typename T1, typename T2>
256 const T2 &bv2);
257
258//! folds a bit vector \c factor times and returns the result
259/*!
260 \param bv1 the vector to be folded
261 \param factor (optional) the number of times to fold it
262
263 \return a pointer to the folded fingerprint, which is
264 <tt>bv1_n/factor</tt> long.
265
266 <b>Note:</b> The caller is responsible for <tt>delete</tt>ing the result.
267 */
268template <typename T1>
270 unsigned int factor = 2);
271
272//! returns a text representation of a bit vector (a string of 0s and 1s)
273/*!
274 \param bv1 the vector to use
275
276 \return an std::string
277
278 */
279template <typename T1>
280RDKIT_DATASTRUCTS_EXPORT std::string BitVectToText(const T1 &bv1);
281
282//! returns a hex representation of a bit vector compatible with Andrew Dalke's
283/// FPS format
284/*!
285 \param bv1 the vector to use
286
287 \return an std::string
288
289 */
290template <typename T1>
292
293//! returns a binary string representation of a bit vector (an array of bytes)
294/*!
295 \param bv1 the vector to use
296
297 \return an std::string
298
299 */
300template <typename T1>
302
303//! updates a bit vector from Andrew Dalke's FPS format
304/*!
305 \param bv1 the vector to use
306 \param fps the FPS hex string
307
308
309 */
310template <typename T1>
312 const std::string &fps);
313
314//! updates a bit vector from a binary string representation of a bit vector (an
315/// array of bytes)
316/*!
317 \param bv1 the vector to use
318 \param fps the binary string
319
320
321 */
322template <typename T1>
324 T1 &bv1, const std::string &fps);
325
326// FIX: docs and tests please
327
329 const unsigned char *bv1, unsigned int nBytes);
330
331RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTanimoto(const unsigned char *bv1,
332 const unsigned char *bv2,
333 unsigned int nBytes);
334RDKIT_DATASTRUCTS_EXPORT double CalcBitmapDice(const unsigned char *bv1,
335 const unsigned char *bv2,
336 unsigned int nBytes);
337RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTversky(const unsigned char *bv1,
338 const unsigned char *bv2,
339 unsigned int nBytes,
340 double ca, double cb);
342 const unsigned char *probe, const unsigned char *ref, unsigned int nBytes);
343#endif
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 McConnaugheySimilarity(const T1 &bv1, const T2 &bv2)
returns the McConnaughey similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT int NumBitsInCommon(const T1 &bv1, const T2 &bv2)
returns the number of common bits (on and off) between two bit vects
RDKIT_DATASTRUCTS_EXPORT double AsymmetricSimilarity(const T1 &bv1, const T2 &bv2)
returns the Asymmetric similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT bool AllProbeBitsMatch(const char *probe, const char *ref)
RDKIT_DATASTRUCTS_EXPORT unsigned int CalcBitmapPopcount(const unsigned char *bv1, unsigned int nBytes)
double SimilarityWrapper(const T &bv1, const T &bv2, double(*metric)(const T &, const T &), bool returnDistance=false)
Definition BitOps.h:31
RDKIT_DATASTRUCTS_EXPORT std::string BitVectToFPSText(const T1 &bv1)
RDKIT_DATASTRUCTS_EXPORT double TverskySimilarity(const T1 &bv1, const T2 &bv2, double a, double b)
returns the Tversky similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double RusselSimilarity(const T1 &bv1, const T2 &bv2)
returns the Russel similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double AllBitSimilarity(const T1 &bv1, const T2 &bv2)
returns the common-bit similarity (on and off) between two bit vects
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 BraunBlanquetSimilarity(const T1 &bv1, const T2 &bv2)
returns the Braun-Blanquet 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 bool CalcBitmapAllProbeBitsMatch(const unsigned char *probe, const unsigned char *ref, unsigned int nBytes)
RDKIT_DATASTRUCTS_EXPORT IntVect OnBitsInCommon(const T1 &bv1, const T2 &bv2)
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 double SokalSimilarity(const T1 &bv1, const T2 &bv2)
returns the Sokal similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double CosineSimilarity(const T1 &bv1, const T2 &bv2)
returns the Cosine similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double DiceSimilarity(const T1 &bv1, const T2 &bv2)
returns the Dice similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double KulczynskiSimilarity(const T1 &bv1, const T2 &bv2)
returns the Kulczynski similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT double RogotGoldbergSimilarity(const T1 &bv1, const T2 &bv2)
returns the Rogot-Goldberg similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT IntVect OffBitsInCommon(const T1 &bv1, const T2 &bv2)
RDKIT_DATASTRUCTS_EXPORT double CalcBitmapDice(const unsigned char *bv1, const unsigned char *bv2, unsigned int nBytes)
RDKIT_DATASTRUCTS_EXPORT double TanimotoSimilarity(const T1 &bv1, const T2 &bv2)
returns the Tanimoto similarity between two bit vects
RDKIT_DATASTRUCTS_EXPORT void UpdateBitVectFromBinaryText(T1 &bv1, const std::string &fps)
RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTversky(const unsigned char *bv1, const unsigned char *bv2, unsigned int nBytes, double ca, double cb)
RDKIT_DATASTRUCTS_EXPORT void UpdateBitVectFromFPSText(T1 &bv1, const std::string &fps)
updates a bit vector from Andrew Dalke's FPS format
RDKIT_DATASTRUCTS_EXPORT double CalcBitmapTanimoto(const unsigned char *bv1, const unsigned char *bv2, unsigned int nBytes)
RDKIT_DATASTRUCTS_EXPORT DoubleVect OffBitProjSimilarity(const T1 &bv1, const T2 &bv2)
returns the on-bit projected similarities between two bit vects
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 DoubleVect OnBitProjSimilarity(const T1 &bv1, const T2 &bv2)
returns the on-bit projected similarities between two bit vects
std::vector< int > IntVect
Definition BitVect.h:17
std::vector< double > DoubleVect
Definition BitVect.h:19
Pulls in all the BitVect classes.
a class for bit vectors that are densely occupied
#define RDKIT_DATASTRUCTS_EXPORT
Definition export.h:89