File: BitOps.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (800 lines) | stat: -rw-r--r-- 23,486 bytes parent folder | download
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// $Id: BitOps.cpp 1927 2012-01-10 03:47:23Z glandrum $
//
//  Copyright (C) 2003-2012 greg Landrum and Rational Discovery LLC
//
//  @@ 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.
//
#include <RDBoost/Exceptions.h>
#include "BitVects.h"
#include "BitOps.h"
#include <math.h>
#include <string>
#include <iostream>
#include <RDGeneral/StreamOps.h>
#include <RDGeneral/types.h>
#include <RDBoost/Exceptions.h>
#include <sstream>

#include <boost/lexical_cast.hpp>

using namespace RDKit;

int getBitId(const char *&text,int format,int size,int curr){
  PRECONDITION(text,"no text");
  int res=-1;
  if( (format==0) || 
      ( (format == 1) && (size >= std::numeric_limits<unsigned short>::max()) ) ) {
    int tmp;
    tmp = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)text);
    text += sizeof(tmp);
    res=tmp;
  } else if (format == 1) { // version 16 and on bits sotred as short ints
    unsigned short tmp;
    tmp = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(unsigned short *)text);
    text += sizeof(tmp);
    res=tmp;
  } else if (format == 2) { // run length encoded format
    res = curr + RDKit::pullPackedIntFromString(text);
  }
  return res;
}



bool AllProbeBitsMatch(const std::string &probe,const std::string &ref){
  return AllProbeBitsMatch(probe.c_str(),ref.c_str());
}

bool AllProbeBitsMatch(const char *probe,const char *ref){
  PRECONDITION(probe,"no probe text");
  PRECONDITION(ref,"no probe text");
  int probeFormat=0;
  int refFormat=0;
  int version=0;

  int probeSize = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)probe);
  probe+=sizeof(probeSize);
  if(probeSize<0){
    version = -1*probeSize;
    if (version == 16) {
      probeFormat=1;
    }
    else if (version == 32) {
      probeFormat=2;
    }
    else {
      throw("Unknown version type for the encode bit vect");
    }
    probeSize = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)probe);
    probe+=sizeof(probeSize);
  }

  int refSize = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)ref);
  ref+=sizeof(refSize);
  if(refSize<0){
    version = -1*refSize;
    if (version == 16) {
      refFormat=1;
    }
    else if (version == 32) {
      refFormat=2;
    }
    else {
      throw("Unknown version type for the encode bit vect");
    }
    refSize = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)ref);
    ref+=sizeof(refSize);
  }


  int nProbeOn = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)probe);
  probe+=sizeof(nProbeOn);
  int nRefOn = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)ref);
  ref+=sizeof(nRefOn);

  int currProbeBit=0;
  currProbeBit=getBitId(probe,probeFormat,probeSize,currProbeBit);
  nProbeOn--;

  int currRefBit=0;
  currRefBit=getBitId(ref,refFormat,refSize,currRefBit);
  nRefOn--;

  while(nProbeOn){
    while(currRefBit<currProbeBit && nRefOn>0){
      if(refFormat==2) currRefBit++;
      currRefBit=getBitId(ref,refFormat,refSize,currRefBit);
      nRefOn--;
    }
    if(currRefBit!=currProbeBit) return false;
    if(probeFormat==2) currProbeBit++;
    currProbeBit=getBitId(probe,probeFormat,probeSize,currProbeBit);
    nProbeOn--;
  }
  return true;
}

