gpt4 book ai didi

python - Biopython 从变量而不是文件解析

转载 作者:行者123 更新时间:2023-11-28 21:09:25 25 4
gpt4 key购买 nike

import gzip
import io
from Bio import SeqIO

infile = "myinfile.fastq.gz"
fileout = open("myoutfile.fastq", "w+")
with io.TextIOWrapper(gzip.open(infile, "r")) as f:
line = f.read()
fileout.write(line)
fileout.seek(0)

count = 0
for rec in SeqIO.parse(fileout, "fastq"): #parsing from file
count += 1
print("%i reads" % count)

当“行”被写入文件并且该文件被提供给解析器时,上面的方法有效,但下面的方法不起作用。为什么不能直接读取行?有没有一种方法可以直接将“行”提供给解析器而无需先写入文件?

infile = "myinfile.fastq.gz"
#fileout = "myoutfile.fastq"
with io.TextIOWrapper(gzip.open(infile, "r")) as f:
line = f.read()
#myout.write(line)

count = 0
for rec in SeqIO.parse(line, "fastq"): #line used instead of writing from file
count += 1
print("%i reads" % count)

最佳答案

这是因为 SeqIO.parse 只接受文件处理程序或文件名作为第一个参数。

如果你想将 gzip 文件直接读入 SeqIO.parse 只需传递一个处理程序给它:

import gzip
from Bio import SeqIO

count = 0
with gzip.open("myinfile.fastq.gz") as f:
for rec in SeqIO.parse(f, "fastq"):
count += 1

print("{} reads".format(count))

关于python - Biopython 从变量而不是文件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38358191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com