记录---Rosalind之problems&Solutions__0001
Rosalind is a platform for learning bioinformatics and programming through problem solving.
http://rosalind.info/problems/list-view/
DNA:Counting DNA Nucleotides(对DNA的每一类碱基计数)
方式一
dic={}
with open('rosalind_dna.txt', 'r') as f:
data = f.read().strip('\n')
for base in data:
# get() 函数返回指定键的值,如果值不在字典中返回默认值。
# dict.get(key, default=None)
dic[base]=1+ dic.get(base,0)
for key in sorted(dic.keys()):
print(dic[key], end =" ")
方式二
with open('rosalind_dna.txt', 'r') as f:
data = f.read().strip('\n')
#count() 方法用于统计字符串里某个字符出现的次数。
#str.count(sub, start= 0,end=len(string))
list = ([data.count(c) for c in 'ACGT'])
for i in list:
print(i, end= " ")
RNA Transcribing DNA into RNA(将T变成U)
方式一
with open('rosalind_rna.txt', 'r') as f:
data = f.read().strip('\n')
print(data.replace("T", "U"))
REVC Complementing a Strand of DNA(DNA的反向互补序列的获取)
方式一
import re
rep = {"A":"T", "T":"A", "G":"C", "C":"G"} # 需要替换的元素
with open('rosalind_revc.txt', 'r') as f:
data = f.read().strip('\n')
dataR = data[::-1] #先逆序排列
#执行替换
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], dataR)
print(text)
方式二
#利用大小写的不同,非常巧妙!!!
with open('rosalind_revc.txt', 'r') as f:
data = f.read().strip('\n')
st = data.replace('A', 't').replace('T', 'a').replace('C', 'g').replace('G', 'c').upper()[::-1]
print(st)
方式三
comp = dict(zip('ACGT', 'TGCA'))
#{'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
result = []
with open('rosalind_revc.txt', 'r') as f:
input = f.read().strip('\n')
for i in reversed(input):
result.append(complement[i])
print(''.join(result))
方式四
comp = dict(zip('ACGT', 'TGCA'))
#{'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
with open('rosalind_revc.txt', 'r') as f:
input = f.read().strip('\n')
print(''.join(comp[x] for x in reversed(input)))