0

在这里,我编写了一个代码来使用“id”提取基因的“locus_tag”。如何将输出保存到制表符分隔格式的文件中????代码采用和修改 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]
4

1 回答 1

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)
于 2016-03-10T16:01:56.973 回答