Skip to content

Commit

Permalink
Molecule.fragmented: maintain atom numbers in fragments
Browse files Browse the repository at this point in the history
do not renumber atoms for fragments
  • Loading branch information
ybyygu committed Feb 9, 2024
1 parent e6df6af commit 4b3dd17
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vecfx = { version = "0.1.2", features = ["nalgebra"] }
gut = { version = "0.4", package = "gchemol-gut" }
neighbors = { version = "0.1", package = "gchemol-neighbors" }
gchemol-geometry = { version = "0.1.1" }
gchemol-graph = "0.1.5"
gchemol-graph = "0.1.6"
gchemol-lattice = "0.1.1"

[dev-dependencies]
Expand Down
14 changes: 12 additions & 2 deletions src/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ impl Molecule {

/// Break molecule into multiple fragments based on its bonding
/// connectivity. Return molecules whole connected by bonds
/// without periodic lattice
/// without periodic lattice. The atom numbers in fragments will
/// be the same as in their parent.
pub fn fragmented(&self) -> impl Iterator<Item = Self> + '_ {
self.graph().connected_components().map(|g| Molecule::from_graph(g))
self.graph().connected_components_node_indices().map(|nodes| {
let numbers: Vec<_> = nodes.iter().map(|&n| self.atom_sn(n)).collect();
let g = self.graph().subgraph(&nodes);
Molecule::from_graph_raw(g, numbers)
})
}

/// Return the number of fragments based on bonding connectivity.
Expand Down Expand Up @@ -154,8 +159,13 @@ fn test_topo_path() {
}
mol2.rebond();
let frags = mol2.fragmented().collect_vec();

assert_eq!(frags.len(), 2);
assert_eq!(frags[0].formula(), "CH4");
assert_eq!(frags[1].formula(), "CH4");

// atom numbers in each fragment should be the same as in `mol2`
let numbers: std::collections::HashSet<_> = frags.iter().map(|frag| frag.numbers()).flatten().collect();
assert_eq!(numbers.len(), mol2.natoms());
}
// cf82e7a7 ends here

0 comments on commit 4b3dd17

Please sign in to comment.