template <typename T1>
bool AllProbeBitsMatch(const T1 &probe,const std::string &pkl){
  const char *text=pkl.c_str();
  int format=0;
  int nOn=0,size,version=0;
  size = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)text);
  text+=sizeof(size);
  if(size<0){
    version = -1*size;
    if (version == 16) {
      format=1;
    }
    else if (version == 32) {
      format=2;
    }
    else {
      throw("Unknown version type for the encode bit vect");
    }
    size = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)text);
    text+=sizeof(size);
  }
  nOn = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(*(int *)text);
  text+=sizeof(nOn);

  int currBit=0;
  currBit=getBitId(text,format,size,currBit);
  nOn--;
  std::vector<int> obl;
  probe.getOnBits(obl);
  

  //for(int i=0;i<probe.getNumBits();i++){
  //  if(probe.getBit(i)){
  for(std::vector<int>::const_iterator i=obl.begin();i!=obl.end();i++){
    while(currBit<*i && nOn>0){
        if(format==2) currBit++;
        currBit=getBitId(text,format,size,currBit);
        nOn--;
      }
      if(currBit!=*i) return false;
      //}
  }
  return true;
}
template bool AllProbeBitsMatch(const SparseBitVect& bv1,const std::string &pkl);
template bool AllProbeBitsMatch(const ExplicitBitVect& bv1,const std::string &pkl);
template <typename T1>
bool AllProbeBitsMatch(const T1 &probe,const T1 &ref){
  for(unsigned int i=0;i<probe.getNumBits();++i){
    if(probe.getBit(i) && !ref.getBit(i)){
      return false;
    }
  }
  return true;
}
template bool AllProbeBitsMatch(const SparseBitVect& bv1,const SparseBitVect& bv2);
template bool AllProbeBitsMatch(const ExplicitBitVect& bv1,const ExplicitBitVect &bv2);

// """ -------------------------------------------------------
//
//  NumOnBitsInCommon(T1,T2)
//  Returns the number of on bits which are set in both T1 and T2.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
int
NumOnBitsInCommon(const T1& bv1,
                const T2& bv2)
{
  return OnBitsInCommon(bv1,bv2).size();
}

int
NumOnBitsInCommon(const ExplicitBitVect& bv1,
                  const ExplicitBitVect& bv2)
{
  //std::cout << "nobic" << std::endl;
  int res = 0;
  unsigned int _sz = bv1.getNumBits()<bv2.getNumBits()?bv1.getNumBits():bv2.getNumBits();
  for(unsigned int i=0;i<_sz;i++) {
    if((*bv1.dp_bits)[i] && (*bv2.dp_bits)[i]) res+=1;
  }
  return res;
}


// In all these similarity metrics the notation is selected to be
//   consistent with J.W. Raymond and P. Willett, JCAMD _16_ 59-71 (2002)
// """ -------------------------------------------------------
//
//  TanimotoSimilarity(T1,T2)
//   returns the Tanamoto similarity between T1 and T2, a double.
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator&, getNumBits()
//  and getOnBits().
//
//  Python Notes: T1 and T2 are BitVects.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
double
TanimotoSimilarity(const T1& bv1,
                   const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();
  if((y+z-x)==0.0) return 1.0;
  else return x / (y+z-x);
}


template <typename T1, typename T2>
double
TverskySimilarity(const T1& bv1,
                  const T2& bv2,
                  double a,
                  double b)
{
  RANGE_CHECK(0,a,1);
  RANGE_CHECK(0,b,1);
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();
  double denom = a*y + b*z + (1-a-b)*x;
  if(denom==0.0) return 1.0;
  else return x / denom;
}

template <typename T1, typename T2>
double
CosineSimilarity(const T1& bv1,
                   const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();

  if(y*z>0.0){
    return x / sqrt(y*z);
  } else {
    return 0.0;
  }
}

template <typename T1, typename T2>
double
KulczynskiSimilarity(const T1& bv1,
                   const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();

  if(y*z>0.0){
    return x*(y+z)/(2*y*z);
  } else {
    return 0.0;
  }
}


template <typename T1, typename T2>
double
DiceSimilarity(const T1& bv1,
              const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();

  if(y+z>0.0){
    return 2*x/(y+z);
  } else {
    return 0.0;
  }
}

template <typename T1, typename T2>
double
SokalSimilarity(const T1& bv1,
              const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();

  return x/(2*y+2*z-3*x);
}

