Python实现DNA序列互补、反向、反向互补

DNA有两条链,但是通常我们只用其中一条链来表示,另外一条链是它的反向互补序列。利用Python我们可以轻松地实现DNA序列的反向、互补、及反向互补。

定义函数DNA_complement()实现DNA序列互补配对,函数DNA_reverse() 实现DNA序列反向。

def DNA_complement(sequence):
    sequence = sequence.upper()
    sequence = sequence.replace('A', 't')
    sequence = sequence.replace('T', 'a')
    sequence = sequence.replace('C', 'g')
    sequence = sequence.replace('G', 'c')
    return sequence.upper()
 
def DNA_reverse(sequence):
    sequence = sequence.upper()
    return sequence[::-1]

我们来演示一下:

>>> DNA = "acctgCaG" #原序列
>>> DNA_complement(DNA) #互补序列
'TGGACGTC'
>>> DNA_reverse(DNA) #反向序列
'GACGTCCA'
>>> DNA_reverse(DNA_complement(DNA)) #反向互补序列
'CTGCAGGT'