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

# katrinleinweber's solution

## to Hamming in the R Track

Published at Jul 13 2018 · 2 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.

## Installation

See this guide for instructions on how to setup your local R environment.

## How to implement your solution

In each problem folder, there is a file named `<exercise_name>.R` containing a function that returns a `NULL` value. Place your implementation inside the body of the function.

## How to run tests

Inside of RStudio, simply execute the `test_<exercise_name>.R` script. This can be conveniently done with testthat's `auto_test` function. Because exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`.

## 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_hamming.R

``````source("./hamming.R")
library(testthat)

context("hamming")

test_that("identical strands", {
strand1 <- "A"
strand2 <- "A"
expect_equal(hamming(strand1, strand2), 0)
})

test_that("long identical strands", {
strand1 <- "GGACTGA"
strand2 <- "GGACTGA"
expect_equal(hamming(strand1, strand2), 0)
})

test_that("complete distance in single nucleotide strands", {
strand1 <- "A"
strand2 <- "G"
expect_equal(hamming(strand1, strand2), 1)
})

test_that("complete distance in small strands", {
strand1 <- "AG"
strand2 <- "CT"
expect_equal(hamming(strand1, strand2), 2)
})

test_that("small distance in small strands", {
strand1 <- "AT"
strand2 <- "CT"
expect_equal(hamming(strand1, strand2), 1)
})

test_that("small distance", {
strand1 <- "GGACG"
strand2 <- "GGTCG"
expect_equal(hamming(strand1, strand2), 1)
})

test_that("small distance in long strands", {
strand1 <- "ACCAGGG"
strand2 <- "ACTATGG"
expect_equal(hamming(strand1, strand2), 2)
})

test_that("non-unique character in first strand", {
strand1 <- "AGA"
strand2 <- "AGG"
expect_equal(hamming(strand1, strand2), 1)
})

test_that("non-unique character in second strand", {
strand1 <- "AGG"
strand2 <- "AGA"
expect_equal(hamming(strand1, strand2), 1)
})

test_that("same nucleotides in different positions", {
strand1 <- "TAG"
strand2 <- "GAT"
expect_equal(hamming(strand1, strand2), 2)
})

test_that("large distance", {
strand1 <- "GATACA"
strand2 <- "GCATAA"
expect_equal(hamming(strand1, strand2), 4)
})

test_that("empty strands", {
strand1 <- ""
strand2 <- ""
expect_equal(hamming(strand1, strand2), 0)
})

test_that("disallow first strand longer", {
strand1 <- "AATG"
strand2 <- "AAA"
expect_error(hamming(strand1, strand2))
})

test_that("disallow second strand longer", {
strand1 <- "ATA"
strand2 <- "AGTG"
expect_error(hamming(strand1, strand2))
})

message("All tests passed for exercise: hamming")``````
``````library(magrittr)

hamming <- function(strand1,strand2) {

dplyr::case_when(
identical(strand1, strand2) ~ as.integer(0),
nchar(strand1) == nchar(strand2) ~ distance(strand1, strand2)
)

# omitting explicit stop("Different lengths!") doesn't cause last test to fail
}

distance <- function(strand1, strand2) {

bases1 <- unlist(strsplit(strand1, ""))
bases2 <- unlist(strsplit(strand2, ""))

# detect base mistmatches, adding them up
purrr::map2_lgl(bases1, bases2, stringi::stri_cmp_neq) %>%
sum() %>%
return()
}``````

Hehe, looks like written by a tydiverse fan! However, I have compared the speed with my implementation and it seems to take longer for such a small task. Give a try to the version I submitted and let me know what do you think.

Solution Author
commented over 3 years ago

Tidy data was pretty much the first R lesson I got, and it helped me find a foot-hold in programming. Always a good feeling to return to that verse :-)

In some other exercises here, I optimised for speed as well. In Jon Calder's R-package, I implemented a shortcut for benchmarking: https://github.com/jonmcalder/exercism/commit/396877f4cd5a9070da1d442eb4d3b6668c173dbc ;-)

### 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?