我尝试使用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”
我喜欢@Chris_Rands的Biopython solution对于小文件更好,但是这里有一个解决方案,它只使用Python附带的电池,并且具有内存效率。它假设fasta和fastq文件以相同的顺序包含相同数量的读取。在
我建议您使用Biopython,这将为您省去很多麻烦,因为它为这些文件格式提供了很好的解析器,这些文件格式不仅可以处理标准情况,还可以处理多行fasta。在
下面是一个用相应的fasta序列行替换fastq序列行的实现:
如果您不想使用头而只依赖顺序,那么解决方案就更简单、更节省内存(只需确保记录的顺序和数量相同即可),无需先定义字典,只需将记录一起迭代:
^{pr2}$相关问题 更多 >
编程相关推荐