Skip to content

Commit

Permalink
#3: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mkviatkovskii committed Jul 6, 2024
1 parent 1a6b78d commit 14763ba
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/ringo/math.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod similarity;
pub mod similarity;
5 changes: 3 additions & 2 deletions src/ringo/math/similarity/tanimoto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use fixedbitset::FixedBitSet;
pub fn tanimoto_bitset(a: &FixedBitSet, b: &FixedBitSet) -> f32 {
let mut and_ = a.clone();
and_.intersect_with(b);
return and_.count_ones(..) as f32 / (a.count_ones(..) + b.count_ones(..) - and_.count_ones(..)) as f32;
return and_.count_ones(..) as f32
/ (a.count_ones(..) + b.count_ones(..) - and_.count_ones(..)) as f32;
}

#[cfg(test)]
mod tests {
use crate::ringo::math::similarity::tanimoto::tanimoto_bitset;
use fixedbitset::FixedBitSet;
use crate::ringo::math::similarity::tanimoto::{tanimoto_bitset};

#[test]
fn test_tanimoto_bitset_033() {
Expand Down
30 changes: 21 additions & 9 deletions src/ringo/molecule/model/molecule.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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;
use fixedbitset::FixedBitSet;
use petgraph::stable_graph::{EdgeIndex, NodeIndex, StableGraph};
use petgraph::visit::EdgeRef;
use petgraph::Undirected;
use std::borrow::Borrow;
use std::collections::{BTreeSet};
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeSet;
use std::hash::Hasher;
use fixedbitset::FixedBitSet;
use crate::ringo::math::similarity::tanimoto::tanimoto_bitset;
use crate::ringo::molecule::smiles::reader::molecule::parse_molecule;

pub struct Molecule {
graph: StableGraph<Atom, Bond, Undirected>,
Expand Down Expand Up @@ -92,7 +92,15 @@ impl Molecule {
let mut fp = FixedBitSet::new();

for node in self.graph.node_indices() {
ecfp_recursive(&self.graph, radius, 1, node, &mut fp, fp_length, &mut DefaultHasher::new());
ecfp_recursive(
&self.graph,
radius,
1,
node,
&mut fp,
fp_length,
&mut DefaultHasher::new(),
);
}

Fingerprint(fp)
Expand All @@ -108,7 +116,6 @@ fn ecfp_recursive(
fp_length: usize,
hasher: &mut DefaultHasher,
) {

if depth > radius {
return;
}
Expand Down Expand Up @@ -136,11 +143,16 @@ fn ecfp_recursive(
}
}


#[test]
fn test_ecfp() {
let ecfp_ibuprofen = parse_molecule("CC(C)CC1=CC=C(C=C1)C(C)C(=O)O").unwrap().1.ecfp(2, 128);
let ecfp_naproxen = parse_molecule("CC(C1=CC2=C(C=C1)C=C(C=C2)OC)C(=O)O").unwrap().1.ecfp(2, 128);
let ecfp_ibuprofen = parse_molecule("CC(C)CC1=CC=C(C=C1)C(C)C(=O)O")
.unwrap()
.1
.ecfp(2, 128);
let ecfp_naproxen = parse_molecule("CC(C1=CC2=C(C=C1)C=C(C=C2)OC)C(=O)O")
.unwrap()
.1
.ecfp(2, 128);
let sim = tanimoto_bitset(&ecfp_ibuprofen.0, &ecfp_naproxen.0);
assert!(0.53 < sim && sim < 0.54);
}
97 changes: 78 additions & 19 deletions src/ringo/molecule/smiles/reader/molecule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub(crate) fn parse_molecule(input: &str) -> IResult<&str, Molecule> {
let mut prev_node = NodeIndex::end();
let mut prev_bond = BondOrder::Single;


for (atom, bond, cycle_digit, open_paren) in atoms_and_bonds {
if let Some(open) = open_paren {
if open {
Expand Down Expand Up @@ -223,11 +222,15 @@ mod tests {
7
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1))
.unwrap()
.order,
BondOrder::Double
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(2)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(2))
.unwrap()
.order,
BondOrder::Single
);
}
Expand All @@ -250,11 +253,15 @@ mod tests {
7
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1))
.unwrap()
.order,
BondOrder::Double
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(2)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(2))
.unwrap()
.order,
BondOrder::Double
);
}
Expand Down Expand Up @@ -285,19 +292,27 @@ mod tests {
7
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1))
.unwrap()
.order,
BondOrder::Double
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(2)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(2))
.unwrap()
.order,
BondOrder::Double
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(3)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(3))
.unwrap()
.order,
BondOrder::Single
);
assert_eq!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(4)).unwrap().order,
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(4))
.unwrap()
.order,
BondOrder::Single
);
}
Expand All @@ -324,16 +339,35 @@ mod tests {
16
);
assert!(m.has_bond(NodeIndex::new(0), NodeIndex::new(1)));
assert!(m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1)).unwrap().order == BondOrder::Single);
assert!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1))
.unwrap()
.order
== BondOrder::Single
);
assert!(m.has_bond(NodeIndex::new(1), NodeIndex::new(2)));
assert!(m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(2)).unwrap().order == BondOrder::Single);
assert!(
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(2))
.unwrap()
.order
== BondOrder::Single
);
assert!(m.has_bond(NodeIndex::new(0), NodeIndex::new(2)));
assert!(m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(2)).unwrap().order == BondOrder::Double);
assert!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(2))
.unwrap()
.order
== BondOrder::Double
);
assert!(m.has_bond(NodeIndex::new(2), NodeIndex::new(3)));
assert!(m.get_bond_by_atoms(NodeIndex::new(2), NodeIndex::new(3)).unwrap().order == BondOrder::Single);
assert!(
m.get_bond_by_atoms(NodeIndex::new(2), NodeIndex::new(3))
.unwrap()
.order
== BondOrder::Single
);
}


