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