From a263e70ea206cb03a5f0d3d7be133aa06e7d4a5c Mon Sep 17 00:00:00 2001 From: Marco Visani <92217074+mvisani@users.noreply.github.com> Date: Mon, 14 Oct 2024 09:46:21 +0200 Subject: [PATCH] added some functions to periodic table, fixed problem with getValenceList. --- rdkit-sys/src/bridge/mod.rs | 2 +- rdkit-sys/src/bridge/periodic_table.rs | 34 ++++++++- rdkit-sys/tests/test_periodic_table.rs | 80 +++++++++++++++++++++- rdkit-sys/wrapper/include/periodic_table.h | 5 +- rdkit-sys/wrapper/src/periodic_table.cc | 15 ++-- src/periodic_table.rs | 6 +- tests/test_periodic_table.rs | 2 +- 7 files changed, 130 insertions(+), 14 deletions(-) diff --git a/rdkit-sys/src/bridge/mod.rs b/rdkit-sys/src/bridge/mod.rs index e85d870..1593316 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; +pub use periodic_table::{ffi as periodic_table_ffi, PeriodicTableOps}; 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 60df0e0..1a32244 100644 --- a/rdkit-sys/src/bridge/periodic_table.rs +++ b/rdkit-sys/src/bridge/periodic_table.rs @@ -1,10 +1,42 @@ +use cxx::{CxxVector, UniquePtr}; +use ffi::PeriodicTable; + #[cxx::bridge(namespace = "RDKit")] pub mod ffi { unsafe extern "C++" { type PeriodicTable; include!("wrapper/include/periodic_table.h"); pub fn get_periodic_table() -> UniquePtr; - pub fn get_valence_list(atomic_number: u32) -> &'static CxxVector; + pub fn getAtomicWeight(self: &PeriodicTable, atomic_number: u32) -> f64; + pub fn getAtomicNumber(self: &PeriodicTable, atom: &CxxString) -> i32; + pub fn getElementSymbol(atomic_number: u32) -> String; + pub fn getElementName(atomic_number: u32) -> String; + pub fn getRvdw(self: &PeriodicTable, atomic_number: u32) -> f64; + pub fn getRcovalent(self: &PeriodicTable, atomic_number: u32) -> f64; + pub fn getRb0(self: &PeriodicTable, atomic_number: u32) -> f64; + pub fn getDefaultValence(self: &PeriodicTable, atomic_number: u32) -> i32; + fn getValenceList(atomic_number: u32) -> &'static CxxVector; + pub fn getNouterElecs(self: &PeriodicTable, atomic_number: u32) -> i32; + pub fn getMostCommonIsotope(self: &PeriodicTable, atomic_number: u32) -> i32; pub fn getMostCommonIsotopeMass(self: &PeriodicTable, atom: &CxxString) -> f64; } } + +pub trait PeriodicTableOps { + fn getElementSymbol(self, atomic_number: u32) -> String; + fn getElementName(self, atomic_number: u32) -> String; + fn getValenceList(self, atomic_number: u32) -> &'static CxxVector; +} +impl<'a> PeriodicTableOps for UniquePtr { + fn getElementSymbol(self, atomic_number: u32) -> String { + ffi::getElementSymbol(atomic_number) + } + + fn getElementName(self, atomic_number: u32) -> String { + ffi::getElementName(atomic_number) + } + + fn getValenceList(self, atomic_number: u32) -> &'static CxxVector { + ffi::getValenceList(atomic_number) + } +} diff --git a/rdkit-sys/tests/test_periodic_table.rs b/rdkit-sys/tests/test_periodic_table.rs index 861c8eb..03f6eff 100644 --- a/rdkit-sys/tests/test_periodic_table.rs +++ b/rdkit-sys/tests/test_periodic_table.rs @@ -1,9 +1,14 @@ +// ignore camel case +#![allow(non_snake_case)] + use cxx::let_cxx_string; +use rdkit_sys::PeriodicTableOps; #[test] fn test_get_valence_list() { - let list = rdkit_sys::periodic_table_ffi::get_valence_list(1); - assert_eq!(list.as_slice(), &[1]); + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let list = periodic_table.getValenceList(6); + assert_eq!(list.as_slice(), &[4]); } #[test] @@ -13,3 +18,74 @@ fn test_get_monoisotopic_mass() { let mass = periodic_table.getMostCommonIsotopeMass(&atom); assert_eq!(mass, 12.00); } + +#[test] +fn test_getAtomicWeight() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let weight = periodic_table.getAtomicWeight(6); + assert_eq!(weight, 12.011); +} + +#[test] +fn test_getAtomicNumber() { + let_cxx_string!(atom = "C"); + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let number = periodic_table.getAtomicNumber(&atom); + assert_eq!(number, 6); +} + +#[test] +fn test_getElementSymbol() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let symbol = periodic_table.getElementSymbol(6); + assert_eq!(symbol, "C"); +} + +#[test] +fn test_getElementName() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let name = periodic_table.getElementName(6); + assert_eq!(name, "Carbon"); +} + +#[test] +fn test_getRvdw() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let rvdw = periodic_table.getRvdw(6); + assert_eq!(rvdw, 1.7); +} + +#[test] +fn test_getRcovalent() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let rcovalent = periodic_table.getRcovalent(6); + assert_eq!(rcovalent, 0.76); +} + +#[test] +fn test_getRb0() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let rb0 = periodic_table.getRb0(6); + assert_eq!(rb0, 0.77); +} + +#[test] +fn test_getDefaultValence() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let valence = periodic_table.getDefaultValence(6); + assert_eq!(valence, 4); +} + +#[test] +fn test_getNouterElecs() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let nouter = periodic_table.getNouterElecs(6); + assert_eq!(nouter, 4); +} + +#[test] +fn test_getMostCommonIsotope() { + let periodic_table = rdkit_sys::periodic_table_ffi::get_periodic_table(); + let isotope = periodic_table.getMostCommonIsotope(6); + assert_eq!(isotope, 12); +} diff --git a/rdkit-sys/wrapper/include/periodic_table.h b/rdkit-sys/wrapper/include/periodic_table.h index 7a28a73..be54cd5 100644 --- a/rdkit-sys/wrapper/include/periodic_table.h +++ b/rdkit-sys/wrapper/include/periodic_table.h @@ -5,6 +5,7 @@ namespace RDKit { std::unique_ptr get_periodic_table(); -const std::vector &get_valence_list(unsigned int atomic_number); -// double get_most_common_isotope_mass(const std::string &symbol); +rust::String getElementSymbol(unsigned int atomic_number); +rust::String getElementName(unsigned int atomic_number); +const std::vector &getValenceList(unsigned int atomic_number); } // namespace RDKit diff --git a/rdkit-sys/wrapper/src/periodic_table.cc b/rdkit-sys/wrapper/src/periodic_table.cc index bafc58d..dc68723 100644 --- a/rdkit-sys/wrapper/src/periodic_table.cc +++ b/rdkit-sys/wrapper/src/periodic_table.cc @@ -8,14 +8,19 @@ std::unique_ptr get_periodic_table() { // so we need to take into account that it is a const pointer return std::make_unique(*PeriodicTable::getTable()); } +rust::String getElementSymbol(unsigned int atomic_number) { + PeriodicTable *pt = RDKit::PeriodicTable::getTable(); + return pt->getElementSymbol(atomic_number); +} + +rust::String getElementName(unsigned int atomic_number) { + PeriodicTable *pt = RDKit::PeriodicTable::getTable(); + return pt->getElementName(atomic_number); +} -const std::vector &get_valence_list(unsigned int atomic_number) { +const std::vector &getValenceList(unsigned int atomic_number) { PeriodicTable *pt = RDKit::PeriodicTable::getTable(); return pt->getValenceList(atomic_number); } -// double get_most_common_isotope_mass(const std::string &symbol) { -// PeriodicTable *pt = RDKit::PeriodicTable::getTable(); -// return pt->getMostCommonIsotopeMass(symbol); -// } } // namespace RDKit \ No newline at end of file diff --git a/src/periodic_table.rs b/src/periodic_table.rs index ff5cc72..12e845c 100644 --- a/src/periodic_table.rs +++ b/src/periodic_table.rs @@ -1,14 +1,16 @@ use cxx::{let_cxx_string, CxxVector}; +use rdkit_sys::PeriodicTableOps; pub struct PeriodicTable {} impl PeriodicTable { pub fn get_valence_list(atomic_number: u32) -> &'static CxxVector { - rdkit_sys::periodic_table_ffi::get_valence_list(atomic_number) + rdkit_sys::periodic_table_ffi::get_periodic_table().getValenceList(atomic_number) } pub fn get_most_common_isotope_mass(atom: &str) -> f64 { let_cxx_string!(atom_cxx_string = atom); - rdkit_sys::periodic_table_ffi::get_most_common_isotope_mass(&atom_cxx_string) + rdkit_sys::periodic_table_ffi::get_periodic_table() + .getMostCommonIsotopeMass(&atom_cxx_string) } } diff --git a/tests/test_periodic_table.rs b/tests/test_periodic_table.rs index cbc8067..1cbe46f 100644 --- a/tests/test_periodic_table.rs +++ b/tests/test_periodic_table.rs @@ -3,7 +3,7 @@ use rdkit::PeriodicTable; #[test] fn test_valence_list() { let valence_list = PeriodicTable::get_valence_list(6); - assert_eq!(&valence_list.iter().cloned().collect::>(), &[4]); + assert_eq!(valence_list.as_slice(), &[4]); } #[test]