diff --git a/Cargo.toml b/Cargo.toml index 3fe0ba4..0389f71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/topology.rs b/src/topology.rs index a6d418a..9d27db5 100644 --- a/src/topology.rs +++ b/src/topology.rs @@ -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 + '_ { - 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. @@ -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