Avatar of 4d47
0
0
Genius
0
1

4d47's solution

to Hamming in the Perl 6 Track

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.

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

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

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Hamming;
plan 15;

my Version:D $version = v3;

if Hamming.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nHamming is {Hamming.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

my $c-data = from-json $=pod.pop.contents;
for $c-data<cases>.values {
  if .<expected><error> {
    throws-like {hamming-distance(|.<input><strand1 strand2>)}, Exception, .<description>;
  } else {
    is hamming-distance(|.<input><strand1 strand2>), |.<expected description>;
  }
}

=head2 Canonical Data
=begin code
{
"exercise": "hamming",
"version": "2.1.0",
  "comments": [
    "Language implementations vary on the issue of unequal length strands.",
    "A language may elect to simplify this task by only presenting equal",
    "length test cases.  For languages handling unequal length strands as",
    "error condition, unequal length test cases are included here and are",
    "indicated with an expected value of -1.  Note however that -1 is",
    "simply an indication here in the JSON.  Actually returning -1 from",
    "a hamming distance function may or may not be idiomatic in a language.",
    "Language idioms of errors or exceptions should be followed.",
    "Alternative interpretations such as ignoring excess length at the end",
    "are not represented here."
  ],
  "cases": [
    {
      "description": "empty strands",
      "property": "distance",
      "input": {
        "strand1": "",
        "strand2": ""
      },
      "expected": 0
    },
    {
      "description": "identical strands",
      "property": "distance",
      "input": {
        "strand1": "A",
        "strand2": "A"
      },
      "expected": 0
    },
    {
      "description": "long identical strands",
      "property": "distance",
      "input": {
        "strand1": "GGACTGA",
        "strand2": "GGACTGA"
      },
      "expected": 0
    },
    {
      "description": "complete distance in single nucleotide strands",
      "property": "distance",
      "input": {
        "strand1": "A",
        "strand2": "G"
      },
      "expected": 1
    },
    {
      "description": "complete distance in small strands",
      "property": "distance",
      "input": {
        "strand1": "AG",
        "strand2": "CT"
      },
      "expected": 2
    },
    {
      "description": "small distance in small strands",
      "property": "distance",
      "input": {
        "strand1": "AT",
        "strand2": "CT"
      },
      "expected": 1
    },
    {
      "description": "small distance",
      "property": "distance",
      "input": {
        "strand1": "GGACG",
        "strand2": "GGTCG"
      },
      "expected": 1
    },
    {
      "description": "small distance in long strands",
      "property": "distance",
      "input": {
        "strand1": "ACCAGGG",
        "strand2": "ACTATGG"
      },
      "expected": 2
    },
    {
      "description": "non-unique character in first strand",
      "property": "distance",
      "input": {
        "strand1": "AAG",
        "strand2": "AAA"
      },
      "expected": 1
    },
    {
      "description": "non-unique character in second strand",
      "property": "distance",
      "input": {
        "strand1": "AAA",
        "strand2": "AAG"
      },
      "expected": 1
    },
    {
      "description": "same nucleotides in different positions",
      "property": "distance",
      "input": {
        "strand1": "TAG",
        "strand2": "GAT"
      },
      "expected": 2
    },
    {
      "description": "large distance",
      "property": "distance",
      "input": {
        "strand1": "GATACA",
        "strand2": "GCATAA"
      },
      "expected": 4
    },
    {
      "description": "large distance in off-by-one strand",
      "property": "distance",
      "input": {
        "strand1": "GGACGGATTCTG",
        "strand2": "AGGACGGATTCT"
      },
      "expected": 9
    },
    {
      "description": "disallow first strand longer",
      "property": "distance",
      "input": {
        "strand1": "AATG",
        "strand2": "AAA"
      },
      "expected": {"error": "left and right strands must be of equal length"}
    },
    {
      "description": "disallow second strand longer",
      "property": "distance",
      "input": {
        "strand1": "ATA",
        "strand2": "AGTG"
      },
      "expected": {"error": "left and right strands must be of equal length"}
    }
  ]
}
=end code
unit module Hamming:ver<1>;

sub hamming-distance ($strand1, $strand2) is export {
    die "left and right strands must be of equal length" when $strand1.chars != $strand2.chars;
    sum ($strand1.comb Z $strand2.comb).flat.map: { $^a ne $^b }
}

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?

Community comments

See what others have said about this solution
11 months ago
4d47 says

There are several things I dislike with this solution:

The zip of the two lists
The flattening
The sum over booleans
The mix of procedural (sum), operator (Z), method (flat) and "alternate method calling syntax using colon i can't find doc about anymore" (map:)