#[test]
fn parse_molecule_cycle_branch() {
let m = parse_molecule("N1C(=P)S=1O").unwrap().1;
Expand All @@ -360,15 +394,40 @@ mod tests {
8
);
assert!(m.has_bond(NodeIndex::new(0), NodeIndex::new(1)));
assert!(m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1)).unwrap().order == BondOrder::Single);
assert!(
m.get_bond_by_atoms(NodeIndex::new(0), NodeIndex::new(1))
.unwrap()
.order
== BondOrder::Single
);
assert!(m.has_bond(NodeIndex::new(1), NodeIndex::new(2)));
assert!(m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(2)).unwrap().order == BondOrder::Double);
assert!(
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(2))
.unwrap()
.order
== BondOrder::Double
);
assert!(m.has_bond(NodeIndex::new(1), NodeIndex::new(3)));
assert!(m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(3)).unwrap().order == BondOrder::Single);
assert!(
m.get_bond_by_atoms(NodeIndex::new(1), NodeIndex::new(3))
.unwrap()
.order
== BondOrder::Single
);
assert!(m.has_bond(NodeIndex::new(3), NodeIndex::new(4)));
assert!(m.get_bond_by_atoms(NodeIndex::new(3), NodeIndex::new(4)).unwrap().order == BondOrder::Single);
assert!(
m.get_bond_by_atoms(NodeIndex::new(3), NodeIndex::new(4))
.unwrap()
.order
== BondOrder::Single
);
assert!(m.has_bond(NodeIndex::new(3), NodeIndex::new(0)));
assert!(m.get_bond_by_atoms(NodeIndex::new(3), NodeIndex::new(0)).unwrap().order == BondOrder::Double);
assert!(
m.get_bond_by_atoms(NodeIndex::new(3), NodeIndex::new(0))
.unwrap()
.order
== BondOrder::Double
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/ringo/ringo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) mod fingerprint;
mod index;
mod search;
mod index_item;
pub(crate) mod fingerprint;
mod search;
7 changes: 5 additions & 2 deletions src/ringo/ringo/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<'de> bincode::BorrowDecode<'de> for Fingerprint {

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

#[test]
fn test_fingerprint_encode_decode() {
Expand All @@ -42,7 +42,10 @@ mod tests {
fp.0.set(17, true);

let encoded = bincode::encode_to_vec(&fp, bincode::config::standard()).unwrap();
let decoded: Fingerprint = bincode::decode_from_slice(&encoded, bincode::config::standard()).unwrap().0;
let decoded: Fingerprint =
bincode::decode_from_slice(&encoded, bincode::config::standard())
.unwrap()
.0;
assert_eq!(decoded.0.ones().collect::<Vec<usize>>(), vec![1, 17]);
}
}
10 changes: 6 additions & 4 deletions src/ringo/ringo/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::File;
use std::io::{BufRead};
use crate::ringo::molecule::smiles::reader::molecule::parse_molecule;
use crate::ringo::ringo::index_item::IndexItem;
use std::fs::File;
use std::io::BufRead;

fn index(smiles_file: &str) {
// open file for reading
Expand All @@ -13,12 +13,14 @@ fn index(smiles_file: &str) {
for line in std::io::BufReader::new(fi).lines() {
let line = line.unwrap();
let molecule = parse_molecule(&line).unwrap().1;
IndexItem{position: offset, fingerprint: molecule.ecfp(2, 512)};
IndexItem {
position: offset,
fingerprint: molecule.ecfp(2, 512),
};
offset += line.len() + 1;
}
}


#[test]
fn test_index() {
index("molecules.smi");
Expand Down
20 changes: 13 additions & 7 deletions src/ringo/ringo/index_item.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
use bincode::{Decode, Encode};
use crate::ringo::ringo::fingerprint::Fingerprint;
use bincode::{Decode, Encode};

#[derive(Debug, Encode, Decode)]
pub struct IndexItem {
pub position: usize,
pub fingerprint: Fingerprint
pub fingerprint: Fingerprint,
}

#[cfg(test)]
mod tests {
use crate::ringo::ringo::fingerprint::Fingerprint;
use crate::ringo::ringo::index_item::IndexItem;
use bincode::config::standard;
use bincode::{decode_from_slice, encode_to_vec};
use fixedbitset::FixedBitSet;
use crate::ringo::ringo::index_item::IndexItem;
use crate::ringo::ringo::fingerprint::Fingerprint;

#[test]
fn test_index_item_encode_decode() {
let fp = Fingerprint(FixedBitSet::with_capacity(512));
let mut ii = IndexItem {position: 0, fingerprint: fp};
let mut ii = IndexItem {
position: 0,
fingerprint: fp,
};
ii.position = 0;
ii.fingerprint.0.set(1, true);
ii.fingerprint.0.set(17, true);

let encoded = encode_to_vec(&ii, standard()).unwrap();
let decoded: IndexItem = decode_from_slice(&encoded, standard()).unwrap().0;
assert_eq!(decoded.fingerprint.0.ones().collect::<Vec<usize>>(), vec![1, 17]);
assert_eq!(
decoded.fingerprint.0.ones().collect::<Vec<usize>>(),
vec![1, 17]
);
}
}
}
1 change: 1 addition & 0 deletions src/ringo/ringo/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 14763ba

Please sign in to comment.