在python循环中打印来自交替文件的行

问题描述 投票:1回答:3

我试图使用python在两个单独的文件中找到感兴趣的四行块,然后按受控顺序打印出一些这些行。下面是两个输入文件和所需输出文件的示例。请注意,Input.fasta中的DNA序列与Input.fastq中的DNA序列不同,因为.fasta文件已被读取更正。

Input.fasta

>read1
AAAGGCTGT
>read2
AGTCTTTAT
>read3
CGTGCCGCT

Input.fastq

@read1
AAATGCTGT
+
'(''%$'))
@read2
AGTCTCTAT
+
&---+2010
@read3
AGTGTCGCT
+
0-23;:677

DesiredOutput.fastq

@read1
AAAGGCTGT
+
'(''%$'))
@read2
AGTCTTTAT
+
&---+2010
@read3
CGTGCCGCT
+
0-23;:677

基本上我需要来自“input.fasta”的序列行“AAAGGCTGT”,“AGTCTTTAT”和“CGTGCCGCT”以及来自“input.fastq”的所有其他行。这允许将质量信息恢复到读取校正的.fasta文件。

这是我最接近失败的尝试:

fastq = open(Input.fastq, "r")
fasta = open(Input.fasta, "r")

ReadIDs = []
IDs = []

with fastq as fq:
    for line in fq:
        if "read" in line:  
            ReadIDs.append(line)
            print(line.strip())
            for ID in ReadIDs:
                IDs.append(ID[1:6])
            with fasta as fa:
                for line in fa:
                    if any(string in line for string in IDs):
                        print(next(fa).strip())
            next(fq)
            print(next(fq).strip())
            print(next(fq).strip())

我想通过尝试在同一个循环中嵌入“with”调用两个不同的文件来遇到麻烦。这将正确打印read1所需的行,但不会继续遍历其余行并抛出错误“ValueError:对已关闭文件的I / O操作”

python bioinformatics biopython fasta fastq
3个回答
3
投票

我建议你使用Biopython,这将为你省去很多麻烦,因为它为这些文件格式提供了很好的解析器,它不仅处理标准情况,而且还处理例如多行fasta。

这是一个用相应的fasta序列行替换fastq序列行的实现:

from Bio import SeqIO

fasta_dict = {record.id: record.seq for record in
              SeqIO.parse('Input.fasta', 'fasta')}

def yield_records():
    for record in SeqIO.parse('Input.fastq', 'fastq'):
        record.seq = fasta_dict[record.id]
        yield record

SeqIO.write(yield_records(), 'DesiredOutput.fastq', 'fastq')

如果您不想使用标题但只是依赖顺序,那么解决方案甚至更简单,内存效率更高(只需确保记录的顺序和数量相同),不需要先定义字典,只需迭代记录在一起:

fasta_records = SeqIO.parse('Input.fasta', 'fasta')
fastq_records = SeqIO.parse('Input.fastq', 'fastq')

def yield_records():
    for fasta_record, fastq_record in zip(fasta_records, fastq_records):
        fastq_record.seq = fasta_record.seq
        yield fastq_record

1
投票
## Open the files (and close them after the 'with' block ends)
with open("Input.fastq", "r") as fq, open("Input.fasta", "r") as fa:

    ## Read in the Input.fastq file and save its content to a list
    fastq = fq.readlines()

    ## Do the same for the Input.fasta file
    fasta = fa.readlines()


## For every line in the Input.fastq file
for i in range(len(fastq)):
    print(fastq[i]))
    print(fasta[2 * i])
    print(fasta[(2 * i) + 1])

1
投票

我喜欢@Chris_Rands的Biopython solution更适合小文件,但这里的解决方案只使用Python附带的电池并且内存效率高。它假定fasta和fastq文件以相同的顺序包含相同数量的读取。

with open('Input.fasta') as fasta, open('Input.fastq') as fastq, open('DesiredOutput.fastq', 'w') as fo:
    for i, line in enumerate(fastq):
        if i % 4 == 1:
            for j in range(2):
                line = fasta.readline()
        print(line, end='', file=fo)
© www.soinside.com 2019 - 2024. All rights reserved.