ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

## to Hamming in the Python Track

Published at Oct 31 2018 · 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.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of raise Exception, you should write:

raise Exception("Meaningful message indicating the source of the error")

## Running the tests

To run the tests, run the appropriate command below (why they are different):

• Python 2.7: py.test hamming_test.py
• Python 3.4+: pytest hamming_test.py

Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): python -m pytest hamming_test.py

### Common pytest options

• -v : enable verbose output
• -x : stop running tests on first failure
• --ff : run failures from previous test before running other test cases

For other options, see python -m pytest -h

## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the \$EXERCISM_WORKSPACE/python/hamming directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace.

For more detailed information about running tests, code style and linting, please see Running the Tests.

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

### hamming_test.py

import unittest

import hamming

# Tests adapted from `problem-specifications//canonical-data.json` @ v2.1.0

class HammingTest(unittest.TestCase):

def test_empty_strands(self):
self.assertEqual(hamming.distance("", ""), 0)

def test_identical_strands(self):
self.assertEqual(hamming.distance("A", "A"), 0)

def test_long_identical_strands(self):
self.assertEqual(hamming.distance("GGACTGA", "GGACTGA"), 0)

def test_complete_distance_in_single_nucleotide_strands(self):
self.assertEqual(hamming.distance("A", "G"), 1)

def test_complete_distance_in_small_strands(self):
self.assertEqual(hamming.distance("AG", "CT"), 2)

def test_small_distance_in_small_strands(self):
self.assertEqual(hamming.distance("AT", "CT"), 1)

def test_small_distance(self):
self.assertEqual(hamming.distance("GGACG", "GGTCG"), 1)

def test_small_distance_in_long_strands(self):
self.assertEqual(hamming.distance("ACCAGGG", "ACTATGG"), 2)

def test_non_unique_character_in_first_strand(self):
self.assertEqual(hamming.distance("AAG", "AAA"), 1)

def test_non_unique_character_in_second_strand(self):
self.assertEqual(hamming.distance("AAA", "AAG"), 1)

def test_same_nucleotides_in_different_positions(self):
self.assertEqual(hamming.distance("TAG", "GAT"), 2)

def test_large_distance(self):
self.assertEqual(hamming.distance("GATACA", "GCATAA"), 4)

def test_large_distance_in_off_by_one_strand(self):
self.assertEqual(hamming.distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)

def test_disallow_first_strand_longer(self):
with self.assertRaisesWithMessage(ValueError):
hamming.distance("AATG", "AAA")

def test_disallow_second_strand_longer(self):
with self.assertRaisesWithMessage(ValueError):
hamming.distance("ATA", "AGTG")

# Utility functions
def setUp(self):
try:
self.assertRaisesRegex
except AttributeError:
self.assertRaisesRegex = self.assertRaisesRegexp

def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")

if __name__ == '__main__':
unittest.main()

### hamming.py

def distance(strand_a, strand_b):
if len(strand_a) != len(strand_b):
raise ValueError("Meaningful message indicating the source of the error")
else:
return sum([int(i!=j) for i,j in zip(strand_a,strand_b)])

### hamming_test.py

import unittest

import hamming

# Tests adapted from `problem-specifications//canonical-data.json` @ v2.1.0

class HammingTest(unittest.TestCase):

def test_empty_strands(self):
self.assertEqual(hamming.distance("", ""), 0)

def test_identical_strands(self):
self.assertEqual(hamming.distance("A", "A"), 0)

def test_long_identical_strands(self):
self.assertEqual(hamming.distance("GGACTGA", "GGACTGA"), 0)

def test_complete_distance_in_single_nucleotide_strands(self):
self.assertEqual(hamming.distance("A", "G"), 1)

def test_complete_distance_in_small_strands(self):
self.assertEqual(hamming.distance("AG", "CT"), 2)

def test_small_distance_in_small_strands(self):
self.assertEqual(hamming.distance("AT", "CT"), 1)

def test_small_distance(self):
self.assertEqual(hamming.distance("GGACG", "GGTCG"), 1)

def test_small_distance_in_long_strands(self):
self.assertEqual(hamming.distance("ACCAGGG", "ACTATGG"), 2)

def test_non_unique_character_in_first_strand(self):
self.assertEqual(hamming.distance("AAG", "AAA"), 1)

def test_non_unique_character_in_second_strand(self):
self.assertEqual(hamming.distance("AAA", "AAG"), 1)

def test_same_nucleotides_in_different_positions(self):
self.assertEqual(hamming.distance("TAG", "GAT"), 2)

def test_large_distance(self):
self.assertEqual(hamming.distance("GATACA", "GCATAA"), 4)

def test_large_distance_in_off_by_one_strand(self):
self.assertEqual(hamming.distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)

def test_disallow_first_strand_longer(self):
with self.assertRaisesWithMessage(ValueError):hamming.distance("AATG", "AAA")

def test_disallow_second_strand_longer(self):
with self.assertRaisesWithMessage(ValueError):
hamming.distance("ATA", "AGTG")

# Utility functions
def setUp(self):
try:
self.assertRaisesRegex
except AttributeError:
self.assertRaisesRegex = self.assertRaisesRegexp

def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")

if __name__ == '__main__':
unittest.main()