生成范围(i,j)之间的核苷酸k-mers的所有组合

ljc*_*ljc 4 python combinations permutation bioinformatics python-itertools

我需要生成所有可能的长度在5至15之间的核苷酸组合的列表。

nucleotides = ['A', 'T', 'G', 'C']
Run Code Online (Sandbox Code Playgroud)

预期成绩:

AAAAA
AAAAT
AAAAC
AAAAG
AAATA
AAATT
...
AAAAAAAAAAAAAAA
AAAAAAAAAAAAAAT
etc.
Run Code Online (Sandbox Code Playgroud)

我试过了:

for i in range(5,16):
    for j in itertools.permutations(nucleotides, i):
        print j
Run Code Online (Sandbox Code Playgroud)

但是,如果这样做是行不通的len(nucleotides) < i

提前致谢!

Sel*_*cuk 5

如果要查找所有组合,则应使用.product()as,因为它们.permutations()不会像AAAAA或那样产生重复的核苷酸AATGC。尝试这个:

for i in range(5, 16):
    combinations = itertools.product(*itertools.repeat(nucleotides, i))
    for j in combinations:
        print(j)
Run Code Online (Sandbox Code Playgroud)

更新:如@JaredGoguen所述,该repeat参数也可以在这里使用:

combinations = itertools.product(nucleotides, repeat=i)
Run Code Online (Sandbox Code Playgroud)

  • 您是否不希望对“组合”有个更好的名称,而该名称不是同一模块中的相关功能?不过,认真的评论是,`product`需要一个冗长的`repeat`,应该在这里使用。 (2认同)