【发布时间】:2018-03-01 14:44:36
【问题描述】:
我正在尝试使用 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
基本上我需要序列行“AAAGGCTGT”, 来自“input.fasta”的“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 operation on closed file”
【问题讨论】:
-
为什么只有读取 1 和 3 的序列
AAAGGCTGT和CGTGCCGCT被复制到新文件中? -
很抱歉给您带来了困惑。需要针对其各自读取的每个序列。我会更新以反映这一点。
-
with语句是上下文管理器。当with完成时,它会关闭文件。将with fasta as fa:替换为with open(Input.fasta, "r") as fa:。好吧,也许这会解决你的问题。你想要的输出没有任何意义。
标签: python bioinformatics biopython fasta fastq