【问题标题】:Splitting Strings by Container Python通过容器 Python 拆分字符串
【发布时间】:2015-06-06 22:49:49
【问题描述】:

所以我正在尝试编写一个可用于分析 DNA 的程序,现在我正在尝试在“链”中拆分基因。为了实现这一点,我需要分析该链并通过三个 STOP 密码子之一(三个碱基对组)将其拆分。我现在的代码是这样的:

class Strand:

  def __init__(self, code):
    self.code = [code]
    self.endCodons = []
    self.genes = []

  def getGenes(self):
    for codon in self.endCodons:
      for code in self.code:
        code = code.split(codon)


strand = Strand("ATCATGCACATAGAAACTGATACACACCACAGTGATCACATGAAGTACACATG")
strand.getGenes()
print(strand.genes)

但是,当我运行它时,它返回一个空列表。 我可以参考一些建议。

【问题讨论】:

  • 你希望你的程序做什么? self.endCodons 是一个空列表,所以 getGenes() 不会做任何事情。
  • 什么是终止密码子?你需要把它们放在你的问题中!
  • @Kasra: StopCondons - en.wikipedia.org/wiki/Stop_codon
  • 我希望从 endCodons 索引结果并使用它们来拆分序列。

标签: python python-3.x split


【解决方案1】:

通过每个终止密码子运行循环并按其拆分会导致输出不正确,因为我认为这些终止密码子可以按序列中的任何顺序出现,并且对终止密码子列表的迭代将要求终止符在该序列中相同的顺序。

所以,如果我理解正确,你需要从左到右扫描你的字符串并以这种方式搜索密码子:

class Strand:
  def __init__(self, code):
    self.code = code
    self.endCodons = ["TAG", "TAA", "TGA"]
    self.genes = []

  def getGenes(self):
    if (len(self.code) % 3 != 0):
      print("Input sequence is not divisible by 3?")

    # In this, we assume each stop codon is always 3 characters.
    iteration = 0
    lastGeneEnd = 0
    while (iteration < len(self.code)):
      # What is our current 3 character sequence? (Unless it's at the end)
      currentSequence = self.code[iteration:iteration + 3]

      # Check if our current 3 character sequence is an end codon
      if (currentSequence in self.endCodons):
        # What will our gene length be?
        geneLength = (iteration + 3) - lastGeneEnd

        # Make sure we only break into multiples of 3
        overlap = 3 - (geneLength % 3)
        # There is no overlap if our length is already a multiple of 3
        if (overlap == 3): overlap = 0

        # Modify the gene length to reflect our overlap into a multiple of 3
        geneLength = geneLength + overlap

        # Update the iteration so we don't process any more than we need
        iteration = iteration + overlap + 3

        # Grab the entire gene sequence, including the stop codon
        gene = self.code[lastGeneEnd:iteration]

        # If we have a 3-length gene and there's nothing left, just append to the last gene retrieved as it has
        # got to be part of the last sequence
        if (len(gene) == 3 and iteration >= len(self.code)):
          lastIndex = len(self.genes) - 1
          self.genes[lastIndex] = self.genes[lastIndex] + gene
          break

        # Make sure we update the last end index so we don't include portions of previous positives
        lastGeneEnd = iteration

        # Append the result to our genes and continue
        self.genes.append(gene)

        continue

      iteration = iteration + 1

strand = Strand("ATCATGCACATAGAAACTGATACACACCACAGTGATCACATGAAGTACACATG")
strand.getGenes()
print("Got Genes: ")
print(strand.genes)

for gene in strand.genes:
  print("Sequence '%s' is a multiple of 3: %u" % (gene, len(gene) % 3 == 0))

我并不是真正的生物学家,所以我可能做了一些不正确的假设。

编辑:

代码保证分成三的倍数,但我似乎仍然不太了解所需的逻辑。它在给定的示例中确实有效,但我不确定它在其他情况下是否可以正常工作。

【讨论】:

  • 您的代码运行良好,只是它没有将基因分成 3 个一组。例如:“AGTAGATAA”应该作为一个基因出现,但它会出现:“AGTAG ATAA”。它应该只将基因分解成 3 的倍数。
  • 啊,好吧,我会纠正那个(因为我确实误解了它应该如何拆分)。
  • 它分为三的倍数,但我不知道这是否正是您所需要的。它还假设输入长度总是可以被三整除(因此如果检查任何悬挂的 3 字符序列),这是一个正确的假设吗?它还假设您不会遇到终止密码子作为字符串中的第一个序列。 (序列从左到右流动,所以这不应该发生?)我认为现在一个更好的例子是适当拆分更长的序列,例如您原始帖子中的内容。
  • 这些链被分成三个称为密码子的组,这些密码子被翻译成氨基酸。所有密码子的长度都是 3 个碱基对,这一点非常重要。
  • 如果我理解正确,这就是我在帖子中编辑的代码所确保的。它被每个终止密码子分开,每个基因的长度可以被 3 整除(因此代码会在之后遍历基因列表,并被 3 修改)。如果我对我对遗传密码的误解向您提出问题,请原谅我。
猜你喜欢
  • 2011-03-29
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多