🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Hamming in the Scala Track

Published at Aug 16 2019 · 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.

Hints

Option is used to indicate a computation that may possibly have no useful result (for example due to an error or invalid input). If you are unfamiliar with Option you may read this tutorial. Option is a so-called Monad which covers a "computational aspect", in this case possible absence of a value. Proper use of Monads can result in very concise yet elegant and readable code. Improper use can easily result in the contrary. Watch this video to learn more.

Common pitfalls that you should avoid

There are a few rules of thumbs for Option:

  1. If you don't need it don't use it. Instead of
def add1(x: Int): Option[Int] = Some(x + 1)

better have

def add1(x: Int): Int = x + 1

(there is Option.map to apply such simple functions, so you don't have to clutter them with Option). 2. Don't "unwrap" if you don't really need to. Often there are built-in functions for your purpose. Indicators of premature unwrapping are isDefined/isEmpty or pattern matching. Instead of

val x: Option[Int] = ...

if (x.isDefined) x.get + 1 else 0
// or
x match {
  case Some(n) => n + 1
  case None => 0
}

better have

x map (_ + 1) getOrElse 0
  1. Monads can be used inside a for-comprehension FTW. This is advisable when you want to "compose" several Option instances. Instead of
val xo: Option[Int] = ...
val yo: Option[Int] = ...
val zo: Option[Int] = ...

xo.flatMap(x =>
  yo.flatMap(y =>
    zo.map(z =>
	  x + y + z)))

better have

for {
  x <- xo
  y <- yo
  z <- zo
} yield x + y + z

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

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.

HammingTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 2.1.0 */
class HammingTest extends FunSuite with Matchers {
  
  test("empty strands") {
    Hamming.distance("", "") should be (Some(0))
  }

  test("identical strands") {
    pending
    Hamming.distance("A", "A") should be (Some(0))
  }

  test("long identical strands") {
    pending
    Hamming.distance("GGACTGA", "GGACTGA") should be (Some(0))
  }

  test("complete distance in single nucleotide strands") {
    pending
    Hamming.distance("A", "G") should be (Some(1))
  }

  test("complete distance in small strands") {
    pending
    Hamming.distance("AG", "CT") should be (Some(2))
  }

  test("small distance in small strands") {
    pending
    Hamming.distance("AT", "CT") should be (Some(1))
  }

  test("small distance") {
    pending
    Hamming.distance("GGACG", "GGTCG") should be (Some(1))
  }

  test("small distance in long strands") {
    pending
    Hamming.distance("ACCAGGG", "ACTATGG") should be (Some(2))
  }

  test("non-unique character in first strand") {
    pending
    Hamming.distance("AAG", "AAA") should be (Some(1))
  }

  test("non-unique character in second strand") {
    pending
    Hamming.distance("AAA", "AAG") should be (Some(1))
  }

  test("same nucleotides in different positions") {
    pending
    Hamming.distance("TAG", "GAT") should be (Some(2))
  }

  test("large distance") {
    pending
    Hamming.distance("GATACA", "GCATAA") should be (Some(4))
  }

  test("large distance in off-by-one strand") {
    pending
    Hamming.distance("GGACGGATTCTG", "AGGACGGATTCT") should be (Some(9))
  }

  test("disallow first strand longer") {
    pending
    Hamming.distance("AATG", "AAA") should be (None)
  }

  test("disallow second strand longer") {
    pending
    Hamming.distance("ATA", "AGTG") should be (None)
  }
}
object Hamming {

  def distance(first: String, second: String): Option[Int] =
    if (first.length != second.length) None
    else Some(first.zip(second).count(a => a._1 != a._2))

}

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?