Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to find canonical substitution of DNA #48

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde = {version = "1.0", features = ["derive"], optional = true}

[dev-dependencies]
criterion = "0.5.1"
quickcheck = "1.0.3"
rand = "0.8.5"
serde_json = "1"

Expand All @@ -32,6 +33,9 @@ default = ["python-support"]
name = "all_windows"
harness = false

[[bench]]
name = "canonical"
harness = false

[[bench]]
name = "expansions"
Expand Down
62 changes: 62 additions & 0 deletions benches/canonical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021-2023 SecureDNA Stiftung (SecureDNA Foundation) <[email protected]>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::hint::black_box;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use rand::{rngs::OsRng, seq::SliceRandom};

use quickdna::canonical::{Canonical, ForwardCanonical};
use quickdna::Nucleotide;

pub fn criterion_benchmark(c: &mut Criterion) {
const NUM_WINDOWS: u64 = 10000;
const WINDOW_LEN: usize = 42;
let dna_len = (NUM_WINDOWS as usize) + WINDOW_LEN - 1;
let dna: Vec<_> = (0..dna_len)
.map(|_| Nucleotide::ALL.choose(&mut OsRng).unwrap())
.collect();

let num_windows_desc = format!("{NUM_WINDOWS} windows");

let mut group = c.benchmark_group("canonicalization");
group.throughput(Throughput::Elements(NUM_WINDOWS));
group.bench_with_input(
BenchmarkId::new("bidirectional", &num_windows_desc),
&(&dna, WINDOW_LEN),
|b, &(dna, window_len)| {
b.iter(|| {
let canonicalized_windows = dna.windows(window_len).map(|window| {
let mut canonical = Canonical::new(window.iter().copied().copied());
let window: [_; WINDOW_LEN] =
std::array::from_fn(|_| canonical.next().unwrap());
window
});
for window in canonicalized_windows {
black_box(&window);
}
})
},
);
group.bench_with_input(
BenchmarkId::new("forward-only", &num_windows_desc),
&(&dna, WINDOW_LEN),
|b, &(dna, window_len)| {
b.iter(|| {
let canonicalized_windows = dna.windows(window_len).map(|window| {
let mut canonical = ForwardCanonical::new(window.iter().copied().copied());
let window: [_; WINDOW_LEN] =
std::array::from_fn(|_| canonical.next().unwrap());
window
});
for window in canonicalized_windows {
black_box(&window);
}
})
},
);
group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading
Loading