user5601425
user5601425

Reputation:

Python list slicing sequence genetic code

I have a list and I want to extract the value of each element from a dictionary and I want to return to get a new list starting from AUG and ending at '*' or an element which doesn't exist in the dictionary.

For example:

CodD = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
 "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
 "UAU":"Y", "UAC":"Y", "UAA":"*", "UAG":"*",
 "UGU":"C", "UGC":"C", "UGA":"*", "UGG":"W",
 "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
 "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
 "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
 "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
 "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
 "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
 "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
 "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
 "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
 "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
 "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
 "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G",}

with input list:

['UAU', 'AUG', 'AAA', 'UAG', 'CAA', 'GUU', 'UUA', 'UUU', 'AAA', 'UAA', 'GGG',
 'UUU', 'AAA', 'UAC', 'AUU', 'ACA', 'CAU', 'AAC', 'AUU', 'UAG', 'ACU', 'UAG',
 'GGG', 'AUG', 'AAA', 'AAA', 'ACC', 'AAA', 'AAC', 'CAG', 'UUU', 'GUU', 'ACU',
 'UAA', 'CAU', 'GGC', 'AUU', 'GGG', 'CAG']

the result would be

['M','K']
['M','K','K','T','K','Q']

These are formed by:

  1. Finding the first 'AUG' element in the list, this starts an output sequence
  2. Add each result of CodD[element] to the output sequence
  3. An output sequence ends if CodD[element] doesn't exist, or is '*'.
  4. go back to 1. until the input list is exhausted

It doesn't matter if 'AUG' is found again during such a sequence, once started.

Upvotes: 1

Views: 211

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122232

You could use a generator:

def sequences(mapping, lst):
    result = None
    for elem in lst:
        if elem == 'AUG' and result is None:
            # start a new list
            result = []
        if result is None:
            # not currently creating an input sequence, ignore this element
            continue
        value = mapping.get(elem)
        if value is None or value == '*':
            # sequence end
            yield result
            result = None
            continue
        result.append(value)

Demo:

>>> # I named your list 'sample' here
...
>>> for result in sequences(CodD, sample):
...     print result
...
['M', 'K']
['M', 'K', 'K', 'T', 'K', 'N', 'Q', 'F', 'V', 'T']

Upvotes: 1

Related Questions