Created
November 21, 2014 14:49
-
-
Save profitware/c079b85c744eb0346af3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment