将fastq信息复制到新的fastq文件中

时间:2014-07-11 01:34:13

标签: python fastq

我正在尝试写一个代码,它将打开一个fasta文件并从另一个fastq文件中提取读取名称(标题),序列(seq)和质量得分(质量),只要它在fasta文件中找到,并将fastq信息写入新的fastq文件。但是,我在如何写这个最后一部分时遇到了麻烦(我在代码中遇到问题时加粗了)。可能有人知道如何编写这个部分,或者我可以在哪里找到有关如何在python中输入它的信息?

到目前为止,我有:

from sys import argv
from Bio.SeqIO.QualityIO import FastqGeneralIterator

script, merged_seqs, raw_seqs = argv
merged_from_raw = "merged_only.fastq"

merged_names = set() 
for line in open(merged_seqs): 
        if line[0] == ">":
                read_name = line.split()[0][1:] 
                merged_names.add(read_name) 

raw_fastq = raw_seqs

temp_handle = open(merged_from_raw, "w") 
for title, seq, qual in FastqGeneralIterator(open(raw_fastq)) :
        if title in merged_names:
                **handle.write() #this is where I don't know how to write what I need in python**

1 个答案:

答案 0 :(得分:1)

除非您有特定的理由自己实现文件解析,否则最好使用SeqIO解析器来处理输入和输出文件。也许类似于以下内容(警告:我之前从未使用过Bio,也没有测试过这段代码):

from sys import argv
from Bio import SeqIO

output_filename = 'merged_only.fastq'
merged_seqs, raw_seqs = argv[1:2]

# Get fasta iterator, and read source fastq file into a dict-like object
merged_names = SeqIO.parse(merged_seqs, 'fasta')
source_seqs = SeqIO.index(raw_seqs, 'fastq')

filtered_seqs = (source_seqs[record.id] for record in merged_names if record.id in source_seqs)
SeqIO.write(filtered_seqs, output_filename, 'fastq')