12 Replies - 467 Views - Last Post: 08 May 2013 - 10:37 PM Rate Topic: -----

#1 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 04 May 2013 - 06:33 PM

I have searched for this in three O'Reilly books over the last few days and cannot find an answer. Please help

I have tried to do it as a string instead of a list, I have tried maketrans, dictionaries etc. I need the length to be infinite because it is based on user input.
Is This A Good Question/Topic? 0
  • +

Replies To: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 05 May 2013 - 05:30 AM

Can you be more specific about what you tried and in what way it did not work? Using dictionaries is what I would suggest and I don't see why that approach would not work.

PS: Lists in Python can't be infinite, so if you really want to handle infinite input (that is the user keeps entering data without ever stopping), you should process the input on the fly, without storing it in a list first.
Was This Post Helpful? 0
  • +
  • -

#3 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 05 May 2013 - 12:15 PM

View Postsepp2k, on 05 May 2013 - 05:30 AM, said:

Can you be more specific about what you tried and in what way it did not work? Using dictionaries is what I would suggest and I don't see why that approach would not work.

PS: Lists in Python can't be infinite, so if you really want to handle infinite input (that is the user keeps entering data without ever stopping), you should process the input on the fly, without storing it in a list first.



I asked this question on stack overflow and the comments i got were all that I was somehow ignorant for not wanting to store the user input as a list.Thank you.

