python 用于帮助从单独文件中绘制序列的脚本

python script used to help draw sequences from a separate file

我正在尝试 运行 一个 python 脚本从一个单独的文件 (merged.fas) 中绘制序列,关于一个列表 (gene_fams_eggnog.txt) 我作为另一个程序的输出。

代码如下:

from Bio import SeqIO
import os, sys, re
sequences = "merged.fas"
all_seqs = SeqIO.index(sequences, "fasta")
gene_fams = {}

gene_fams_file = open("gene_fams_eggnog.txt")
for line in gene_fams_file:
   fields = re.split("\t", line.rstrip())
   gene_fams[fields[0]].append[fields[1]]

for fam in gene_fams.keys():
   output_filename = str(fam) + ".fasta"
   outh = open(output_filename, "w")
   for id in gene_fams[fam]:
     if id in all_seqs:
         outh.write(">" + all_seqs[id].description + "\n" + str(all_seqs[id].seq) + "\n")
     else:
        print "Uh oh! Sequence with ID " + str(id) + " is not in the all_seqs file!"
        quit()
     outh.close()

但是我收到一条错误消息:

"File "make_fastafiles_from_gene_family_assignments.py", line 15, in <module>
    gene_fams[fields[0]].append(fields[1])
KeyError: '1'"

它出于某种原因无法识别该字段,但他的文件肯定有 2 个字段 (0,1)

文件如下所示:

1   Saccharomycescerevisiae_DAA09367.1

1   bieneu_EED42827.1

1   Asp_XP_749186.1 

1   Mag_XP_003717339.1 

1   Mag_XP_003716586.1 

1   Mag_XP_003709453.1 

1   Asp_XP_749329.1 

(字里行间没有space,不知道为什么本站是这样排版的) 字段 0 会在一段时间后发生变化,但这正是分组的本质所在。

感谢任何帮助, JT

您需要先初始化 gene_fams[fields[0]]=[],然后附加到此列表 gene_fams[fields[0]].append(fields[1])