首页 > 解决方案 > 确定蛋白质片段的组合是否可能覆盖完整的蛋白质序列

问题描述

FASTA 文件包含单个蛋白质序列。第二个 FASTA 文件包含作为第一个文件中序列片段的序列。计算每个序列的分子量,并使用这些确定是否存在可能覆盖完整蛋白质序列的片段组合,而这些片段不重叠

我试图制作以下脚本,但我无法将其全部放入可运行的代码中

所以在

seqs

我把蛋白质片段的重量放在里面

total_weight

我已经放置了完整片段的重量,以测试我正在尝试使用的身体功能。

seqs = [50,70,30]
total_weight = 100
current_weight = 0
for weight in seqs:
    if current_weight + weight == total_weight:
        print(True)
    elif current_weight + weight < total_weight:
        current_weight += weight
    if current_weight > total_weight:
        current_weight -= weight

显然,在这种情况下,我希望这段代码返回 True。为了解决这个问题,我想省略第一个元素

seqs

列出然后重做我所做的'for'循环。不知何故,我无法通过省略第一个元素并再次为新元素运行 for 循环来完成代码

seqs

列表。有人可以指导我正确的方向吗?

标签: pythonbioinformatics

解决方案


这是另一种递归方法,它实际上为您提供列表中的任何值加起来为 100,并将打印出新列表,即语句True

seqs = [50,70,30]
total_weight = 100

def protein_summation_check(target, lst, newLst=[]):
    print(newLst)
    for index,protein in enumerate(lst):
        newLst.append(protein)
        protein_summation_check(target, lst[index+1:], newLst)
        if sum(newLst) == target:
            return ("True",newLst)
        newLst.pop()
    else:
        return False
print(protein_summation_check(total_weight, seqs))

对于并非真正适用于所有解决方案的循环迭代,但适用于您提供的解决方案;

seqs = [50,70,30]
total_weight = 100
current_weight = 0

for index, item in enumerate(seqs):
    if  current_weight == total_weight or item == total_weight:
        print("True")
        break
    for otheritem in seqs[index+1:]:
        if otheritem == total_weight:
            current_weight = total_weight
            break
        if current_weight < total_weight:
            current_weight += otheritem + item
        if current_weight > total_weight:
            if otheritem >= total_weight:
                current_weight -= item
            else:
                current_weight -= otheritem

推荐阅读