一个py脚本运行时报错,
python3 test.py data1.csv data2.tab data3.txt output.txt
nr[line[2]] = [line[6]]
IndexError: list index out of range
test.py如下:
import sys
nr_cog = sys.argv[1]
cog_tab = sys.argv[2]
blast = sys.argv[3]
output = sys.argv[4]
#读取 NR 和 COG 映射关系
nr = {}
nr_cog = open(nr_cog, 'r')
for line in nr_cog:
line = line.split(',')
if line[2] not in nr:
nr[line[2]] = [line[6]]
elif line[6] not in nr[line[2]]:
nr[line[2]].append(line[6])
nr_cog.close()
#读取 COG 功能和分类描述
cog = {}
cog_tab = open(cog_tab, 'r')
for line in cog_tab:
line = line.split('\t')
cog[line[0]] = '\t'.join(line[0:3])
cog_tab.close()
#读取 blast 比对结果,根据 NR id,将 COG id、描述、分类等信息匹配在后方
output = open(output, 'w')
blast = open(blast, 'r')
print(f'{blast.readline().strip()}\tCOG_name\tCOG_class\tCOG_description', file = output)
for line in blast:
line = line.strip()
id = line.split('\t')[5]
if id in nr:
for id2 in nr[id]:
print(f'{line}\t{cog[id2]}', file = output)
blast.close()
output.close()
请问如何解决?谢谢!