Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed swarm output directory to be the current swarm id #12

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/bin/lightdock-rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ fn run() {
}
}

fn parse_swarm_id(path: &Path) -> Option<i32> {
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::<i32>().ok())
}

fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) {

let seed:u64 = match setup.seed {
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>], seed: u64, scoring: &'a Box<dyn Score>, 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
Expand All @@ -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),
}
Expand Down
4 changes: 2 additions & 2 deletions src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading