【问题标题】:reading large fastq file with python faster用python更快地读取大型fastq文件
【发布时间】:2018-07-24 05:41:09
【问题描述】:

我有几个 fastq 文件,平均有 500.000.000 行(125.000.000 个序列)。有没有一种快速的方法可以更快地读取这些 fastq 文件。

我想做的是读取每个序列并将前 16 个序列用作条形码。然后统计每个文件中的条码数量。

这是我的脚本,需要几个小时:

import os, errno
from Bio import SeqIO
import gzip
files = os.listdir(".")
for file in files[:]:
    if not file.endswith(".fastq.gz"):
        files.remove(file)

maps = {}
for file in files:
    print "Now Parsing file %s"%file
    maps[file] = {}
    with gzip.open(file,"r") as handle:
        recs = SeqIO.parse(handle,"fastq")
        for rec in recs:
            tag = str(rec.seq)[0:16]
            if tag not in map[file]:
                maps[file][tag] = 1
            else:
                maps[file][tag] += 1

我有 250 GB RAM 和 20 个可用于多线程的 CPU ...

谢谢。

【问题讨论】:

  • 您是否已经像 this question 那样对将 fastq 文件解析为 Pandas 进行了基准测试?如果这是可行的,那么我可以想出几种方法来简化这个过程。

标签: python multithreading multiprocessing fastq


【解决方案1】:

未经测试,但您可以通过以下方式以“令人尴尬的并行”方式做到这一点:

import multiprocessing as mp
import os, errno
from Bio import SeqIO
import gzip

def ImportFile(file):

    maps = {}
    with gzip.open(file,"r") as handle:
        recs = SeqIO.parse(handle,"fastq")
        for rec in recs:
            tag = str(rec.seq)[0:16]
            if tag not in maps.keys():
                maps[tag] = 1
            else:
                maps[tag] += 1

    return {file:maps}


files = os.listdir(".")
for file in files[:]:
    if not file.endswith(".fastq.gz"):
        files.remove(file)

# I'd test this with smaller numbers before using up all 20 cores
pool = mp.Pool(processes=10)
output = pool.map(ImportFile,files)

【讨论】:

  • 谢谢。这会分别读取每个文件。但是,仍然读取一个 fastq 文件需要 45-80 分钟。有没有办法通过多处理读取一个 fastq 文件以加快速度。
  • 这会在一个单独的进程中并行读取每个文件,因此,如果你有 RAM,应该只需要(45-80 分钟)*(n_files / 个进程)。如果构建它以便您以某种并行方式读取每个单个文件,您仍然需要连续执行多次。
  • 可能有,通过使用recs 作为您在pool.map 中使用的列表,但是您必须处理进程之间的消息传递,这样您就不会得到重复的键您的 dict,对于从 all 文件加载信息的任务,它可能不会比这更快。
猜你喜欢
  • 2015-08-07
  • 2016-02-21
  • 1970-01-01
  • 2017-03-26
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多