Skip to main content
 首页 » 编程设计

python之通过容器 Python 拆分字符串

2025年05月04日18lautakyan007

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

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) 

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

请您参考如下方法:

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

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

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)) 

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

编辑:

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