2017-09-13 22 views
2

파이썬 2.6.6을 사용 중이고 fastq (file2)이 file1과 겹치는 (즉, 동일 함) 읽기를 제거하려고합니다.SeqIO.index에서 생성 한 사전에서 항목 삭제

AttributeError : _IndexedSeqFileDict 인스턴스가 어떤 속성 '__delitem__'

인가가없는 나는 del 내 사용과 관련이 오류,

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] 

을하지만 : 여기 코드는 내가 구현하기 위해 노력하고있다 현재 공식을 사용하여 항목을 삭제할 수 있습니까? 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() 

답변

1

진정한 사전을 반환하지 않습니다 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 인스턴스

은 또한 다음과 같은 시도했다. 문서화 문자열은 언급 : 파일이 SeqIO.parse() 작업 너무 큰 경우

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] 

:

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 파일을 변환해야합니다 가능하지, 그럼 내가 이런 짓을 할 것이다 :

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") 
+0

유용한 조언을 구하십시오. 첫 번째 해결 방법은 효과가 있었지만 향상시키려는 코드에 비해 느립니다. 두 번째 코드 블록에 관한 추가 정보를 제공해 주시겠습니까? 나는 file2.fastq에있는 file1.fastq에서 읽는 것을 제거하려고합니다. 두 번째 솔루션을 기반으로 SeqIO.index ("file1.fastq", "fastq")에서 항목을 삭제할 때의 원래 문제를 다시 해결하려면 어떻게해야합니까? 최근 시도를 반영하여 질문을 업데이트했습니다. – wa3j

+0

@ wa3j : 위의 편집을 참조하십시오. – BioGeek