okkey 没有使用 'SeqIO.to_dict()' 不知道,也许其他人会使用 [EDITED] 发布答案
record_dict = SeqIO.to_dict(SeqIO.parse(fasta_file , "fasta"))
会提出一个
raise ValueError("Duplicate key '%s'" % key)
ValueError: Duplicate key 'NW_020169394.1_41'
我输入的“res.txt”文件是:
>NW_020169394.1_41 [10497-10619]|KE647364.1_346 [38084-37959]
MDQLSRKLNLTYLKVGILTSQNEFVTKHLLIIKGLKIFTET
>NW_020169394.1_41 [10497-10619]|KE646921.1_20 [383-240]
MDQLSRKLNLTYLKVGILTSQNEFVTKHLLIIKGLKIFTET
>NW_020169394.1_41 [10497-10619]|KE647277.1_227 [70875-70720]
MDQLSRKLNLTYLKVGILTSQNEFVTKHLLIIKGLKIFTET
我的代码是:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 28 15:57:59 2021
@author: Pietro
https://stackoverflow.com/questions/68160254/manipulate-blast-result-file-python
"""
from Bio import SeqIO
fasta_file = 'res.txt'
sequences = {}
sequences2 = {}
sequa = SeqIO.parse(fasta_file, "fasta")
count = 0
for seq_record in sequa :
sequences[count] = seq_record
count +=1
for key,seq_record in sequences.items():
countz = 0
if seq_record.seq not in [valuez.seq for keiz,valuez in sequences2.items()]:
sequences2[countz] = seq_record
countz += 1
else:
for keiz,valuez in sequences2.items():
if seq_record.seq == valuez.seq:
new = valuez.description.replace('valuez.id', '')
newnew = new.replace(valuez.name , '')
sequences2[keiz].description = seq_record.description + (newnew)
print('########################')
print(sequences2)
with open('modified_res.text', 'w') as handle:
for key,value in sequences2.items():
print(value)
SeqIO.write(value, handle, 'fasta')
我的输出“modified_res.text”文件是:
>NW_020169394.1_41 [10497-10619]|KE647277.1_227 [70875-70720] [10497-10619]|KE646921.1_20 [383-240] [10497-10619]|KE647364.1_346 [38084-37959]
MDQLSRKLNLTYLKVGILTSQNEFVTKHLLIIKGLKIFTET
无法摆脱重复的[10497-10619] 并且订单与您的不同
对不起,我尽力了
此外,如果您的序列可以通过特定的 seqrecord 格式解析(即 genbank 等,这里不是专家,可能会以不同的方式使用它们的描述,请告诉我)。