生物信息脚本练习(3)gb文件转换

来源:互联网 发布:java apache 编辑:程序博客网 时间:2024/05/17 09:36

这是个genebank的序列文件

https://www.ncbi.nlm.nih.gov/nuccore/NC_000012.12?report=genbank&from=25204789&to=25252093&strand=true

这个文件需要转换成fasta格式的文件,脚本如下:

import reoutput = open("data3.txt","w")  with open("sequence.gb","r") as f:    read = f.readlines()title = read[0]title = ">"+title[12:20]print(title)output.write(title)output.write("\n")seq = read[81:138]seq_complete = []for i in seq:    for e in i:        if re.match("[a-z]",e):            seq_complete.append(e)seq_complete = "".join(seq_complete)#print(seq_complete)output.write(seq_complete)output.close()

我写的这个只对某一个gb文件有效,因为我数了一下序列的行数,然后切片取的,其实还有完全使用正则的更好的方法。

import ref=open('sequence.gb','r')seq=""for line in f:    if re.search('ACCESSION',line):        m=line.split('ACCESSION')[1].strip()    if re.search('ORGANISM',line):        n=line.split('ORGANISM')[1].strip()        print ('> %s : %s' % (n,m))    if re.search('[0-9] [a-z]{10}',line):        s=re.split('[0-9] ',line)[1]        print (s.split(" "))        for i in s.split(" "):            seq=seq.rstrip()+i.rstrip()

不错吧!

原创粉丝点击