TypeError: list indices must be integers or slices, not str in BioPython

TypeError: list indices must be integers or slices, not str in BioPython

以下代码导致了这个错误:

Traceback (most recent call last):
  File "/t.py", line 19, in <module>
    seqsInfo[record.id] = SeqInfo(len(record), record.seq[0])
TypeError: list indices must be integers or slices, not str
from Bio import SeqIO

class SeqInfo():

    def __init__(self, length, seqStart):
        self.length = length
        self.seqStart = seqStart

seqsInfo=[]
for record in SeqIO.parse("data/hybrid.AA.fasta", "fasta"):
    seqsInfo[record.id] = SeqInfo(len(record), record.seq[0])

这是输入文件:

>STRG2.t1 gene=STRG2 seq_id=NbV1Ch01 type=cds
PYKTCHKKFRNKMPSFYLASFCIMVLSFSITFASGNSVSNKTCIDDATIVLSFGLYQNSC
LEAEAIIYSWVERAVSQDPRMAASLLRLHFHDCFVNGCDASVLLDDTPNFIGEKTAAPNL
NSLRGFEVIDSIKADLELACPQTVSCADILAIAARDSVVLSGGWRWKVQMGRKDSLTAST
KAVNNNIPGPNSNIATLVSSFQNIGLSLQDMVTLSGAHTIGSARCSTFSSRLNGGGNSDM
NLDFLQSLQQLCSVSDTNITLANLDDMTPSTFDNQYYVNLLSGKGLLVSDQVLASGDDNT
REIVQTYVDDPSAFFDDFRNSMLKMGSLAPPTGTTGEIRVNCRV

我错过了什么?

由于您试图将非整数指定为列表的索引,因此出现此类错误。请将列表更改为字典,因为 record.id 是 'STRG2.t1'。希望这可以解决问题:

from Bio import SeqIO

class SeqInfo():

    def __init__(self, length, seqStart):
        self.length = length
        self.seqStart = seqStart

seqsInfo={}
for record in SeqIO.parse("data/hybrid.fasta", "fasta"):
    seqsInfo[record.id] = SeqInfo(len(record), record.seq[0])
print(seqsInfo)

输出: {'STRG2.t1': <main.SeqInfo 对象在 0x1178b3710>}