美文网首页
用python把文件内容按照染色体编号写出(第十一题)

用python把文件内容按照染色体编号写出(第十一题)

作者: 多啦A梦詹 | 来源:发表于2020-03-05 19:37 被阅读0次

测试文件见我的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')

相关文章

网友评论

      本文标题:用python把文件内容按照染色体编号写出(第十一题)

      本文链接:https://www.haomeiwen.com/subject/bbwcrhtx.html