生成循环频谱

Generating a Cyclic Spectrum

我正在尝试从我的肽生成所有循环组合的光谱。

这是我的线性光谱代码:

#For example: LEQN
#L:113 E:129 Q:128 N:114
    peptide = [113,129,128,114]
        for a in peptide:
            for i in peptide[b:]:
                s+= i
                spectrum.append(s)
            s=0
            b += 1

spectrum.sort()
print spectrum

输出:[113、114、128、129、242、242、257、370、371、484]

我的代码成功添加了这些总和 L(113), E(129), Q(128), N(114), LE(113+129), LEQ(113+129+128), LEQN(113 +129+128+114), EQ(129+128), EQN(129+128+114), QN(128+114)

但是 缺少 QNL(128+113+114)、NL(114+113)、NLE(114+113+129)

例如。 QNL 应该是 128+114+113,它是元素 2、3 和 1 的总和。 NL 是 114+133 是元素 3 和 0 的和。NLE 是 113+114+129 是元素 3, 0, 1 的和。

*我不需要添加 EQNL 或 QNLE,因为它们与 LEQN 完全相同。

*但是 LE=242 和 QN=242 具有相同的质量,但不是一回事。

预期输出:113、114、128、129、227(N+L)、242、242、257、355(Q+N+L)、356( N+L+E), 370, 371, 484

如果我对你的问题理解正确,你想要所有可能的子列表,每个长度不超过 peptide 列表的长度,并且对于该列表中的每个起始位置,在列表。一种方法是使用 itertools.

中的 cycle and islice
from itertools import cycle, islice

peptide = [113, 129, 128, 114]
spectrum = []
for num in range(1, len(peptide)):
    for start in range(len(peptide)):
        group = islice(cycle(peptide), start, start + num)
        spectrum.append(sum(group))
spectrum.append(sum(peptide)) # add the entire list only once

这样,sorted(spectrum) 就变成了 [113, 114, 128, 129, 227, 242, 242, 257, 355, 356, 370, 371, 484],这似乎是你想要的。

但不确定这如何扩展到更长的肽列表(我假设在实践中这些列表有四个以上的元素)。

peptide = [113,129,128,114]
peptide *= 2
spectrum = []
for i in range(len(peptide)/2):
    for j in range(i+1,i+4):
       s = 0
       for a in peptide[i:j]:
           s+= a
       spectrum.append(s)
spectrum.sort()
print spectrum