66use std:: fmt;
77use std:: fs:: File ;
88use std:: io:: { self , BufReader } ;
9- use std:: path:: Path ;
9+ use std:: path:: { Path , PathBuf } ;
1010use std:: process;
1111use std:: str;
1212
@@ -29,11 +29,14 @@ pub struct Chaos {
2929
3030impl 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}
0 commit comments