template <typename T1, typename T2>
double
McConnaugheySimilarity(const T1& bv1,
                       const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();

  if(y*z>0.0){
    return (x*(y+z)-(y*z))/(y*z);
  } else {
    return 0.0;
  }
}

template <typename T>
inline T tmin(T v1,T v2) {
  if(v1<v2) return v1;
  return v2;
}

template <typename T>
inline T tmax(T v1,T v2) {
  if(v1>v2) return v1;
  return v2;
}

template <typename T1, typename T2>
double
AsymmetricSimilarity(const T1& bv1,
                     const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();
  
  if(tmin(y,z)>0){
    return x/tmin(y,z);
  } else {
    return 0.0;
  }
}

template <typename T1, typename T2>
double
BraunBlanquetSimilarity(const T1& bv1,
                     const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  double y = bv1.getNumOnBits();
  double z = bv2.getNumOnBits();

  if(tmax(y,z)>0){
    return x/tmax(y,z);
  } else {
    return 0.0;
  }
}

template <typename T1, typename T2>
double
RusselSimilarity(const T1& bv1,
                     const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  double x = NumOnBitsInCommon(bv1,bv2);
  return x/bv1.getNumBits();
}




// """ -------------------------------------------------------
//
//  OnBitSimilarity(T1,T2)
//  Returns the percentage of possible on bits in common
//  between T1 and T2 (a double)
//
//  C++ Notes: T1 and T2 must support operator|, operator&
//  and getOnBits().
//
//  Python Notes: T1 and T2 are BitVects.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
double
OnBitSimilarity(const T1& bv1,
                const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");

  double num = NumOnBitsInCommon(bv1,bv2);
  double denom=(bv1|bv2).getNumOnBits();

  if(denom>0){
    return num/denom;
  } else {
    return 0;
  }
}

// """ -------------------------------------------------------
//
//  NumBitsInCommon(T1,T2)
//  Returns the number of bits in common (on and off)
//  between T1 and T2 (an int)
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator^, getNumBits().
//
//  Python Notes: T1 and T2 are BitVects.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
int
NumBitsInCommon(const T1& bv1,
                const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");

  return bv1.getNumBits() - (bv1^bv2).getNumOnBits();
}


// """ -------------------------------------------------------
//
//  AllBitSimilarity(T1,T2)
//  Returns the percentage of bits in common (on and off)
//  between T1 and T2 (a double)
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator^, getNumBits()
//  and getNumOnBits().
//
//  Python Notes: T1 and T2 are BitVects.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
double
AllBitSimilarity(const T1& bv1,
                const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");

  return double(NumBitsInCommon(bv1,bv2))/bv1.getNumBits();
}




// """ -------------------------------------------------------
//
//  OnBitsInCommon(T1,T2)
//  Returns the on bits which are set in both T1 and T2.
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator&, getNumBits()
//  and getOnBits(), the return value is an IntVect.
//
//  Python Notes: T1 and T2 are BitVects, the return value
//  is a tuple of ints.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
IntVect
OnBitsInCommon(const T1& bv1,
                const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  IntVect res;
  (bv1&bv2).getOnBits(res);
  return res;
}

// """ -------------------------------------------------------
//
//  OffBitsInCommon(T1,T2)
//  Returns the off bits which are set in both T1 and T2.
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator|, operator~,
// getNumBits() and getOnBits(), the return value is an IntVect.
//
//  Python Notes: T1 and T2 are BitVects, the return value
//  is a tuple of ints.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
IntVect
OffBitsInCommon(const T1& bv1,
                const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  IntVect res;
  (~(bv1|bv2)).getOnBits(res);
  return res;
}

