-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamming.sh
63 lines (50 loc) · 1.43 KB
/
hamming.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env bash
# Calculate the Hamming Distance between two DNA strands.
# If we compare two strands of DNA and count the differences between them
# we can see how many mistakes occurred. This is known as the "Hamming Distance".
# The Hamming distance is only defined for sequences of equal length.
# We read DNA using the letters C,A,G and T. Two strands might look like this:
# GAGCCTACTAACGGGAT
# CATCGTAATGACGGCCT
# ^ ^ ^ ^ ^ ^^
#They have 7 differences, and therefore the Hamming Distance is 7.
# Must receive two strings
if [ $# -ne 2 ]; then
echo "Usage: $( basename $0 ) <string1> <string2>"
exit 1
fi
# Check strings are of equal length
if [ ${#1} -ne ${#2} ]; then
echo "left and right strands must be of equal length"
exit 1
fi
# Compares strings character by character to check differences
compare () {
strandone="$1"
strandtwo="$2"
i=0
count=0
# As strings are equal, length of one works as end-of-while condition
while [ $i -le ${#strandone} ]; do
letterone=$(echo "${strandone:$i:1}")
lettertwo=$(echo "${strandtwo:$i:1}")
if [[ "$letterone" != "$lettertwo" ]]; then
let count++
fi
let i++
done
echo $count
return 0
}
main () {
# Good idea to have variables get values from parameters
strandone="$1"
strandtwo="$2"
# Returns the number of differences between DNA strands
answer=$(compare "$strandone" "$strandtwo")
# STDOUT of Hamming Distance
echo "$answer"
return 0
}
main "$@"
exit 0