Coder Social home page Coder Social logo

hirschberg's Introduction

Hirschberg

Hirschberg provides a generic implementation of Hirschberg's algorithm for finding the optimal global alignment of two sequences.

Example usage:

let a = "ACCCGGTCGTCAATTA".chars().collect::<Vec<_>>();
let b = "ACCACCGGTTGGTCCAATAA".chars().collect::<Vec<_>>();

let output = hirschberg::Config::default().compute(&a, &b);

let (aligned_a, aligned_b): (String, String) = output
    .alignment()
    .iter()
    .map(|(a, b)| (a.copied().unwrap_or('_'), b.copied().unwrap_or('_')))
    .unzip();

assert_eq!(output.score(), 8);
assert_eq!(
    [aligned_a, aligned_b],
    [
        "A_C_CCGG_TCGT_CAATTA".to_string(),
        "ACCACCGGTTGGTCCAATAA".to_string()
    ]
);

The match, mismatch and gap contribution to the score may be configured:

let a = "ACCCGGTCGTCAATTA".chars().collect::<Vec<_>>();
let b = "ACCACCGGTTGGTCCAATAA".chars().collect::<Vec<_>>();

let output = hirschberg::Config::default().mismatch_score(-4).compute(&a, &b);

let (aligned_a, aligned_b): (String, String) = output
    .alignment()
    .iter()
    .map(|(a, b)| (a.copied().unwrap_or('_'), b.copied().unwrap_or('_')))
    .unzip();

assert_eq!(output.score(), 6);
assert_eq!(
    [aligned_a, aligned_b],
    [
        "A_C_CCGG_T_CGT_CAAT_TA".to_string(),
        "ACCACCGGTTG_GTCCAATA_A".to_string()
    ]
);

hirschberg's People

Contributors

nicksenger avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

hirschberg's Issues

Fraud! This is a Needleman wish and not a Hirschberg algorithm!

A proper hirschberg would look like this:

fn hirschberg(s1: &str, s1_len: usize, s2: &str, s2_len: usize) -> String {
    if s1_len == 0 || s2_len == 0 {
        return String::new();
    }

    if s1_len == 1 || s2_len == 1 {
        let mut lcs = String::new();
        for c in s1.chars() {
            if s2.contains(c) {
                lcs.push(c);
                break;
            }
        }
        return lcs;
    }

    let mid1 = s1_len / 2;
    let mid2 = s2_len / 2;

    let lcs_rev = hirschberg(&s1[mid1..].to_string().chars().rev().collect::<String>(), s1_len - mid1,
                             &s2[mid2..].to_string().chars().rev().collect::<String>(), s2_len - mid2);

    let lcs_fwd = hirschberg(&s1[..mid1].to_string(), mid1,
                             &s2[..mid2].to_string(), mid2);

    if lcs_fwd.len() > lcs_rev.chars().rev().collect::<String>().len() {
        lcs_fwd
    } else {
        lcs_rev.chars().rev().collect::<String>()
    }
}

fn main() {
    let s1 = "AGGTAB";
    let s2 = "GXTXAYB";
    let lcs = hirschberg(s1, s1.len(), s2, s2.len());
    println!("LCS: {}", lcs); // Output: LCS: GTAB
}

As can be seen, the Hirschberg algorithm is based on the principle of divide and conquer in order to enable a linear runtime on its data structure. Recursive calls are necessary for this. Your algorithm has a quadratic runtime on its data structure. This is completely understandable, as you have implemented a needleman algorithm. This is therefore cheating, as you are using the wrong algorithm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.