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

feat: bio_diff #34

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions crates/bio_diff/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "bio_diff"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
differ-rs = { path = "/Users/alfredo/Documents/differ.rs/crates/differ"}
Empty file added crates/bio_diff/README.md
Empty file.
17 changes: 17 additions & 0 deletions crates/bio_diff/benches/criterion/fibb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

#[inline]
fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
19 changes: 19 additions & 0 deletions crates/bio_diff/benches/iai/fibb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use iai::{black_box, main};

fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}

fn iai_benchmark_short() -> u64 {
fibonacci(black_box(10))
}

fn iai_benchmark_long() -> u64 {
fibonacci(black_box(30))
}

main!(iai_benchmark_short, iai_benchmark_long);
3 changes: 3 additions & 0 deletions crates/bio_diff/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
corpus
artifacts
25 changes: 25 additions & 0 deletions crates/bio_diff/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "rust-template-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.rust-template]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "fuzz_target_1"
path = "fuzz_targets/fuzz_target_1.rs"
test = false
doc = false
6 changes: 6 additions & 0 deletions crates/bio_diff/fuzz/fuzz_targets/fuzz_target_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
// fuzzed code goes here
});
6 changes: 6 additions & 0 deletions crates/bio_diff/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod needlemanwunsch;
pub use crate::needlemanwunsch::NeedlemanWunsch;

pub trait StringAlignAlgorithm {
fn align<'a>(&self, s1: &'a str, s2: &'a str) -> (String, String);
}
136 changes: 136 additions & 0 deletions crates/bio_diff/src/needlemanwunsch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use crate::StringAlignAlgorithm;
pub struct NeedlemanWunsch {}

impl NeedlemanWunsch {
pub(crate) fn debug_vec<T: std::fmt::Debug>(my_vector: &Vec<Vec<T>>) {
for i in my_vector.iter() {
println!("{:?}", i);
}
}
pub(crate) fn max_dist_with_dir(x: isize, y: isize, z: isize) -> (isize, char) {
if x >= y && x >= z {
return (x, '^');
}
if y >= x && y >= z {
return (y, '<');
}
if z >= x && z >= y {
return (z, '\\');
}
unreachable!()
}
pub(crate) fn align_strings<'a>(
s1: &'a str,
s2: &'a str,
dir_vector: &Vec<Vec<char>>,
) -> (String, String) {
let mut top_str_len = s1.len();
let mut left_str_len = s2.len();

let mut top_string: String = String::new();
let mut left_string: String = String::new();

loop {
if top_str_len == 0 && left_str_len == 0 {
break;
}
match dir_vector[left_str_len][top_str_len] {
//Vertical arrows will align a gap ("-") to the letter of the row
'^' => {
top_string.insert(0, '-');
left_string.insert(0, s2.chars().nth(left_str_len - 1).unwrap());
left_str_len -= 1;
}
//horizontal arrows will align a gap to the letter of the column
'<' => {
top_string.insert(0, s1.chars().nth(top_str_len - 1).unwrap());
left_string.insert(0, '-');
top_str_len -= 1;
}

'\\' => {
top_string.insert(0, s1.chars().nth(top_str_len - 1).unwrap());
left_string.insert(0, s2.chars().nth(left_str_len - 1).unwrap());
top_str_len -= 1;
left_str_len -= 1;
}
_ => {
panic!("UNRECOGNIZED SYMBOL OPERATION !")
}
}
}

(top_string.to_owned(), left_string.to_owned())
}
}

impl StringAlignAlgorithm for NeedlemanWunsch {
fn align<'a>(&self, s1: &'a str, s2: &'a str) -> (String, String) {
let top_str_len: usize = s1.len();
let left_str_len: usize = s2.len();

let mut align_vector: Vec<Vec<isize>> =
vec![vec![0isize; top_str_len + 1]; left_str_len + 1];
let mut dir_vector: Vec<Vec<char>> = vec![vec![' '; top_str_len + 1]; left_str_len + 1];

for i in 0..top_str_len + 1 {
align_vector[0][i] = -(i as isize);
}
for i in 0..left_str_len + 1 {
align_vector[i][0] = -(i as isize);
}

dir_vector[0][0] = '\\';
for j in 1..left_str_len + 1 {
dir_vector[j][0] = '^';
}
for i in 1..top_str_len + 1 {
dir_vector[0][i] = '<';
}
//Match: +1
//Mismatch or Indel: −1
for i in 1..left_str_len + 1 {
for j in 1..top_str_len + 1 {
let mut diagnal_cost = 0;
if s1.chars().nth(j - 1).unwrap() == s2.chars().nth(i - 1).unwrap() {
diagnal_cost = 1;
} else {
diagnal_cost = -1
}

(align_vector[i][j], dir_vector[i][j]) = NeedlemanWunsch::max_dist_with_dir(
align_vector[i - 1][j] + (-1),
align_vector[i][j - 1] + (-1),
align_vector[i - 1][j - 1] + (diagnal_cost),
)
}
}
NeedlemanWunsch::debug_vec(&align_vector);
NeedlemanWunsch::debug_vec(&dir_vector);
let (test, test2) = NeedlemanWunsch::align_strings(s1, s2, &dir_vector);

return (test, test2);

//("str", "str")
}
}

#[cfg(test)]
mod tests {

use crate::StringAlignAlgorithm;
#[test]
fn test_needlemanwunsch_align() {
let test_struct = super::NeedlemanWunsch {};

assert_eq!(
test_struct.align("GCATGCG", "GATTACA"),
(String::from("GCATG-CG"), String::from("G-ATTACA"))
);

assert_eq!(
test_struct.align("CAGTG", "ATCTC"),
(String::from("CAG-TG"), String::from("-ATCTC"))
);
}
}