-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.php
30 lines (23 loc) · 1.02 KB
/
example.php
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
function generateFingerprint($molecule)
{
return RDKit\Molecule::fromSmiles($molecule)->morganFingerprint();
}
$db = pg_connect('postgres://localhost/pgvector_example');
pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');
pg_query($db, 'DROP TABLE IF EXISTS molecules');
pg_query($db, 'CREATE TABLE molecules (id text PRIMARY KEY, fingerprint bit(2048))');
$molecules = ['Cc1ccccc1', 'Cc1ncccc1', 'c1ccccn1'];
foreach ($molecules as $molecule) {
$fingerprint = generateFingerprint($molecule);
pg_query_params($db, 'INSERT INTO molecules (id, fingerprint) VALUES ($1, $2)', [$molecule, $fingerprint]);
}
$queryMolecule = 'c1ccco1';
$queryFingerprint = generateFingerprint($queryMolecule);
$result = pg_query_params($db, 'SELECT id, fingerprint <%> $1 AS distance FROM molecules ORDER BY distance LIMIT 5', [$queryFingerprint]);
while ($row = pg_fetch_array($result)) {
echo $row['id'] . ': ' . $row['distance'] . "\n";
}
pg_free_result($result);
pg_close($db);