Skip to content

Commit

Permalink
doc: Add usage for fasta and fastq generator
Browse files Browse the repository at this point in the history
  • Loading branch information
natir committed Apr 22, 2024
1 parent a230299 commit ca2adcf
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(&self, path: P, rng: &mut rand::rngs::StdRng, number: usize) -> error::Result<()>
where
P: core::convert::AsRef<std::path::Path>,
{
let mut output = std::io::BufWriter::new(std::fs::File::create(path)?);

self.header(&mut output, rng)?;
Expand Down
50 changes: 50 additions & 0 deletions src/format/fasta.rs
Original file line number Diff line number Diff line change
@@ -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 */

Expand Down
56 changes: 56 additions & 0 deletions src/format/fastq.rs
Original file line number Diff line number Diff line change
@@ -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 */

Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ca2adcf

Please sign in to comment.