How to use the dtaidistance.alignment.needleman_wunsch function in dtaidistance

To help you get started, we’ve selected a few dtaidistance examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wannesm / dtaidistance / tests / test_alignment.py View on Github external
def test_sequences2():
    s1 = "GAAAAAAAT"
    s2 = "GAAT"
    value, matrix = alignment.needleman_wunsch(s1, s2)
    algn, s1a1, s2a1 = alignment.best_alignment(matrix, s1, s2, gap='-')
    algn_sol1 = [list('GAAAAAAAT'), list('GAA-----T')]
    assert s1a1 == algn_sol1[0]
    assert s2a1 == algn_sol1[1]
github wannesm / dtaidistance / tests / test_alignment.py View on Github external
def test_sequences_blosum():
    matrix = read_substitution_matrix(
        Path(__file__).parent / "rsrc" / "substitution.txt"
    )
    substitution = alignment.make_substitution_fn(matrix)
    s1 = "AGACTAGTTAC"
    s2 = "CGAGACGT"
    value, matrix = alignment.needleman_wunsch(s1, s2, substitution=substitution)
    algn, s1a1, s2a1 = alignment.best_alignment(matrix, s1, s2, gap='-')
    algn_sol1 = [list('--AGACTAGTTAC'),
                 list('CGAGAC--GT---')]
    assert s1a1 == algn_sol1[0]
    assert s2a1 == algn_sol1[1]
github wannesm / dtaidistance / tests / test_alignment.py View on Github external
def test_sequences3():
    s1 = "GAAAAAAAT"
    s2 = "GAATA"
    value, matrix = alignment.needleman_wunsch(s1, s2)
    algn, s1a1, s2a1 = alignment.best_alignment(matrix, s1, s2, gap='-')
    algn_sol1 = [list('GAA-AAAAAT'), list('GAATA-----')]
    assert s1a1 == algn_sol1[0]
    assert s2a1 == algn_sol1[1]
github wannesm / dtaidistance / tests / test_alignment.py View on Github external
def test_sequences4():
    s1 = "AGACTAGTTACC"
    s2 = "CGAGACGTC"
    value, matrix = alignment.needleman_wunsch(s1, s2)
    algn, s1a1, s2a1 = alignment.best_alignment(matrix, s1, s2, gap='-')
    # print(matrix)
    # print(algn)
    # print(s1a1)
    # print(s2a1)
    algn_sol1 = [list("--AGACTAGTTACC"), list("CGAGAC--GT--C-")]
    assert s1a1 == algn_sol1[0]
    assert s2a1 == algn_sol1[1]
github wannesm / dtaidistance / tests / test_alignment.py View on Github external
def test_sequences_custom():

    matrix = read_substitution_matrix(
        Path(__file__).parent / "rsrc" / "substitution.txt"
    )
    substitution = alignment.make_substitution_fn(matrix)

    s1 = "CCAGG"
    s2 = "CCGA"

    value, matrix = alignment.needleman_wunsch(s1, s2)
    algn, s1a1, s2a1 = alignment.best_alignment(matrix, s1, s2, gap='-')
    algn_sol1 = [list("CCAGG"), list("CC-GA")]
    assert s1a1 == algn_sol1[0]
    assert s2a1 == algn_sol1[1]

    value, matrix = alignment.needleman_wunsch(s1, s2, substitution=substitution)
    algn, s1a2, s2a2 = alignment.best_alignment(matrix, s1, s2, gap='-')
    algn_sol2 = [list("CC-AGG"), list("CCGA--")]
    assert s1a2 == algn_sol2[0]
    assert s2a2 == algn_sol2[1]
github wannesm / LeuvenMapMatching / leuvenmapmatching / util / evaluation.py View on Github external
:math:`f = \frac{d_{-} + d_{+}}{d_0}`

    With :math:`d_{-}` the length that is erroneously subtracted,
    :math:`d_{+}` the length that is erroneously added, and :math:`d_0` the
    distance of the correct route.

    This function only supports connected states (thus not switching between states
    that are not connected (e.g. parallel roads).

    Also computes the Accuracy by Number (AN) and Accuracy by Length (AL) metrics from
    Zheng et al. (2009).
    """
    if dist_fn is None:
        dist_fn = dist_latlon.distance
    _, matrix = needleman_wunsch(path_pred, path_grnd, window=window)
    print(matrix[:10, :10])
    algn, _, _ = best_alignment(matrix)
    print(algn[:10])
    d_plus = 0  # length erroneously added
    d_min = 0  # length erroneously subtracted
    d_zero = 0  # length of correct route
    cnt_matches = 0  # number of perfect matches
    cnt_mismatches = 0
    mismatches = [] if keep_mismatches else None

    prev_grnd_pi = None
    for pred_pi, grnd_pi in algn:
        pred_p = path_pred[pred_pi]
        grnd_p = path_grnd[grnd_pi]
        grnd_d = map_con.path_dist(grnd_p)
        d_zero += grnd_d