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

Allow for relative paths in read_json_file #5

Merged
merged 2 commits into from
Jun 12, 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
25 changes: 24 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
use crate::Interactor;
use std::fs::File;
use std::io::BufReader;
use std::process;

pub fn read_json_file(file_path: &str) -> Result<Vec<Interactor>, Box<dyn std::error::Error>> {
let file = File::open(file_path)?;
let reader = BufReader::new(file);
let data: Vec<Interactor> = serde_json::from_reader(reader)?;
let mut data: Vec<Interactor> = serde_json::from_reader(reader)?;

let wd = std::path::Path::new(file_path).parent().unwrap();

data.iter_mut().for_each(|interactor| {
if !interactor.structure().is_empty() {
let pdb_path = wd.join(interactor.structure());

if pdb_path.exists() {
interactor.set_structure(pdb_path.to_str().unwrap());
} else {
eprintln!("\n### ERROR LOADING STRUCTURE ###");
eprintln!("# The file `{}` does not exist", interactor.structure());
eprintln!(
"# If you are using relative paths, they should be relative to `{}`\n",
file_path
);
process::exit(1);
}

interactor.set_structure(pdb_path.to_str().unwrap());
}
});

Ok(data)
}
8 changes: 0 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn gen_tbl(input_file: &str) {
let mut interactors = input::read_json_file(input_file).unwrap();

let wd = std::path::Path::new(input_file)
.parent()
.unwrap()
.to_str()
.unwrap();

interactors.iter_mut().for_each(|interactor| {
if !interactor.structure().is_empty() {
interactor.set_structure(format!("{}/{}", wd, interactor.structure()).as_str());

if interactor.passive_from_active() {
interactor.set_passive_from_active();
}
Expand Down