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

mtsknn's solution

to Hamming in the Clojure Track

Published at Mar 28 2021 · 0 comments
Instructions
Test suite
Solution

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.

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

``````(ns hamming-test
(:require [clojure.test :refer [deftest is]]
hamming))

(deftest no-difference-between-empty-strands
(is (= 0 (hamming/distance "" ""))))

(deftest no-difference-between-identical-strands
(is (= 0 (hamming/distance "GGACTGA" "GGACTGA"))))

(deftest complete-distance-in-small-strand
(is (= 3 (hamming/distance "ACT" "GGA"))))

(deftest small-distance-in-middle-somewhere
(is (= 1 (hamming/distance "GGACG" "GGTCG"))))

(deftest larger-distance
(is (= 2 (hamming/distance "ACCAGGG" "ACTATGG"))))

(deftest undefined-when-lengths-are-different
(is (= nil (hamming/distance "AAAC" "TAGGGGAGGCTAGCGGTAGGAC")))
(is (= nil (hamming/distance "GACTACGGACAGGGTAACATAG" "GACA"))))``````
``````(ns hamming)

(defn distance [strand1 strand2]
(if (not= (count strand1) (count strand2))
nil
(->> (interleave strand1 strand2)
(partition 2)
(remove (fn [[a b]] (= a b)))
count)))``````