Simultaneous functionality.

I have already converted the user input of the DNA code (A,T,G,C) to the RNA code (A,U,G,C) . It was pretty easy.

 RNA_Code=DNA_Code.replace('T','U') 

Now the next thing I need to do is convert the RNA_Code to this add-on. This means that I need to replace A with U, U with A, G with C and C with G, but all at the same time.

if i say

 RNA_Code.replace('A','U') RNA_Code.replace('U','A') 

it converts all As to Us, and then all Us to As, but I stayed with everyone for both.

I need something like AUUUGCGGCAAA and convert it to UAAACGCCGUUU .

Any ideas on how to do this? (3.3)

+6
source share
4 answers

Use the translation table:

 RNA_compliment = { ord('A'): 'U', ord('U'): 'A', ord('G'): 'C', ord('C'): 'G'} RNA_Code.translate(RNA_compliment) 

The str.translate() method translates str.translate() number) into a replacement character. The ord() function gives us a code point for a given character, making it easy to create your map.

Demo version:

 >>> RNA_compliment = {ord('A'): 'U', ord('U'): 'A', ord('G'): 'C', ord('C'): 'G'} >>> 'AUUUGCGGCAAA'.translate(RNA_compliment) 'UAAACGCCGUUU' 
+12
source

You can use the mapping dictionary:

 In [1]: dic={"A":"U","U":"A","G":"C","C":"G"} In [2]: strs="AUUUGCGGCAAA" In [3]: "".join(dic[x] for x in strs) Out[3]: 'UAAACGCCGUUU' 
+5
source

If you are not using it yet, I suggest trying Biopython . It has all sorts of functions for working with biological data, including a pretty cool Seq object. There is a reverse_complement() function that does exactly what you are trying to do, and even more that you did not even think about. Check this out, this is a real time saver.

 >>> from Bio.Seq import Seq >>> from Bio.Alphabet import generic_dna >>> my_dna = Seq("AGTACACTGGT", generic_dna) >>> my_dna Seq('AGTACACTGGT', DNAAlphabet()) >>> my_dna.complement() Seq('TCATGTGACCA', DNAAlphabet()) >>> my_dna.reverse_complement() Seq('ACCAGTGTACT', DNAAlphabet()) 
+1
source

I have a simple solution:

 # get the sequence from the user: dna_seq = input("Please enter your sequence here: ") # make a for loop to read the seq one nucleotide at a time and add each one in a new variable compliment = "" for n in dna_seq: if n == "A": compliment = compliment + "T" elif n == "T": compliment = compliment + "A" elif n == "G": compliment = compliment + "C" elif n == "C": compliment = compliment + "G" print(compliment) 
0
source

Source: https://habr.com/ru/post/944062/


All Articles