Skip to content

Commit

Permalink
changing unwraps to expect
Browse files Browse the repository at this point in the history
  • Loading branch information
wdecoster committed Jan 22, 2024
1 parent ff31715 commit b94a60f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chopper"
version = "0.7.0"
version = "0.7.1"
authors = ["wdecoster <decosterwouter@gmail.com>"]
edition = "2021"

Expand Down
38 changes: 22 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,26 @@ where
let mut output_reads = 0;
let aligner = setup_contamination_filter(fas);
fastq::Reader::new(input).records().for_each(|record| {
let record = record.unwrap();
let record = record.expect("ERROR: problem parsing fastq record");
total_reads += 1;
if !record.is_empty() {
let read_len = record.seq().len();
// If a read is shorter than what is to be cropped the read is dropped entirely (filtered out)
if args.headcrop + args.tailcrop < read_len {
let average_quality =
ave_qual(&record.qual().iter().map(|i| i - 33).collect::<Vec<u8>>());
if (!args.inverse && average_quality >= args.minqual
if (!args.inverse
&& average_quality >= args.minqual
&& average_quality <= args.maxqual
&& read_len >= args.minlength
&& read_len <= args.maxlength
&& !is_contamination(&record.seq(), &aligner))
|| (args.inverse && (average_quality < args.minqual
|| average_quality > args.maxqual
|| read_len < args.minlength
|| read_len > args.maxlength
|| is_contamination(&record.seq(), &aligner)))
|| (args.inverse
&& (average_quality < args.minqual
|| average_quality > args.maxqual
|| read_len < args.minlength
|| read_len > args.maxlength
|| is_contamination(&record.seq(), &aligner)))
{
write_record(record, &args, read_len);
output_reads += 1;
Expand All @@ -112,12 +114,12 @@ where
rayon::ThreadPoolBuilder::new()
.num_threads(args.threads)
.build_global()
.unwrap();
.expect("Error: Unable to build threadpool");
fastq::Reader::new(input)
.records()
.par_bridge()
.for_each(|record| {
let record = record.unwrap();
let record = record.expect("ERROR: problem parsing fastq record");
total_reads_.fetch_add(1, Ordering::SeqCst);
if !record.is_empty() {
let read_len = record.seq().len();
Expand All @@ -126,14 +128,16 @@ where
let average_quality = ave_qual(
&record.qual().iter().map(|i| i - 33).collect::<Vec<u8>>(),
);
if (!args.inverse && average_quality >= args.minqual
if (!args.inverse
&& average_quality >= args.minqual
&& average_quality <= args.maxqual
&& read_len >= args.minlength
&& read_len <= args.maxlength)
|| (args.inverse && (average_quality < args.minqual
|| average_quality > args.maxqual
|| read_len < args.minlength
|| read_len > args.maxlength))
|| (args.inverse
&& (average_quality < args.minqual
|| average_quality > args.maxqual
|| read_len < args.minlength
|| read_len > args.maxlength))
{
write_record(record, &args, read_len);
output_reads_.fetch_add(1, Ordering::SeqCst);
Expand All @@ -159,8 +163,10 @@ fn write_record(record: fastq::Record, args: &Cli, read_len: usize) {
println!(
"@{}\n{}\n+\n{}",
header,
std::str::from_utf8(&record.seq()[args.headcrop..read_len - args.tailcrop]).unwrap(),
std::str::from_utf8(&record.qual()[args.headcrop..read_len - args.tailcrop]).unwrap()
std::str::from_utf8(&record.seq()[args.headcrop..read_len - args.tailcrop])
.expect("ERROR: problem writing fastq seq"),
std::str::from_utf8(&record.qual()[args.headcrop..read_len - args.tailcrop])
.expect("ERROR: problem writing fastq qual")
);
}

Expand Down

0 comments on commit b94a60f

Please sign in to comment.