我需要处理两个大文件(> 10亿行),并根据一个文件中特定行中的信息将每个文件拆分为小文件。这些文件在blocks中记录高通量的测序数据(我们称之为测序reads),而每个read包含4行(name、sequence、n、quality)。read记录在两个文件中的顺序相同。
to-do
基于file1.fq中的id字段拆分file2.fq,
这两个文件如下所示:
$ head -n 4 file1.fq
@name1_1
ACTGAAGCGCTACGTCAT
+
A#AAFJJJJJJJJFJFFF
$ head -n 4 file2.fq
@name1_2
TCTCCACCAACAACAGTG
+
FJJFJJJJJJJJJJJAJJ为此,我编写了以下python函数:
def p7_bc_demx_pe(fn1, fn2, id_dict):
"""Demultiplex PE reads, by p7 index and barcode"""
# prepare writers for each small files
fn_writer = {}
for i in id_dict:
fn_writer[i] = [open(id_dict[i] + '.1.fq', 'wt'),
open(id_dict[i] + '.2.fq', 'wt')]
# go through each record in two files
with open(fn1, 'rt') as f1, open(fn2, 'rt') as f2:
while True:
try:
s1 = [next(f1), next(f1), next(f1), next(f1)]
s2 = [next(f2), next(f2), next(f2), next(f2)]
tag = func(s2) # a function to classify the record
fn_writer[tag][0].write(''.join(s1))
fn_writer[tag][1].write(''.join(s2))
except StopIteration:
break
# close writers
for tag in p7_bc_writer:
fn_writer[tag][0].close() # close writers
fn_writer[tag][1].close() # close writers问题
有没有办法加快这一进程?(以上功能太慢了)
如何使用特定的lines (如f.seek())将大文件分割成块,并与多个核并行运行该进程?
编辑-1
在每个文件中总共读取5亿(~180 GB的大小)。瓶颈是reading and writing文件。下面是我目前的解决方案(它可以工作,但肯定不是最好的)
我首先使用shell命令:split -l将大文件分割成较小的文件(耗时3小时)。
然后,将这些函数并行地应用于8个小文件(大约需要1小时)。
最后,合并结果(需要2小时)
还没有尝试PySpark,谢谢@John
发布于 2018-11-05 14:25:03
调查一下星火。您可以将文件分散到集群中,以便进行更快的处理。有一个python : pyspark。
https://spark.apache.org/docs/0.9.0/python-programming-guide.html
这也为您提供了实际执行Java代码的优势,Java代码不受GIL的影响,并且允许真正的多线程。
https://stackoverflow.com/questions/53155645
复制相似问题