Skip to content

Commit

Permalink
Sync and re-implement hamming (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jul 28, 2024
1 parent bee86ed commit 2f4189c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 42 deletions.
19 changes: 11 additions & 8 deletions exercises/practice/hamming/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

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!
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".
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:
We read DNA using the letters C,A,G and T.
Two strands might look like this:

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
Expand All @@ -16,9 +22,6 @@ 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
## 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.
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.
3 changes: 2 additions & 1 deletion exercises/practice/hamming/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"stevejb71"
],
"contributors": [
"keiravillekode",
"NobbZ",
"yurrriq"
],
Expand All @@ -19,5 +20,5 @@
},
"blurb": "Calculate the Hamming difference between two DNA strands.",
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "http://rosalind.info/problems/hamm/"
"source_url": "https://rosalind.info/problems/hamm/"
}
10 changes: 10 additions & 0 deletions exercises/practice/hamming/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,46 @@ description = "long different strands"

[919f8ef0-b767-4d1b-8516-6379d07fcb28]
description = "disallow first strand longer"
include = false

[b9228bb1-465f-4141-b40f-1f99812de5a8]
description = "disallow first strand longer"
reimplements = "919f8ef0-b767-4d1b-8516-6379d07fcb28"
include = false

[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e]
description = "disallow second strand longer"
include = false

[dab38838-26bb-4fff-acbe-3b0a9bfeba2d]
description = "disallow second strand longer"
reimplements = "8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e"
include = false

[5dce058b-28d4-4ca7-aa64-adfe4e17784c]
description = "disallow left empty strand"
include = false

[db92e77e-7c72-499d-8fe6-9354d2bfd504]
description = "disallow left empty strand"
include = false
reimplements = "5dce058b-28d4-4ca7-aa64-adfe4e17784c"

[b764d47c-83ff-4de2-ab10-6cfe4b15c0f3]
description = "disallow empty first strand"
reimplements = "db92e77e-7c72-499d-8fe6-9354d2bfd504"
include = false

[38826d4b-16fb-4639-ac3e-ba027dec8b5f]
description = "disallow right empty strand"
include = false

[920cd6e3-18f4-4143-b6b8-74270bb8f8a3]
description = "disallow right empty strand"
include = false
reimplements = "38826d4b-16fb-4639-ac3e-ba027dec8b5f"

[9ab9262f-3521-4191-81f5-0ed184a5aa89]
description = "disallow empty second strand"
reimplements = "920cd6e3-18f4-4143-b6b8-74270bb8f8a3"
include = false
6 changes: 1 addition & 5 deletions exercises/practice/hamming/example/Hamming.idr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ implementation Eq Nucleotide where
export
hamming_distance : Eq a => Vect n a -> Vect n a -> Nat
hamming_distance s1 s2 =
fst $ filter ((/=) 0) $ map (\(n1,n2) => if n1 == n2 then 0 else 1) $ zip s1 s2

export
version : String
version = "1.0.0"
fst $ filter (\(n1,n2) => n1 /= n2) $ zip s1 s2
4 changes: 0 additions & 4 deletions exercises/practice/hamming/src/Hamming.idr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,3 @@ implementation Eq Nucleotide where
export
hamming_distance : Eq a => Vect n a -> Vect n a -> Nat
hamming_distance s1 s2 = ?hamming_distance_rhs

export
version : String
version = "1.0.0"
35 changes: 11 additions & 24 deletions exercises/practice/hamming/test/src/Main.idr
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
module Main

import Data.Vect
import System
import Tester
import Tester.Runner

import Hamming

collect : {default 0 acc : Nat} -> (List (IO Bool)) -> IO Nat
collect {acc} [] = pure acc
collect {acc} (test :: tests) = do
bool <- test
case bool of
True => collect {acc=acc} tests
False => collect {acc=S acc} tests
import Data.Vect

tests : List Test
tests = map (test "")
[ assertEq (hamming_distance [A] [A]) 0
, assertEq (hamming_distance [G,G,A,C,T,G,A] [G,G,A,C,T,G,A]) 0
, assertEq (hamming_distance [A] [G]) 1
, assertEq (hamming_distance [A,G] [C,T]) 2
, assertEq (hamming_distance [A,T] [C,T]) 1
, assertEq (hamming_distance [G,G,A,C,G] [G,G,T,C,G]) 1
, assertEq (hamming_distance [A,C,C,A,G,G,G] [A,C,T,A,T,G,G]) 2
, assertEq (hamming_distance [A,G,A] [A,G,G]) 1
, assertEq (hamming_distance [A,G,G] [A,G,A]) 1
, assertEq (hamming_distance [T,A,G] [G,A,T]) 2
, assertEq (hamming_distance [G,A,T,A,C,A] [G,C,A,T,A,A]) 4
, assertEq (hamming_distance [G,G,A,C,G,G,A,T,T,C,T,G] [A,G,G,A,C,G,G,A,T,T,C,T]) 9
-- , assertEq (hamming_distance empty empty) 0
]
tests =
[ test "empty strands" (assertEq (hamming_distance (the (Vect 0 Nucleotide) []) (the (Vect 0 Nucleotide) [])) 0)
, test "single letter identical strands" (assertEq (hamming_distance [A] [A]) 0)
, test "single letter different strands" (assertEq (hamming_distance [G] [T]) 1)
, test "long identical strands" (assertEq (hamming_distance [G, G, A, C, T, G, A, A, A, T, C, T, G] [G, G, A, C, T, G, A, A, A, T, C, T, G]) 0)
, test "long different strands" (assertEq (hamming_distance [G, G, A, C, G, G, A, T, T, C, T, G] [A, G, G, A, C, G, G, A, T, T, C, T]) 9)
, test "short identical strands" (assertEq (hamming_distance [G, G, A, C, T, G, A] [G, G, A, C, T, G, A]) 0)
, test "short different strands" (assertEq (hamming_distance [A, C, C, A, G, G, G] [A, C, T, A, T, G, G]) 2)
]

export
main : IO ()
main = do
Expand Down
38 changes: 38 additions & 0 deletions generators/exercises/hamming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

EXTRA_CASES = [
{
"description": "short identical strands",
"input": {
"strand1": "GGACTGA",
"strand2": "GGACTGA"
},
"expected": 0
},
{
"description": "short different strands",
"input": {
"strand1": "ACCAGGG",
"strand2": "ACTATGG"
},
"expected": 2
}
]

def header():
return "import Data.Vect\n"

def extra_cases():
return EXTRA_CASES

def generate_test(case):
def to_vect(strand):
if strand == "":
return "(the (Vect 0 Nucleotide) [])"
return str(list(strand)).replace("'", "")

property = "hamming_distance"
expected = case["expected"]
strand1 = to_vect(case["input"]["strand1"])
strand2 = to_vect(case["input"]["strand2"])

return f'assertEq ({property} {strand1} {strand2}) {expected}'

0 comments on commit 2f4189c

Please sign in to comment.