From 92d8f9937e7048bf7da3dbdae0f373fb38548229 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 22:37:49 -0400 Subject: [PATCH 01/14] Upgrade C++ standard from C++17 to C++20 RDKit 2025.09.x uses constexpr virtual destructors and override functions in its headers (e.g. Geometry/point.h), which require C++20. Building with -std=c++17 causes compilation failures. Co-Authored-By: Claude Opus 4.6 (1M context) --- rdkit-sys/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdkit-sys/build.rs b/rdkit-sys/build.rs index 782861d..d5ba17e 100644 --- a/rdkit-sys/build.rs +++ b/rdkit-sys/build.rs @@ -1,4 +1,4 @@ -const CPP_VERSION_FLAG: &str = "-std=c++17"; +const CPP_VERSION_FLAG: &str = "-std=c++20"; fn main() { if std::env::var("DOCS_RS").is_ok() { From e5a0ea96dd2f440be4c39a64319956d1bc4052e2 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 22:37:53 -0400 Subject: [PATCH 02/14] Add explicit lifetime annotation to atom_with_idx return type Fixes mismatched_lifetime_syntaxes warning by making the elided lifetime on Atom<'_> explicit, matching the &mut self borrow. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/graphmol/ro_mol.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphmol/ro_mol.rs b/src/graphmol/ro_mol.rs index bcdd30c..8b13489 100644 --- a/src/graphmol/ro_mol.rs +++ b/src/graphmol/ro_mol.rs @@ -89,7 +89,7 @@ impl ROMol { ro_mol_ffi::get_num_atoms(&self.ptr, only_explicit) } - pub fn atom_with_idx(&mut self, idx: u32) -> Atom { + pub fn atom_with_idx(&mut self, idx: u32) -> Atom<'_> { let ptr = ro_mol_ffi::get_atom_with_idx(&mut self.ptr, idx); Atom::from_ptr(ptr) } From 6a2d625aa0222c93276e7b288d68537a03700554 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 22:38:08 -0400 Subject: [PATCH 03/14] Fix clippy warnings: unused lifetime and len_zero Remove unused lifetime parameter 'a from PeriodicTableOps impl, and replace names.len() != 0 with !names.is_empty(). Co-Authored-By: Claude Opus 4.6 (1M context) --- rdkit-sys/src/bridge/periodic_table.rs | 2 +- src/descriptors.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rdkit-sys/src/bridge/periodic_table.rs b/rdkit-sys/src/bridge/periodic_table.rs index b08b1fb..21e0dfa 100644 --- a/rdkit-sys/src/bridge/periodic_table.rs +++ b/rdkit-sys/src/bridge/periodic_table.rs @@ -43,7 +43,7 @@ pub trait PeriodicTableOps { fn getElementName(self, atomic_number: u32) -> String; fn getValenceList(self, atomic_number: u32) -> &'static CxxVector; } -impl<'a> PeriodicTableOps for UniquePtr { +impl PeriodicTableOps for UniquePtr { fn getElementSymbol(self, atomic_number: u32) -> String { ffi::getElementSymbol(atomic_number) } diff --git a/src/descriptors.rs b/src/descriptors.rs index a3272c9..a6511d6 100644 --- a/src/descriptors.rs +++ b/src/descriptors.rs @@ -25,7 +25,7 @@ impl Properties { let names = rdkit_sys::descriptors_ffi::get_property_names(&self.ptr); let computed = rdkit_sys::descriptors_ffi::compute_properties(&self.ptr, &ro_mol.ptr); - assert!(names.len() != 0); + assert!(!names.is_empty()); assert!(computed.len() == names.len()); names From 4d6f31321fe2fec7f84a811d7600db11f9fb0eac Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 22:38:14 -0400 Subject: [PATCH 04/14] Replace deprecated rustfmt version option with style_edition The 'version' key is deprecated in current rustfmt. Replace with 'style_edition = "2021"' in both workspace and rdkit-sys configs. Co-Authored-By: Claude Opus 4.6 (1M context) --- rdkit-sys/rustfmt.toml | 2 +- rustfmt.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rdkit-sys/rustfmt.toml b/rdkit-sys/rustfmt.toml index 7de3b73..6158963 100644 --- a/rdkit-sys/rustfmt.toml +++ b/rdkit-sys/rustfmt.toml @@ -49,7 +49,7 @@ match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 edition = "2021" -version = "One" +style_edition = "2021" inline_attribute_width = 0 format_generated_files = true merge_derives = true diff --git a/rustfmt.toml b/rustfmt.toml index 7de3b73..6158963 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -49,7 +49,7 @@ match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 edition = "2021" -version = "One" +style_edition = "2021" inline_attribute_width = 0 format_generated_files = true merge_derives = true From d399a7427bf8b119941353bf0a3b34e19bfc7038 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 23:12:41 -0400 Subject: [PATCH 05/14] Fix tail-expression drop order for Rust 2024 compatibility Bind get_periodic_table() to a local variable instead of using it as a temporary in tail expressions. In Rust 2024, temporaries in tail expressions are dropped before locals, which would drop the PeriodicTable UniquePtr before the CxxString it borrows. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/periodic_table.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/periodic_table.rs b/src/periodic_table.rs index 60fc473..cf66658 100644 --- a/src/periodic_table.rs +++ b/src/periodic_table.rs @@ -1,4 +1,4 @@ -use cxx::{let_cxx_string, CxxVector}; +use cxx::{CxxVector, let_cxx_string}; use rdkit_sys::PeriodicTableOps; pub struct PeriodicTable {} @@ -18,8 +18,8 @@ impl PeriodicTable { /// * `atom` - The symbol of the element pub fn get_most_common_isotope_mass(atom: &str) -> f64 { let_cxx_string!(atom_cxx_string = atom); - rdkit_sys::periodic_table_ffi::get_periodic_table() - .getMostCommonIsotopeMass(&atom_cxx_string) + let pt = rdkit_sys::periodic_table_ffi::get_periodic_table(); + pt.getMostCommonIsotopeMass(&atom_cxx_string) } /// Returns the atomic weight of the atom @@ -32,7 +32,8 @@ impl PeriodicTable { /// * `atom` - The symbol of the element pub fn get_atomic_number(atom: &str) -> i32 { let_cxx_string!(atom_cxx_string = atom); - rdkit_sys::periodic_table_ffi::get_periodic_table().getAtomicNumber(&atom_cxx_string) + let pt = rdkit_sys::periodic_table_ffi::get_periodic_table(); + pt.getAtomicNumber(&atom_cxx_string) } /// Returns the symbol of the element From fd0639e0122aa66e9bbe5de1226bfffbc1b410d9 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 23:12:47 -0400 Subject: [PATCH 06/14] Upgrade to Rust edition 2024 Update edition to 2024 in both Cargo.toml files and rustfmt configs. Apply 2024 style formatting: types before functions/macros in imports, and long assert_eq! macro calls wrapped across multiple lines. Co-Authored-By: Claude Opus 4.6 (1M context) --- Cargo.toml | 2 +- rdkit-sys/Cargo.toml | 2 +- rdkit-sys/rustfmt.toml | 4 ++-- rdkit-sys/src/bridge/mod.rs | 2 +- rdkit-sys/tests/test_atoms.rs | 4 +++- rdkit-sys/tests/test_ro_mol.rs | 5 ++++- rdkit-sys/tests/test_rw_mol.rs | 7 +++++-- rustfmt.toml | 4 ++-- src/graphmol/rw_mol.rs | 2 +- tests/test_graphmol.rs | 26 +++++++++++++++++++------- tests/test_mol_ops.rs | 2 +- tests/test_substruct.rs | 2 +- 12 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1086859..295dbe2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rdkit" version = "0.4.12" -edition = "2021" +edition = "2024" authors = ["Xavier Lange ", "Javier Pineda , SharedPtr>(rw_mol) }; let smiles = rdkit_sys::ro_mol_ffi::mol_to_smiles(&ro_mol); - assert_eq!("[H]C([H])([H])C(=O)OC([H])(C([H])([H])C(=O)[O-])C([H])([H])[N+](C([H])([H])[H])(C([H])([H])[H])C([H])([H])[H]", &smiles); + assert_eq!( + "[H]C([H])([H])C(=O)OC([H])(C([H])([H])C(=O)[O-])C([H])([H])[N+](C([H])([H])[H])(C([H])([H])[H])C([H])([H])[H]", + &smiles + ); } #[test] diff --git a/rustfmt.toml b/rustfmt.toml index 6158963..6edb4d8 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -48,8 +48,8 @@ trailing_comma = "Vertical" match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 -edition = "2021" -style_edition = "2021" +edition = "2024" +style_edition = "2024" inline_attribute_width = 0 format_generated_files = true merge_derives = true diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index 6467120..b640d50 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -1,6 +1,6 @@ use std::fmt::Formatter; -use cxx::{let_cxx_string, SharedPtr}; +use cxx::{SharedPtr, let_cxx_string}; use rdkit_sys::*; use crate::ROMol; diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index 64587f1..e167672 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -1,7 +1,7 @@ use rdkit::{ - detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, - MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, - TautomerEnumerator, Uncharger, + CleanupParameters, MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, + SubstructMatchParameters, TautomerEnumerator, Uncharger, detect_chemistry_problems, + fragment_parent, substruct_match, }; #[test] @@ -15,7 +15,10 @@ fn test_neutralize() { let romol = ROMol::from_smiles(smiles).unwrap(); let uncharger = Uncharger::new(false); let uncharged_mol = uncharger.uncharge(&romol); - assert_eq!("CCOC(=O)C(C)(C)Oc1ccc(Cl)cc1.CO.Nc1nc2ncc(CNc3ccc(C(=O)N[C@@H](CCC(=O)O)C(=O)O)cc3)nc2c(=O)[nH]1", uncharged_mol.as_smiles()); + assert_eq!( + "CCOC(=O)C(C)(C)Oc1ccc(Cl)cc1.CO.Nc1nc2ncc(CNc3ccc(C(=O)N[C@@H](CCC(=O)O)C(=O)O)cc3)nc2c(=O)[nH]1", + uncharged_mol.as_smiles() + ); } #[test] @@ -29,7 +32,10 @@ fn test_fragment_parent() { "Nc1nc2ncc(CNc3ccc(C(=O)N[C@@H](CCC(=O)O)C(=O)O)cc3)nc2c(=O)[nH]1", parent_rwmol.as_smiles() ); - assert_eq!("CCOC(=O)C(C)(C)Oc1ccc(Cl)cc1.CO.Nc1nc2ncc(CNc3ccc(C(=O)N[C@@H](CCC(=O)O)C(=O)O)cc3)nc2c(=O)[nH]1", rwmol.as_smiles()); + assert_eq!( + "CCOC(=O)C(C)(C)Oc1ccc(Cl)cc1.CO.Nc1nc2ncc(CNc3ccc(C(=O)N[C@@H](CCC(=O)O)C(=O)O)cc3)nc2c(=O)[nH]1", + rwmol.as_smiles() + ); } #[test] @@ -271,7 +277,10 @@ CC(=O)OC(CC(=O)[O-])C[N+](C)(C)C "#; let rw_mol = RWMol::from_mol_block(mol_block, false, false, false).unwrap(); - assert_eq!("[H]C([H])([H])C(=O)OC([H])(C([H])([H])C(=O)[O-])C([H])([H])[N+](C([H])([H])[H])(C([H])([H])[H])C([H])([H])[H]", &rw_mol.as_smiles()); + assert_eq!( + "[H]C([H])([H])C(=O)OC([H])(C([H])([H])C(=O)[O-])C([H])([H])[N+](C([H])([H])[H])(C([H])([H])[H])C([H])([H])[H]", + &rw_mol.as_smiles() + ); } #[test] @@ -326,5 +335,8 @@ fn mol_to_molblock_test() { let smiles = "CC"; let romol = ROMol::from_smiles(&smiles).unwrap(); let molblock = romol.to_molblock(); - assert_eq!(molblock, "\n RDKit 2D\n\n 2 1 0 0 0 0 0 0 0 0999 V2000\n 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 1.2990 0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 1 2 1 0\nM END\n"); + assert_eq!( + molblock, + "\n RDKit 2D\n\n 2 1 0 0 0 0 0 0 0 0999 V2000\n 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 1.2990 0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 1 2 1 0\nM END\n" + ); } diff --git a/tests/test_mol_ops.rs b/tests/test_mol_ops.rs index 42e4cb3..56dc92c 100644 --- a/tests/test_mol_ops.rs +++ b/tests/test_mol_ops.rs @@ -1,4 +1,4 @@ -use rdkit::{add_hs, clean_up, remove_hs, set_hybridization, ROMol, RemoveHsParameters}; +use rdkit::{ROMol, RemoveHsParameters, add_hs, clean_up, remove_hs, set_hybridization}; #[test] fn test_remove_hs() { diff --git a/tests/test_substruct.rs b/tests/test_substruct.rs index 851e0bc..dbe2b7f 100644 --- a/tests/test_substruct.rs +++ b/tests/test_substruct.rs @@ -1,4 +1,4 @@ -use rdkit::{substruct_match, ROMol, SubstructMatchItem, SubstructMatchParameters}; +use rdkit::{ROMol, SubstructMatchItem, SubstructMatchParameters, substruct_match}; #[test] fn test_substruct_match() { From adfd9a5a482a2620124d5e469d88d20bc98e84a3 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 23:54:27 -0400 Subject: [PATCH 07/14] Add const atom access FFI path Add get_atom_with_idx_const that takes const shared_ptr& and returns const Atom&, using RDKit's const overload of getAtomWithIdx. This maps to Pin<&Atom> in CXX, enabling &self access on the Rust side. Co-Authored-By: Claude Opus 4.6 (1M context) --- rdkit-sys/src/bridge/ro_mol.rs | 1 + rdkit-sys/wrapper/include/ro_mol.h | 2 ++ rdkit-sys/wrapper/src/ro_mol.cc | 4 ++++ 3 files changed, 7 insertions(+) diff --git a/rdkit-sys/src/bridge/ro_mol.rs b/rdkit-sys/src/bridge/ro_mol.rs index fffa0e7..2e5ac6e 100644 --- a/rdkit-sys/src/bridge/ro_mol.rs +++ b/rdkit-sys/src/bridge/ro_mol.rs @@ -59,6 +59,7 @@ pub mod ffi { pub fn get_num_atoms(mol: &SharedPtr, onlyExplicit: bool) -> u32; pub fn get_atom_with_idx(mol: &mut SharedPtr, idx: u32) -> Pin<&mut Atom>; + pub fn get_atom_with_idx_const(mol: &SharedPtr, idx: u32) -> Pin<&Atom>; pub fn get_symbol(atom: Pin<&Atom>) -> String; pub fn get_is_aromatic(atom: Pin<&Atom>) -> bool; pub fn get_atomic_num(atom: Pin<&Atom>) -> i32; diff --git a/rdkit-sys/wrapper/include/ro_mol.h b/rdkit-sys/wrapper/include/ro_mol.h index 6cbe18d..6fbe6ea 100644 --- a/rdkit-sys/wrapper/include/ro_mol.h +++ b/rdkit-sys/wrapper/include/ro_mol.h @@ -29,6 +29,8 @@ unsigned int atom_sanitize_exception_get_atom_idx(const MolSanitizeExceptionUniq unsigned int get_num_atoms(const std::shared_ptr &mol, bool only_explicit); Atom &get_atom_with_idx(std::shared_ptr &mol, unsigned int idx); +const Atom &get_atom_with_idx_const(const std::shared_ptr &mol, + unsigned int idx); rust::String get_symbol(const Atom &atom); bool get_is_aromatic(const Atom &atom); int get_atomic_num(const Atom &atom); diff --git a/rdkit-sys/wrapper/src/ro_mol.cc b/rdkit-sys/wrapper/src/ro_mol.cc index 2442a0b..d3ee865 100644 --- a/rdkit-sys/wrapper/src/ro_mol.cc +++ b/rdkit-sys/wrapper/src/ro_mol.cc @@ -69,6 +69,10 @@ unsigned int get_num_atoms(const std::shared_ptr &mol, bool only_explicit Atom &get_atom_with_idx(std::shared_ptr &mol, unsigned int idx) { return *mol->getAtomWithIdx(idx); } +const Atom &get_atom_with_idx_const(const std::shared_ptr &mol, unsigned int idx) { + return *mol->getAtomWithIdx(idx); +} + rust::String get_symbol(const Atom &atom) { return atom.getSymbol(); } bool get_is_aromatic(const Atom &atom) { return atom.getIsAromatic(); } From b0a74d43b384e957c778ef823420f749cb84140f Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 23:54:35 -0400 Subject: [PATCH 08/14] Add AtomRef for read-only atom access without &mut self AtomRef<'a> holds Pin<&'a Atom> (const) and exposes all read-only atom methods. ROMol::atom_ref(&self) returns AtomRef, eliminating the need to clone molecules just to read atom properties. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/graphmol/atom_ref.rs | 88 ++++++++++++++++++++++++++++++++++++++++ src/graphmol/mod.rs | 3 ++ src/graphmol/ro_mol.rs | 11 ++++- 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/graphmol/atom_ref.rs diff --git a/src/graphmol/atom_ref.rs b/src/graphmol/atom_ref.rs new file mode 100644 index 0000000..c6422d7 --- /dev/null +++ b/src/graphmol/atom_ref.rs @@ -0,0 +1,88 @@ +use std::{fmt::Formatter, pin::Pin}; + +use rdkit_sys::ro_mol_ffi; +pub use rdkit_sys::ro_mol_ffi::HybridizationType; + +/// Read-only view of an atom within a molecule. +/// +/// Unlike [`Atom`](crate::Atom), this borrows the parent molecule immutably +/// (`&self`), so multiple `AtomRef`s can coexist and no clone is needed. +pub struct AtomRef<'a> { + ptr: Pin<&'a ro_mol_ffi::Atom>, +} + +impl<'a> std::fmt::Display for AtomRef<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let symbol = self.symbol(); + f.write_str(&symbol) + } +} + +impl<'a> std::fmt::Debug for AtomRef<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let symbol = self.symbol(); + f.write_str(&symbol) + } +} + +impl<'a> AtomRef<'a> { + pub fn from_ptr(ptr: Pin<&'a ro_mol_ffi::Atom>) -> Self { + Self { ptr } + } + + pub fn symbol(&self) -> String { + ro_mol_ffi::get_symbol(self.ptr) + } + + pub fn get_is_aromatic(&self) -> bool { + ro_mol_ffi::get_is_aromatic(self.ptr) + } + + pub fn get_atomic_num(&self) -> i32 { + ro_mol_ffi::get_atomic_num(self.ptr) + } + + pub fn get_formal_charge(&self) -> i32 { + ro_mol_ffi::get_formal_charge(self.ptr) + } + + pub fn get_total_num_hs(&self) -> u32 { + ro_mol_ffi::get_total_num_hs(self.ptr) + } + + pub fn get_total_valence(&self) -> u32 { + ro_mol_ffi::get_total_valence(self.ptr) + } + + pub fn get_hybridization_type(&self) -> HybridizationType { + ro_mol_ffi::atom_get_hybridization(self.ptr) + } + + pub fn get_num_radical_electrons(&self) -> u32 { + ro_mol_ffi::get_num_radical_electrons(self.ptr) + } + + pub fn get_degree(&self) -> u32 { + ro_mol_ffi::get_degree(self.ptr) + } + + pub fn get_int_prop(&self, key: &str) -> Result { + cxx::let_cxx_string!(key = key); + ro_mol_ffi::get_int_prop(self.ptr, &key) + } + + pub fn get_float_prop(&self, key: &str) -> Result { + cxx::let_cxx_string!(key = key); + ro_mol_ffi::get_float_prop(self.ptr, &key) + } + + pub fn get_bool_prop(&self, key: &str) -> Result { + cxx::let_cxx_string!(key = key); + ro_mol_ffi::get_bool_prop(self.ptr, &key) + } + + pub fn get_prop(&self, key: &str) -> Result { + cxx::let_cxx_string!(key = key); + ro_mol_ffi::get_prop(self.ptr, &key) + } +} diff --git a/src/graphmol/mod.rs b/src/graphmol/mod.rs index f23a721..1b1309b 100644 --- a/src/graphmol/mod.rs +++ b/src/graphmol/mod.rs @@ -1,6 +1,9 @@ mod atom; pub use atom::*; +mod atom_ref; +pub use atom_ref::*; + mod mol_ops; pub use mol_ops::*; diff --git a/src/graphmol/ro_mol.rs b/src/graphmol/ro_mol.rs index 8b13489..045d2ed 100644 --- a/src/graphmol/ro_mol.rs +++ b/src/graphmol/ro_mol.rs @@ -3,7 +3,7 @@ use std::fmt::{Debug, Formatter}; use cxx::let_cxx_string; use rdkit_sys::*; -use crate::{Atom, Fingerprint, RWMol}; +use crate::{Atom, AtomRef, Fingerprint, RWMol}; pub struct ROMol { pub(crate) ptr: cxx::SharedPtr, @@ -94,6 +94,15 @@ impl ROMol { Atom::from_ptr(ptr) } + /// Returns a read-only reference to the atom at `idx`. + /// + /// Unlike [`atom_with_idx`](Self::atom_with_idx), this takes `&self`, + /// so no mutable borrow (or clone) is needed for read-only access. + pub fn atom_ref(&self, idx: u32) -> AtomRef<'_> { + let ptr = ro_mol_ffi::get_atom_with_idx_const(&self.ptr, idx); + AtomRef::from_ptr(ptr) + } + pub fn update_property_cache(&mut self, strict: bool) { ro_mol_ffi::ro_mol_update_property_cache(&mut self.ptr, strict) } From eed9fd03bdff9e4d26b174200095e8f6a68b32ad Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 23:54:40 -0400 Subject: [PATCH 09/14] Add parity tests for AtomRef vs Atom Verify AtomRef returns identical values for all read-only methods, property getters work, multiple AtomRefs coexist (no &mut needed), and iteration over all atoms works via &self. Co-Authored-By: Claude Opus 4.6 (1M context) --- tests/test_atom_ref.rs | 81 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/test_atom_ref.rs diff --git a/tests/test_atom_ref.rs b/tests/test_atom_ref.rs new file mode 100644 index 0000000..55c80e9 --- /dev/null +++ b/tests/test_atom_ref.rs @@ -0,0 +1,81 @@ +/// Verify AtomRef returns identical results to Atom for all read-only methods. +#[test] +fn test_atom_ref_parity() { + let mut romol = rdkit::ROMol::from_smiles("[NH4+]").unwrap(); + + // Read via mutable Atom + let atom = romol.atom_with_idx(0); + let symbol = atom.symbol(); + let is_aromatic = atom.get_is_aromatic(); + let atomic_num = atom.get_atomic_num(); + let hybridization = atom.get_hybridization_type(); + let formal_charge = atom.get_formal_charge(); + let total_num_hs = atom.get_total_num_hs(); + let total_valence = atom.get_total_valence(); + let num_radical_electrons = atom.get_num_radical_electrons(); + let degree = atom.get_degree(); + + // Read via immutable AtomRef — must match exactly + let atom_ref = romol.atom_ref(0); + assert_eq!(atom_ref.symbol(), symbol); + assert_eq!(atom_ref.get_is_aromatic(), is_aromatic); + assert_eq!(atom_ref.get_atomic_num(), atomic_num); + assert_eq!(atom_ref.get_hybridization_type(), hybridization); + assert_eq!(atom_ref.get_formal_charge(), formal_charge); + assert_eq!(atom_ref.get_total_num_hs(), total_num_hs); + assert_eq!(atom_ref.get_total_valence(), total_valence); + assert_eq!(atom_ref.get_num_radical_electrons(), num_radical_electrons); + assert_eq!(atom_ref.get_degree(), degree); +} + +/// Verify AtomRef property getters work. +#[test] +fn test_atom_ref_properties() { + let mut romol = rdkit::ROMol::from_smiles("CC").unwrap(); + + // Set properties via mutable Atom + { + let mut carbon = romol.atom_with_idx(0); + carbon.set_prop("int_key", 42); + carbon.set_prop("float_key", 3.14); + carbon.set_prop("bool_key", true); + carbon.set_prop("str_key", "hello"); + } + + // Read back via immutable AtomRef + let atom_ref = romol.atom_ref(0); + assert_eq!(atom_ref.get_int_prop("int_key").unwrap(), 42); + assert_eq!(atom_ref.get_float_prop("float_key").unwrap(), 3.14); + assert_eq!(atom_ref.get_bool_prop("bool_key").unwrap(), true); + assert_eq!(atom_ref.get_prop("str_key").unwrap(), "hello"); +} + +/// Verify multiple AtomRefs can coexist (no &mut self needed). +#[test] +fn test_atom_ref_no_clone_needed() { + let romol = rdkit::ROMol::from_smiles("CCO").unwrap(); + + // This would not compile with atom_with_idx since it needs &mut self. + // With atom_ref, we can hold multiple references simultaneously. + let c1 = romol.atom_ref(0); + let c2 = romol.atom_ref(1); + let o = romol.atom_ref(2); + + assert_eq!(c1.symbol(), "C"); + assert_eq!(c2.symbol(), "C"); + assert_eq!(o.symbol(), "O"); +} + +/// Verify AtomRef works across all atoms in a molecule. +#[test] +fn test_atom_ref_iteration() { + let romol = rdkit::ROMol::from_smiles("c1ccccc1").unwrap(); + let n = romol.num_atoms(true); + assert_eq!(n, 6); + + for i in 0..n { + let atom = romol.atom_ref(i); + assert_eq!(atom.symbol(), "C"); + assert!(atom.get_is_aromatic()); + } +} From e3ef2d332e6b8b23235bbea289c5698116f3116c Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Fri, 13 Mar 2026 23:54:47 -0400 Subject: [PATCH 10/14] Add atom iteration benchmarks comparing AtomRef vs Atom MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Benchmarks for the featurization hot path with 7 drug-like molecules: - atom_ref (const, &self): ~7μs for 7 properties across all atoms - atom_with_idx (&mut self): ~7μs same, but requires &mut - clone + featurize (&ROMol callers): ~46μs — 6.5x slower due to clone Shows AtomRef eliminates the clone tax for read-only workflows. Co-Authored-By: Claude Opus 4.6 (1M context) --- benches/atom_iteration_benchmark.rs | 194 ++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 benches/atom_iteration_benchmark.rs diff --git a/benches/atom_iteration_benchmark.rs b/benches/atom_iteration_benchmark.rs new file mode 100644 index 0000000..8c6d4dc --- /dev/null +++ b/benches/atom_iteration_benchmark.rs @@ -0,0 +1,194 @@ +#![allow(soft_unstable)] +#![feature(test)] +extern crate test; + +use rdkit::ROMol; + +/// Drug-like molecules of varying size for realistic benchmarking. +/// These cover common pharmaceutical scaffolds and natural products. +const SMILES_SET: &[&str] = &[ + // aspirin (21 atoms with H) + "CC(=O)Oc1ccccc1C(=O)O", + // ibuprofen + "CC(C)Cc1ccc(cc1)C(C)C(=O)O", + // caffeine + "Cn1c(=O)c2c(ncn2C)n(C)c1=O", + // diazepam + "O=C1CN=C(c2ccccc2)c2cc(Cl)ccc2N1C", + // atorvastatin (lipitor) — large drug molecule + "CC(C)c1c(C(=O)Nc2ccccc2)c(-c2ccccc2)c(-c2ccc(F)cc2)n1CC[C@@H](O)C[C@@H](O)CC(=O)O", + // taxol core — complex natural product + "CC1=C2C(OC(=O)c3ccccc3)C(O)C4(OC(=O)C(O)(CC(OC(=O)c5ccccc5)C1O)C24C)C(=O)c1ccc(OC)cc1", + // vancomycin fragment — large, many atoms + "OC1C(O)C(OC2C(O)C(O)C(O)C(CO)O2)OC(CO)C1NC(=O)C1CC(O)CN1C(=O)C(NC(=O)C1CC(=O)NC(=O)C1O)C(O)c1ccc(O)cc1", +]; + +/// Benchmark: parse SMILES into molecules. +/// Baseline cost — everything else is on top of this. +#[bench] +fn bench_parse_smiles(b: &mut test::bench::Bencher) { + b.iter(|| { + for smiles in SMILES_SET { + test::black_box(ROMol::from_smiles(smiles).unwrap()); + } + }); +} + +/// Benchmark: iterate all atoms and read one cheap property (atomic number). +/// Each iteration: N atoms * 2 FFI calls (get_atom + get_atomic_num). +#[bench] +fn bench_atom_one_property(b: &mut test::bench::Bencher) { + let mut mols: Vec = SMILES_SET + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mut mols { + let n = mol.num_atoms(true); + for i in 0..n { + let atom = mol.atom_with_idx(i); + test::black_box(atom.get_atomic_num()); + } + } + }); +} + +/// Benchmark: iterate all atoms, read 7 properties per atom. +/// This is the realistic "featurization" workload — the hot path +/// in ML pipelines, QSAR descriptor computation, etc. +/// Each iteration: N atoms * 8 FFI calls (1 get_atom + 7 properties). +#[bench] +fn bench_atom_all_properties(b: &mut test::bench::Bencher) { + let mut mols: Vec = SMILES_SET + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mut mols { + let n = mol.num_atoms(true); + for i in 0..n { + let atom = mol.atom_with_idx(i); + test::black_box(atom.symbol()); + test::black_box(atom.get_atomic_num()); + test::black_box(atom.get_formal_charge()); + test::black_box(atom.get_is_aromatic()); + test::black_box(atom.get_hybridization_type()); + test::black_box(atom.get_degree()); + test::black_box(atom.get_total_num_hs()); + } + } + }); +} + +/// Benchmark: full pipeline — parse + featurize. +/// Measures end-to-end cost of the most common workflow. +#[bench] +fn bench_parse_and_featurize(b: &mut test::bench::Bencher) { + b.iter(|| { + for smiles in SMILES_SET { + let mut mol = ROMol::from_smiles(smiles).unwrap(); + let n = mol.num_atoms(true); + for i in 0..n { + let atom = mol.atom_with_idx(i); + test::black_box(atom.symbol()); + test::black_box(atom.get_atomic_num()); + test::black_box(atom.get_formal_charge()); + test::black_box(atom.get_is_aromatic()); + test::black_box(atom.get_hybridization_type()); + test::black_box(atom.get_degree()); + test::black_box(atom.get_total_num_hs()); + } + } + }); +} + +/// Benchmark: atom_ref with one property (const path, no &mut needed). +#[bench] +fn bench_atom_ref_one_property(b: &mut test::bench::Bencher) { + let mols: Vec = SMILES_SET + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mols { + let n = mol.num_atoms(true); + for i in 0..n { + let atom = mol.atom_ref(i); + test::black_box(atom.get_atomic_num()); + } + } + }); +} + +/// Benchmark: atom_ref with all 7 properties (const path). +/// Compare directly against bench_atom_all_properties. +#[bench] +fn bench_atom_ref_all_properties(b: &mut test::bench::Bencher) { + let mols: Vec = SMILES_SET + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mols { + let n = mol.num_atoms(true); + for i in 0..n { + let atom = mol.atom_ref(i); + test::black_box(atom.symbol()); + test::black_box(atom.get_atomic_num()); + test::black_box(atom.get_formal_charge()); + test::black_box(atom.get_is_aromatic()); + test::black_box(atom.get_hybridization_type()); + test::black_box(atom.get_degree()); + test::black_box(atom.get_total_num_hs()); + } + } + }); +} + +/// Benchmark: clone + featurize via atom_with_idx (the old &ROMol workflow). +/// This is the cost users pay when they only have &ROMol. +#[bench] +fn bench_clone_and_featurize(b: &mut test::bench::Bencher) { + let mols: Vec = SMILES_SET + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mols { + let mut mol = mol.clone(); + let n = mol.num_atoms(true); + for i in 0..n { + let atom = mol.atom_with_idx(i); + test::black_box(atom.symbol()); + test::black_box(atom.get_atomic_num()); + test::black_box(atom.get_formal_charge()); + test::black_box(atom.get_is_aromatic()); + test::black_box(atom.get_hybridization_type()); + test::black_box(atom.get_degree()); + test::black_box(atom.get_total_num_hs()); + } + } + }); +} + +/// Benchmark: clone cost alone. +/// atom_with_idx requires &mut self, so callers who have &ROMol must clone. +/// This measures that tax. +#[bench] +fn bench_clone_molecules(b: &mut test::bench::Bencher) { + let mols: Vec = SMILES_SET + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mols { + test::black_box(mol.clone()); + } + }); +} From a8ddd2febd97c7998ec0e0f7960ab93d4a8fda4f Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Sat, 14 Mar 2026 00:50:01 -0400 Subject: [PATCH 11/14] Update dependencies to latest major versions - thiserror 1 -> 2 (derive macro API unchanged for our usage) - env_logger 0.9/0.10 -> 0.11 (init() API unchanged) - which 4 -> 8 (which::which() API unchanged) Co-Authored-By: Claude Opus 4.6 (1M context) --- Cargo.toml | 4 ++-- rdkit-sys/Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 295dbe2..93aeeb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ cxx = "1" flate2 = "1" log = "0.4" rdkit-sys = { path = "rdkit-sys", version = "0.4.9" } -thiserror = "1" +thiserror = "2" [dev-dependencies] -env_logger = "0.9.0" +env_logger = "0.11" diff --git a/rdkit-sys/Cargo.toml b/rdkit-sys/Cargo.toml index 53c8e9b..553f31b 100644 --- a/rdkit-sys/Cargo.toml +++ b/rdkit-sys/Cargo.toml @@ -14,9 +14,9 @@ exclude = ["rdkit-*", "*.tar.gz", "examples/"] cxx = "1.0.109" [build-dependencies] -env_logger = "0.10.0" +env_logger = "0.11" cxx-build = "1.0.109" -which = "4.4.2" +which = "8" [features] default = [] From d3563170d11063035f39c9e48a42f8d396a9ac53 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Sat, 14 Mar 2026 00:50:07 -0400 Subject: [PATCH 12/14] Trim benchmarks to honest comparisons only Remove inflated clone+featurize and redundant one-property benchmarks. Keep: atom_ref vs atom_mut (regression guard), clone cost, parse baseline. Co-Authored-By: Claude Opus 4.6 (1M context) --- benches/atom_iteration_benchmark.rs | 120 ++++------------------------ 1 file changed, 14 insertions(+), 106 deletions(-) diff --git a/benches/atom_iteration_benchmark.rs b/benches/atom_iteration_benchmark.rs index 8c6d4dc..47b5927 100644 --- a/benches/atom_iteration_benchmark.rs +++ b/benches/atom_iteration_benchmark.rs @@ -7,7 +7,7 @@ use rdkit::ROMol; /// Drug-like molecules of varying size for realistic benchmarking. /// These cover common pharmaceutical scaffolds and natural products. const SMILES_SET: &[&str] = &[ - // aspirin (21 atoms with H) + // aspirin "CC(=O)Oc1ccccc1C(=O)O", // ibuprofen "CC(C)Cc1ccc(cc1)C(C)C(=O)O", @@ -15,16 +15,15 @@ const SMILES_SET: &[&str] = &[ "Cn1c(=O)c2c(ncn2C)n(C)c1=O", // diazepam "O=C1CN=C(c2ccccc2)c2cc(Cl)ccc2N1C", - // atorvastatin (lipitor) — large drug molecule + // atorvastatin (lipitor) "CC(C)c1c(C(=O)Nc2ccccc2)c(-c2ccccc2)c(-c2ccc(F)cc2)n1CC[C@@H](O)C[C@@H](O)CC(=O)O", - // taxol core — complex natural product + // taxol core "CC1=C2C(OC(=O)c3ccccc3)C(O)C4(OC(=O)C(O)(CC(OC(=O)c5ccccc5)C1O)C24C)C(=O)c1ccc(OC)cc1", - // vancomycin fragment — large, many atoms + // vancomycin fragment "OC1C(O)C(OC2C(O)C(O)C(O)C(CO)O2)OC(CO)C1NC(=O)C1CC(O)CN1C(=O)C(NC(=O)C1CC(=O)NC(=O)C1O)C(O)c1ccc(O)cc1", ]; -/// Benchmark: parse SMILES into molecules. -/// Baseline cost — everything else is on top of this. +/// Baseline: SMILES parsing cost. #[bench] fn bench_parse_smiles(b: &mut test::bench::Bencher) { b.iter(|| { @@ -34,97 +33,8 @@ fn bench_parse_smiles(b: &mut test::bench::Bencher) { }); } -/// Benchmark: iterate all atoms and read one cheap property (atomic number). -/// Each iteration: N atoms * 2 FFI calls (get_atom + get_atomic_num). -#[bench] -fn bench_atom_one_property(b: &mut test::bench::Bencher) { - let mut mols: Vec = SMILES_SET - .iter() - .map(|s| ROMol::from_smiles(s).unwrap()) - .collect(); - - b.iter(|| { - for mol in &mut mols { - let n = mol.num_atoms(true); - for i in 0..n { - let atom = mol.atom_with_idx(i); - test::black_box(atom.get_atomic_num()); - } - } - }); -} - -/// Benchmark: iterate all atoms, read 7 properties per atom. -/// This is the realistic "featurization" workload — the hot path -/// in ML pipelines, QSAR descriptor computation, etc. -/// Each iteration: N atoms * 8 FFI calls (1 get_atom + 7 properties). -#[bench] -fn bench_atom_all_properties(b: &mut test::bench::Bencher) { - let mut mols: Vec = SMILES_SET - .iter() - .map(|s| ROMol::from_smiles(s).unwrap()) - .collect(); - - b.iter(|| { - for mol in &mut mols { - let n = mol.num_atoms(true); - for i in 0..n { - let atom = mol.atom_with_idx(i); - test::black_box(atom.symbol()); - test::black_box(atom.get_atomic_num()); - test::black_box(atom.get_formal_charge()); - test::black_box(atom.get_is_aromatic()); - test::black_box(atom.get_hybridization_type()); - test::black_box(atom.get_degree()); - test::black_box(atom.get_total_num_hs()); - } - } - }); -} - -/// Benchmark: full pipeline — parse + featurize. -/// Measures end-to-end cost of the most common workflow. -#[bench] -fn bench_parse_and_featurize(b: &mut test::bench::Bencher) { - b.iter(|| { - for smiles in SMILES_SET { - let mut mol = ROMol::from_smiles(smiles).unwrap(); - let n = mol.num_atoms(true); - for i in 0..n { - let atom = mol.atom_with_idx(i); - test::black_box(atom.symbol()); - test::black_box(atom.get_atomic_num()); - test::black_box(atom.get_formal_charge()); - test::black_box(atom.get_is_aromatic()); - test::black_box(atom.get_hybridization_type()); - test::black_box(atom.get_degree()); - test::black_box(atom.get_total_num_hs()); - } - } - }); -} - -/// Benchmark: atom_ref with one property (const path, no &mut needed). -#[bench] -fn bench_atom_ref_one_property(b: &mut test::bench::Bencher) { - let mols: Vec = SMILES_SET - .iter() - .map(|s| ROMol::from_smiles(s).unwrap()) - .collect(); - - b.iter(|| { - for mol in &mols { - let n = mol.num_atoms(true); - for i in 0..n { - let atom = mol.atom_ref(i); - test::black_box(atom.get_atomic_num()); - } - } - }); -} - -/// Benchmark: atom_ref with all 7 properties (const path). -/// Compare directly against bench_atom_all_properties. +/// Iterate all atoms via atom_ref (&self), read 7 properties per atom. +/// This is the realistic featurization workload. #[bench] fn bench_atom_ref_all_properties(b: &mut test::bench::Bencher) { let mols: Vec = SMILES_SET @@ -149,18 +59,17 @@ fn bench_atom_ref_all_properties(b: &mut test::bench::Bencher) { }); } -/// Benchmark: clone + featurize via atom_with_idx (the old &ROMol workflow). -/// This is the cost users pay when they only have &ROMol. +/// Same workload via atom_with_idx (&mut self). +/// Regression guard: should be the same speed as atom_ref. #[bench] -fn bench_clone_and_featurize(b: &mut test::bench::Bencher) { - let mols: Vec = SMILES_SET +fn bench_atom_mut_all_properties(b: &mut test::bench::Bencher) { + let mut mols: Vec = SMILES_SET .iter() .map(|s| ROMol::from_smiles(s).unwrap()) .collect(); b.iter(|| { - for mol in &mols { - let mut mol = mol.clone(); + for mol in &mut mols { let n = mol.num_atoms(true); for i in 0..n { let atom = mol.atom_with_idx(i); @@ -176,9 +85,8 @@ fn bench_clone_and_featurize(b: &mut test::bench::Bencher) { }); } -/// Benchmark: clone cost alone. -/// atom_with_idx requires &mut self, so callers who have &ROMol must clone. -/// This measures that tax. +/// Clone cost alone. Useful for understanding the cost of cloning +/// molecules when only &ROMol is available but mutation is needed. #[bench] fn bench_clone_molecules(b: &mut test::bench::Bencher) { let mols: Vec = SMILES_SET From d1a0fa2a37081f131570c2ec3e369bfb214114d2 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Sat, 14 Mar 2026 20:56:57 -0400 Subject: [PATCH 13/14] Fix broken rdkit-sys link in README MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crates.io/crate/ → crates.io/crates/ (missing plural) Closes #49 Co-Authored-By: Claude Opus 4.6 (1M context) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a3a1a69..137cfa3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ RDKit --- A high-level library for performing common RDKit tasks such as SMILES parsing, molecule normalization, etc. Uses -the C++ API via bindings from [rdkit-sys](https://crates.io/crate/rdkit-sys). +the C++ API via bindings from [rdkit-sys](https://crates.io/crates/rdkit-sys). Notice: Requires rdkit 2023.09.1 or higher (like Ubuntu Noble 24.04) From 47066c7f1cfc3c1c5829ef47b57b5e905379f70d Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Sun, 15 Mar 2026 00:35:59 -0400 Subject: [PATCH 14/14] Add configurable fingerprints and MACCS keys Morgan, RDK, and Pattern fingerprints now accept custom parameters (radius, nBits, path length, size). Adds MACCS 166-bit keys. Fingerprint::new() now correctly truncates to actual bit count, fixing padding for non-power-of-64 fingerprints like MACCS. Co-Authored-By: Claude Opus 4.6 (1M context) --- rdkit-sys/src/bridge/fingerprint.rs | 19 ++++++++++ rdkit-sys/wrapper/include/fingerprint.h | 11 ++++++ rdkit-sys/wrapper/src/fingerprint.cc | 25 ++++++++++++++ src/fingerprint.rs | 10 ++++++ src/graphmol/ro_mol.rs | 27 +++++++++++++++ tests/test_fingerprint_params.rs | 46 +++++++++++++++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 tests/test_fingerprint_params.rs diff --git a/rdkit-sys/src/bridge/fingerprint.rs b/rdkit-sys/src/bridge/fingerprint.rs index 44eb48c..4951740 100644 --- a/rdkit-sys/src/bridge/fingerprint.rs +++ b/rdkit-sys/src/bridge/fingerprint.rs @@ -17,5 +17,24 @@ pub mod ffi { pub fn explicit_bit_vect_to_u64_vec( bitvect: &SharedPtr, ) -> UniquePtr>; + + // Configurable fingerprints + pub fn morgan_fingerprint_mol_with_params( + mol: &SharedPtr, + radius: u32, + n_bits: u32, + ) -> SharedPtr; + pub fn rdk_fingerprint_mol_with_params( + mol: &SharedPtr, + min_path: u32, + max_path: u32, + fp_size: u32, + ) -> SharedPtr; + pub fn pattern_fingerprint_mol_with_params( + mol: &SharedPtr, + fp_size: u32, + ) -> SharedPtr; + pub fn maccs_fingerprint_mol(mol: &SharedPtr) -> SharedPtr; + pub fn explicit_bit_vect_num_bits(bitvect: &SharedPtr) -> u32; } } diff --git a/rdkit-sys/wrapper/include/fingerprint.h b/rdkit-sys/wrapper/include/fingerprint.h index f1fe3e7..2c37b49 100644 --- a/rdkit-sys/wrapper/include/fingerprint.h +++ b/rdkit-sys/wrapper/include/fingerprint.h @@ -11,4 +11,15 @@ std::shared_ptr copy_explicit_bit_vect(const std::shared_ptr &bitvect); std::unique_ptr> explicit_bit_vect_to_u64_vec(const std::shared_ptr &bitvect); + +// Configurable fingerprints +std::shared_ptr morgan_fingerprint_mol_with_params(const std::shared_ptr &mol, + unsigned int radius, unsigned int n_bits); +std::shared_ptr rdk_fingerprint_mol_with_params(const std::shared_ptr &mol, + unsigned int min_path, unsigned int max_path, + unsigned int fp_size); +std::shared_ptr pattern_fingerprint_mol_with_params(const std::shared_ptr &mol, + unsigned int fp_size); +std::shared_ptr maccs_fingerprint_mol(const std::shared_ptr &mol); +unsigned int explicit_bit_vect_num_bits(const std::shared_ptr &bitvect); } // namespace RDKit \ No newline at end of file diff --git a/rdkit-sys/wrapper/src/fingerprint.cc b/rdkit-sys/wrapper/src/fingerprint.cc index 83c939f..a98541e 100644 --- a/rdkit-sys/wrapper/src/fingerprint.cc +++ b/rdkit-sys/wrapper/src/fingerprint.cc @@ -1,6 +1,7 @@ #include "rust/cxx.h" #include #include +#include #include namespace RDKit { @@ -28,4 +29,28 @@ std::unique_ptr> explicit_bit_vect_to_u64_vec(const std::s std::vector *bytes_heap = new std::vector(bytes); return std::unique_ptr>(bytes_heap); } + +std::shared_ptr morgan_fingerprint_mol_with_params(const std::shared_ptr &mol, + unsigned int radius, unsigned int n_bits) { + return std::shared_ptr(MorganFingerprints::getFingerprintAsBitVect(*mol, radius, n_bits)); +} + +std::shared_ptr rdk_fingerprint_mol_with_params(const std::shared_ptr &mol, + unsigned int min_path, unsigned int max_path, + unsigned int fp_size) { + return std::shared_ptr(RDKFingerprintMol(*mol, min_path, max_path, fp_size)); +} + +std::shared_ptr pattern_fingerprint_mol_with_params(const std::shared_ptr &mol, + unsigned int fp_size) { + return std::shared_ptr(PatternFingerprintMol(*mol, fp_size)); +} + +std::shared_ptr maccs_fingerprint_mol(const std::shared_ptr &mol) { + return std::shared_ptr(MACCSFingerprints::getFingerprintAsBitVect(*mol)); +} + +unsigned int explicit_bit_vect_num_bits(const std::shared_ptr &bitvect) { + return bitvect->getNumBits(); +} } // namespace RDKit \ No newline at end of file diff --git a/src/fingerprint.rs b/src/fingerprint.rs index e2bdc46..41ccb29 100644 --- a/src/fingerprint.rs +++ b/src/fingerprint.rs @@ -6,9 +6,11 @@ pub struct Fingerprint(pub BitVec); impl Fingerprint { pub fn new(ptr: SharedPtr) -> Self { + let num_bits = rdkit_sys::fingerprint_ffi::explicit_bit_vect_num_bits(&ptr) as usize; let unique_ptr_bytes = rdkit_sys::fingerprint_ffi::explicit_bit_vect_to_u64_vec(&ptr); let rdkit_fingerprint_bytes: Vec = unique_ptr_bytes.into_iter().copied().collect(); let mut bitvec_u64 = bitvec::vec::BitVec::::from_vec(rdkit_fingerprint_bytes); + bitvec_u64.truncate(num_bits); let mut idiomatic_bitvec_u8 = bitvec::vec::BitVec::::new(); idiomatic_bitvec_u8.append(&mut bitvec_u64); @@ -16,6 +18,14 @@ impl Fingerprint { Fingerprint(idiomatic_bitvec_u8) } + pub fn len(&self) -> usize { + self.0.len() + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + pub fn tanimoto_distance(&self, other: &Fingerprint) -> f32 { let and = self.0.clone() & &other.0; let or = self.0.clone() | &other.0; diff --git a/src/graphmol/ro_mol.rs b/src/graphmol/ro_mol.rs index 045d2ed..5b0bb5b 100644 --- a/src/graphmol/ro_mol.rs +++ b/src/graphmol/ro_mol.rs @@ -85,6 +85,33 @@ impl ROMol { Fingerprint::new(ptr) } + pub fn morgan_fingerprint_with_params(&self, radius: u32, n_bits: u32) -> Fingerprint { + let ptr = fingerprint_ffi::morgan_fingerprint_mol_with_params(&self.ptr, radius, n_bits); + Fingerprint::new(ptr) + } + + pub fn rdk_fingerprint_with_params( + &self, + min_path: u32, + max_path: u32, + fp_size: u32, + ) -> Fingerprint { + let ptr = fingerprint_ffi::rdk_fingerprint_mol_with_params( + &self.ptr, min_path, max_path, fp_size, + ); + Fingerprint::new(ptr) + } + + pub fn pattern_fingerprint_with_params(&self, fp_size: u32) -> Fingerprint { + let ptr = fingerprint_ffi::pattern_fingerprint_mol_with_params(&self.ptr, fp_size); + Fingerprint::new(ptr) + } + + pub fn maccs_fingerprint(&self) -> Fingerprint { + let ptr = fingerprint_ffi::maccs_fingerprint_mol(&self.ptr); + Fingerprint::new(ptr) + } + pub fn num_atoms(&self, only_explicit: bool) -> u32 { ro_mol_ffi::get_num_atoms(&self.ptr, only_explicit) } diff --git a/tests/test_fingerprint_params.rs b/tests/test_fingerprint_params.rs new file mode 100644 index 0000000..85fdd80 --- /dev/null +++ b/tests/test_fingerprint_params.rs @@ -0,0 +1,46 @@ +use rdkit::ROMol; + +#[test] +fn test_morgan_different_radius() { + let mol = ROMol::from_smiles("c1ccccc1CC").unwrap(); + let fp_r2 = mol.morgan_fingerprint_with_params(2, 2048); + let fp_r3 = mol.morgan_fingerprint_with_params(3, 2048); + assert_ne!(fp_r2.0, fp_r3.0); + assert_eq!(fp_r2.len(), 2048); +} + +#[test] +fn test_morgan_custom_nbits() { + let mol = ROMol::from_smiles("c1ccccc1").unwrap(); + let fp = mol.morgan_fingerprint_with_params(2, 1024); + assert_eq!(fp.len(), 1024); +} + +#[test] +fn test_rdk_fingerprint_with_params() { + let mol = ROMol::from_smiles("c1ccccc1CCCC").unwrap(); + let fp = mol.rdk_fingerprint_with_params(1, 7, 1024); + assert_eq!(fp.len(), 1024); +} + +#[test] +fn test_pattern_fingerprint_with_params() { + let mol = ROMol::from_smiles("c1ccccc1").unwrap(); + let fp = mol.pattern_fingerprint_with_params(1024); + assert_eq!(fp.len(), 1024); +} + +#[test] +fn test_maccs_fingerprint() { + let mol = ROMol::from_smiles("c1ccccc1").unwrap(); + let fp = mol.maccs_fingerprint(); + assert_eq!(fp.len(), 167); +} + +#[test] +fn test_fingerprint_len() { + let mol = ROMol::from_smiles("CC").unwrap(); + let fp = mol.morgan_fingerprint(); + assert!(!fp.is_empty()); + assert_eq!(fp.len(), 2048); +}