diff --git a/src/io.rs b/src/io.rs index b3361a9..f753709 100644 --- a/src/io.rs +++ b/src/io.rs @@ -7,6 +7,7 @@ use std::io::{BufReader,BufWriter}; use k_B; use std::path::Path; use super::errors::*; +use f64; // Returns the path to path2 relative to path1 // path1: "path/to/file.dat" @@ -112,7 +113,7 @@ fn is_in_hist_boundaries(values: &Vec, cfg: &Config) -> bool { // parse a timeseries file into a histogram fn read_window_file(window_file: &str, cfg: &Config) -> Result { let f = File::open(window_file) - .chain_err(|| format!("Failed to open sample data file {}", window_file))?; + .chain_err(|| format!("Failed to open sample data file {}.", window_file))?; let mut buf = BufReader::new(&f); // total number of bins is the product of all dimensions length @@ -126,7 +127,9 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Result { // read and parse each timeseries line let mut line = String::new(); + let mut linecount = 0; while buf.read_line(&mut line).chain_err(|| "Failed to read line")? > 0 { + linecount += 1; // skip comments and empty lines if line.starts_with("#") || line.starts_with("@") || line.len() == 0 { line.clear(); @@ -134,12 +137,22 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Result { } { - let mut split = line.split_whitespace(); - split.next(); // skip time/step column - let values: Vec = (0..cfg.dimens).collect::>().iter().map(|_| { - split.next().unwrap().parse::().unwrap() - }).collect(); + let split: Vec<&str> = line.split_whitespace().collect(); + if split.len() < cfg.dimens+1 { + bail!(format!("Wrong number of columns in line {} of window file {}. Empty Line?.", linecount, window_file)); + } + + let mut values: Vec = vec![f64::NAN; cfg.dimens]; + for i in 0..values.len() { + values[i] = split[i+1].parse::() + .chain_err(|| format!("Failed to parse line {} of window file {}.", linecount, window_file))?; + } + println!("{:?}", values); + + // (1..cfg.dimens).collect::>().iter().map(|_| { + // split.next().unwrap().parse::().unwrap() + // }).collect(); if is_in_hist_boundaries(&values, cfg) { let bin_indeces = (0..cfg.dimens).map(|dimen: usize| { diff --git a/tests/COLVAR_unparseable_1.xvg b/tests/COLVAR_unparseable_1.xvg new file mode 100644 index 0000000..0f62fde --- /dev/null +++ b/tests/COLVAR_unparseable_1.xvg @@ -0,0 +1,11 @@ +84.840004 -2.878293 +84.860004 -2.827750 +84.880004 -2.962375 +84.900004 +84.920004 -2.748589 +84.940004 -2.726197 +84.960004 -2.802982 +84.980004 -2.771980 +85.000004 -2.706482 +85.020004 -2.776983 +85.040004 -2.883180 \ No newline at end of file diff --git a/tests/COLVAR_unparseable_2.xvg b/tests/COLVAR_unparseable_2.xvg new file mode 100644 index 0000000..dffbbdb --- /dev/null +++ b/tests/COLVAR_unparseable_2.xvg @@ -0,0 +1,11 @@ +84.840004 -2.878293 +84.860004 -2.827750 +84.880004 -2.962375 +84.900004 qwee +84.920004 -2.748589 +84.940004 -2.726197 +84.960004 -2.802982 +84.980004 -2.771980 +85.000004 -2.706482 +85.020004 -2.776983 +85.040004 -2.883180 \ No newline at end of file diff --git a/tests/integration.rs b/tests/integration.rs index 1be8480..0e4422f 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -77,6 +77,36 @@ mod integration { )); } + #[test] + fn unparseable_timeseries() { + let output = Command::new("./target/debug/wham") + .args(&["--bins", "100", "--min", "-3.0", "--max", "3.0", "-T", "300"]) + .args(&["-f", "tests/metadata_unparseable3.dat"]) + .args(&["-o", "/dev/null"]) + .output() + .expect("failed to execute process"); + let output = String::from_utf8_lossy(&output.stderr); + println!("{}", output); + assert!(output.to_string().contains( + "Failed to parse line" + )); + } + + #[test] + fn unparseable_timeseries_empty() { + let output = Command::new("./target/debug/wham") + .args(&["--bins", "100", "--min", "-3.0", "--max", "3.0", "-T", "300"]) + .args(&["-f", "tests/metadata_unparseable4.dat"]) + .args(&["-o", "/dev/null"]) + .output() + .expect("failed to execute process"); + let output = String::from_utf8_lossy(&output.stderr); + println!("{}", output); + assert!(output.to_string().contains( + "Wrong number of columns in line" + )); + } + #[test] fn wham_1d() {; Command::new("./target/debug/wham") diff --git a/tests/metadata_unparseable3.dat b/tests/metadata_unparseable3.dat new file mode 100644 index 0000000..28d8937 --- /dev/null +++ b/tests/metadata_unparseable3.dat @@ -0,0 +1 @@ +COLVAR_unparseable_2.xvg -3.0 100 diff --git a/tests/metadata_unparseable4.dat b/tests/metadata_unparseable4.dat new file mode 100644 index 0000000..3478746 --- /dev/null +++ b/tests/metadata_unparseable4.dat @@ -0,0 +1 @@ +COLVAR_unparseable_1.xvg -3.0 100