Skip to content

Commit 319b4c0

Browse files
authored
Merge pull request #12 from wjs20/change-swarm-output-directory
Changed swarm output directory to be the current swarm id
2 parents fc77b2d + 3d8e58e commit 319b4c0

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/bin/lightdock-rust.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ fn run() {
129129
}
130130
}
131131

132+
fn parse_swarm_id(path: &Path) -> Option<i32> {
133+
path.file_name()
134+
.and_then(|s| s.to_str())
135+
.and_then(|s| s.strip_prefix("initial_positions_"))
136+
.and_then(|s| s.strip_suffix(".dat"))
137+
.and_then(|s| s.parse::<i32>().ok())
138+
}
139+
132140
fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, steps: u32, method: Method) {
133141

134142
let seed:u64 = match setup.seed {
@@ -141,6 +149,16 @@ fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, step
141149
};
142150

143151
println!("Reading starting positions from {:?}", swarm_filename);
152+
let file_path = Path::new(swarm_filename);
153+
let swarm_id = parse_swarm_id(file_path).expect("Could not parse swarm from swarm filename");
154+
println!("Swarm ID {:?}", swarm_id);
155+
let swarm_directory = format!("swarm_{}", swarm_id);
156+
157+
if !fs::metadata(&swarm_directory).map(|m| m.is_dir()).unwrap_or(false) {
158+
panic!("Output directory does not exist for swarm {:?}", swarm_id);
159+
}
160+
161+
println!("Writing to swarm dir {:?}", swarm_directory);
144162
let positions = parse_input_coordinates(swarm_filename);
145163

146164
// Parse receptor input PDB structure
@@ -206,7 +224,15 @@ fn simulate(simulation_path: &str, setup: &SetupFile, swarm_filename: &str, step
206224

207225
// Glowworm Swarm Optimization algorithm
208226
println!("Creating GSO with {} glowworms", positions.len());
209-
let mut gso = GSO::new(&positions, seed, &scoring, setup.use_anm, setup.anm_rec, setup.anm_lig);
227+
let mut gso = GSO::new(
228+
&positions,
229+
seed,
230+
&scoring,
231+
setup.use_anm,
232+
setup.anm_rec,
233+
setup.anm_lig,
234+
swarm_directory
235+
);
210236

211237
// Simulate for the given steps
212238
println!("Starting optimization ({} steps)", steps);

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ use log::info;
2222
pub struct GSO<'a> {
2323
pub swarm: Swarm<'a>,
2424
pub rng: StdRng,
25+
pub output_directory: String
2526
}
2627

2728

2829
impl<'a> GSO<'a> {
2930
pub fn new(positions: &[Vec<f64>], seed: u64, scoring: &'a Box<dyn Score>, use_anm: bool,
30-
rec_num_anm: usize, lig_num_anm: usize) -> Self {
31+
rec_num_anm: usize, lig_num_anm: usize, output_directory: String) -> Self {
3132
let mut gso = GSO {
3233
swarm: Swarm::new(),
3334
rng: SeedableRng::seed_from_u64(seed),
35+
output_directory
3436
};
3537
gso.swarm.add_glowworms(positions, scoring, use_anm, rec_num_anm, lig_num_anm);
3638
gso
@@ -42,7 +44,7 @@ impl<'a> GSO<'a> {
4244
self.swarm.update_luciferin();
4345
self.swarm.movement_phase(&mut self.rng);
4446
if step % 10 == 0 || step == 1 {
45-
match self.swarm.save(step) {
47+
match self.swarm.save(step, &self.output_directory) {
4648
Ok(ok) => ok,
4749
Err(why) => panic!("Error saving GSO output: {:?}", why),
4850
}

src/swarm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ impl<'a> Swarm<'a> {
112112
}
113113
}
114114

115-
pub fn save(&mut self, step: u32) -> Result<(), Error> {
116-
let path = format!("gso_{:?}.out", step);
115+
pub fn save(&mut self, step: u32, output_directory: &str) -> Result<(), Error> {
116+
let path = format!("{}/gso_{:?}.out", output_directory, step);
117117
let mut output = File::create(path)?;
118118
writeln!(output, "#Coordinates RecID LigID Luciferin Neighbor's number Vision Range Scoring")?;
119119
for glowworm in self.glowworms.iter() {

0 commit comments

Comments
 (0)