在dna列表中找到导致最小汉明距离的k聚体

时间:2017-08-18 17:30:37

标签: python hamming-distance

我正在编写一个程序,给出输入k =某个数字,DNA = dna的片段列表,输出应该给出一个大小为k的k-mer,它在字符串数组中具有最小的汉明距离。我有三个函数,1。计算k-mer和片段dna的不同窗口之间的汉明距离,并返回具有最低分数的窗口的汉明距离,2。生成所有可能的k-mers大小的汉明距离k,和3.遍历所有大小为k的窗口和每个可能的k-mer的窗口。不幸的是,我的程序给了我输出AAA,这是不正确的。我知道我的逻辑错误不在组合(k)和hammingDistance中,因为我之前使用它们来获得正确的结果。

import itertools
def combination(k):
    bases=['A','T','G','C']
    combo=[''.join(p) for p in itertools.product(bases, repeat=k)]
    return combo

def hammingDistance(pattern, seq):
        if pattern == seq:
               return 0
        else:
                dist=0
                for i in range(len(seq)):
                        if pattern[i] != seq[i]:
                                dist += 1
        return dist

def median_string(k, DNA):
    k_mers = combination(k)
    distance = 0 
    temp = 1000000000000000000
    for string in DNA:
        hamming = 1000000000000000000
        c = 0
        for k_mer in k_mers:
            for subset in string[c: len(string) - k]:
                if hamming > hammingDistance(k_mer, string[c : c+k]):
                    hamming = hammingDistance(k_mer, string[c : c+k])
                c += 1
            distance += hamming
            if distance < temp:
                temp = distance
                best_pattern = k_mer
            distance = 0
    return best_pattern

1 个答案:

答案 0 :(得分:0)

事实证明,在最后一个条件中只是一个缩进错误。

def median_string(k, DNA):
    k_mers = combination(k)
    distance = 0
    temp = 1000000000000000000
    for k_mer in k_mers:
        for string in DNA:
            hamming = 1000000000000000000
            c = 0
            for subset in string[c: len(string) - k]:
                if hamming > hammingDistance(k_mer, string[c : c+k]):
                    hamming = hammingDistance(k_mer, string[c : c+k])
                c += 1
            distance += hamming
        if distance < temp:
                temp = distance
                best_pattern=k_mer
        distance=0
    return best_pattern