【问题标题】:determine whether combination of protein fragments might cover a complete protein sequence确定蛋白质片段的组合是否可能覆盖完整的蛋白质序列
【发布时间】:2019-08-03 08:11:44
【问题描述】:

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

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

【问题讨论】:

  • 您可能想知道 Stack Exchange 网络上有一个以生物信息学为重点的问答网站:bioinformatics.stackexchange.com。您会在 StackOverflow 上从更广泛的程序员那里获得回复,但在 BINFX 网站上,您会收到每天使用相关数据类型的人的回复。

标签: python bioinformatics


【解决方案1】:

这是另一种递归方法,它实际上给您列表中的任何值加起来为 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

【讨论】:

  • seqs = [15,100] 不匹配并且您不保存 item+otheritem 所以seqs = [30,30,40] 也不会匹配
  • @IQbrod 它检查第一个等于 100 的结果,然后中断循环,因此只打印一个 True 语句。 seqs = [15,100]seqs = [0,15,100] 都打印一次 True。我不确定我是否理解您所说的不起作用。
  • if current_weight == total_weight 应该放在第二个循环之前,您不会在每个循环中重置 current_wieghtseqs = [15,100] 不匹配 0 + 15 ? 15 | 15 == 100 ? False | 15 + 100 ? Nothing | 15 == 100 ? False
  • if current_weight &gt; total_weight: 永远不会到达,因为如果它低于 total_weight,您将不会添加到 current_weight。您还必须将 &lt; 替换为 &lt;= 否则将不匹配
  • N+ 组合也不起作用。 seqs = [15, 70 ,80, 5] As 15 +70 + 5 将导致 90
【解决方案2】:

考虑在您的 seq 列表中使用来自 itertools 的排列:

from itertools import permutations 
perm_list = list(permutations(seqs))
perm_list

提供以下输出:

[(50, 70, 30),
 (50, 30, 70),
 (70, 50, 30),
 (70, 30, 50),
 (30, 50, 70),
 (30, 70, 50)]

然后您可以遍历这些组合,看看哪些值可能等于总权重。

希望这是有用的,干杯!

【讨论】:

  • 您将为我认为过于消耗的 n 向量生成 n!
【解决方案3】:

您的代码显然不会打印 True as

0 + 50 = 50
50 & 70 => Nothing happens
50 + 30 = 80

对于每个条目,您可能会尝试添加或不添加下一个条目,因此您的函数将有两个参数,已经分组的参数和其余参数:

def calculate(current: int, next: int[]):
  pass

您想检查当前元素是否是您的总重量,如果无法添加任何内容,您将无法获得更多信息

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

def calculate(current: int, next: int[]):
  if(current == total_weight):
    return True
  if(not next):
    return False

现在你检查你们中的一个计算是否会导致你的总数

def calculate(current: int, next: int[]):
  if(current == total_weight):
    return True
  if(not next):
    return False
  #Edit: x does not require to be calculated in every cases
  x = False
  if current+ next[0] <= total_weight:
    x = calculate(current+ next[0], next[1:]) #with
  y = calculate(current, next[1:]) #without
  return x or y

print(calculate(current_weight, data))

您可能需要线程来更快地执行并中止大型数据集上的下一个计算步骤

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2022-07-14
    • 2020-12-22
    相关资源
    最近更新 更多