Skip to main content
 首页 » 编程设计

python之从 SeqIO.index 生成的字典中删除一个项目

2025年02月15日9Renyi-Fan

我正在使用 Python 2.6.6,我正在尝试删除 file2 中与 file1< 中重叠(即相同)读取的 fastq 读取。这是我要实现的代码:

ref_reads = SeqIO.index("file1.fastq", "fastq") 
spk_reads = SeqIO.index("file2.fastq", "fastq") 
 
for spk in spk_reads: 
    if spk in ref_reads: 
    del ref_reads[spk] 

但是,我收到与使用 del 相关的错误:

AttributeError: _IndexedSeqFileDict 实例没有属性 '__delitem__'

是否可以使用当前公式删除项目?如何从使用 SeqIO.index() 生成的字典中删除项目?

我还尝试了以下方法:

# import read data 
ref_reads = SeqIO.index("main.fastq", "fastq") 
spk_reads = SeqIO.index("over.fastq", "fastq") 
 
# note that ref_reads.keys() doesn't return a list but a 'dictionary-       keyiterator',  
# so we turn it into a set to work with it 
ref_keys = set(ref_reads.keys())   
spk_keys = set(spk_reads.keys()) 
 
# loop to remove overlap reads 
for spk in spk_keys: 
    if spk in ref_keys: 
        del ref_keys[spk] 
 
# output data 
output_handle = open(fname_out, "w") 
SeqIO.write(ref_reads[ref_keys], output_handle, "fastq") 
output_handle.close() 

请您参考如下方法:

SeqIO.index() 不返回真正的字典,但是 a dictionary like object, giving the SeqRecord objects as values :

Note that this pseudo dictionary will not support all the methods of a true Python dictionary, for example values() is not defined since this would require loading all of the records into memory at once.

这个类似字典的对象是一个 _IndexedSeqFileDict实例。文档字符串提到:

Note that this dictionary is essentially read only. You cannot add or change values, pop values, nor clear the dictionary.

因此,您需要使用 SeqIO.parse() 和 SeqIO.to_dict() 将您的 fastq 文件转换为内存中的 Python 字典:

from Bio import SeqIO 
 
ref_reads = SeqIO.parse("file1.fastq", "fastq") 
spk_reads = SeqIO.parse("file1.fastq", "fastq") 
 
ref_reads_dict = SeqIO.to_dict(ref_reads) 
 
for spk in spk_reads: 
    if spk.id in ref_reads_dict: 
        del ref_reads_dict[spk.id] 

如果您的文件太大以至于无法使用 SeqIO.parse(),那么我会这样做:

from Bio import SeqIO 
 
ref_reads = SeqIO.index("file1.fastq", "fastq") 
spk_reads = SeqIO.index("file2.fastq", "fastq") 
 
# note that ref_reads.keys() doesn't return a list but a 'dictionary-keyiterator',  
# so we turn it into a set to work with it 
ref_keys = set(ref_reads.keys())   
spk_keys = set(spk_reads.keys()) 
 
unique_ref_keys = ref_keys - spk_keys 
 
# this step might take a long time if your files are large 
unique_ref_reads = {key: ref_reads[key] for key in unique_ref_keys}  

编辑,回复您的评论:

how can I again solve the original problem of deleting items from SeqIO.index("file1.fastq", "fastq")?

如我上面所述,SeqIO.index("file1.fastq", "fastq") 返回一个只读的 _IndexedSeqFileDict 对象。因此,您不能按照设计从中删除项目。

下面更新的代码显示了如何创建一个新的 fastq 文件,其中重叠的读取被删除。

如果你真的想要一个新的SeqIO.index()对象,那么你可以用SeqIO.index()再次读入这个文件。

from Bio import SeqIO 
 
ref_reads = SeqIO.index("file1.fastq", "fastq") 
spk_reads = SeqIO.index("file2.fastq", "fastq") 
 
ref_keys = set(ref_reads.keys())   
spk_keys = set(spk_reads.keys()) 
 
unique_ref_keys = ref_keys - spk_keys 
 
# conserve memory by using a generator expression 
unique_ref_records = (ref_reads[key] for key in unique_ref_keys) 
 
# output new file with overlapping reads removed 
with open(fname_out, "w") as output_handle: 
    SeqIO.write(unique_ref_records , output_handle, "fastq") 
 
# optionally, create a new SeqIO.index() object  
unique_ref_reads = SeqIO.index(fname_out, "fastq")