# artemkorsakov's solution

## to Hamming in the Java Track

Published at Feb 06 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Calculate the Hamming Distance between two DNA strands.

Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. 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".

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.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

# 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.

# Java Tips

## Hints

This is the first exercise with tests that require you to throw an `Exception`. `Exception`s are typically thrown to indicate that a program has encountered an unexpected input or state.

We use JUnit's `ExpectedException` rule throughout the track to verify that the exceptions you throw are:

1. instances of a specified Java type;
2. (optionally) initialized with a specified message.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

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

## Submitting Incomplete Solutions

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

### HammingTest.java

``````import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public class HammingTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testNoDistanceBetweenEmptyStrands() {
assertEquals(0, new Hamming("", "").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testNoDistanceBetweenShortIdenticalStrands() {
assertEquals(0, new Hamming("A", "A").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testNoDistanceBetweenLongIdenticalStrands() {
assertEquals(0, new Hamming("GGACTGA", "GGACTGA").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testCompleteDistanceInSingleNucleotideStrand() {
assertEquals(1, new Hamming("A", "G").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testCompleteDistanceInSmallStrand() {
assertEquals(2, new Hamming("AG", "CT").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testSmallDistanceInSmallStrand() {
assertEquals(1, new Hamming("AT", "CT").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testSmallDistanceInMediumStrand() {
assertEquals(1, new Hamming("GGACG", "GGTCG").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testSmallDistanceInLongStrand() {
assertEquals(2, new Hamming("ACCAGGG", "ACTATGG").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testNonUniqueCharacterInFirstStrand() {
assertEquals(1, new Hamming("AAG", "AAA").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testNonUniqueCharacterInSecondStrand() {
assertEquals(1, new Hamming("AAA", "AAG").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
assertEquals(2, new Hamming("TAG", "GAT").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testLargeDistanceInPermutedStrand() {
assertEquals(4, new Hamming("GATACA", "GCATAA").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testLargeDistanceInOffByOneStrand() {
assertEquals(9, new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance());
}

@Ignore("Remove to run test")
@Test
public void testValidatesFirstStrandNotLonger() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("leftStrand and rightStrand must be of equal length.");

new Hamming("AATG", "AAA");
}

@Ignore("Remove to run test")
@Test
public void testValidatesSecondStrandNotLonger() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("leftStrand and rightStrand must be of equal length.");

new Hamming("ATA", "AGTG");
}

}``````
``````class Hamming {
private int distance = 0;

Hamming(String leftStrand, String rightStrand) {
if (leftStrand.length() != rightStrand.length()) {
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
}
calculateDistance(leftStrand, rightStrand);
}

int getHammingDistance() {
return distance;
}

private void calculateDistance(String leftStrand, String rightStrand) {
for (int i = 0; i < leftStrand.length(); i++) {
if (leftStrand.charAt(i) != rightStrand.charAt(i)) {
distance++;
}
}
}

}``````