Skip to content

Instantly share code, notes, and snippets.

@zantetsuken88
Created July 20, 2018 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zantetsuken88/3f95bd074c08932c1b45fcebe1f699bc to your computer and use it in GitHub Desktop.
Save zantetsuken88/3f95bd074c08932c1b45fcebe1f699bc to your computer and use it in GitHub Desktop.

Hamming 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. This means that based on the definition, each language could deal with getting sequences of equal length differently.

Java Tips Hints This is the first exercise with tests that require you to throw an Exception. Exceptions 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:

instances of a specified Java type; (optionally) initialized with a specified message. Running the tests You can run all the tests for an exercise by entering

$ gradle test in your terminal.

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.

class Hamming {
private String leftStrand;
private String rightStrand;
Hamming(String leftStrand, String rightStrand) {
this.leftStrand = leftStrand;
this.rightStrand = rightStrand;
if (leftStrand.length() != rightStrand.length()) {
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
}
}
int getHammingDistance() {
int hammingDistance = 0;
int count = 0;
for (char c : leftStrand.toCharArray()) {
if (c != rightStrand.toCharArray()[count]) {
hammingDistance++;
}
count++;
}
return hammingDistance;
}
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
public void testSameNucleotidesInDifferentPositions() {
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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment