Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rna_codon_tbl = {'UUU': 'F', 'CUU': 'L', 'AUU': 'I', 'GUU': 'V',
- 'UUC': 'F', 'CUC': 'L', 'AUC': 'I', 'GUC': 'V',
- 'UUA': 'L', 'CUA': 'L', 'AUA': 'I', 'GUA': 'V',
- 'UUG': 'L', 'CUG': 'L', 'AUG': 'M', 'GUG': 'V',
- 'UCU': 'S', 'CCU': 'P', 'ACU': 'T', 'GCU': 'A',
- 'UCC': 'S', 'CCC': 'P', 'ACC': 'T', 'GCC': 'A',
- 'UCA': 'S', 'CCA': 'P', 'ACA': 'T', 'GCA': 'A',
- 'UCG': 'S', 'CCG': 'P', 'ACG': 'T', 'GCG': 'A',
- 'UAU': 'Y', 'CAU': 'H', 'AAU': 'N', 'GAU': 'D',
- 'UAC': 'Y', 'CAC': 'H', 'AAC': 'N', 'GAC': 'D',
- 'UAA': 'Stop', 'CAA': 'Q', 'AAA': 'K', 'GAA': 'E',
- 'UAG': 'Stop', 'CAG': 'Q', 'AAG': 'K', 'GAG': 'E',
- 'UGU': 'C', 'CGU': 'R', 'AGU': 'S', 'GGU': 'G',
- 'UGC': 'C', 'CGC': 'R', 'AGC': 'S', 'GGC': 'G',
- 'UGA': 'Stop', 'CGA': 'R', 'AGA': 'R', 'GGA': 'G',
- 'UGG': 'W', 'CGG': 'R', 'AGG': 'R', 'GGG': 'G'}
- # DNA
- from collections import Counter
- with open("rosalind_dna.txt", 'r') as f:
- file = f.readline()
- Counter(sorted(file))
- # RNA
- from collections import Counter
- with open("rosalind_rna.txt", 'r') as f:
- file = f.readline()
- Counter(file.replace('T', 'U').strip())
- # REVC
- with open("rosalind_revc.txt", 'r') as f:
- file = f.readline().strip()
- mapping = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
- file_reversed = file[::-1]
- solution = ''.join([mapping[letter] for letter in file_reversed])
- # fib
- def fib(n, k):
- if n == 0: return 0
- if n == 1: return 1
- return fib(n-1, k) + k*fib(n-2, k)
- with open("rosalind_fib.txt", 'r') as f:
- file = f.readline().strip()
- fib(int(file.split()[0]), int(file.split()[1]))
- # gc
- import numpy as np
- with open("rosalind_gc.txt", 'r') as f:
- file0 = f.read()
- file1 = file0.split('>')[1:]
- file2 = [f.strip().split('\n') for f in file1]
- file3 = {f[0]: ''.join(f[1:]) for f in file2}
- file4 = {f[0]: 100*np.mean([1 if x in ['C', 'G'] else 0 for x in f[1]]) for f in file3.items()}
- for x in list(sorted(file4.items(), key = lambda x: x[1])[-1]): print(x)
- # hamm
- with open("rosalind_hamm.txt", 'r') as f:
- file = f.read()
- f = file.strip().split('\n')
- sum([1 if f[0][i] != f[1][i] else 0 for i in range(len(f[0]))])
- # iprb
- from itertools import product, combinations
- import numpy as np
- with open("rosalind_iprb.txt", 'r') as f:
- file = f.read()
- inds = [int(i) for i in file.replace('\n', '').split(' ')]
- genes = [['Y', 'Y'], ['Y', 'y'], ['y', 'y']]
- pop = sum([[genes[i]]*inds[i] for i in range(3)], [])
- combs = list(combinations(pop, 2))
- children = sum([list(product(*comb)) for comb in combs], [])
- result = np.mean([1 if 'Y' in child else 0 for child in children])
- result
- # prot
- with open("rosalind_prot.txt", 'r') as f:
- file = f.read()
- s = file
- r = list(range(0, len(s) + 1, 3))
- t = [s[r[i]:r[(i+1)]] for i, _ in enumerate(r[:-1])]
- ''.join([rna_codon_tbl[snippet] for snippet in t]).replace('Stop', '')
- # subs
- import re
- with open("rosalind_subs.txt", 'r') as f:
- file = f.readlines()
- file1 = [f.strip() for f in file]
- s, t = file1[0], file1[1]
- 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