Skip to content

Commit 1562765

Browse files
committed
Added Morgan fingerprint example [skip ci]
1 parent 90abf7f commit 1562765

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Or check out some examples:
2525

2626
- [Embeddings](examples/openai_embeddings.rb) with OpenAI
2727
- [Binary embeddings](examples/cohere_embeddings.rb) with Cohere
28+
- [Morgan fingerprints](examples/morgan_fingerprints.rb) with RDKit.rb
2829
- [Topic modeling](examples/topic_modeling.rb) with tomoto.rb
2930
- [User-based recommendations](examples/disco_user_recs.rb) with Disco
3031
- [Item-based recommendations](examples/disco_item_recs.rb) with Disco

examples/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ gemspec path: ".."
55
gem "disco"
66
gem "numo-narray"
77
gem "pg"
8+
gem "rdkit-rb"
89
gem "sequel"
910
gem "tomoto"

examples/morgan_fingerprints.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "pg"
2+
require "rdkit-rb"
3+
4+
def generate_fingerprint(molecule)
5+
RDKit::Molecule.from_smiles(molecule).morgan_fingerprint
6+
end
7+
8+
conn = PG.connect(dbname: "pgvector_example")
9+
conn.exec("CREATE EXTENSION IF NOT EXISTS vector")
10+
11+
conn.exec("DROP TABLE IF EXISTS molecules")
12+
conn.exec("CREATE TABLE molecules (id text PRIMARY KEY, fingerprint bit(2048))")
13+
14+
molecules = ["Cc1ccccc1", "Cc1ncccc1", "c1ccccn1"]
15+
molecules.each do |molecule|
16+
fingerprint = generate_fingerprint(molecule)
17+
conn.exec_params("INSERT INTO molecules (id, fingerprint) VALUES ($1, $2)", [molecule, fingerprint])
18+
end
19+
20+
query_molecule = "c1ccco1"
21+
query_fingerprint = generate_fingerprint(query_molecule)
22+
result = conn.exec_params("SELECT id, fingerprint <%> $1 AS distance FROM molecules ORDER BY distance LIMIT 5", [query_fingerprint])
23+
result.each do |row|
24+
p row
25+
end

0 commit comments

Comments
 (0)