// """ -------------------------------------------------------
//
//  OnBitProjSimilarity(T1,T2)
//  Returns the projected similarity between the on bits of
//  T1 and T2.
//
//  The on bit projected similarity of T1 onto T2 is the
//  percentage of T1's on bits which are on in T2.
//
//  This type of measure may be useful for substructure-type
//  searches.
//
//  Two values are returned, the projection of T1 onto T2
//  and the projection of T2 onto T1
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator&, getNumBits()
//  and getNumOnBits(), the return value is an DoubleVect with
//  two elements.
//
//  Python Notes: T1 and T2 are BitVects, the return value
//  is a 2-tuple of doubles.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
DoubleVect
OnBitProjSimilarity(const T1& bv1,
                     const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  DoubleVect res(2,0.0);
  double num=NumOnBitsInCommon(bv1,bv2);
  if(num){
    res[0] = num/bv1.getNumOnBits();
    res[1] = num/bv2.getNumOnBits();
  }
  return res;
}

// """ -------------------------------------------------------
//
//  OffBitProjSimilarity(T1,T2)
//  Returns the projected similarity between the off bits of
//  T1 and T2.
//
//  The off bit projected similarity of T1 onto T2 is the
//  percentage of T1's off bits which are off in T2.
//
//  This type of measure may be useful for substructure-type
//  searches.
//
//  Two values are returned, the projection of T1 onto T2
//  and the projection of T2 onto T1
//
//  T1 and T2 should be the same length.
//
//  C++ Notes: T1 and T2 must support operator|, getNumBits()
//  and getNumOffBits(), the return value is an DoubleVect with
//  two elements.
//
//  Python Notes: T1 and T2 are BitVects, the return value
//  is a 2-tuple of doubles.
//
// """ -------------------------------------------------------
template <typename T1, typename T2>
DoubleVect
OffBitProjSimilarity(const T1& bv1,
                     const T2& bv2)
{
  if(bv1.getNumBits()!=bv2.getNumBits())
    throw ValueErrorException("BitVects must be same length");
  DoubleVect res(2,0.0);
  double num=(bv1|bv2).getNumOffBits();
  if(num){
    res[0] = num/bv1.getNumOffBits();
    res[1] = num/bv2.getNumOffBits();
  }
  return res;
}




template <typename T1>
T1 *
FoldFingerprint(const T1 &bv1,unsigned int factor)
{
  if(factor <=0 || factor >= bv1.getNumBits())
    throw ValueErrorException("invalid fold factor");

  int initSize = bv1.getNumBits();
  int resSize = initSize/factor;
  T1 *res = new T1(resSize);

  IntVect onBits;
  bv1.getOnBits(onBits);
  for(IntVectIter iv=onBits.begin();iv!=onBits.end();iv++){
    int pos = (*iv) % resSize;
    res->setBit(pos);
  }
  return res;
}

template <typename T1>
std::string
BitVectToText(const T1& bv1){
  std::string res(bv1.getNumBits(),'0');
  for(unsigned int i=0;i<bv1.getNumBits();i++){
    if(bv1.getBit(i)) res[i] = '1';
  }
  return res;
}

const char bin2Hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
template <typename T1>
std::string
BitVectToFPSText(const T1& bv1){
  unsigned int size=2*(bv1.getNumBits()/8 + (bv1.getNumBits()%8?1:0));
  std::string res(size,0);
  unsigned char c=0;
  unsigned int byte=0;
  for(unsigned int i=0;i<bv1.getNumBits();i++){
    if(bv1.getBit(i)) {
      c |= 1<<(i%8);
    }
    if(!((i+1)%8)){
      res[byte++]=bin2Hex[(c>>4)%16];
      res[byte++]=bin2Hex[c%16];
      c=0;
    }
  }
  if(byte<size){
    res[byte++]=bin2Hex[(c>>4)%16];
    res[byte++]=bin2Hex[c%16];
  }
  return res;
}

template <typename T1>
std::string
BitVectToBinaryText(const T1& bv1){
  std::string res(bv1.getNumBits()/8 + (bv1.getNumBits()%8?1:0),0);
  unsigned char c=0;
  unsigned int byte=0;
  for(unsigned int i=0;i<bv1.getNumBits();i++){
    if(bv1.getBit(i)) {
      c |= 1<<(i%8);
    }
    if(!((i+1)%8)){
      res[byte++]=c;
      c=0;
    }
  }
  if(bv1.getNumBits()%8){
    res[byte]=c;
  }
  return res;
}


