use crate::Interactor;
use std::fs::File;
use std::io::BufReader;
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 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());
match interactor.load_structure(pdb_path.to_str().unwrap()) {
Ok(_) => {}
Err(e) => {
eprintln!("Error opening {:?}", pdb_path);
eprintln!("{:?}", e);
std::process::exit(1)
}
}
}
});
Ok(data)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_read_json_file_success() {
let json_content = r#"
[
{
"id": 1,
"target": [1, 2, 3],
"structure": "test.pdb",
"chain": "A",
"active": [],
"passive": []
}
]
"#;
let mut json_file = NamedTempFile::new().unwrap();
write!(json_file, "{}", json_content).unwrap();
let pdb_path = json_file.path().parent().unwrap().join("test.pdb");
let mut pdb_file = File::create(&pdb_path).unwrap();
write!(
pdb_file,
"ATOM 1 N MET A 1 11.111 22.222 33.333 1.00 0.00 N"
)
.unwrap();
let result = read_json_file(json_file.path().to_str().unwrap());
println!("{:?}", result);
assert!(result.is_ok());
let interactors = result.unwrap();
assert_eq!(interactors.len(), 1);
assert_eq!(interactors[0].structure(), pdb_path.to_string_lossy());
assert!(interactors[0].pdb().is_some());
}
#[test]
fn test_read_json_file_invalid_json() {
let json_content = "invalid json";
let mut json_file = NamedTempFile::new().unwrap();
write!(json_file, "{}", json_content).unwrap();
let result = read_json_file(json_file.path().to_str().unwrap());
assert!(result.is_err());
}
#[test]
fn test_read_json_file_no_structure() {
let json_content = r#"
[
{
"id": 1,
"target": [1, 2, 3],
"chain": "A",
"active": [],
"passive": []
}
]
"#;
let mut json_file = NamedTempFile::new().unwrap();
write!(json_file, "{}", json_content).unwrap();
let result = read_json_file(json_file.path().to_str().unwrap());
assert!(result.is_ok());
let interactors = result.unwrap();
assert_eq!(interactors.len(), 1);
assert!(interactors[0].structure().is_empty());
assert!(interactors[0].pdb().is_none());
}
}