Skip to content

Commit

Permalink
#6: code refactored and reorganized, added new binaries stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkviatkovskii committed Jul 6, 2024
1 parent 1db971c commit bc6a100
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 213 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ bincode = "2.0.0-rc.3"
fixedbitset = "0.5.7"
nom = "7.1.3"
petgraph = "0.6.5"

[[bin]]
name = "ringo-index"
path = "src/ringo/ringo/index/main.rs"

[[bin]]
name = "ringo-search"
path = "src/ringo/ringo/search/main.rs"
1 change: 1 addition & 0 deletions src/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod bin;
pub mod ringo;
1 change: 1 addition & 0 deletions src/ringo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod fingerprint;
pub mod math;
pub mod molecule;
mod ringo;
1 change: 1 addition & 0 deletions src/ringo/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod fingerprint;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'de> bincode::BorrowDecode<'de> for Fingerprint {

#[cfg(test)]
mod tests {
use crate::ringo::ringo::fingerprint::{Fingerprint, FINGERPRINT_SIZE};
use crate::ringo::fingerprint::fingerprint::{Fingerprint, FINGERPRINT_SIZE};
use fixedbitset::FixedBitSet;

#[test]
Expand All @@ -44,10 +44,9 @@ mod tests {
let mut buf = vec![0u8; FINGERPRINT_SIZE / 8];
bincode::encode_into_slice(&fp, buf.as_mut_slice(), bincode::config::standard()).unwrap();

let decoded: Fingerprint =
bincode::decode_from_slice(&buf, bincode::config::standard())
.unwrap()
.0;
let decoded: Fingerprint = bincode::decode_from_slice(&buf, bincode::config::standard())
.unwrap()
.0;
assert_eq!(decoded.0.ones().collect::<Vec<usize>>(), vec![1, 17]);
}
}
21 changes: 20 additions & 1 deletion src/ringo/molecule/model/atom.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
use crate::ringo::molecule::model::element::Element;

#[derive(Hash, Eq, PartialEq)]
#[derive(Hash, Eq, PartialEq, Debug)]
pub struct Atom {
pub element: Element,
pub isotope: u8,
pub charge: i8,
pub hs: u8,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_atom() {
let atom = Atom {
element: Element { atomic_number: 6 },
isotope: 12,
charge: 0,
hs: 0,
};
assert_eq!(atom.element, Element { atomic_number: 6 });
assert_eq!(atom.isotope, 12);
assert_eq!(atom.charge, 0);
assert_eq!(atom.hs, 0);
}
}
13 changes: 13 additions & 0 deletions src/ringo/molecule/model/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ pub enum BondOrder {
pub struct Bond {
pub order: BondOrder,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_bond_order() {
let bond = Bond {
order: BondOrder::Single,
};
assert_eq!(bond.order, BondOrder::Single);
}
}
187 changes: 100 additions & 87 deletions src/ringo/molecule/model/element.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,106 @@
#[derive(Hash, Eq, PartialEq)]
#[derive(Hash, Eq, PartialEq, Debug)]
pub struct Element {
pub atomic_number: u8,
}

pub fn atomic_weight(element: &Element) -> f64 {
match element.atomic_number {
1 => 1.007825,
2 => 4.002603,
3 => 6.938,
4 => 9.0121831,
5 => 10.806,
6 => 12.011,
7 => 14.007,
8 => 15.999,
9 => 18.998403163,
10 => 20.180,
11 => 22.990,
12 => 24.305,
13 => 26.9815385,
14 => 28.085,
15 => 30.973761998,
16 => 32.06,
17 => 35.45,
18 => 39.948,
19 => 39.0983,
20 => 40.078,
21 => 44.955908,
22 => 47.867,
23 => 50.9415,
24 => 51.9961,
25 => 54.938044,
26 => 55.845,
27 => 58.933194,
28 => 58.6934,
29 => 63.546,
30 => 65.38,
31 => 69.723,
32 => 72.630,
33 => 74.921595,
34 => 78.96,
35 => 79.904,
36 => 83.798,
37 => 85.4678,
38 => 87.62,
39 => 88.90584,
40 => 91.224,
41 => 92.90637,
42 => 95.95,
43 => 98.0,
44 => 101.07,
45 => 102.90550,
46 => 106.42,
47 => 107.8682,
48 => 112.414,
49 => 114.818,
50 => 118.710,
51 => 121.760,
52 => 127.60,
53 => 126.90447,
54 => 131.293,
55 => 132.90545196,
56 => 137.327,
57 => 138.90547,
58 => 140.116,
59 => 140.90766,
60 => 144.242,
61 => 145.0,
62 => 150.36,
63 => 151.964,
64 => 157.25,
65 => 158.92535,
66 => 162.500,
67 => 164.93033,
68 => 167.259,
69 => 168.93422,
70 => 173.054,
71 => 174.9668,
72 => 178.49,
73 => 180.94788,
74 => 183.84,
75 => 186.207,
76 => 190.23,
77 => 192.217,
78 => 195.084,
79 => 196.966569,
80 => 200.592,
81 => 204.38,
82 => 207.2,
83 => 208.98040,
_ => panic!("Unsupported atomic number {}", element.atomic_number),
impl Element {
pub fn atomic_weight(&self) -> f64 {
match self.atomic_number {
1 => 1.007825,
2 => 4.002603,
3 => 6.938,
4 => 9.0121831,
5 => 10.806,
6 => 12.011,
7 => 14.007,
8 => 15.999,
9 => 18.998403163,
10 => 20.180,
11 => 22.990,
12 => 24.305,
13 => 26.9815385,
14 => 28.085,
15 => 30.973761998,
16 => 32.06,
17 => 35.45,
18 => 39.948,
19 => 39.0983,
20 => 40.078,
21 => 44.955908,
22 => 47.867,
23 => 50.9415,
24 => 51.9961,
25 => 54.938044,
26 => 55.845,
27 => 58.933194,
28 => 58.6934,
29 => 63.546,
30 => 65.38,
31 => 69.723,
32 => 72.630,
33 => 74.921595,
34 => 78.96,
35 => 79.904,
36 => 83.798,
37 => 85.4678,
38 => 87.62,
39 => 88.90584,
40 => 91.224,
41 => 92.90637,
42 => 95.95,
43 => 98.0,
44 => 101.07,
45 => 102.90550,
46 => 106.42,
47 => 107.8682,
48 => 112.414,
49 => 114.818,
50 => 118.710,
51 => 121.760,
52 => 127.60,
53 => 126.90447,
54 => 131.293,
55 => 132.90545196,
56 => 137.327,
57 => 138.90547,
58 => 140.116,
59 => 140.90766,
60 => 144.242,
61 => 145.0,
62 => 150.36,
63 => 151.964,
64 => 157.25,
65 => 158.92535,
66 => 162.500,
67 => 164.93033,
68 => 167.259,
69 => 168.93422,
70 => 173.054,
71 => 174.9668,
72 => 178.49,
73 => 180.94788,
74 => 183.84,
75 => 186.207,
76 => 190.23,
77 => 192.217,
78 => 195.084,
79 => 196.966569,
80 => 200.592,
81 => 204.38,
82 => 207.2,
83 => 208.98040,
_ => panic!("Unsupported atomic number {}", self.atomic_number),
}
}
}

#[cfg(test)]
mod test {
use crate::ringo::molecule::model::element::Element;

#[test]
fn test_element() {
let element = Element { atomic_number: 1 };
assert_eq!(element.atomic_weight(), 1.007825);
}
}
5 changes: 2 additions & 3 deletions src/ringo/molecule/model/molecule.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::ringo::fingerprint::fingerprint::{Fingerprint, FINGERPRINT_SIZE};
use crate::ringo::math::similarity::tanimoto::tanimoto_bitset;
use crate::ringo::molecule::model::atom::Atom;
use crate::ringo::molecule::model::bond::Bond;
use crate::ringo::molecule::model::element::atomic_weight;
use crate::ringo::molecule::smiles::reader::molecule::parse_molecule;
use crate::ringo::ringo::fingerprint::{Fingerprint, FINGERPRINT_SIZE};
use fixedbitset::FixedBitSet;
use petgraph::stable_graph::{EdgeIndex, NodeIndex, StableGraph};
use petgraph::visit::EdgeRef;
Expand Down Expand Up @@ -82,7 +81,7 @@ impl Molecule {
pub fn weight(&self) -> f64 {
let mut weight: f64 = 0.0;
for atom in self.graph.node_weights() {
weight += atomic_weight(atom.element.borrow())
weight += atom.element.atomic_weight();
}
weight
}
Expand Down
2 changes: 0 additions & 2 deletions src/ringo/ringo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pub(crate) mod fingerprint;
mod index;
mod index_item;
mod search;
44 changes: 3 additions & 41 deletions src/ringo/ringo/index.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
use crate::ringo::molecule::smiles::reader::molecule::parse_molecule;
use crate::ringo::ringo::index_item::IndexItem;
use std::fs::File;
use std::io::{BufRead, BufWriter, Write};
use bincode::{encode_into_slice};
use crate::ringo::ringo::fingerprint::FINGERPRINT_SIZE;

#[cfg(windows)]
const LINE_ENDING_LENGTH: usize = 2;
#[cfg(not(windows))]
const LINE_ENDING_LENGTH: usize = 1;

fn index(smiles_file: &str) {
// open file for reading
let fi = File::open(smiles_file).expect("Could not open file");

// open binary file for index
let mut offset = 0;
let fo = File::create(smiles_file.to_owned() + ".fp");
let mut writer = BufWriter::new(fo.unwrap());

for line in std::io::BufReader::new(fi).lines() {
let line = line.unwrap();
let molecule = parse_molecule(&line).unwrap().1;
let index_item = IndexItem {
position: offset,
fingerprint: molecule.ecfp(2, 512),
};
offset += line.len() + LINE_ENDING_LENGTH;

let mut buf = vec![0u8; FINGERPRINT_SIZE / 8 + 8];

encode_into_slice(&index_item, buf.as_mut_slice(), bincode::config::standard()).unwrap();
writer.write(&buf).unwrap();
}
}

#[test]
fn test_index() {
index("molecules.smi");
}
pub(crate) mod index;
pub mod index_item;
mod main;
Loading

0 comments on commit bc6a100

Please sign in to comment.