使用python查找频繁的字符串模式

dhi*_*v10 9 python string bioinformatics

所以我试图解决这个问题,我必须在python的某些行中找到最常用的6个字母的字符串,所以我意识到可以做这样的事情:

>>> from collections import Counter
>>> x = Counter("ACGTGCA")
>>> x
Counter({'A': 2, 'C': 2, 'G': 2, 'T': 1})
Run Code Online (Sandbox Code Playgroud)

现在,我使用的数据是DNA文件,文件的格式如下:

> name of the protein
ACGTGCA ... < more sequences> 
ACGTGCA ... < more sequences> 
ACGTGCA ... < more sequences> 
ACGTGCA ... < more sequences> 

> another protein 
AGTTTCAGGAC ... <more sequences>
AGTTTCAGGAC ... <more sequences>
AGTTTCAGGAC ... <more sequences>
AGTTTCAGGAC ... <more sequences>
Run Code Online (Sandbox Code Playgroud)

我们可以从一次运行一个蛋白质开始,但是如何修改上面的代码块来搜索最常见的6个字符的字符串模式?谢谢.

Dun*_*can 4

我认为最简单的方法就是这样做:

>>> from collections import Counter
>>> protein = "AGTTTCAGGAC"
>>> Counter(protein[i:i+6] for i in range(len(protein)-5))
Counter({'TTCAGG': 1, 'AGTTTC': 1, 'CAGGAC': 1, 'TCAGGA': 1, 'GTTTCA': 1, 'TTTCAG': 1})
Run Code Online (Sandbox Code Playgroud)

  • @dhillonv10:`Counter.most_common` (http://docs.python.org/library/collections.html#collections.Counter.most_common)。`Counter` 对象有许多有用的方法。 (2认同)