File: wrap_FPB.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (204 lines) | stat: -rw-r--r-- 8,988 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
//
//  Copyright (C) 2016 greg Landrum
//
//  @@ 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/Wrap.h>
#include <DataStructs/FPBReader.h>
#include <DataStructs/MultiFPBReader.h>
#include <RDBoost/PySequenceHolder.h>
#include "wrap_helpers.h"

namespace python = boost::python;
using namespace RDKit;

namespace {
python::tuple taniNbrHelper(const FPBReader *self, const std::string &bytes,
                            double threshold) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  std::vector<std::pair<double, unsigned int>> nbrs =
      self->getTanimotoNeighbors(bv, threshold);
  python::list result;
  for (auto &nbr : nbrs) {
    result.append(python::make_tuple(nbr.first, nbr.second));
  }
  return python::tuple(result);
}
python::tuple tverskyNbrHelper(const FPBReader *self, const std::string &bytes,
                               double ca, double cb, double threshold) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  std::vector<std::pair<double, unsigned int>> nbrs =
      self->getTverskyNeighbors(bv, ca, cb, threshold);
  python::list result;
  for (auto &nbr : nbrs) {
    result.append(python::make_tuple(nbr.first, nbr.second));
  }
  return python::tuple(result);
}
python::tuple containingNbrHelper(const FPBReader *self,
                                  const std::string &bytes) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  std::vector<unsigned int> nbrs = self->getContainingNeighbors(bv);
  python::list result;
  for (auto &nbr : nbrs) {
    result.append(nbr);
  }
  return python::tuple(result);
}

python::tuple multiTaniNbrHelper(const MultiFPBReader *self,
                                 const std::string &bytes, double threshold,
                                 unsigned int numThreads) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  std::vector<MultiFPBReader::ResultTuple> nbrs =
      self->getTanimotoNeighbors(bv, threshold, numThreads);
  python::list result;
  for (auto &nbr : nbrs) {
    result.append(python::make_tuple(nbr.get<0>(), nbr.get<1>(), nbr.get<2>()));
  }
  return python::tuple(result);
}
python::tuple multiTverskyNbrHelper(const MultiFPBReader *self,
                                    const std::string &bytes, double ca,
                                    double cb, double threshold,
                                    unsigned int numThreads) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  std::vector<MultiFPBReader::ResultTuple> nbrs =
      self->getTverskyNeighbors(bv, ca, cb, threshold, numThreads);
  python::list result;
  for (auto &nbr : nbrs) {
    result.append(python::make_tuple(nbr.get<0>(), nbr.get<1>(), nbr.get<2>()));
  }
  return python::tuple(result);
}
python::tuple multiContainingNbrHelper(const MultiFPBReader *self,
                                       const std::string &bytes,
                                       unsigned int numThreads) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  std::vector<std::pair<unsigned int, unsigned int>> nbrs =
      self->getContainingNeighbors(bv, numThreads);
  python::list result;
  for (auto &nbr : nbrs) {
    result.append(python::make_tuple(nbr.first, nbr.second));
  }
  return python::tuple(result);
}

python::object getBytesHelper(const FPBReader *self, unsigned int which) {
  boost::shared_array<boost::uint8_t> bv = self->getBytes(which);
  python::object retval =
      python::object(python::handle<>(PyBytes_FromStringAndSize(
          reinterpret_cast<const char *>(bv.get()), self->nBits() / 8)));
  return retval;
}

double getTaniHelper(const FPBReader *self, unsigned int which,
                     const std::string &bytes) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  return self->getTanimoto(which, bv);
}
python::tuple getItemHelper(const FPBReader *self, unsigned int which) {
  std::pair<boost::shared_ptr<ExplicitBitVect>, std::string> v = (*self)[which];
  return python::make_tuple(v.first, v.second);
}
double getTverskyHelper(const FPBReader *self, unsigned int which,
                        const std::string &bytes, double ca, double cb) {
  const boost::uint8_t *bv =
      reinterpret_cast<const boost::uint8_t *>(bytes.c_str());
  return self->getTversky(which, bv, ca, cb);
}
}

