# magandrez's solution

## to Hamming in the Bash Track

Published at Mar 04 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the 'Hamming distance'.

It is found by comparing two DNA strands and counting how many of the nucleotides are different from their equivalent in the other string.

``````GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^
``````

The Hamming distance between these two DNA strands is 7.

# Implementation notes

The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work. The general handling of this situation (e.g., raising an exception vs returning a special value) may differ between languages.

Run the tests with:

``````bats hamming_test.sh
``````

After the first test(s) pass, continue by commenting out or removing the `skip` annotations prepending other tests.

## Source

The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/

## External utilities

`Bash` is a language to write scripts that works closely with various system utilities, like `sed`, `awk`, `date` and even other programming languages, like `Python`. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installing third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure `Bash`, without using any external tools.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### hamming_test.sh

``````#!/usr/bin/env bash

@test 'empty strands' {
#skip
run bash hamming.sh '' ''
[ "\$status" -eq 0 ]
[ "\$output" -eq 0 ]
}

@test 'single letter identical strands' {
skip
run bash hamming.sh 'A' 'A'
[ "\$status" -eq 0 ]
[ "\$output" -eq 0 ]
}

@test 'single letter different strands' {
skip
run bash hamming.sh 'G' 'T'
[ "\$status" -eq 0 ]
[ "\$output" -eq 1 ]
}

@test 'long identical strands' {
skip
run bash hamming.sh 'GGACTGAAATCTG' 'GGACTGAAATCTG'
[ "\$status" -eq 0 ]
[ "\$output" -eq 0 ]
}

@test 'long different strands' {
skip
run bash hamming.sh 'GGACGGATTCTG' 'AGGACGGATTCT'
[ "\$status" -eq 0 ]
[ "\$output" -eq 9 ]
}

@test 'disallow first strand longer' {
skip
run bash hamming.sh 'AATG' 'AAA'
[ "\$status" -eq 1 ]
[ "\$output" == 'left and right strands must be of equal length' ]
}

@test 'disallow second strand longer' {
skip
run bash hamming.sh 'ATA' 'AGTG'
[ "\$status" -eq 1 ]
[ "\$output" == 'left and right strands must be of equal length' ]
}

@test "no input" {
skip
run bash hamming.sh
[ "\$status" -eq 1 ]
[ "\$output" == 'Usage: hamming.sh <string1> <string2>' ]
}``````
``````#!/usr/bin/env bash

USAGE="Usage: hamming.sh <string1> <string2>"

validate_args(){

if [ "\$#" -ne 2 ]
then
echo "\$USAGE"
exit 1
fi
if [ \${#1} -ne \${#2} ]
then
echo "left and right strands must be of equal length"
exit 1
fi

}

run(){
count=0
for ((i=0; i<\${#1}; i++)); do
[ "\${1:i:1}" == "\${2:i:1}" ] || (( "count++" ))
done
echo \$count

}

main(){
validate_args "\$@"
run "\$@"
}

main "\$@"``````