Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find the Hamming distance between two DNA strings

i'm just learning python 3 now. '''It's ask the user for two string and find the Hamming distance between the strings.Input sequences should only include nucleotides ‘A’, ’T’, ‘G’ and ‘C’. Program should ask the user to reenter the sequence if user enter an invalid character.Program should be able to compare the strings are of same length. If the strings are not of the same length program should ask the user to enter the strings again.User should be able to enter upper, lower or both cases as an input '''

The program should print the output in the following format:

please enter string one: GATTACA
please enter string two: GACTATA
GATTACA
|| || |  
GACTATA
The hamming distance of sequence GATTACA and GACTATA is 2
So the Hamming distance is 2.

What I already try below, but could not get answer.

def hamming_distance(string1, string2):
    string1 = input("please enter first sequence")
    string2 = input("please enter second sequence")
    distance = 0
     L = len(string1)
    for i in range(L):
        if string1[i] != string2[i]:
            distance += 1
    return distance
like image 222
Lena Avatar asked Feb 15 '18 04:02

Lena


People also ask

How do you calculate Hamming distance between strings?

Example: Lets say i have 2 strings of equal length "ABCD" and "ACCC". In this example first and second last are same in both string so you need to substitute 2 character in 2nd to make it same hence, hamming distance is 2 in this case. so total hamming distance is 1 + 1 = 2.

What is Hamming distance in DNA?

The number of positions that two codewords of the same length differ is the Hamming distance. 27. In case of DNA sequences, we define this distance as the number of bases by which they differ.

What is the Hamming distance of bit strings 10101 and 11110?

The Hamming distance d(10101, 11110) is 3 because 10101 ⊕ 11110 is 01011 (three 1s).

How do you find the distance between two strings?

There are several ways to measure the distance between two strings. The simplest one is to use hamming distance to find the number of mismatch between two strings. However, the two strings must have the same length.


1 Answers

the line indent error: L = len(strings1)

def hamming_distance(s1, s2):
    if len(s1) != len(s2):
        raise ValueError("Strand lengths are not equal!")
    return sum(ch1 != ch2 for ch1,ch2 in zip(s1,s2))
like image 167
walkman Avatar answered Sep 28 '22 16:09

walkman