for infinite i just mean i would like to take any gene(which is represented by four letters), and genes can be thousands of letters long. Then I need to translate those letters as chunks of 3 as in codon to amino acid translation(like this http://www.biogem.org/codon.jpg)

For dictionaries I cannot seem to make a translation table like i did for the compliment part of the program(i.e. As became Us\, Us became As, Gs became Cs, and Cs became Gs.

replacements = {ord('A'): 'U', ord('U'): 'A', ord('G'): 'C', ord('C'): 'G'}
RNA_Comp=RNA_Code.translate(replacements)
print ('The result of transcription is:' + RNA_Comp)



This doesn't work here because ord only takes single character string arguments.
I cant get a normal dictionary to do anything for me at all.

I tried to do .maketrans to make a table and it wont let me because it wants a 1:1 character translation.

I tried a couple other things but don't remember the specifics of them now.

But I need to take user input, so ATCGGCGCT or ATG or ATGCCGCGCGTTAGCTGTCGATGCTGATGCTTGGCTTTTGCAAATGCTGATGCTGATGGCTGATGCTGATTTGCTAT
or very much longer and translate every 3 letters( so the first 3 letters then the second 3 then the third 3) into one letter Strings like the codon table I sourced here.

I will continue to search for it ( I have recently bought the bioinformatics for python book by O'Reilly so maybe it will shed some light on this for me).

Thanks for thinking abut it anyway.

Sorry the link to the codon table didnt work here is one from wikipedia

http://en.wikipedia....DNA_codon_table
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 05 May 2013 - 12:46 PM

Why ord? Why can't you use a dictionary from strings to strings? And you won't be able to use the translate method - that only works for simple substitutions where there's one single replacement character for each input character.

If you do have your input in a list of 3-character strings, you can simply create a new list where each codon in the list is replaced by dict[codon] (dict being your dicitionary mapping codons to amino acids).
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5776
  • View blog
  • Posts: 12,590
  • Joined: 16-October 07

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 05 May 2013 - 01:21 PM

You seem to have several problems. Break them up into functions and then do the work. e.g.
def codeTrans(code):
	""" take a three letter code, return a single letter """
	# your code here

der breakIntoThrees(s):
	""" take a string and break it into a list of three letter strings """
	# your code here

def transcribe(s):
	triples = breakIntoThrees(s)
	for code in triples:
		result = codeTrans(code)
	...



What might trans look like? It could have a dictionary, or a list of pairs, it could be as simple as:
def codeTrans(code):
	if code=='FOO': return 'X'
	if code=='FOO': return 'Y'
	return ''


Was This Post Helpful? 0
  • +
  • -

#6 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 07 May 2013 - 07:31 AM

The dictionary method makes sense but I can't seem to get it to work. I'm fairly new to programming but I follow along with textbooks on the subject. The problem with them is they all show examples using the interpreter and this isn't exactly realistic(at least i dont think it is). I have to take user input which ends in the form of a list of strings.

I know how to make dictionaries and nest them in a list, which would be great if I could convert the user input list of strings into a list of variables.

Otherwise I don't know how to use dictionaries for this purpose.Simply because one time the list may be ['AUG','UUU',UAA'] and the next could be ['AUG','AUG','AUG'] so simple indexing won;t work.

So I figure I can call the user input list by a name ie NAME=[user input,user input,user input], and make dictionaries for all of the different permutations of the 3 character strings. This would work fine if I could convert the strings into variable names somehow.

I know I can user getattr for sing strings, but once again the user input can be thousands of characters long potentially.

All in all I can't figure out how to use a dictionary for this purpose though everyone tells me that is the way. Wish books would show examples based on user input and not just in the Shell.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5776
  • View blog
  • Posts: 12,590
  • Joined: 16-October 07

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 07 May 2013 - 08:33 AM

View PostDeaven, on 07 May 2013 - 10:31 AM, said:

I have to take user input which ends in the form of a list of strings.


User input is the last step on the journey. You have a function called something like processInput(userInput) and go from there. You can reasonably mock up everything in IDLE; if you want.

View PostDeaven, on 07 May 2013 - 10:31 AM, said:

I know how to make dictionaries


Ok.

View PostDeaven, on 07 May 2013 - 10:31 AM, said:

and nest them in a list


You lost me.

View PostDeaven, on 07 May 2013 - 10:31 AM, said:

I could convert the user input list of strings into a list of variables.


From what to what? What have you tried?


To clarify what I meant by codeTrans:
>>> def codeTrans(code):
...     if code=='FOO': return 'X'
...     if code=='BAR': return 'Y'
...     return '-'
... 
>>> codeTrans('BAR')
'Y'
>>> codeTrans('BAZ')
'-'
>>>



With a dictionary:
>>> codeLookup = {'FOO':'A', 'BAR': 'Z'}
>>> 
>>> def codeTrans(code):
...     if code in codeLookup:
...             return codeLookup[code]
...     return '+'
... 
>>> codeTrans('BAR')
'Z'
>>> codeTrans('BAZ')
'+'
>>> 



It doesn't matter if you use a dictionary, what matters is if you take a code and translate to another one.

The other method I noted would behave something like:
>>> breakIntoThrees('FOOXXXBARFOO')
['FOO', 'XXX', 'BAR', 'FOO']
>>> 



If you loop over that list with the translator, you'd get something like:
>>> [codeTrans(x) for x in breakIntoThrees('FOOXXXBARFOO')]
['X', '-', 'Y', 'X']
>>> 



That result can be joined back up into a string.
Was This Post Helpful? 2
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 07 May 2013 - 08:33 AM

I don't understand why you think you need to convert strings to variable names. Could you explain that?
Was This Post Helpful? 0
  • +
  • -

#9 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 07 May 2013 - 09:18 AM

I do have code for breaking the string into 3s.

n = 3
Chunks=[RNA_Comp[i:i+n] for i in range(0, len(RNA_Comp), n)]
print(Chunks)



This works

I tried the dictionary method

Ribosome={'AUG':'Start', 'UUU':'F', 'UUC':'F'}

def codetrans(Chunks):
    if Chunks in Ribosome:
        return Ribosome[Chunks]
codetrans(Chunks)



and I get the error message
Traceback (most recent call last):
File "C:\Python33\DNA.py", line 65, in <module>
codetrans(Chunks)
File "C:\Python33\DNA.py", line 63, in codetrans
if Chunks in Ribosome:
TypeError: unhashable type: 'list'

I tried the simpler codetrans method

def codetrans(Chunks):
    if Chunks=='AUG':return 'Start'
    if Chunks=='UUU':return 'F'
    if Chunks=='UUC':return 'F'
codetrans(Chunks)
print(Chunks)




And this does nothing to the list

I changed it slightly to
def codetrans(Chunks):
    if 'AUG' in Chunks:return 'Start'



and even though i did the the if this is in Chunks statement for multiple strings I got only Start returned.


I wanted to convert the list to variables ie. [AUG,UUU,UUC] instead of ['AUG','UUU','UUC']
because it's easy in the Shell to go

AUG=['codon':'Start']
UUU=['codon':'F']
UUC=['codon':'F']

Codons=[AUG,UUU,UUC]

for x in Codons:
return x['codon']

Usually I get the error message that a keyword can't be an expression when I make dictionaries as well.
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 07 May 2013 - 09:32 AM

You're not supposed to call codetrans on the list - you're supposed to call it on each element in the list. Look at baavgai's transcribe method.
Was This Post Helpful? 0
  • +
  • -

#11 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 08 May 2013 - 08:26 AM

View Postsepp2k, on 07 May 2013 - 09:32 AM, said:

You're not supposed to call codetrans on the list - you're supposed to call it on each element in the list. Look at baavgai's transcribe method.



Okay I got some progress now I did that, but I needed a place to store the output so I did this

NEWLIST=[]

def codetrans(Chunks):
    if Chunks=='AUG':return 'Start'
    if Chunks=='UUU':return 'F'
    if Chunks=='UUC':return 'F'
NEWLIST.append(codetrans('AUG'))
NEWLIST.append(codetrans('UUU'))
NEWLIST.append(codetrans('UUC'))
print(NEWLIST)




Then I got this output

Your genetic code of interest is:TACAAATACAAG
The result of transcription is:AUGUUUAUGUUC
['AUG', 'UUU', 'AUG', 'UUC']
['Start', 'F', 'F']


Based on the list['AUG', 'UUU', 'AUG', 'UUC']

My output should have been ['Start', 'F','Start','F']

So now my issue is that it is only translating the first instance.

maybe I need to set up a loop that will go through the list one at a time and append to the list one at a time
???????????????
Was This Post Helpful? 0
  • +
  • -

#12 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 08 May 2013 - 08:50 AM

Got it!!!!!!!!!!

This is what I did.

NEWLIST=[]

rock=int(0)

while rock < len(Chunks):
    if Chunks[rock]=='AUG': NEWLIST.append ( 'Start')
    if Chunks[rock]=='UUU': NEWLIST.append ( 'F')
    if Chunks[rock]=='UUC': NEWLIST.append ('F')
    rock= rock +1

print(NEWLIST)



Was This Post Helpful? 0
  • +
  • -

#13 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Translate List i.e.[AUG,UUG,UCC] into this [S,F,W]

Posted 08 May 2013 - 10:37 PM

What you have done is not Pythonic.
I think you would be better off with something like this:
newlist = [] #All caps is for constants of which this is not.

CONVERT_DICT = {"AUG":"Start","UUU":"F","UUC":"F"}

for rock in chunks: #Capitalized names are for classes (CamelCase)
    if rock in CONVERT_DICT:
        newlist.append(CONVERT_DICT[rock])

-Mek

Edit: Looking back at previous posts, this is exactly what people were trying to tell you to do earlier.

This post has been edited by Mekire: 08 May 2013 - 10:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1