且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

互补DNA序列

更新时间:2022-10-15 13:04:32

这是一个例子,我将如何做 - 只有两行代码:

 从串进口maketrans 

DNA = CCAGCTTATCGGGGTACCTAAATACAGAGATAT #示例DNA片段

def complement(序列):
reverse = sequence [:: - 1]
return reverse.translate(maketrans('ATCG','TAGC'))

print complement(DNA)


I'm having a problem writing this loop; it seems to stop after the second sequence.

I want to return the complementary DNA sequence to the given DNA sequence.

E.g. ('AGATTC') -> ('TCTAAG'), where A:T and C:G

def get_complementary_sequence(dna):
    """(str) -> str

> Return the DNA sequence that is complementary to the given DNA sequence

    >>> get_complementary_sequence('AT')
    ('TA')
    >>> get_complementary_sequence('AGATTC')
    ('TCTAAG')

    """

    x = 0
    complementary_sequence = ''

    for char in dna:
            complementary_sequence = (get_complement(dna))

    return complementary_sequence + (dna[x:x+1])

Can anyone spot why the loop does not continue?

Here is an example how I would do it - only two lines of code really:

from string import maketrans

DNA="CCAGCTTATCGGGGTACCTAAATACAGAGATAT" #example DNA fragment

def complement(sequence):
  reverse = sequence[::-1]
  return reverse.translate(maketrans('ATCG','TAGC'))

print complement(DNA)