diff --git a/Cargo.toml b/Cargo.toml index 1086859..93aeeb6 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 = 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()); + } + } + }); +} + +/// Same workload via atom_with_idx (&mut self). +/// Regression guard: should be the same speed as atom_ref. +#[bench] +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 &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()); + } + } + }); +} + +/// 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 + .iter() + .map(|s| ROMol::from_smiles(s).unwrap()) + .collect(); + + b.iter(|| { + for mol in &mols { + test::black_box(mol.clone()); + } + }); +} diff --git a/rdkit-sys/Cargo.toml b/rdkit-sys/Cargo.toml index 722c75a..553f31b 100644 --- a/rdkit-sys/Cargo.toml +++ b/rdkit-sys/Cargo.toml @@ -2,7 +2,7 @@ name = "rdkit-sys" authors = ["Xavier Lange (xrlange@gmail.com)", "chrissly31415"] version = "0.4.12" -edition = "2021" +edition = "2024" license = "MIT" description = "RDKit CFFI library builder and bindings" repository = "https://github.com/rdkit-rs/rdkit/tree/main/rdkit-sys" @@ -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 = [] 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() { diff --git a/rdkit-sys/rustfmt.toml b/rdkit-sys/rustfmt.toml index 7de3b73..6edb4d8 100644 --- a/rdkit-sys/rustfmt.toml +++ b/rdkit-sys/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" -version = "One" +edition = "2024" +style_edition = "2024" inline_attribute_width = 0 format_generated_files = true merge_derives = true 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/src/bridge/mod.rs b/rdkit-sys/src/bridge/mod.rs index 1593316..e020a81 100644 --- a/rdkit-sys/src/bridge/mod.rs +++ b/rdkit-sys/src/bridge/mod.rs @@ -11,7 +11,7 @@ mod mol_standardize; pub use mol_standardize::ffi as mol_standardize_ffi; mod periodic_table; -pub use periodic_table::{ffi as periodic_table_ffi, PeriodicTableOps}; +pub use periodic_table::{PeriodicTableOps, ffi as periodic_table_ffi}; mod ro_mol; pub use ro_mol::ffi as ro_mol_ffi; 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/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/tests/test_atoms.rs b/rdkit-sys/tests/test_atoms.rs index f5d9d5c..fe12146 100644 --- a/rdkit-sys/tests/test_atoms.rs +++ b/rdkit-sys/tests/test_atoms.rs @@ -17,7 +17,9 @@ fn test_atoms() { assert_eq!( &atoms, - &["C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C"] + &[ + "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C" + ] ); let mut atom = rdkit_sys::ro_mol_ffi::get_atom_with_idx(&mut romol, 2); diff --git a/rdkit-sys/tests/test_ro_mol.rs b/rdkit-sys/tests/test_ro_mol.rs index 50b6bc8..db77dc7 100644 --- a/rdkit-sys/tests/test_ro_mol.rs +++ b/rdkit-sys/tests/test_ro_mol.rs @@ -69,7 +69,10 @@ fn mol_to_molblock_test() { cxx::let_cxx_string!(smiles = "CC"); let romol = rdkit_sys::ro_mol_ffi::smiles_to_mol(&smiles).unwrap(); let molblock = rdkit_sys::ro_mol_ffi::mol_to_molblock(&romol); - 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" + ); } #[test] diff --git a/rdkit-sys/tests/test_rw_mol.rs b/rdkit-sys/tests/test_rw_mol.rs index f31ee43..c42dfeb 100644 --- a/rdkit-sys/tests/test_rw_mol.rs +++ b/rdkit-sys/tests/test_rw_mol.rs @@ -1,4 +1,4 @@ -use cxx::{let_cxx_string, SharedPtr}; +use cxx::{SharedPtr, let_cxx_string}; use rdkit_sys::{ro_mol_ffi::ROMol, rw_mol_ffi::RWMol}; #[test] @@ -191,7 +191,10 @@ CC(=O)OC(CC(=O)[O-])C[N+](C)(C)C let ro_mol = unsafe { std::mem::transmute::, 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/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/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/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/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(); } diff --git a/rustfmt.toml b/rustfmt.toml index 7de3b73..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" -version = "One" +edition = "2024" +style_edition = "2024" inline_attribute_width = 0 format_generated_files = true merge_derives = true 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 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/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 bcdd30c..5b0bb5b 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, @@ -85,15 +85,51 @@ 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) } - 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) } + /// 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) } 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/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 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()); + } +} 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); +} 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() {