user15229523
user15229523

Reputation:

a program to change letters in DNA nucleotide

I'm making a program to change the sequence of DNA. When finding the letter "a", replace it with a "t", and opposite. But I've encountered a problem cause when the program runs, it replaces "a" with "t", then replaces "t" with "a" again.

How can I fix that?

The code:

def opposite_nucleotide(dna):
     dna = dna.replace("a", "t")
     dna = dna.replace("t", "a")
     dna = dna.replace("g", "c")
     dna = dna.replace("c", "g")
     return dna

dna3 = input("Enter the DNA to represent it's oppsite one: ")
dna4 = opposite_nucleotide(dna3)
print(dna4)

Upvotes: 0

Views: 571

Answers (6)

Aparna Prasad
Aparna Prasad

Reputation: 91

what is the input file format? is it a fasta file ? do you want the program to just replace all the strings in the file with t to a and vice versa? or just replace only the string you enter?

Upvotes: 0

Silvio Mayolo
Silvio Mayolo

Reputation: 70267

As it turns out, this functionality is built-in in Python.

str.translate(table)

Return a copy of the string in which each character has been mapped through the given translation table. ...

You can use str.maketrans() to create a translation map from character-to-character mappings in different formats.

Example usage:

def opposite_nucleotide(dna):
    return dna.translate(str.maketrans("atgc", "tacg"))

str.maketrans can be called several ways, but the form I use above is similar to how you would use tr// in Perl: each character in the first argument will be replaced by the corresponding character in the second.

Obviously, if you're doing this for coursework or educational purposes, don't just appeal to a built-in.

Upvotes: 9

summea
summea

Reputation: 7583

Would an approach like this work?

def opp(string):
    result = []
    for char in string:
        if char == 'a':
            result.append('t')
        elif char == 't':
            result.append('a')
        elif char == 'g':
            result.append('c')
        elif char == 'c':
            result.append('g')
    return ''.join(result)

print(opp('atgc'))

Output:

tacg

Thanks @bullseye for the idea; I changed the extra if statements to elif for now!

Upvotes: 0

DarkLeader
DarkLeader

Reputation: 589

 def opposite_nucleotide(dna):
     new_dna = ""
     for char in dna:
         if char == "a":
             new_dna += "t"
         elif char == "t":
             new_dna += "a"
         elif char == "g":
             new_dna += "c"
         else:
             new_dna += "g"
     return new_dna

I guess this should work.. not pretty but should work

Upvotes: 3

Chris Clayton
Chris Clayton

Reputation: 251

The simplest modification to your code to do this (though likely not the most efficient), would be to use temporary replacements, which are characters known to be missing from the string. By replacing "a" with "x", for example, then replacing "t" with "a", and finally "x" with "t", you will not replace letters which have already been replaced.

def opposite_nucleotide(dna):
     dna = dna.replace("a", "x")
     dna = dna.replace("t", "a")
     dna = dna.replace("x", "t")
     dna = dna.replace("g", "y")
     dna = dna.replace("c", "g")
     dna = dna.replace("y", "c")
     return dna

dna3 = input("Enter the DNA to represent it's oppsite one: ")
dna4 = opposite_nucleotide(dna3)
print(dna4)

Upvotes: 0

Bing Wang
Bing Wang

Reputation: 1598

#this won't work well for large DNA strings

m=dict(zip('atgc','tacg'))
dna4=''.join([m[c] for c in dna3])

Upvotes: 2

Related Questions