Skip to content

Commit 53222d1

Browse files
committed
fix: changes draw output from dir to file
1 parent 52edef0 commit 53222d1

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/cgr.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::fmt;
77
use std::fs::File;
88
use std::io::{self, BufReader};
9-
use std::path::Path;
9+
use std::path::{Path, PathBuf};
1010
use std::process;
1111
use std::str;
1212

@@ -29,11 +29,14 @@ pub struct Chaos {
2929

3030
impl Chaos {
3131
/// Draws the CGR and saves it as a PNG file
32-
fn draw(&self, outdir: &Path) -> anyhow::Result<()> {
33-
let png = format!("{}.png", self.id);
34-
let opath = outdir.join(&png);
32+
fn draw(&self, output: Option<PathBuf>) -> anyhow::Result<()> {
33+
let image = if let Some(out) = output {
34+
out
35+
} else {
36+
PathBuf::from(format!("{}.png", self.id))
37+
};
3538

36-
let root_area = BitMapBackend::new(&opath, (1024, 768)).into_drawing_area();
39+
let root_area = BitMapBackend::new(&image, (1024, 768)).into_drawing_area();
3740
root_area.fill(&WHITE)?;
3841

3942
let mut ctx = ChartBuilder::on(&root_area)
@@ -85,15 +88,15 @@ impl DnaToChaos for fasta::Record {
8588
}
8689

8790
/// Reads a FASTA file, generates its CGR, and saves it as an image.
88-
pub fn draw<R: io::Read>(source: R, destination: &Path) -> anyhow::Result<String> {
91+
pub fn draw<R: io::Read>(source: R, destination: Option<PathBuf>) -> anyhow::Result<()> {
8992
let mut reader = fasta::Reader::new(BufReader::new(source));
9093

91-
for record in reader.records() {
92-
let chaos = record?.record_to_chaos();
93-
chaos.draw(destination)?;
94+
for result in reader.records() {
95+
let record = result?;
96+
let chaos = record.record_to_chaos();
97+
chaos.draw(destination.clone())?;
9498
}
95-
96-
Err(anyhow::anyhow!("No records found in FASTA file."))
99+
Ok(())
97100
}
98101

99102
/// Structure to store SSIM results
@@ -145,12 +148,14 @@ pub fn compare_genomes(query: &str, reference: &str) -> anyhow::Result<SSIMResul
145148
let attr = dssim_core::Dssim::new();
146149
let mut result = SSIMResult::new();
147150

148-
let qimg = draw(File::open(query)?, dir.path())?;
149-
let rimg = draw(File::open(reference)?, dir.path())?;
151+
let qimg_out = PathBuf::from(format!("{:?}/query.png", dir.path()));
152+
let rimg_out = PathBuf::from(format!("{:?}/reference.png", dir.path()));
153+
draw(File::open(query)?, Some(qimg_out.clone()))?;
154+
draw(File::open(reference)?, Some(rimg_out.clone()))?;
150155

151156
// Read images
152-
let qimage = utils::get_image(&dir.path().join(&qimg))?;
153-
let rimage = utils::get_image(&dir.path().join(&rimg))?;
157+
let qimage = utils::get_image(&qimg_out)?;
158+
let rimage = utils::get_image(&rimg_out)?;
154159

155160
if utils::is_same_width_height(&qimage, &rimage) {
156161
let (dssim, _) = attr.compare(&qimage.0, &rimage.0);
@@ -205,11 +210,11 @@ mod tests {
205210
],
206211
};
207212

208-
let ot = Path::new(odir);
213+
let ot = PathBuf::from(odir);
209214
std::fs::create_dir(&ot).unwrap();
210215

211-
chaos.draw(&ot).unwrap();
216+
chaos.draw(Some(ot.clone())).unwrap();
212217

213-
fs::remove_dir_all(&ot).unwrap();
218+
fs::remove_dir_all(ot).unwrap();
214219
}
215220
}

src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ pub struct DrawArgs {
7878
/// Input sequence file in FASTA format
7979
pub file: PathBuf,
8080

81-
/// Output directory for images
82-
#[arg(short)]
81+
/// Output file
82+
#[arg(short, value_parser = must_not_exist)]
8383
pub output: Option<PathBuf>,
8484
}
8585

src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ fn main() -> anyhow::Result<()> {
111111
}
112112
Commands::Draw(args) => {
113113
let source = File::open(args.file)?;
114-
let out = args.output.unwrap_or(
115-
std::env::current_dir().expect("Currrent working directory should be valid"),
116-
);
117-
cgr::draw(source, &out)?;
114+
cgr::draw(source, args.output)?
118115
}
119116
Commands::Compare(args) => {
120117
let mut qfiles = Vec::new();

0 commit comments

Comments
 (0)