[英]searching for the presence of a substring in a larger string with Hamming distance
我有 2 個文件,file1 和 file2 文件 1 具有完整字符串“abcdef”的所有 4-mer、5-mer 和 6-mer 子字符串
file2 有更長的字符串,如
ddghtgabcdtttfwe
dghtgabdatttfwe
hhttaaddsbcdeggd
等我想看看 file2 中的字符串是否與 file1 中的字符串匹配,允許一些不匹配(最大漢明距離 2)。 例如 ddghtgabcdtttfwe 和 ddghtgabcdatttfwe 分別是子串 abcd 和 abcd,abcde 的命中。 你能建議一個在python中做到這一點的好方法嗎
部分解決方案:
def hamming(s1,s2):
return len([(c1,c2) for (c1,c2) in zip(s1,s2) if c1 != c2])
進而:
def almostIn(s1,s2):
n = len(s1)
for s in (s2[i:i+n] for i in range(1 + len(s2)-n)):
if hamming(s1,s) <= 2: return True
return False
如果 s1 在漢明距離 <= 2 的情況下出現在 s2 中,那么后面的函數將返回 True。有一定數量的重新掃描相同的字符,因此這可能不是最佳的,但可能足以滿足您的預期應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.