0

I am trying to make a program, which replaces letters with other letters, for instance 'Q' into 'R', or 'D' into 'U'. I'm stuck here:

def dna():
    seq = input('Enter the sequence:')
    list1 = list(seq)
    for i in seq:
       if i == 'A':
           seq[i] = 'T'
    return ''.join(list1)

This returns an error. What can I do?

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • 2
    Look into the built-in function [`str.translate()`](https://docs.python.org/3/library/stdtypes.html#str.translate). – zwer Jun 07 '18 at 09:10
  • 2
    What is the bioinformatic transformation you want? Is this transcribing, reverse complementing, or translating sequences, or something else? Whatever it is, biopython can do this for you https://biopython.org/ Without more details you won't get a clear ansewr – Chris_Rands Jun 07 '18 at 09:24
  • 1
    @Chris_Rands I just want to make a simple code which transforms A(Adenin) into T(Thymin), G(Guanin) into C(Cytosin) and vice versa. I am a newbie in both biology and python and this doesn't have a particular purpose. –  Jun 07 '18 at 09:40
  • @coldspeed, that's not really an appropriate dupe IMO, there is a specific bioinformatics solution to this problem – Chris_Rands Jun 07 '18 at 15:10

6 Answers6

1

You have many answers to your problem but none address your issue, so looking at your code and hopefully this can help:

seq is a string and as such is immutable in python, so:

seq[i] = 'T'

is not valid python. You already created a list1 so did you mean:

list1[i] = 'T'

Note, this still wouldn't work because i is not the index but a character in seq but you can get both the index and character with enumerate(), e.g.:

for i, c in enumerate(seq):
    if c == 'A':
        list1[i] = 'T'
...

In python str.translate() is ideal for translating multiple characters, e.g. for your simple example:

def dna():
    t = str.maketrans('A', 'T')
    seq = input('Enter the sequence: ')
    return seq.translate(t)

And this is easy to extend, e.g. A->T and G->C and vice versa would look like:

    t = str.maketrans('AGTC', 'TCAG')
AChampion
  • 29,683
  • 4
  • 59
  • 75
0

If it's as easy as translating one character into another you can use replace. Is this what you want?

def dna():
    seq = input('Enter the sequence: ')
    return seq.replace('A', 'T')

print(dna())
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
0

You can use str.replace:

def dna(seq):
  # Replace Q with R
  seq = seq.replace("Q", "R")
  # Replace D with U
  seq = seq.replace("D", "U")
  return seq

seq = input('Enter the sequence:')
print(dna(seq))

Output:

Enter the sequence: QQQDDD
RRRUUU
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

Here's one way using a comprehension and a lookup dictionary, which should be more easily expandable to include more characters:

mapping = {'Q': 'R', 'D': 'U'}
seq = input('Enter the sequence:')
new = ''.join(mapping.get(c, c) for c in seq)

Example input 'QUAD' becomes 'RUAU'.

sjw
  • 6,213
  • 2
  • 24
  • 39
0

I just want to make a simple code which transforms A(Adenin) into T(Thymin), G(Guanin) into C(Cytosin) and vice versa.

Based on this clarification what you actually want to do is complement your DNA sequences. Biopython provides this functionality:

>>> from Bio.Seq import Seq
>>> seq = Seq('ATCGGAT')
>>> seq.complement()
Seq('TAGCCTA', Alphabet())

You could build your own implementation if you needed, the biopython one uses str.translate in an alphabet aware way (to check your not mixing DNA/RNA etc.), see the source code.

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
0
def dna():
    d={'Adenin':'Thymin','Thymin':'Adenin','Guanin':'Cytosin','Cytosin':'Guanin'}
    seq = raw_input('Enter the sequence:')
    return d[seq]
print dna()

In simple terms, you can try making a dictionary and try to transforms A(Adenin) into T(Thymin), G(Guanin) into C(Cytosin) and vice versa.