在 DNA 序列中寻找团块

Clump Finding in DNA sequence

我正在尝试理解用于解决 DNA 序列中的团簇发现问题的简短代码。问题是

Given integers L and t, a string Pattern forms an (L, t)-clump inside a (larger) string Genome if there is an interval of Genome of length L in which Pattern appears at least t times.

For example, TGCA forms a (25,3)-clump in the following Genome: gatcagcataagggtcccTGCAaTGCAtgacaagccTGCAgttgttttac.

Clump Finding Problem

Find patterns forming clumps in a string.

Given: A string Genome, and integers k, L, and t.

Return: All distinct k-mers forming (L, t)-clumps in Genome.

代码如下:

from collections import defaultdict

def search(inseq, k, L, t):
    lookup = defaultdict(list)
    result = set()
    
    for cursor in range(len(inseq) - k + 1):
        seg = inseq[cursor:cursor + k]
        
        # remove prior positions of the same segment
        # if they are more than L distance far
        while lookup[seg] and cursor + k - lookup[seg][0] > L:
            lookup[seg].pop(0)
        
        lookup[seg].append(cursor)
        if len(lookup[seg]) == t:
            result.add(seg)
    
    return result

这是我的问题,

(1)用defaultdict代替dict的目的是什么?

(2) 什么是查找[seg]?是k-mer团块的起始位置吗?

defaultdict 是一个 Python 对象,如果您请求不在字典中的键,它只是 return 一个 'default' 对象。在这种情况下,默认项是一个列表。 Here is the documentation for defaultdict

看起来 lookup[seg] return 是段 seg 的位置列表,如果它们在被解析的段部分的 L 距离内。因此 lookup[seg] 的 return 对象是您的 DNA 序列的索引列表。

1) 使用defaultdict的目的是什么?

defaultdict(list) 允许您使用 lookup[seg] 访问密钥,并且 "magically" 可以找到一个就绪列表。如果密钥 (seg) 已经存在,这就是您将得到的。否则,您将得到一个空列表。用普通的字典,第二个是错误的。

(2) 什么是查找[seg]?

它是一个序列中的位置列表,只要它们足够接近即可。