template <typename T1>
void
UpdateBitVectFromFPSText(T1& bv1,const std::string &fps){
  PRECONDITION(fps.length()*4>=bv1.getNumBits(),"bad FPS length");
  PRECONDITION(fps.length()%2==0,"bad FPS length");
  unsigned int bitIdx=0;
  for(unsigned int i=0;
      i<fps.size() && bitIdx<bv1.getNumBits();
      i+=2){
    unsigned short c=0;
    try {
      std::istringstream in(fps.substr(i,2));
      in >> std::hex >> c;
    } catch (...) {
      std::ostringstream errout;
      errout << "Cannot convert FPS word: " << fps.substr(i,2) << " to int";
      std::cerr<<errout.str()<<std::endl;
      throw ValueErrorException(errout.str()) ;
    }
    for(unsigned int bit=0;
        bit<8 && bitIdx<bv1.getNumBits();
        ++bit, ++bitIdx){
      if(c&(1<<bit)) bv1.setBit(bitIdx);
    }
  }
}

template <typename T1>
void
UpdateBitVectFromBinaryText(T1& bv1,const std::string &fps){
  PRECONDITION(fps.length()*8>=bv1.getNumBits(),"bad FPS length");
  unsigned int bitIdx=0;
  for(unsigned int i=0;i<fps.size()  && bitIdx<bv1.getNumBits();i++){
    unsigned short c=fps[i];
    for(unsigned int bit=0;
        bit<8 && bitIdx<bv1.getNumBits();
        ++bit, ++bitIdx){
      if(c&(1<<bit)) bv1.setBit(bitIdx);
    }
  }
}


template double TanimotoSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double TverskySimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2,double a, double b);
template double CosineSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double KulczynskiSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double DiceSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double SokalSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double McConnaugheySimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double AsymmetricSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double BraunBlanquetSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double RusselSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double OnBitSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template int NumBitsInCommon(const SparseBitVect& bv1,const SparseBitVect& bv2);
template double AllBitSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template int NumOnBitsInCommon(const SparseBitVect& bv1,const SparseBitVect& bv2);
template IntVect OnBitsInCommon(const SparseBitVect& bv1,const SparseBitVect& bv2);
template IntVect OffBitsInCommon(const SparseBitVect& bv1,const SparseBitVect& bv2);
template DoubleVect OnBitProjSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);
template DoubleVect OffBitProjSimilarity(const SparseBitVect& bv1,const SparseBitVect& bv2);

template double TanimotoSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double TverskySimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2,double a, double b);
template double CosineSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double KulczynskiSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double DiceSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double SokalSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double McConnaugheySimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double AsymmetricSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double BraunBlanquetSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double RusselSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double OnBitSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template int NumBitsInCommon(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template double AllBitSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template IntVect OnBitsInCommon(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template IntVect OffBitsInCommon(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template DoubleVect OnBitProjSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);
template DoubleVect OffBitProjSimilarity(const ExplicitBitVect& bv1,const ExplicitBitVect& bv2);

template SparseBitVect *FoldFingerprint(const SparseBitVect &,unsigned int);
template ExplicitBitVect *FoldFingerprint(const ExplicitBitVect &,unsigned int);

template std::string BitVectToText(const SparseBitVect &);
template std::string BitVectToText(const ExplicitBitVect &);

template std::string BitVectToFPSText(const SparseBitVect &);
template std::string BitVectToFPSText(const ExplicitBitVect &);
template void UpdateBitVectFromFPSText(SparseBitVect &,const std::string &);
template void UpdateBitVectFromFPSText(ExplicitBitVect &,const std::string &);

template std::string BitVectToBinaryText(const SparseBitVect &);
template std::string BitVectToBinaryText(const ExplicitBitVect &);
template void UpdateBitVectFromBinaryText(SparseBitVect &,const std::string &);
template void UpdateBitVectFromBinaryText(ExplicitBitVect &,const std::string &);