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
|
/* MaxMinPicker.cpp */
#include <cstring>
#include <cstdio>
#include <sys/time.h>
#include <vector>
#include <string>
#include <GraphMol/GraphMol.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/Fingerprints/Fingerprints.h>
#include <GraphMol/Fingerprints/MorganFingerprints.h>
#include <DataStructs/BitOps.h>
#if 0
#include <SimDivPickers/MaxMinPicker.h>
#include <SimDivPickers/LeaderPicker.h>
#else
#include "MaxMinPicker.h"
#include "LeaderPicker.h"
#endif
std::vector<RDKit::ROMol *> mols;
std::vector<ExplicitBitVect *> fps;
// ExplicitBitVect **fps;
const char *poolname;
const char *pickname;
unsigned int origsize;
unsigned int picksize;
unsigned int poolsize;
unsigned int newpicks;
int seed;
unsigned long ticks = 0;
static unsigned int LoadDatabase(FILE *fp) {
char buffer[32768];
unsigned int result = 0;
while (fgets(buffer, 327666, fp)) {
if (buffer[0] == '#' || buffer[0] == ' ' || buffer[0] == '\t') continue;
char *ptr = buffer;
while (*ptr && *ptr != ' ' && *ptr != '\t') ptr++;
if (*ptr) {
*ptr++ = '\0';
char *end = ptr;
while (*end && *end != '\n' && *end != '\r') end++;
*end = '\0';
}
RDKit::RWMol *mol;
try {
mol = RDKit::SmilesToMol(buffer);
} catch (...) {
mol = (RDKit::RWMol *)0;
}
if (mol) {
#if 0
if (*ptr) {
static std::string key("_Name");
mol->setProp(key,std::string(ptr));
}
mols.push_back(mol);
#endif
ExplicitBitVect *fp;
fp = RDKit::MorganFingerprints::getFingerprintAsBitVect(*mol, 2, 2048);
fps.push_back(fp);
delete mol;
result++;
}
}
fprintf(stderr, "%u molecules\n", result);
return result;
}
static unsigned int LoadDatabase(const char *fname) {
if (fname && strcmp(fname, "-")) {
FILE *fp = fopen(fname, "rb");
if (!fp) return 0;
unsigned int result = LoadDatabase(fp);
fclose(fp);
return result;
} else
return LoadDatabase(stdin);
}
#ifdef UNUSED
static void GenerateFingerprints() {
unsigned int count = (unsigned int)mols.size();
unsigned int size = (unsigned int)(count * sizeof(void *));
fps = (ExplicitBitVect **)malloc(size);
for (unsigned int i = 0; i < count; i++)
fps[i] =
RDKit::MorganFingerprints::getFingerprintAsBitVect(*mols[i], 2, 2048);
fprintf(stderr, "%u Fingerprints\n", count);
}
static void DestroyFingerprints() {
unsigned int count = (unsigned int)mols.size();
for (unsigned int i = 0; i < count; i++) delete fps[i];
free(fps);
}
#endif
static void DisplayUsage() {
fputs("usage: MaxMinPicker [options] <poolfile> [<pickfile>]\n", stderr);
exit(1);
}
static void ProcessCommandLine(int argc, char *argv[]) {
poolname = (const char *)0;
pickname = (const char *)0;
newpicks = 1000;
for (int i = 1; i < argc; i++) {
const char *ptr = argv[i];
if (ptr[0] == '-' && ptr[1]) {
if (ptr[1] >= '0' && ptr[1] <= '9') {
newpicks = atoi(ptr + 1);
} else
DisplayUsage();
} else if (!poolname) {
poolname = ptr;
} else if (!pickname) {
pickname = ptr;
} else
DisplayUsage();
}
if (!poolname) DisplayUsage();
}
double MyDist(int i, int j) {
ticks++;
return 1.0 - TanimotoSimilarity(*fps[i], *fps[j]);
}
int main(int argc, char *argv[]) {
struct timeval beg, end;
double elapsed;
ProcessCommandLine(argc, argv);
gettimeofday(&beg, (struct timezone *)0);
origsize = LoadDatabase(poolname);
gettimeofday(&end, (struct timezone *)0);
elapsed = (end.tv_sec + 0.000001 * end.tv_usec) -
(beg.tv_sec + 0.000001 * beg.tv_usec);
fprintf(stderr, "Elapsed time: %g secs\n", elapsed);
if (pickname) {
gettimeofday(&beg, (struct timezone *)0);
picksize = LoadDatabase(pickname);
gettimeofday(&end, (struct timezone *)0);
elapsed = (end.tv_sec + 0.000001 * end.tv_usec) -
(beg.tv_sec + 0.000001 * beg.tv_usec);
fprintf(stderr, "Elapsed time: %g secs\n", elapsed);
} else
picksize = 0;
poolsize = origsize + picksize;
if (newpicks == 0) newpicks = origsize;
#ifdef UNUSED
gettimeofday(&beg, (struct timezone *)0);
GenerateFingerprints();
gettimeofday(&end, (struct timezone *)0);
elapsed = (end.tv_sec + 0.000001 * end.tv_usec) -
(beg.tv_sec + 0.000001 * beg.tv_usec);
fprintf(stderr, "Elapsed time: %g secs\n", elapsed);
#endif
RDKit::INT_VECT firstPicks;
if (picksize) {
firstPicks.reserve(picksize);
for (unsigned int i = 0; i < picksize; i++)
firstPicks.push_back((int)(i + origsize));
}
// RDPickers::MaxMinPicker mmpicker;
double threshold = 0.9; // 0.675;
RDPickers::LeaderPicker ldpicker(threshold, 4);
gettimeofday(&beg, (struct timezone *)0);
#if 0
RDKit::INT_VECT iv = mmpicker.lazyPick(MyDist,poolsize,picksize+newpicks,
firstPicks,seed,threshold);
#else
RDKit::INT_VECT iv = ldpicker.lazyPick(MyDist, poolsize, picksize + newpicks,
firstPicks, threshold, 16);
#endif
gettimeofday(&end, (struct timezone *)0);
elapsed = (end.tv_sec + 0.000001 * end.tv_usec) -
(beg.tv_sec + 0.000001 * beg.tv_usec);
fprintf(stderr, "Elapsed time: %g secs\n", elapsed);
unsigned int count = (unsigned int)iv.size();
#if 0
for (unsigned int j=0; j<count; j++)
printf("%d\n",iv[j]);
#endif
for (unsigned int i = 0; i < std::min(iv.size(), (size_t)10); ++i) {
for (unsigned int j = 0; j < i; ++j) {
std::cerr << iv[i] << " " << iv[j] << ": " << MyDist(iv[i], iv[j])
<< std::endl;
}
}
fprintf(stderr, "%u picks\n", count);
fprintf(stderr, "%g threshold\n", threshold);
fprintf(stderr, "%lu comparisons\n", ticks);
// DestroyFingerprints();
return 0;
}
|