-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathhamming_distance.jl
More file actions
35 lines (34 loc) · 1005 Bytes
/
hamming_distance.jl
File metadata and controls
35 lines (34 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Hamming Distance
# ================
#
# The Hamming distance algorithm.
#
# This file is a part of BioJulia.
# License is MIT: https://github.com/BioJulia/BioAlignments.jl/blob/master/LICENSE.md
function hamming_distance(::Type{T}, a, b) where T
m = length(a)
@assert m == length(b)
anchors = [AlignmentAnchor(0, 0, 0, OP_START)]
if m == 0
# empty string
return 0, anchors
end
was_match = a[1] == b[1]
d = ifelse(was_match, T(0), T(1))
for i in 2:m
if a[i] == b[i]
if !was_match
push!(anchors, AlignmentAnchor(i - 1, i - 1, i - 1, OP_SEQ_MISMATCH))
end
was_match = true
else
if was_match
push!(anchors, AlignmentAnchor(i - 1, i - 1, i - 1, OP_SEQ_MATCH))
end
d += T(1)
was_match = false
end
end
push!(anchors, AlignmentAnchor(m, m, m, was_match ? OP_SEQ_MATCH : OP_SEQ_MISMATCH))
return d, anchors
end