Skip to content

Commit

Permalink
Add ability to find canonical substitution of DNA (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
swooster authored Oct 24, 2023
1 parent 410c55b commit a5a4f1f
Show file tree
Hide file tree
Showing 5 changed files with 542 additions and 1 deletion.
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

0 comments on commit a5a4f1f

Please sign in to comment.