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..16b78a3 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() { @@ -122,17 +122,21 @@ fn main() { // "ChemReactions", // "ChemTransforms", "DataStructs", - // "Depictor", + "Depictor", "Descriptors", + "DistGeomHelpers", + "EigenSolvers", "FileParsers", "Fingerprints", + "ForceField", + "ForceFieldHelpers", // "GenericGroups", "GraphMol", "MolStandardize", // "MolTransforms", - // "PartialCharges", + "PartialCharges", "RDGeneral", - // "RDGeometryLib", + "RDGeometryLib", // "RingDecomposerLib", "ScaffoldNetwork", "SmilesParse", 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/conformer.rs b/rdkit-sys/src/bridge/conformer.rs new file mode 100644 index 0000000..87c9321 --- /dev/null +++ b/rdkit-sys/src/bridge/conformer.rs @@ -0,0 +1,23 @@ +#[cxx::bridge(namespace = "RDKit")] +pub mod ffi { + unsafe extern "C++" { + include!("wrapper/include/conformer.h"); + + pub type ROMol = crate::ro_mol_ffi::ROMol; + + pub fn embed_molecule(mol: &mut SharedPtr) -> i32; + pub fn embed_multiple_confs( + mol: &mut SharedPtr, + num_confs: u32, + ) -> UniquePtr>; + pub fn compute_2d_coords(mol: &mut SharedPtr) -> u32; + + pub fn mol_get_num_conformers(mol: &SharedPtr) -> u32; + pub fn conformer_is_3d(mol: &SharedPtr, conf_id: i32) -> bool; + pub fn get_atom_pos( + mol: &SharedPtr, + conf_id: i32, + atom_idx: u32, + ) -> UniquePtr>; + } +} diff --git a/rdkit-sys/src/bridge/mod.rs b/rdkit-sys/src/bridge/mod.rs index 1593316..4bc46aa 100644 --- a/rdkit-sys/src/bridge/mod.rs +++ b/rdkit-sys/src/bridge/mod.rs @@ -1,3 +1,6 @@ +mod conformer; +pub use conformer::ffi as conformer_ffi; + mod descriptors; pub use descriptors::ffi as descriptors_ffi; @@ -11,7 +14,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/mol_ops.rs b/rdkit-sys/src/bridge/mol_ops.rs index e7e89ce..8c2d52a 100644 --- a/rdkit-sys/src/bridge/mol_ops.rs +++ b/rdkit-sys/src/bridge/mol_ops.rs @@ -83,5 +83,14 @@ pub mod ffi { pub fn romol_set_hybridization(mol: &mut SharedPtr); pub fn clean_up(rw_mol: &mut SharedPtr); + + pub fn set_aromaticity(mol: &mut SharedPtr); + pub fn assign_stereochemistry(mol: &mut SharedPtr); + pub fn mol_get_formal_charge(mol: &SharedPtr) -> i32; + + pub type ROMolVec; + pub fn get_mol_frags(mol: &SharedPtr) -> SharedPtr; + pub fn romol_vec_size(vec: &SharedPtr) -> u32; + pub fn romol_vec_get(vec: &SharedPtr, idx: u32) -> SharedPtr; } } 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/conformer.h b/rdkit-sys/wrapper/include/conformer.h new file mode 100644 index 0000000..383195a --- /dev/null +++ b/rdkit-sys/wrapper/include/conformer.h @@ -0,0 +1,17 @@ +#pragma once + +#include "rust/cxx.h" +#include + +namespace RDKit { + +int embed_molecule(std::shared_ptr &mol); +std::unique_ptr> embed_multiple_confs(std::shared_ptr &mol, unsigned int num_confs); +unsigned int compute_2d_coords(std::shared_ptr &mol); + +unsigned int mol_get_num_conformers(const std::shared_ptr &mol); +bool conformer_is_3d(const std::shared_ptr &mol, int conf_id); +std::unique_ptr> get_atom_pos(const std::shared_ptr &mol, int conf_id, + unsigned int atom_idx); + +} // namespace RDKit diff --git a/rdkit-sys/wrapper/include/mol_ops.h b/rdkit-sys/wrapper/include/mol_ops.h index 654bce2..3eca29e 100644 --- a/rdkit-sys/wrapper/include/mol_ops.h +++ b/rdkit-sys/wrapper/include/mol_ops.h @@ -67,4 +67,13 @@ void romol_set_hybridization(std::shared_ptr &mol); // pub fn clean_up(rw_mol: &mut SharedPtr) void clean_up(std::shared_ptr &rw_mol); + +void set_aromaticity(std::shared_ptr &mol); +void assign_stereochemistry(std::shared_ptr &mol); +int mol_get_formal_charge(const std::shared_ptr &mol); + +struct ROMolVec; +std::shared_ptr get_mol_frags(const std::shared_ptr &mol); +unsigned int romol_vec_size(const std::shared_ptr &vec); +std::shared_ptr romol_vec_get(const std::shared_ptr &vec, unsigned int idx); } // namespace RDKit 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/conformer.cc b/rdkit-sys/wrapper/src/conformer.cc new file mode 100644 index 0000000..1cfc12e --- /dev/null +++ b/rdkit-sys/wrapper/src/conformer.cc @@ -0,0 +1,27 @@ +#include "rust/cxx.h" +#include +#include +#include + +namespace RDKit { + +int embed_molecule(std::shared_ptr &mol) { return DGeomHelpers::EmbedMolecule(*mol); } + +std::unique_ptr> embed_multiple_confs(std::shared_ptr &mol, unsigned int num_confs) { + auto ids = DGeomHelpers::EmbedMultipleConfs(*mol, num_confs); + return std::make_unique>(ids.begin(), ids.end()); +} + +unsigned int compute_2d_coords(std::shared_ptr &mol) { return RDDepict::compute2DCoords(*mol); } + +unsigned int mol_get_num_conformers(const std::shared_ptr &mol) { return mol->getNumConformers(); } + +bool conformer_is_3d(const std::shared_ptr &mol, int conf_id) { return mol->getConformer(conf_id).is3D(); } + +std::unique_ptr> get_atom_pos(const std::shared_ptr &mol, int conf_id, + unsigned int atom_idx) { + const auto &pos = mol->getConformer(conf_id).getAtomPos(atom_idx); + return std::make_unique>(std::vector{pos.x, pos.y, pos.z}); +} + +} // namespace RDKit diff --git a/rdkit-sys/wrapper/src/mol_ops.cc b/rdkit-sys/wrapper/src/mol_ops.cc index 66b11f5..d091a4a 100644 --- a/rdkit-sys/wrapper/src/mol_ops.cc +++ b/rdkit-sys/wrapper/src/mol_ops.cc @@ -106,4 +106,27 @@ std::shared_ptr add_hs(const std::shared_ptr &mol, bool explicit_o void romol_set_hybridization(std::shared_ptr &mol) { MolOps::setHybridization(*mol); } void clean_up(std::shared_ptr &rw_mol) { MolOps::cleanUp(*rw_mol); } + +void set_aromaticity(std::shared_ptr &mol) { MolOps::setAromaticity(*mol); } + +void assign_stereochemistry(std::shared_ptr &mol) { MolOps::assignStereochemistry(*mol, true, true, true); } + +int mol_get_formal_charge(const std::shared_ptr &mol) { return MolOps::getFormalCharge(*mol); } + +struct ROMolVec { + std::vector> mols; +}; + +std::shared_ptr get_mol_frags(const std::shared_ptr &mol) { + auto container = std::make_shared(); + auto frags = MolOps::getMolFrags(*mol); + for (auto &frag : frags) { container->mols.push_back(std::shared_ptr(new ROMol(*frag))); } + return container; +} + +unsigned int romol_vec_size(const std::shared_ptr &vec) { return vec->mols.size(); } + +std::shared_ptr romol_vec_get(const std::shared_ptr &vec, unsigned int idx) { + return vec->mols.at(idx); +} } // 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/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/conformer.rs b/src/graphmol/conformer.rs new file mode 100644 index 0000000..a911d19 --- /dev/null +++ b/src/graphmol/conformer.rs @@ -0,0 +1,30 @@ +use crate::ROMol; + +impl ROMol { + pub fn embed_molecule(&mut self) -> i32 { + rdkit_sys::conformer_ffi::embed_molecule(&mut self.ptr) + } + + pub fn embed_multiple_confs(&mut self, num_confs: u32) -> Vec { + let ids = rdkit_sys::conformer_ffi::embed_multiple_confs(&mut self.ptr, num_confs); + ids.iter().copied().collect() + } + + pub fn compute_2d_coords(&mut self) -> u32 { + rdkit_sys::conformer_ffi::compute_2d_coords(&mut self.ptr) + } + + pub fn num_conformers(&self) -> u32 { + rdkit_sys::conformer_ffi::mol_get_num_conformers(&self.ptr) + } + + pub fn conformer_is_3d(&self, conf_id: i32) -> bool { + rdkit_sys::conformer_ffi::conformer_is_3d(&self.ptr, conf_id) + } + + pub fn atom_position(&self, conf_id: i32, atom_idx: u32) -> (f64, f64, f64) { + let pos = rdkit_sys::conformer_ffi::get_atom_pos(&self.ptr, conf_id, atom_idx); + let s = pos.as_ref().unwrap().as_slice(); + (s[0], s[1], s[2]) + } +} diff --git a/src/graphmol/mod.rs b/src/graphmol/mod.rs index f23a721..75f69b3 100644 --- a/src/graphmol/mod.rs +++ b/src/graphmol/mod.rs @@ -1,6 +1,11 @@ mod atom; pub use atom::*; +mod atom_ref; +pub use atom_ref::*; + +mod conformer; + mod mol_ops; pub use mol_ops::*; diff --git a/src/graphmol/mol_ops.rs b/src/graphmol/mol_ops.rs index 774cf85..fa47b25 100644 --- a/src/graphmol/mol_ops.rs +++ b/src/graphmol/mol_ops.rs @@ -209,3 +209,26 @@ pub fn set_hybridization(romol: &mut ROMol) { pub fn clean_up(rw_mol: &mut RWMol) { rdkit_sys::mol_ops_ffi::clean_up(&mut rw_mol.ptr); } + +pub fn set_aromaticity(rw_mol: &mut RWMol) { + rdkit_sys::mol_ops_ffi::set_aromaticity(&mut rw_mol.ptr); +} + +pub fn assign_stereochemistry(romol: &mut ROMol) { + rdkit_sys::mol_ops_ffi::assign_stereochemistry(&mut romol.ptr); +} + +pub fn get_formal_charge(romol: &ROMol) -> i32 { + rdkit_sys::mol_ops_ffi::mol_get_formal_charge(&romol.ptr) +} + +pub fn get_mol_frags(romol: &ROMol) -> Vec { + let container = rdkit_sys::mol_ops_ffi::get_mol_frags(&romol.ptr); + let size = rdkit_sys::mol_ops_ffi::romol_vec_size(&container); + (0..size) + .map(|i| { + let ptr = rdkit_sys::mol_ops_ffi::romol_vec_get(&container, i); + ROMol { ptr } + }) + .collect() +} diff --git a/src/graphmol/ro_mol.rs b/src/graphmol/ro_mol.rs index bcdd30c..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, @@ -89,11 +89,20 @@ 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) } + /// 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_conformer.rs b/tests/test_conformer.rs new file mode 100644 index 0000000..ba70558 --- /dev/null +++ b/tests/test_conformer.rs @@ -0,0 +1,55 @@ +use rdkit::ROMol; + +#[test] +fn test_no_conformers_initially() { + let mol = ROMol::from_smiles("CCO").unwrap(); + assert_eq!(mol.num_conformers(), 0); +} + +#[test] +fn test_embed_molecule() { + let mut mol = ROMol::from_smiles("CCO").unwrap(); + let conf_id = mol.embed_molecule(); + assert!(conf_id >= 0); + assert_eq!(mol.num_conformers(), 1); +} + +#[test] +fn test_embed_molecule_3d() { + let mut mol = ROMol::from_smiles("CCO").unwrap(); + let conf_id = mol.embed_molecule(); + assert!(mol.conformer_is_3d(conf_id)); +} + +#[test] +fn test_embed_multiple_confs() { + let mut mol = ROMol::from_smiles("CCO").unwrap(); + let ids = mol.embed_multiple_confs(5); + assert_eq!(ids.len(), 5); + assert_eq!(mol.num_conformers(), 5); +} + +#[test] +fn test_compute_2d_coords() { + let mut mol = ROMol::from_smiles("c1ccccc1").unwrap(); + let conf_id = mol.compute_2d_coords(); + assert!(!mol.conformer_is_3d(conf_id as i32)); +} + +#[test] +fn test_atom_position_3d() { + let mut mol = ROMol::from_smiles("CCO").unwrap(); + let conf_id = mol.embed_molecule(); + let (x, y, z) = mol.atom_position(conf_id, 0); + assert!(x != 0.0 || y != 0.0 || z != 0.0); +} + +#[test] +fn test_2d_coords_z_zero() { + let mut mol = ROMol::from_smiles("CCO").unwrap(); + let conf_id = mol.compute_2d_coords(); + for i in 0..mol.num_atoms(true) { + let (_, _, z) = mol.atom_position(conf_id as i32, i); + assert_eq!(z, 0.0); + } +} 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_mol_ops_expanded.rs b/tests/test_mol_ops_expanded.rs new file mode 100644 index 0000000..56182aa --- /dev/null +++ b/tests/test_mol_ops_expanded.rs @@ -0,0 +1,45 @@ +use rdkit::{ROMol, assign_stereochemistry, get_formal_charge, get_mol_frags, set_aromaticity}; + +#[test] +fn test_set_aromaticity() { + let romol = ROMol::from_smiles("C1=CC=CC=C1").unwrap(); + let mut rwmol = romol.as_rw_mol(false, -1); + set_aromaticity(&mut rwmol); +} + +#[test] +fn test_assign_stereochemistry() { + let mut mol = ROMol::from_smiles("C/C=C/C").unwrap(); + assign_stereochemistry(&mut mol); +} + +#[test] +fn test_get_formal_charge() { + let mol = ROMol::from_smiles("CCO").unwrap(); + assert_eq!(get_formal_charge(&mol), 0); + let mol = ROMol::from_smiles("[NH4+]").unwrap(); + assert_eq!(get_formal_charge(&mol), 1); + let mol = ROMol::from_smiles("CC(=O)[O-]").unwrap(); + assert_eq!(get_formal_charge(&mol), -1); +} + +#[test] +fn test_get_mol_frags_single() { + let mol = ROMol::from_smiles("CCO").unwrap(); + let frags = get_mol_frags(&mol); + assert_eq!(frags.len(), 1); +} + +#[test] +fn test_get_mol_frags_multiple() { + let mol = ROMol::from_smiles("CCO.CC").unwrap(); + let frags = get_mol_frags(&mol); + assert_eq!(frags.len(), 2); +} + +#[test] +fn test_get_mol_frags_salt() { + let mol = ROMol::from_smiles("[Na+].[Cl-]").unwrap(); + let frags = get_mol_frags(&mol); + assert_eq!(frags.len(), 2); +} 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() {