From 6975ae9730d1b7c7a91822d4f0b09111d9443189 Mon Sep 17 00:00:00 2001 From: William Steele Date: Sun, 10 Mar 2024 18:08:57 +0000 Subject: [PATCH 1/2] Changed swarm output directory to be the current swarm id --- src/bin/lightdock-rust.rs | 26 +++++++++++++++++++++++++- src/lib.rs | 6 ++++-- src/swarm.rs | 4 ++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/bin/lightdock-rust.rs b/src/bin/lightdock-rust.rs index ccad741..9a10a48 100644 --- a/src/bin/lightdock-rust.rs +++ b/src/bin/lightdock-rust.rs @@ -124,6 +124,13 @@ fn run() { } } +fn parse_swarm_id(filename: &str) -> Option { + filename + .strip_prefix("initial_positions_") + .and_then(|s| s.strip_suffix(".dat")) + .and_then(|s| s.parse::().ok()) +} + fn simulate(setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) { let seed:u64 = match setup.seed { @@ -136,6 +143,15 @@ fn simulate(setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) }; println!("Reading starting positions from {:?}", swarm_filename); + let swarm_id = parse_swarm_id(swarm_filename).expect("Could not parse swarm from swarm filename"); + println!("Swarm ID {:?}", swarm_id); + let swarm_directory = format!("swarm_{}", swarm_id); + + if !fs::metadata(&swarm_directory).map(|m| m.is_dir()).unwrap_or(false) { + panic!("Output directory does not exist for swarm {:?}", swarm_id); + } + + println!("Writing to swarm dir {:?}", swarm_directory); let positions = parse_input_coordinates(swarm_filename); // Parse receptor input PDB structure @@ -199,7 +215,15 @@ fn simulate(setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) // Glowworm Swarm Optimization algorithm println!("Creating GSO with {} glowworms", positions.len()); - let mut gso = GSO::new(&positions, seed, &scoring, setup.use_anm, setup.anm_rec, setup.anm_lig); + let mut gso = GSO::new( + &positions, + seed, + &scoring, + setup.use_anm, + setup.anm_rec, + setup.anm_lig, + swarm_directory + ); // Simulate for the given steps println!("Starting optimization ({} steps)", steps); diff --git a/src/lib.rs b/src/lib.rs index a088613..b10aaef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,15 +20,17 @@ use scoring::Score; pub struct GSO<'a> { pub swarm: Swarm<'a>, pub rng: StdRng, + pub output_directory: String } impl<'a> GSO<'a> { pub fn new(positions: &[Vec], seed: u64, scoring: &'a Box, use_anm: bool, - rec_num_anm: usize, lig_num_anm: usize) -> Self { + rec_num_anm: usize, lig_num_anm: usize, output_directory: String) -> Self { let mut gso = GSO { swarm: Swarm::new(), rng: SeedableRng::seed_from_u64(seed), + output_directory }; gso.swarm.add_glowworms(positions, scoring, use_anm, rec_num_anm, lig_num_anm); gso @@ -40,7 +42,7 @@ impl<'a> GSO<'a> { self.swarm.update_luciferin(); self.swarm.movement_phase(&mut self.rng); if step % 10 == 0 || step == 1 { - match self.swarm.save(step) { + match self.swarm.save(step, &self.output_directory) { Ok(ok) => ok, Err(why) => panic!("Error saving GSO output: {:?}", why), } diff --git a/src/swarm.rs b/src/swarm.rs index e2643be..3d09102 100644 --- a/src/swarm.rs +++ b/src/swarm.rs @@ -112,8 +112,8 @@ impl<'a> Swarm<'a> { } } - pub fn save(&mut self, step: u32) -> Result<(), Error> { - let path = format!("gso_{:?}.out", step); + pub fn save(&mut self, step: u32, output_directory: &str) -> Result<(), Error> { + let path = format!("{}/gso_{:?}.out", output_directory, step); let mut output = File::create(path)?; writeln!(output, "#Coordinates RecID LigID Luciferin Neighbor's number Vision Range Scoring")?; for glowworm in self.glowworms.iter() { From edb6c37d6d6148f2258f2ec83567f943ed6b71fc Mon Sep 17 00:00:00 2001 From: William Steele Date: Mon, 11 Mar 2024 09:26:45 +0000 Subject: [PATCH 2/2] Fixed issue with nested paths --- src/bin/lightdock-rust.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin/lightdock-rust.rs b/src/bin/lightdock-rust.rs index 9a10a48..13a8e2c 100644 --- a/src/bin/lightdock-rust.rs +++ b/src/bin/lightdock-rust.rs @@ -124,9 +124,10 @@ fn run() { } } -fn parse_swarm_id(filename: &str) -> Option { - filename - .strip_prefix("initial_positions_") +fn parse_swarm_id(path: &Path) -> Option { + path.file_name() + .and_then(|s| s.to_str()) + .and_then(|s| s.strip_prefix("initial_positions_")) .and_then(|s| s.strip_suffix(".dat")) .and_then(|s| s.parse::().ok()) } @@ -143,7 +144,8 @@ fn simulate(setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) }; println!("Reading starting positions from {:?}", swarm_filename); - let swarm_id = parse_swarm_id(swarm_filename).expect("Could not parse swarm from swarm filename"); + let file_path = Path::new(swarm_filename); + let swarm_id = parse_swarm_id(file_path).expect("Could not parse swarm from swarm filename"); println!("Swarm ID {:?}", swarm_id); let swarm_directory = format!("swarm_{}", swarm_id);