Skip to content

Commit

Permalink
added some functions to periodic table, fixed problem with getValence…
Browse files Browse the repository at this point in the history
…List.
  • Loading branch information
mvisani committed Oct 14, 2024
1 parent 7e3c57b commit a263e70
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 14 deletions.
2 changes: 1 addition & 1 deletion rdkit-sys/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 33 additions & 1 deletion rdkit-sys/src/bridge/periodic_table.rs
Original file line number Diff line number Diff line change
@@ -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<PeriodicTable>;
pub fn get_valence_list(atomic_number: u32) -> &'static CxxVector<i32>;
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<i32>;
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<i32>;
}
impl<'a> PeriodicTableOps for UniquePtr<PeriodicTable> {
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<i32> {
ffi::getValenceList(atomic_number)
}
}
80 changes: 78 additions & 2 deletions rdkit-sys/tests/test_periodic_table.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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);
}
5 changes: 3 additions & 2 deletions rdkit-sys/wrapper/include/periodic_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace RDKit {
std::unique_ptr<PeriodicTable> get_periodic_table();
const std::vector<int> &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<int> &getValenceList(unsigned int atomic_number);
} // namespace RDKit
15 changes: 10 additions & 5 deletions rdkit-sys/wrapper/src/periodic_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ std::unique_ptr<PeriodicTable> get_periodic_table() {
// so we need to take into account that it is a const pointer
return std::make_unique<PeriodicTable>(*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<int> &get_valence_list(unsigned int atomic_number) {
const std::vector<int> &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
6 changes: 4 additions & 2 deletions src/periodic_table.rs
Original file line number Diff line number Diff line change
@@ -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<i32> {
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)
}
}
2 changes: 1 addition & 1 deletion tests/test_periodic_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(), &[4]);
assert_eq!(valence_list.as_slice(), &[4]);
}

#[test]
Expand Down

0 comments on commit a263e70

Please sign in to comment.