Avatar of jon-mqn

jon-mqn's solution

to Hamming in the Scheme Track

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

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

;; Load SRFI-64 lightweight testing specification
(use-modules (srfi srfi-64))

;; Suppress log file output. To write logs, comment out the following line:
(module-define! (resolve-module '(srfi srfi-64)) 'test-log-to-file #f)

(add-to-load-path (dirname (current-filename)))
(use-modules (hamming))

(test-begin "hamming")

(test-eqv "no-difference-between-empty-strands"
          0
          (hamming-distance "" ""))

(test-eqv "no-difference-between-identical-strands"
          0
          (hamming-distance "GATTACA" "GATTACA"))

(test-eqv "complete-hamming-distance-in-small-strand"
          3
          (hamming-distance "ACT" "GGA"))

(test-eqv "small-hamming-distance-in-middle-somewhere"
          1
          (hamming-distance "GGACG" "GGTCG"))

(test-eqv "larger-difference"
          2
          (hamming-distance "ACCAGGG" "ACTATGG"))

(test-error "invalid-to-get-distance-for-different-length-strings"
         #t
         (hamming-distance "AGACAACAGCCAGCCGCCGGATT" "AGGCAA"))


(test-end "hamming")
(define-module (hamming)
  #:export (hamming-distance))
(define (hamming-distance sequenceA sequenceB) 
  (if (equal? sequenceA sequenceB) 0 (ham sequenceA sequenceB 0 0)))

(define (ham a b difs index)
  (define (dif? index)
    (if (equal? (string-ref a index) (string-ref b index)) 0 1))
  (if (= index (string-length a))
    difs
    (ham a b (+ difs (dif? index)) (+ index 1))))

(display (hamming-distance "ABABD" "ABABD"))
(newline)
(display (hamming-distance "ABCBD" "ABABD"))
(newline)

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?