在python中,从交替文件循环打印行

2024-09-28 01:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在输入.fasta在

>read1
AAAGGCTGT
>read2
AGTCTTTAT
>read3
CGTGCCGCT

在输入.fastq在

^{pr2}$

在期望输出.fastq在

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

基本上我需要序列行“AAAGGCTGT”, “AGTCTTTAT”和“cgtgct”来自输入.fasta“以及所有其他行”输入.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 operation on closed file”


Tags: 文件inidsforlinefastqfastanext
3条回答

我喜欢@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)
## 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])

我建议您使用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')

如果您不想使用头而只依赖顺序,那么解决方案就更简单、更节省内存(只需确保记录的顺序和数量相同即可),无需先定义字典,只需将记录一起迭代:

^{pr2}$

相关问题 更多 >

    热门问题