Last active
May 7, 2017 13:34
-
-
Save puhitaku/ee39afe21d5c7dac4b9baee88ed98450 to your computer and use it in GitHub Desktop.
Be an AGTC programmer.
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 python3.6 | |
| import sys | |
| a = 'AGTC' | |
| def encode(s): | |
| out = [] | |
| for c in s.encode(): | |
| seq = [ | |
| a[(c & 0xC0) >> 6], | |
| a[(c & 0x30) >> 4], | |
| a[(c & 0x0C) >> 2], | |
| a[(c & 0x03) >> 0]] | |
| out.append(''.join(seq)) | |
| return ''.join(out) | |
| def decode(s): | |
| out = [] | |
| for c in range(0, len(s), 4): | |
| seq = [ | |
| a.index(s[c+0]) << 6, | |
| a.index(s[c+1]) << 4, | |
| a.index(s[c+2]) << 2, | |
| a.index(s[c+3]) << 0] | |
| out.append(sum(seq)) | |
| return(bytes(out).decode()) | |
| def transcript(s): | |
| lut = dict(zip('AGTC', 'UCAG')) | |
| out = '' | |
| for c in s: | |
| out += lut[c] | |
| return out | |
| def main(): | |
| plain = sys.argv[-1] | |
| print(f'Plain: {plain}') | |
| print(f'DNA: {encode(plain)}') | |
| print(f'RNA: {transcript(encode(plain))}') | |
| print(f'Enc->Dec: {decode(encode(plain))}') | |
| if __name__ == '__main__': | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment