#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Sergey Sobko' __email__ = 'S.Sobko@profitware.ru' __copyright__ = 'Copyright 2014, The Profitware Group' def nucleobase_yielder(): while True: yield 'A' yield 'A' yield 'U' yield 'G' rna_new_state = { 'AUG': 'READING_TRIPLETS', 'UAA': 'STOP_CODON', 'UAG': 'STOP_CODON', 'UGA': 'STOP_CODON' } def get_triplets_and_states(nucleobase_list=None): if nucleobase_list is None: return state = 'TRYING_TO_FIND_START_CODON' all_nucleobases = list() all_nucleobases_current_position = 0 remainder = 0 last_triplet = None for nucleobase in nucleobase_yielder(): all_nucleobases.append(nucleobase) all_nucleobases_current_position += 1 if state == 'READING_TRIPLETS' and all_nucleobases_current_position % 3 != remainder: continue if len(all_nucleobases) > 2: last_triplet = all_nucleobases[all_nucleobases_current_position - 3:] maybe_new_state = rna_new_state.get(''.join(last_triplet)) if maybe_new_state: if state == 'TRYING_TO_FIND_START_CODON' and maybe_new_state == 'READING_TRIPLETS': remainder = all_nucleobases_current_position % 3 state = maybe_new_state if len(all_nucleobases) > 100: break if last_triplet and state: yield ''.join(last_triplet), state if state == 'STOP_CODON': break return def main(): for triplet, state in get_triplets_and_states(nucleobase_list=nucleobase_yielder()): print triplet, state if __name__ == '__main__': main()