Skip to content

Commit

Permalink
feat: use NonZeroUsize for Number of Records
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Mar 22, 2024
1 parent 451a2a4 commit aa0880d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/convert/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Functionality related to the `ngs convert` command itself.

use std::num::NonZeroUsize;
use std::path::PathBuf;

use anyhow::bail;
Expand Down Expand Up @@ -31,8 +32,8 @@ pub struct ConvertArgs {
to: PathBuf,

/// Number of records to process before exiting the conversion.
#[arg(short = 'n', long, value_name = "USIZE")]
num_records: Option<usize>,
#[arg(short = 'n', long, value_name = "NonZeroUsize")]
num_records: Option<NonZeroUsize>,

/// If available, the FASTA reference file used to generate the file.
#[arg(short, long)]
Expand Down
5 changes: 3 additions & 2 deletions src/derive/command/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use noodles::bam;
use num_format::{Locale, ToFormattedString};
use std::collections::HashSet;
use std::io::BufReader;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use tracing::info;

Expand All @@ -21,8 +22,8 @@ pub struct DeriveEncodingArgs {
src: PathBuf,

/// Examine the first `n` records in the file.
#[arg(short, long, value_name = "USIZE")]
num_records: Option<usize>,
#[arg(short, long, value_name = "NonZeroUsize")]
num_records: Option<NonZeroUsize>,
}

/// Main function for the `ngs derive encoding` subcommand.
Expand Down
7 changes: 4 additions & 3 deletions src/derive/command/endedness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Context;
use clap::Args;
use num_format::{Locale, ToFormattedString};
use std::collections::{HashMap, HashSet};
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::sync::Arc;
use tracing::{info, trace};
Expand All @@ -24,9 +25,9 @@ pub struct DeriveEndednessArgs {
#[arg(value_name = "BAM")]
src: PathBuf,

/// Only examine the first n records in the file.
#[arg(short, long, value_name = "USIZE")]
num_records: Option<usize>,
/// Examine the first `n` records in the file.
#[arg(short, long, value_name = "NonZeroUsize")]
num_records: Option<NonZeroUsize>,

/// Distance from 0.5 split between number of f+l- reads and f-l+ reads
/// allowed to be called 'Paired-End'. The default value of `0.0` is only appropriate
Expand Down
13 changes: 9 additions & 4 deletions src/derive/command/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::bail;
use clap::Args;
use num_format::{Locale, ToFormattedString};
use std::collections::HashSet;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use tracing::info;

Expand All @@ -21,9 +22,10 @@ pub struct DeriveInstrumentArgs {
#[arg(value_name = "BAM")]
src: PathBuf,

/// Only examine the first n records in the file.
#[arg(short, long, value_name = "USIZE")]
num_records: Option<usize>,
/// Examine the first `n` records in the file.
/// If `0`, all records are examined.
#[arg(short, long, value_name = "USIZE", default_value = "10000000")]
num_records: usize,
}

/// Main function for the `ngs derive instrument` subcommand.
Expand All @@ -41,7 +43,10 @@ pub fn derive(args: DeriveInstrumentArgs) -> anyhow::Result<()> {

// (1) Collect instrument names and flowcell names from reads within the
// file. Support for sampling only a portion of the reads is provided.
let num_records = NumberOfRecords::from(args.num_records);
let num_records = match args.num_records {
0 => NumberOfRecords::All,
_ => NumberOfRecords::Some(NonZeroUsize::new(args.num_records).unwrap()),
};
let mut counter = RecordCounter::default();

for result in reader.records(&header.parsed) {
Expand Down
13 changes: 9 additions & 4 deletions src/derive/command/readlen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Context;
use clap::Args;
use num_format::{Locale, ToFormattedString};
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use tracing::info;

Expand All @@ -21,9 +22,10 @@ pub struct DeriveReadlenArgs {
#[arg(value_name = "BAM")]
src: PathBuf,

/// Only examine the first n records in the file.
#[arg(short, long, value_name = "USIZE")]
num_records: Option<usize>,
/// Examine the first `n` records in the file.
/// If `0`, all records are examined.
#[arg(short, long, value_name = "USIZE", default_value = "10000000")]
num_records: usize,

/// Majority vote cutoff value as a fraction between [0.0, 1.0].
#[arg(short, long, value_name = "F64", default_value = "0.7")]
Expand All @@ -46,7 +48,10 @@ pub fn derive(args: DeriveReadlenArgs) -> anyhow::Result<()> {

// (1) Collect read lengths from reads within the
// file. Support for sampling only a portion of the reads is provided.
let num_records = NumberOfRecords::from(args.num_records);
let num_records = match args.num_records {
0 => NumberOfRecords::All,
_ => NumberOfRecords::Some(NonZeroUsize::new(args.num_records).unwrap()),
};
let mut counter = RecordCounter::default();

for result in reader.records(&header.parsed) {
Expand Down
5 changes: 3 additions & 2 deletions src/qc/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Functionality related to the `ngs qc` command itself.

use std::fs::File;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::rc::Rc;

Expand Down Expand Up @@ -50,8 +51,8 @@ pub struct QcArgs {
/// to process per sequence in the second pass.
///
/// This is generally only used for testing purposes.
#[arg(short = 'n', long, value_name = "USIZE")]
num_records: Option<usize>,
#[arg(short = 'n', long, value_name = "NonZeroUsize")]
num_records: Option<NonZeroUsize>,

/// Directory to output files to. Defaults to current working directory.
#[arg(short = 'o', long, value_name = "PATH")]
Expand Down
7 changes: 4 additions & 3 deletions src/utils/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Utilities related to the parsing of arguments.

use std::fmt::Display;
use std::num::NonZeroUsize;

use noodles::{bgzf::writer::CompressionLevel, sam::record::MappingQuality};
use tracing::debug;
Expand All @@ -17,11 +18,11 @@ pub enum NumberOfRecords {

/// Designates that we should review _some_ of the records in the file. The
/// exact count of records is stored in the `usize`.
Some(usize),
Some(NonZeroUsize),
}

impl From<Option<usize>> for NumberOfRecords {
fn from(num_records: Option<usize>) -> Self {
impl From<Option<NonZeroUsize>> for NumberOfRecords {
fn from(num_records: Option<NonZeroUsize>) -> Self {
match num_records {
Some(n) => {
debug!("Reading a maximum of {} records.", n);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl RecordCounter {
/// (if it exists, otherwise it loops forever).
pub fn time_to_break(&self, limit: &NumberOfRecords) -> bool {
match limit {
NumberOfRecords::Some(v) => self.count >= *v,
NumberOfRecords::Some(v) => self.count >= <usize>::from(*v),
NumberOfRecords::All => false,
}
}
Expand Down

0 comments on commit aa0880d

Please sign in to comment.