struct FPB_wrapper {
  static void wrap() {
    std::string FPBReaderClassDoc =
        "A class for reading and searching FPB files from Andrew Dalke's chemfp.\n\
    Note that this functionality is still experimental and the API may\n\
    change in future releases.\n";
    python::class_<FPBReader, boost::noncopyable>(
        "FPBReader", FPBReaderClassDoc.c_str(),
        python::init<std::string, python::optional<bool>>(
            (python::arg("filename"), python::arg("lazy") = false),
            "docstring"))
        .def("Init", &FPBReader::init,
             "Read the fingerprints from the file. This can take a while.\n")
        .def("__len__", &FPBReader::length)
        .def("__getitem__", &getItemHelper)
        .def("GetNumBits", &FPBReader::nBits,
             "returns the number of bits in a fingerprint")
        .def("GetFP", &FPBReader::getFP,
             "returns a particular fingerprint as an ExplicitBitVect")
        .def("GetBytes", &getBytesHelper,
             "returns a particular fingerprint as bytes")
        .def("GetId", &FPBReader::getId,
             "returns the id of a particular fingerprint")
        .def("GetTanimoto", &getTaniHelper,
             "return the tanimoto similarity of a particular fingerprint to "
             "the bytes provided")
        .def("GetTanimotoNeighbors", &taniNbrHelper,
             (python::arg("bv"), python::arg("threshold") = 0.7),
             "returns tanimoto similarities to and indices of all neighbors "
             "above the specified threshold")
        .def("GetTversky", &getTverskyHelper,
             "return the Tverksy similarity of a particular fingerprint to "
             "the bytes provided")
        .def("GetTverskyNeighbors", &tverskyNbrHelper,
             (python::arg("bv"), python::arg("ca"), python::arg("cb"),
              python::arg("threshold") = 0.7),
             "returns Tversky similarities to and indices of all neighbors "
             "above the specified threshold")
        .def(
            "GetContainingNeighbors", &containingNbrHelper, (python::arg("bv")),
            "returns indices of neighbors that contain this fingerprint (where "
            "all bits from this fingerprint are also set)");

    std::string MultiFPBReaderClassDoc =
        "A class for reading and searching multiple FPB files from Andrew Dalke's chemfp.\n\
    Note that this functionality is still experimental and the API may\n\
    change in future releases.\n";

    python::class_<MultiFPBReader, boost::noncopyable>(
        "MultiFPBReader", MultiFPBReaderClassDoc.c_str(),
        python::init<python::optional<bool>>(
            (python::arg("initOnSearch") = false), "docstring"))
        .def("Init", &MultiFPBReader::init,
             "Call Init() on each of our children. This can take a while.\n")
        .def("__len__", &MultiFPBReader::length)
        .def("GetNumBits", &MultiFPBReader::nBits,
             "returns the number of bits in a fingerprint")
        .def("AddReader", &MultiFPBReader::addReader,
             python::with_custodian_and_ward<1, 2>(),
             "adds an FPBReader to our set of readers")
        .def("GetReader", &MultiFPBReader::getReader,
             python::return_value_policy<python::reference_existing_object>(),
             "returns one of our readers")
        .def("GetTanimotoNeighbors", &multiTaniNbrHelper,
             (python::arg("bv"), python::arg("threshold") = 0.7,
              python::arg("numThreads") = 1),
             "returns tanimoto similarities to and indices of all neighbors "
             "above the specified threshold")
        .def("GetTverskyNeighbors", &multiTverskyNbrHelper,
             (python::arg("bv"), python::arg("ca"), python::arg("cb"),
              python::arg("threshold") = 0.7, python::arg("numThreads") = 1),
             "returns Tversky similarities to and indices of all neighbors "
             "above the specified threshold")
        .def(
            "GetContainingNeighbors", &multiContainingNbrHelper,
            (python::arg("bv"), python::arg("numThreads") = 1),
            "returns indices of neighbors that contain this fingerprint (where "
            "all bits from this fingerprint are also set)");
  }
};

void wrap_FPB() { FPB_wrapper::wrap(); }