Counting Point Mutations

Evolution as a Sequence of Mistake

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. In the case of DNA, a point mutation must change the complementary base accordingly.
Two DNA strands taken from different organism or species genomes are homologous if they share a recent ancestor; thus, counting the number of bases at which homologous strands differ provides us with the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.
We are interested in minimizing the number of (point) mutations separating two species because of the biological principle of parsimony, which demands that evolutionary histories should be as simply explained as possible.

Problem

Given two strings s and t of equal length, the Hamming distance between s and t, denoted dH(s, t), is the number of corresponding symbols that differ in s and t..

Given

Two DNA strings s and t of equal length (not exceeding 1 kbp).

Return

The Hamming distance dH(s, t).


Solution

Let’s define the function that would calculate the point mutations between two strings given:

def hamming_distance(seq1, seq2):
    """Calculate the Hamming difference between 'seq1' and 'seq2'."""
    return sum(0 if a == b else 1 for (a, b) in zip(seq1, seq2))

Then, open and read the dataset:

with open("rosalind_hamm.txt","r") as fasta:
    sequence = str()
    for line in fasta:
        sequence = sequence + line
    list = sequence.split("\n")

sequence_1 = list[0]
sequence_2 = list[1]

And finally, run the code to find out the Hamming distance (or point mutations):

hamming_distance(sequence_1, sequence_2)
Important note:

This problem is taken from rosalind.info. Please visit ROSALIND to find out more about Bioinformatics problems. You may also clink onto links for the definitions of each terminology.