【问题标题】:Is there a way to replace the first and last three characters in a list of sequences using Python?有没有办法使用 Python 替换序列列表中的第一个和最后三个字符?
【发布时间】:2020-02-27 09:53:32
【问题描述】:

我正在尝试使用 Python 替换序列列表中的某些字符,这些序列将被发送出去进行合成。有问题的字符是每个序列的第一个和最后三个。我也试图在每个字符之间添加一个 * 。

棘手的部分是第一个和最后一个字符需要与其他两个不同。

例如:DNA 序列 TGTACGTTGCTCCGAC 需要更改为/52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*/i2MOErA/*/32MOErC/

第一个字符需要是 /52MOEr_/,最后一个字符需要是 /32MOEr_/,其中 _ 是该索引处的字符。对于上面的示例,第一个是 T,最后一个是 C。另外两个,GT 和 GA 需要 /i2MOEr_/ 修改。

到目前为止,我已经使用.split() 函数将序列转换为列表。最终结果是 ['AAGTCTGGTTAACCAT', 'AATACTAGGTAACTAC', 'TGTACGTTGCTCCGTC', 'TGTAGTTAGCTCCGTC']。我已经玩了一段时间,但我觉得我需要一些指导。

这不是我想的那么容易吗?

【问题讨论】:

    标签: python python-3.x bioinformatics


    【解决方案1】:

    您可以只使用分治算法。这是我实现目标的解决方案。

    dna = "TGTACGTTGCTCCGAC"
    dnaFirst3Chars = '/52MOEr' + dna[0] + '/*/i2MOEr' + dna[1] + '/*/i2MOEr' + dna[2] + '/*'
    dnaMiddle = '*'.join(dna[3:-3])
    dnaLast3Chars = '*/i2MOEr' + dna[-3] + '/*i2MOEr' + dna[-2] + '/*/32MOEr' + dna[-1] + '/'
    
    dnaTransformed = dnaFirst3Chars + dnaMiddle + dnaLast3Chars
    
    print(dnaTransformed)
    

    输出:

    /52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*i2MOErA/*/32MOErC/
    

    更新:

    为简单起见,您可以将上述代码转换成这样的函数:

    def dna_transformation(dna):
        """ Takes a DNA string and returns the transformed DNA """
    
        dnaFirst3Chars = '/52MOEr' + dna[0] + '/*/i2MOEr' + dna[1] + '/*/i2MOEr' + dna[2] + '/*'
        dnaMiddle = '*'.join(dna[3:-3])
        dnaLast3Chars = '*/i2MOEr' + dna[-3] + '/*i2MOEr' + dna[-2] + '/*/32MOEr' + dna[-1] + '/'
    
        return dnaFirst3Chars + dnaMiddle + dnaLast3Chars
    
    print(dna_transformation("TGTACGTTGCTCCGAC")) # call the function
    

    输出: /52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*i2MOErA/*/32MOErC/

    【讨论】:

      【解决方案2】:

      假设您的预期结果中有错字,实际上应该是 /52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*/i2MOErA/*/32MOErC/ 下面的代码将起作用:

      # python3
      def encode_sequence(seq):
          seq_front = seq[:3]
          seq_back = seq[-3:]
          seq_middle = seq[3:-3]
          front_ix = ["/52MOEr{}/", "/i2MOEr{}/", "/i2MOEr{}/"]
          back_ix = ["/i2MOEr{}/", "/i2MOEr{}/", "/32MOEr{}/"]
          encoded = []
          for base, index in zip(seq_front, front_ix):
              encoded.append(index.format(base))
          encoded.extend(seq_middle)
          for base, index in zip(seq_back, back_ix):
              encoded.append(index.format(base))
          return "*".join(encoded)
      

      通读代码并确保您理解它。本质上,我们只是对原始字符串进行切片并将碱基插入到您需要的格式中。最终输出的每个元素都被添加到一个列表中,并在末尾用 * 字符连接。

      如果您需要动态指定从序列前后提取的碱基的数量和名称,您可以使用此版本。注意{} 大括号告诉string.format 函数在哪里插入基数。

      def encode_sequence_2(seq, front_ix, back_ix):
          seq_front = seq[:len(front_ix)]
          seq_back = seq[-len(back_ix):]
          seq_middle = seq[len(front_ix):-len(back_ix)]
          encoded = []
          for base, index in zip(seq_front, front_ix):
              encoded.append(index.format(base))
          encoded.extend(seq_middle)
          for base, index in zip(seq_back, back_ix):
              encoded.append(index.format(base))
          return "*".join(encoded)
      

      这是输出:

      > seq = "TGTACGTTGCTCCGAC"
      > encode_sequence(seq)
      /52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*/i2MOErA/*/32MOErC/
      

      如果您有要编码的序列列表,您可以遍历该列表并对每个序列进行编码:

      encoded_list = []
      for seq in dna_list:
          encoded_list.append(encode_sequence(seq))
      

      或者使用列表理解:

      encoded_list = [encode_sequence(seq) for seq in dna_list)]
      

      【讨论】:

      • 谢谢埃文。你是对的,我已经修正了错字,非常抱歉。我想我能够理解代码是如何工作的。有没有办法将这种方法用于序列列表?例如,我有一堆 DNA 序列并想应用该算法。 >>> dna = "'AAGTCTGGTTAACCAT AATACTAGGTAACTAC TGTACGTTGCTCCGTC TGTAGTTAGCTCCGTC" >>> dna_list = dna.split() >>> encode_sequence(dna_list) "/52MOEr'AAGTCTGGTTAACCAT/*/i2MOErAATACTAGGTAACTAC/*/i2MOErTGTACGTTGCTCCGTC/*/i2MOErAATACTAGGTAACTAC/*/i2MOErTGTACGTTGCTCCGTC/*/32MOErTGTAGTTAGCTCCGTC/" 这就是我得到的输出。
      • 您希望每个序列都有一个单独的编码字符串,对吧?试试我添加到答案末尾的示例。这将对列表中的每个序列进行编码,并创建一个新的编码序列列表。这就是你想要的吗?
      猜你喜欢
      • 2021-07-06
      • 2019-09-30
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多