将biopython对象的输出保存到文件中?

时间:2016-03-10 15:54:26

标签: python biopython genbank

这里我编写了一个代码来提取" locus_tag"使用" id"的基因。如何将此输出保存到以制表符分隔格式的文件中????采用和修改的代码https://www.biostars.org/p/110284/

from Bio import SeqIO
foo = open("geneid.txt")
lines = foo.read().splitlines()

genbank_file = open("example.gbk")

for record in SeqIO.parse(genbank_file, "genbank"):
    for f in record.features:
        if f.type == "CDS" and "protein_id" in f.qualifiers:
            protein_id = f.qualifiers["protein_id"][0]
            if protein_id  in lines:
                print f.qualifiers["protein_id"][0],f.qualifiers["locus_tag"][0]

1 个答案:

答案 0 :(得分:0)

尝试添加类似的内容 - 但您需要使用已编写的代码确定缩进是否正确。

with open(your_outputFileName, 'w') as outputFile:
         string = '\t'.join([f.qualifiers['protein_id'][0],f.qualifiers['locus_tag'][0]])

         outputFile.write(string + '\n')

您还应该考虑使用“with”打开初始文件。完成后会自动关闭文件 - 否则 - 确定要关闭文件(例如,foo.close())。

   for record in SeqIO.parse(genbank_file, 'genbank'):
      for f in record.features:
         if f.type == 'CDS' and 'protein_id' in f.qualifiers:
            protein_id = f.qualifiers['protein_id'][0]
            if protein_id  in lines:
               print f.qualifiers['protein_id'][0],f.qualifiers['locus_tag'][0]
               with open('your_outputFileName', 'w') as outputFile:
                  string = '\t'.join([f.qualifiers['protein_id'][0],f.qualifiers['locus_tag'][0]]) + '\n'
                  outputFile.write(string)