1

So, the task is quite simple. I just need to replace all "T"s with "U"s in an input string of DNA. I have written the following code:

def transcribe_dna_to_rna(s):
base_change = {"t":"U", "T":"U"}
replace = "".join([base_change(n,n) for n in s])
return replace.upper()

and for some reason, I get the following error code:

'dict' object is not callable

Why is it that my dictionary is not callable? What should I change in my code? Thanks for any tips in advance!

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Maskurate
  • 21
  • 4
  • Note that in bioinformatics, case (lower or upper) is often important and should be preserved, so keeping both t ->u and T -> U is important. See, for example, https://bioinformatics.stackexchange.com/q/225/6251 – Timur Shtatland Sep 10 '21 at 14:18

4 Answers4

3

To correctly convert DNA to RNA nucleotides in string s, use a combination of str.maketrans and str.translate, which replaces thymine to uracil while preserving the case. For example:

s = 'ACTGactgACTG'
s = s.translate(str.maketrans("tT", "uU"))
print(s)
# ACUGacugACUG

Note that in bioinformatics, case (lower or upper) is often important and should be preserved, so keeping both t -> u and T -> U is important. See, for example:
Uppercase vs lowercase letters in reference genome

SEE ALSO:
Character Translation using Python (like the tr command)

Note that there are specialized bioinformatics tools specifically for handling biological sequences.

For example, BioPython offers transcribe:

from Bio.Seq import Seq
my_seq = Seq('ACTGactgACTG')
my_seq = my_seq.transcribe()
print(my_seq)
# ACUGacugACUG

To install BioPython, use conda install biopython or conda create --name biopython biopython.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
1

The syntax error tells you that base_change(n,n) looks like you are trying to use base_change as the name of a function, when in fact it is a dictionary.

I guess what you wanted to say was

def transcribe_dna_to_rna(s):
    base_change = {"t":"U", "T":"U"}
    replace = "".join([base_change.get(n, n) for n in s])
    return replace.upper()

where the function is the .get(x, y) method of the dictionary, which returns the value for the key in x if it is present, and otherwise y (so in this case, return the original n if it's not in the dictionary).

But this is overcomplicating things; Python very easily lets you replace characters in strings.

def transcribe_dna_to_rna(s):
    return s.upper().replace("T", "U")

(Stole the reordering to put the .upper() first from @norie's answer; thanks!)

If your real dictionary was much larger, your original attempt might make more sense, as long chains of .replace().replace().replace()... are unattractive and eventually inefficient when you have a lot of them.

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

In python 3, use str.translate:

dna = "ACTG"
rna = dna.translate(str.maketrans("T", "U"))  # "ACUG"
choroba
  • 231,213
  • 25
  • 204
  • 289
-1

Change s to upper and then do the replacement.

def transcribe_dna_to_rna(s):
    return s.upper().replace("T", "U")
norie
  • 9,609
  • 2
  • 11
  • 18