Advertisement
Guest User

Untitled

a guest
Aug 31st, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. rna_codon_tbl = {'UUU': 'F', 'CUU': 'L', 'AUU': 'I', 'GUU': 'V',
  2. 'UUC': 'F', 'CUC': 'L', 'AUC': 'I', 'GUC': 'V',
  3. 'UUA': 'L', 'CUA': 'L', 'AUA': 'I', 'GUA': 'V',
  4. 'UUG': 'L', 'CUG': 'L', 'AUG': 'M', 'GUG': 'V',
  5. 'UCU': 'S', 'CCU': 'P', 'ACU': 'T', 'GCU': 'A',
  6. 'UCC': 'S', 'CCC': 'P', 'ACC': 'T', 'GCC': 'A',
  7. 'UCA': 'S', 'CCA': 'P', 'ACA': 'T', 'GCA': 'A',
  8. 'UCG': 'S', 'CCG': 'P', 'ACG': 'T', 'GCG': 'A',
  9. 'UAU': 'Y', 'CAU': 'H', 'AAU': 'N', 'GAU': 'D',
  10. 'UAC': 'Y', 'CAC': 'H', 'AAC': 'N', 'GAC': 'D',
  11. 'UAA': 'Stop', 'CAA': 'Q', 'AAA': 'K', 'GAA': 'E',
  12. 'UAG': 'Stop', 'CAG': 'Q', 'AAG': 'K', 'GAG': 'E',
  13. 'UGU': 'C', 'CGU': 'R', 'AGU': 'S', 'GGU': 'G',
  14. 'UGC': 'C', 'CGC': 'R', 'AGC': 'S', 'GGC': 'G',
  15. 'UGA': 'Stop', 'CGA': 'R', 'AGA': 'R', 'GGA': 'G',
  16. 'UGG': 'W', 'CGG': 'R', 'AGG': 'R', 'GGG': 'G'}
  17.  
  18. # DNA
  19. from collections import Counter
  20.  
  21. with open("rosalind_dna.txt", 'r') as f:
  22. file = f.readline()
  23.  
  24. Counter(sorted(file))
  25.  
  26. # RNA
  27.  
  28. from collections import Counter
  29.  
  30. with open("rosalind_rna.txt", 'r') as f:
  31. file = f.readline()
  32.  
  33. Counter(file.replace('T', 'U').strip())
  34.  
  35. # REVC
  36.  
  37. with open("rosalind_revc.txt", 'r') as f:
  38. file = f.readline().strip()
  39.  
  40. mapping = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
  41. file_reversed = file[::-1]
  42. solution = ''.join([mapping[letter] for letter in file_reversed])
  43.  
  44. # fib
  45.  
  46. def fib(n, k):
  47.  
  48. if n == 0: return 0
  49. if n == 1: return 1
  50.  
  51. return fib(n-1, k) + k*fib(n-2, k)
  52.  
  53. with open("rosalind_fib.txt", 'r') as f:
  54. file = f.readline().strip()
  55.  
  56. fib(int(file.split()[0]), int(file.split()[1]))
  57.  
  58. # gc
  59.  
  60. import numpy as np
  61.  
  62. with open("rosalind_gc.txt", 'r') as f:
  63. file0 = f.read()
  64.  
  65. file1 = file0.split('>')[1:]
  66. file2 = [f.strip().split('\n') for f in file1]
  67. file3 = {f[0]: ''.join(f[1:]) for f in file2}
  68. file4 = {f[0]: 100*np.mean([1 if x in ['C', 'G'] else 0 for x in f[1]]) for f in file3.items()}
  69.  
  70. for x in list(sorted(file4.items(), key = lambda x: x[1])[-1]): print(x)
  71.  
  72. # hamm
  73.  
  74. with open("rosalind_hamm.txt", 'r') as f:
  75. file = f.read()
  76.  
  77. f = file.strip().split('\n')
  78. sum([1 if f[0][i] != f[1][i] else 0 for i in range(len(f[0]))])
  79.  
  80. # iprb
  81.  
  82. from itertools import product, combinations
  83. import numpy as np
  84.  
  85. with open("rosalind_iprb.txt", 'r') as f:
  86. file = f.read()
  87.  
  88. inds = [int(i) for i in file.replace('\n', '').split(' ')]
  89. genes = [['Y', 'Y'], ['Y', 'y'], ['y', 'y']]
  90. pop = sum([[genes[i]]*inds[i] for i in range(3)], [])
  91.  
  92. combs = list(combinations(pop, 2))
  93. children = sum([list(product(*comb)) for comb in combs], [])
  94. result = np.mean([1 if 'Y' in child else 0 for child in children])
  95. result
  96.  
  97. # prot
  98. with open("rosalind_prot.txt", 'r') as f:
  99. file = f.read()
  100.  
  101. s = file
  102. r = list(range(0, len(s) + 1, 3))
  103. t = [s[r[i]:r[(i+1)]] for i, _ in enumerate(r[:-1])]
  104. ''.join([rna_codon_tbl[snippet] for snippet in t]).replace('Stop', '')
  105.  
  106. # subs
  107. import re
  108.  
  109. with open("rosalind_subs.txt", 'r') as f:
  110. file = f.readlines()
  111.  
  112. file1 = [f.strip() for f in file]
  113. s, t = file1[0], file1[1]
  114. for i in [(m.start() + 1) for m in re.finditer('(?={})'.format(t), s)]: print(i, end = " ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement