diff --git a/src/bin/lightdock-rust.rs b/src/bin/lightdock-rust.rs index 712f43b..202e9b4 100644 --- a/src/bin/lightdock-rust.rs +++ b/src/bin/lightdock-rust.rs @@ -129,6 +129,14 @@ fn run() { } } +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()) +} + fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) { let seed:u64 = match setup.seed { @@ -141,6 +149,16 @@ fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, step }; println!("Reading starting positions 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); + + 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 @@ -206,7 +224,15 @@ fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, step // 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 ce63a9a..3315664 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,15 +22,17 @@ use log::info; 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 @@ -42,7 +44,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 cd45080..2b13846 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() {