如何对连字符的fasta格式字符串进行编码,以将所有连续的核苷酸和连字符以及encode them as run length分组。在
把我的序列看作“ATGC----CGCTA--G---”。字符串的序列是Nucleotide,后面是连字符序列。我尝试将所有连续的核苷酸分组为字母M
,连续连字符为字母D
,并以子序列的大小作为前缀。在
此编码的最终结果应该是4M4D5M5D1M3D
。在
下面的图片进一步说明了这一点
ATGC----CGCTA-----G---
| | | | | |
V V V V V V
4M 4D 5M 5D 1M 3D
当我使用Counter
或list.count()
时,我得到"M":10 "D":12
:
经典方法:
这个问题是itertools.groupby的理想问题
实施
输出 “4M4D5M5D1M3D”
说明
值得注意的是,关键功能在这里至关重要。根据是否是字母表对序列进行分组。一旦完成,就应该直接计算每个组的大小,并从关键元素中找出组的类型。在
对代码的一些解释
'DM'[k]
:这只是表示"M" if k == True else "D"
的一种巧妙方式len(list(g))
:确定每个组的大小。或者,它可以写成sum(1 for e in g)
'{}{}'.format
:字符串格式,用于创建连续频率和类型的连接''.join(
:将列表元素作为字符串序列联接。在相关问题 更多 >
编程相关推荐