用python把文件内容按照染色体编号写出(第十一题)
2020-03-05 本文已影响0人
多啦A梦詹
测试文件见我的github.
import os
os.chdir('G:/R/Genome/hsa')
def readfasta(filename):
"""
:param filename: ;要读取的FASTA文件名
:return: 返回基因名:序列的字典文件
"""
fa = open(filename, 'r')
res = {}
ID = ''
for line in fa:
if line.startswith('>'):
ID = line.strip('\n')[1:]
res[ID] = ''
else:
res[ID] += line.strip('\n')
return res
def seperatefile(filename, outfile):
data=readfasta(filename)
for key, value in data.items():
(file, ext)=os.path.splitext(outfile)
fo=open('%s_%s%s' % (file, key, ext), 'w')
fo.write('>%s\n'%key)
fo.write('%s\n'%value)
seperatefile('genome.fa', 'output.fa')