diff --git a/src/format.rs b/src/format.rs index d05ef59..a05eced 100644 --- a/src/format.rs +++ b/src/format.rs @@ -48,12 +48,10 @@ pub trait Format { } /// Create a file at path with header and multiple records - fn create( - &self, - path: &std::path::Path, - rng: &mut rand::rngs::StdRng, - number: usize, - ) -> error::Result<()> { + fn create

(&self, path: P, rng: &mut rand::rngs::StdRng, number: usize) -> error::Result<()> + where + P: core::convert::AsRef, + { let mut output = std::io::BufWriter::new(std::fs::File::create(path)?); self.header(&mut output, rng)?; diff --git a/src/format/fasta.rs b/src/format/fasta.rs index e03c78c..381707c 100644 --- a/src/format/fasta.rs +++ b/src/format/fasta.rs @@ -1,4 +1,54 @@ //! Fasta generation +//! +//! Usage: +//! ``` +//! use biotest::Format as _; // import Format trait is required +//! +//! # fn main() -> Result<(), biotest::error::Error> { +//! let mut rng = biotest::rand(); // Create a random generator with a fixed seed +//! +//! let mut output = Vec::new(); +//! let generator = biotest::Fasta::builder().build().unwrap(); +//! +//! generator.record(&mut output, &mut rng)?; // Write one fasta record in output +//! generator.records(&mut output, &mut rng, 5)?; // Write five fasta records in output +//! +//! generator.create("test.fasta", &mut rng, 5)?; // Write five fasta record in "test.fasta" +//! # Ok(()) +//! # } +//! ``` +//! +//! Read generate follow this template +//! ```ignore +//! >{id_prefix}{id}{id_suffix} {comment_prefix}{comment}{comment_suffix} +//! {sequence} +//! ``` +//! +//! Many think could be configurable with builder patern: +//! ``` +//! use rand; +//! use rand::SeedableRng; +//! use biotest::Format; +//! +//! # fn main() -> Result<(), biotest::error::Error> { +//! let mut rng = rand::rngs::StdRng::from_entropy(); // Create a random generator with a 'random' seed +//! +//! let generator = biotest::Fasta::builder() +//! .id(biotest::values::Alphabet::Lower) // Set alphabet use to generate sequence id +//! .id_len(10) // Set length of id +//! .id_prefix(b"prefix".to_vec()) // Set read id prefix +//! .id_suffix(b"suffix".to_vec()) // Set read id prefix +//! .comment(biotest::values::Alphabet::Upper) // Set alphabet use to generate sequence comment +//! .comment_len(0) // If comment length is set to 0 prefix and suffix isn't write +//! .comment_prefix(b"prefix".to_vec()) // Set read id prefix +//! .comment_suffix(b"suffix".to_vec()) // Set read id prefix +//! .build() +//! .unwrap(); +//! +//! generator.create("test.fasta", &mut rng, 5)?; // Write five fasta record in "test.fasta" +//! # Ok(()) +//! # } +//! ``` /* std use */ diff --git a/src/format/fastq.rs b/src/format/fastq.rs index 012f830..abea96c 100644 --- a/src/format/fastq.rs +++ b/src/format/fastq.rs @@ -1,4 +1,60 @@ //! Fastq generation +//! +//! Usage: +//! ``` +//! use biotest::Format; +//! +//! # fn main() -> Result<(), biotest::error::Error> { +//! let mut rng = biotest::rand(); // Create a random generator with a fixed seed +//! +//! let mut output = Vec::new(); +//! let generator = biotest::Fastq::builder().build().unwrap(); +//! +//! generator.record(&mut output, &mut rng)?; // Write one fastq record in output +//! generator.records(&mut output, &mut rng, 5)?; // Write five fastq records in output +//! +//! generator.create("test.fastq", &mut rng, 5)?; // Write five fastq record in "test.fasta" +//! # Ok(()) +//! # } +//! ``` +//! +//! Read generate follow this template +//! ```ignore +//! >{id_prefix}{id}{id_suffix} {comment_prefix}{comment}{comment_suffix} +//! {sequence} +//! +{plus_prefix}{plus}{plus_suffix} +//! {quality} +//! ``` +//! +//! Many think could be configurable with builder patern: +//! ``` +//! use rand; +//! use rand::SeedableRng; +//! use biotest::Format; +//! +//! # fn main() -> Result<(), biotest::error::Error> { +//! let mut rng = rand::rngs::StdRng::from_entropy(); // Create a random generator with a 'random' seed +//! +//! let generator = biotest::Fastq::builder() +//! .id(biotest::values::Alphabet::Lower) // Set alphabet use to generate sequence id +//! .id_len(10) // Set length of id +//! .id_prefix(b"prefix".to_vec()) // Set read id prefix +//! .id_suffix(b"suffix".to_vec()) // Set read id prefix +//! .comment(biotest::values::Alphabet::Upper) // Set alphabet use to generate sequence comment +//! .comment_len(0) // If comment length is set to 0 prefix and suffix isn't write +//! .comment_prefix(b"prefix".to_vec()) // Set read id prefix +//! .comment_suffix(b"suffix".to_vec()) // Set read id prefix +//! .plus(biotest::values::Alphabet::Upper) // Set alphabet use to generate sequence plus +//! .plus_len(0) // If plus length is set to 0 prefix and suffix isn't write +//! .plus_prefix(b"prefix".to_vec()) // Set read id prefix +//! .plus_suffix(b"suffix".to_vec()) // Set read id prefix +//! .build() +//! .unwrap(); +//! +//! generator.create("test.fastq", &mut rng, 5)?; // Write five fasta record in "test.fastq" +//! # Ok(()) +//! # } +//! ``` /* std use */ diff --git a/src/lib.rs b/src/lib.rs index e8c4f1c..b204fe8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,17 +21,19 @@ pub mod constants; pub mod values; #[macro_use] pub mod error; -mod format; +pub mod format; /* reexport */ +pub use format::Format; + #[cfg(feature = "fasta")] -pub use format::fasta; +pub use format::fasta::Fasta; #[cfg(feature = "fastq")] -pub use format::fastq; +pub use format::fastq::Fastq; #[cfg(feature = "vcf")] -pub use format::vcf; +pub use format::vcf::Vcf; /// Create a random generator with [constants::SEED] pub fn rand() -> rand::rngs::StdRng {