Skip to content

Instantly share code, notes, and snippets.

@profitware
Created November 21, 2014 14:49
Show Gist options
  • Select an option

  • Save profitware/c079b85c744eb0346af3 to your computer and use it in GitHub Desktop.

Select an option

Save profitware/c079b85c744eb0346af3 to your computer and use it in GitHub Desktop.

Revisions

  1. profitware created this gist Nov 21, 2014.
    59 changes: 59 additions & 0 deletions bio_test2_5.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    __author__ = 'Sergey Sobko'
    __email__ = '[email protected]'
    __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()