ros_bio5_GC

如果第一次阅读,请查看写在前面

import re
fasta = {}
content = {}
#读取fasta文件——字典
with open("../examples/ros_bio5_GC.txt") as f:
    file = f.readlines()
    for line in file:
        line = re.sub(r'\n$', "", line)
        match = re.match(r'^>.*', line)
        if match:
            fasta[match.group()] = ''
            title = match.group()
        else:
            fasta[title] = fasta[title] + line
print(fasta)
#计算GC含量
for key in fasta:
    s = fasta[key].count('G')/len(fasta[key]) + fasta[key].count('C')/len(fasta[key])
    content[key] = s
maxgc = max(content[key] for key in content)
name = ''.join([key for key, value in content.items() if value == maxgc])
print(name, '{:.6f}'.format(content[name] * 100), sep="\n")



#列表写法——作者:未琢 https://www.bilibili.com/read/cv1986543 出处:bilibili
'''
with open("../examples/ros_bio5_GC.txt") as f:
    lines = f.readlines()
seq = []
index = []
seqplast = ""
numlines = 0
for i in lines:

    if '>' in i:# 判断是序列行还是说明行

        index.append(i.replace("\n", "").replace(">", ""))

        seq.append(seqplast.replace("\n", ""))

        seqplast = ""

        numlines += 1

    else:
        seqplast = seqplast + i.replace("\n", "")# 把分行的序列拼接成一个字符串
        numlines += 1
    if numlines == len(lines):

        seq.append(seqplast.replace("\n", ""))

seq = seq[1:]
print(index, seq)
'''



'''
失败!!
import re
title = []
i = 0
list = []
sequence = []
with open("../examples/ros_bio5_GC.txt") as f:
    fasta = f.read().rstrip()
    print(fasta)
    for line in fasta:
        match = re.match(r'^>.*', line)
        if match:
            title.append(match.group())
            i += 1
            continue
        else:
            list.append(line)
            dna = "".join(list)
            list = []
        sequence.insert(i, dna)
    print(sequence)
    #print(dna)
    print(title)
    #fasta = f.readline().rstrip()


pattern = re.compile(r'^>.*')
title = pattern.match(fasta)
print(title.group(0))

for line in fasta:
    print(line)
    title = re.search(r'^>.*', fasta)
    #print(title.group())
'''

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值