|
a |
|
b/singlecellmultiomics/fastqProcessing/fastqHandle.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
import gzip |
|
|
4 |
from singlecellmultiomics.pyutils.handlelimiter import HandleLimiter |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
class FastqHandle: |
|
|
8 |
def __init__( |
|
|
9 |
self, |
|
|
10 |
path, |
|
|
11 |
pairedEnd=False, |
|
|
12 |
single_cell=False, |
|
|
13 |
maxHandles=500): |
|
|
14 |
self.pe = pairedEnd |
|
|
15 |
self.sc = single_cell |
|
|
16 |
self.path = path |
|
|
17 |
if not self.sc: |
|
|
18 |
if pairedEnd: |
|
|
19 |
self.handles = [ |
|
|
20 |
gzip.open( |
|
|
21 |
path + |
|
|
22 |
'R1.fastq.gz', |
|
|
23 |
'wt',compresslevel=1), |
|
|
24 |
gzip.open( |
|
|
25 |
path + |
|
|
26 |
'R2.fastq.gz', |
|
|
27 |
'wt',compresslevel=1)] |
|
|
28 |
else: |
|
|
29 |
self.handles = [gzip.open(path + 'R1.fastq.gz', 'wt',compresslevel=1)] |
|
|
30 |
else: |
|
|
31 |
|
|
|
32 |
self.handles = HandleLimiter( |
|
|
33 |
compressionLevel=1, maxHandles=maxHandles) |
|
|
34 |
|
|
|
35 |
def write(self, records): |
|
|
36 |
if self.sc: |
|
|
37 |
for readIdx, record in zip(('R1', 'R2'), records): |
|
|
38 |
# Obtain cell from record: |
|
|
39 |
cell = f"{record.tags.get('bi','no_cell_id')}.{record.tags.get('MX','unk')}" |
|
|
40 |
self.handles.write( |
|
|
41 |
f'{self.path}.{cell}.{readIdx}.fastq.gz', |
|
|
42 |
str(record), |
|
|
43 |
method=1) |
|
|
44 |
else: |
|
|
45 |
for handle, record in zip(self.handles, records): |
|
|
46 |
handle.write(str(record)) |
|
|
47 |
|
|
|
48 |
def close(self): |
|
|
49 |
if self.sc: |
|
|
50 |
self.handles.close() |
|
|
51 |
else: |
|
|
52 |
for handle in self.handles: |
|
|
53 |
handle.close() |