0

I just started learning Python, and I'm stuck in this problem: I have a DNA sequence, and I need to return its complementary sequence. For example, if I have ATTGCA, it should return TAACGT. This is, replace A by T, T by A, C by G and G by C. It's an exercise, and I'm not supposed to use string methods. Everything I tried until now, return me 'T' as answer. Seems it only recognize the first letter, then stops. How can I do it?

I tried:

>>> def get_complementary_sequence(dna):
    for char in dna:
        if char == 'A':
            return 'T'
        elif char == 'T':
            return 'A'
        elif char == 'C':
            return 'G'
        elif char == 'G':
            return 'C'


>>> get_complementary_sequence('ATTGCA')
'T'

And also tried:

def get_complementary_sequence(dna):
    sequence = ""
    for nucleotide in dna:
        if nucleotide == 'A':
            return sequence + 'T'
        elif nucleotide == 'T':
            return sequence + 'A'
        elif nucleotide == 'C':
            return sequence + 'G'
        elif nucleotide == 'G':
            return sequence + 'C'
        return sequence


>>> get_complementary_sequence('ATTGCA')
'T'
4
  • 8
    Everything I tried until now -> then show us what you tried? Commented Oct 18, 2012 at 20:09
  • >>> def get_complementary_sequence(dna): for char in dna: if char == 'A': return 'T' elif char == 'T': return 'A' elif char == 'C': return 'G' elif char == 'G': return 'C' >>> get_complementary_sequence('ATTGCA') 'T' >>> Commented Oct 18, 2012 at 20:56
  • Post this code in your question.. Edit your question. Commented Oct 18, 2012 at 20:57
  • Question edited with the codes! Commented Oct 18, 2012 at 21:19

5 Answers 5

5

try a dictionary, that way you'll also don't need the if's and elif's:

In [45]: dic={'A':'T','T':'A','C':'G','G':'C'}

In [46]: strs="ATTGCA"

In [47]: ''.join(dic[x] for x in strs)
Out[47]: 'TAACGT'

or using map():

In [52]: ''.join(map(dic.get,strs))
Out[52]: 'TAACGT'
Sign up to request clarification or add additional context in comments.

6 Comments

join is a "string method", so I wonder if this meets the requirements. Though I suppose one could argue that even + is a string method (__add__).
@LaurenceGonsalves IMHO the string method he's concerned with is replace().
Maybe. He said "I'm not supposed to use string methods" not "I'm not supposed to use replace()".
@AshwiniChaudhary: I would have been more concerned about his using "ATAGGTCTCTTA".translate({65: 84, 67: 71, 84: 65, 71: 67}). replace() would have been difficult to use because of the symmetric replacements...
@TimPietzcker I never knew about this method ,thanks for that. But this only works in py3k I guess?
|
3

A string is also a sequence of characters, so you can iterate through it:

for char in sequence:

Comments

2

Here is an equivalent of Ashwini Chaudhary's elegant solution which maybe more comprehensible for beginners:

complements = {'A': 'T',
               'T': 'A',
               'C': 'G',
               'G': 'C'}
dna_sequence = 'ATTGCA'
new_sequence = []
for char in dna_sequence:
    new_sequence.append(complements[char])
# new_sequence is now ['T', 'A', 'A', 'C', 'G', 'T']
result = ''.join(new_sequence) # result in 'TAACGT'

Comments

2

Another way using dictionaries, entirely without string methods :)

trans = {'A':'T','T':'A','C':'G','G':'C'}
with open("temp.txt", "w") as outfile:
    for character in mystring:
        outfile.write(trans[character])
with open("temp.txt") as infile:
    result = infile.read()

2 Comments

+1 why doesn't this came to my mind :), but you can reduce your solution to this
Neat. You might like to use Python tempfile
1

This solution is easy to understand. Hope it helps.

def get_complement(nucleotide):
    """ (str) ->str
    """

    sequence = ""
    for char in nucleotide:
        if char == "A":
            sequence = sequence + "T"
        if char == "T":
            sequence = sequence + "A"
        if char == "G":
            sequence = sequence + "C"
        if char == "C":
            sequence = sequence + "G"

    return sequence

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.