记录---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)))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值