1- use std:: fs:: File ;
2- use std:: io;
3- use std:: io:: Write ;
4-
51extern crate thiserror;
62use thiserror:: Error ;
73
@@ -21,30 +17,26 @@ impl ReadPrediction {
2117 }
2218 }
2319
24- pub fn print_meta ( & self , file : & mut File ) -> Result < ( ) , GeneError > {
20+ pub fn meta ( & self , buf : & mut Vec < u8 > ) -> Result < ( ) , GeneError > {
2521 if !self . genes . is_empty ( ) {
26- file . write_all ( & format ! ( ">{}\n " , std:: str :: from_utf8( & self . head) ?) . into_bytes ( ) ) ? ;
22+ buf . append ( & mut format ! ( ">{}\n " , std:: str :: from_utf8( & self . head) ?) . into_bytes ( ) )
2723 }
2824 for gene in & self . genes {
29- gene. print_meta ( file ) ? ;
25+ gene. meta ( buf ) ;
3026 }
3127 Ok ( ( ) )
3228 }
3329
34- pub fn print_dna ( & self , file : & mut File , formatted : bool ) -> Result < ( ) , GeneError > {
30+ pub fn dna ( & self , buf : & mut Vec < u8 > , formatted : bool ) -> Result < ( ) , GeneError > {
3531 for gene in & self . genes {
36- gene. print_dna ( file , & self . head , formatted) ?;
32+ gene. dna ( buf , & self . head , formatted) ?;
3733 }
3834 Ok ( ( ) )
3935 }
4036
41- pub fn print_protein < W : Write > (
42- & self ,
43- whole_genome : bool ,
44- file : & mut W ,
45- ) -> Result < ( ) , GeneError > {
37+ pub fn protein ( & self , buf : & mut Vec < u8 > , whole_genome : bool ) -> Result < ( ) , GeneError > {
4638 for gene in & self . genes {
47- gene. print_protein ( file , & self . head , whole_genome) ?;
39+ gene. protein ( buf , & self . head , whole_genome) ?;
4840 }
4941 Ok ( ( ) )
5042 }
@@ -63,9 +55,9 @@ pub struct Gene {
6355}
6456
6557impl Gene {
66- pub fn print_meta ( & self , file : & mut File ) -> Result < ( ) , GeneError > {
67- file . write_all (
68- & format ! (
58+ pub fn meta ( & self , buf : & mut Vec < u8 > ) {
59+ buf . append (
60+ & mut format ! (
6961 "{}\t {}\t {}\t {}\t {:.6}\t I:{}\t D:{}\n " ,
7062 self . metastart,
7163 self . end,
@@ -82,16 +74,10 @@ impl Gene {
8274 . collect:: <String >( )
8375 )
8476 . into_bytes ( ) ,
85- ) ?;
86- Ok ( ( ) )
77+ ) ;
8778 }
8879
89- pub fn print_dna (
90- & self ,
91- file : & mut File ,
92- head : & Vec < u8 > ,
93- formatted : bool ,
94- ) -> Result < ( ) , GeneError > {
80+ pub fn dna ( & self , buf : & mut Vec < u8 > , head : & Vec < u8 > , formatted : bool ) -> Result < ( ) , GeneError > {
9581 let dna: Vec < u8 > = match ( self . forward_strand , formatted) {
9682 ( true , true ) => self . dna . iter ( ) . map ( |& n| u8:: from ( n) ) . collect ( ) ,
9783 ( true , false ) => self
@@ -110,8 +96,8 @@ impl Gene {
11096 . collect ( ) ,
11197 } ;
11298
113- file . write_all (
114- & format ! (
99+ buf . append (
100+ & mut format ! (
115101 ">{}_{}_{}_{}\n {}\n " ,
116102 std:: str :: from_utf8( head) ?,
117103 self . start,
@@ -120,14 +106,14 @@ impl Gene {
120106 std:: str :: from_utf8( & dna) ?,
121107 )
122108 . into_bytes ( ) ,
123- ) ? ;
109+ ) ;
124110
125111 Ok ( ( ) )
126112 }
127113
128- pub fn print_protein < W : Write > (
114+ pub fn protein (
129115 & self ,
130- file : & mut W ,
116+ buf : & mut Vec < u8 > ,
131117 head : & Vec < u8 > ,
132118 whole_genome : bool ,
133119 ) -> Result < ( ) , GeneError > {
@@ -167,8 +153,8 @@ impl Gene {
167153 }
168154 }
169155
170- file . write_all (
171- & format ! (
156+ buf . append (
157+ & mut format ! (
172158 ">{}_{}_{}_{}\n {}\n " ,
173159 std:: str :: from_utf8( head) ?,
174160 self . start,
@@ -177,15 +163,13 @@ impl Gene {
177163 std:: str :: from_utf8( & protein) ?,
178164 )
179165 . into_bytes ( ) ,
180- ) ? ;
166+ ) ;
181167 Ok ( ( ) )
182168 }
183169}
184170
185171#[ derive( Error , Debug ) ]
186172pub enum GeneError {
187- #[ error( "could not write to file" ) ]
188- IoError ( #[ from] io:: Error ) ,
189173 #[ error( "could not convert header back to UTF-8" ) ]
190174 Utf8Error ( #[ from] std:: str:: Utf8Error ) ,
191175}
0 commit comments