Avatar of shmibs

shmibs's solution

to Hamming in the OCaml Track

Published at Nov 16 2018 · 0 comments
Instructions
Test suite
Solution

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.

Getting Started

For installation and learning resources, refer to the exercism help page.

Installation

To work on the exercises, you will need Opam and Base. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install base:

opam install base

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

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.

test.ml

open Base
open OUnit2
open Hamming

let printer = function
| None -> "None"
| Some x -> Int.to_string x

let ae exp got _test_ctxt = assert_equal ~printer exp got

let dna_of_string s =
  let f = function
    | 'A' -> A
    | 'C' -> C
    | 'G' -> G
    | 'T' -> T
    | _   -> failwith "Big news! New nucleotide discovered" in
  String.to_list s |> List.map ~f

let hamdist a b = hamming_distance (dna_of_string a) (dna_of_string b)

let tests = [
   "empty strands" >::
      ae (Some 0) (hamdist "" "");
   "identical strands" >::
      ae (Some 0) (hamdist "A" "A");
   "long identical strands" >::
      ae (Some 0) (hamdist "GGACTGA" "GGACTGA");
   "complete distance in single nucleotide strands" >::
      ae (Some 1) (hamdist "A" "G");
   "complete distance in small strands" >::
      ae (Some 2) (hamdist "AG" "CT");
   "small distance in small strands" >::
      ae (Some 1) (hamdist "AT" "CT");
   "small distance" >::
      ae (Some 1) (hamdist "GGACG" "GGTCG");
   "small distance in long strands" >::
      ae (Some 2) (hamdist "ACCAGGG" "ACTATGG");
   "non-unique character in first strand" >::
      ae (Some 1) (hamdist "AAG" "AAA");
   "non-unique character in second strand" >::
      ae (Some 1) (hamdist "AAA" "AAG");
   "same nucleotides in different positions" >::
      ae (Some 2) (hamdist "TAG" "GAT");
   "large distance" >::
      ae (Some 4) (hamdist "GATACA" "GCATAA");
   "large distance in off-by-one strand" >::
      ae (Some 9) (hamdist "GGACGGATTCTG" "AGGACGGATTCT");
   "disallow first strand longer" >::
      ae None (hamdist "AATG" "AAA");
   "disallow second strand longer" >::
      ae None (hamdist "ATA" "AGTG");
]

let () =
  run_test_tt_main ("hamming tests" >::: tests)
type nucleotide = A | C | G | T

let hamming_distance (l1 : nucleotide list) (l2 : nucleotide list) =
  try Some (
    List.fold_left2 (fun a e1 e2 -> if e1 = e2 then a else a + 1) 0 l1 l2)
  with Invalid_argument _ -> None

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?