python文本处理---fasta文件提取指定ID的序列

利用python脚本,提取指定ID名称的序列

#!/usr/bin/python3
#-*- coding:utf-8 -*-
#提取指定ID的序列

import sys
args=sys.argv
fr=open(args[1], 'r')
fw=open('./out.fasta', 'w')
dict={}
for line in fr:
    if line.startswith('>'):
        name=line.split()[0]
        dict[name]=''
    else:
        dict[name]+=line.replace('
','')
fr.close()
for ID in dict.keys():
    if ID ==args[2]:
        fw.write(ID)
        fw.write('
')
        fw.write(dict[ID])
fw.write(' ')
fw.write(str((dict[ID].count('G')+dict[ID].count('C'))/len(dict[ID]))) #计算指定序列中的GC含量 fw.close()

用法:python3 filename 'ID_name'
输出的结果保存在文件:out.fasta中
原文地址:https://www.cnblogs.com/lmt921108/p/8027494.html