-
Notifications
You must be signed in to change notification settings - Fork 1
/
ktoolu_io.py
325 lines (279 loc) · 10.6 KB
/
ktoolu_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python
#
# Copyright (c) 2014-2016 Christian Schudoma, The Sainsbury Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import bz2
import gzip
import unittest
'''
from http://stackoverflow.com/questions/13044562/python-mechanism-to-identify-compressed-file-type-and-uncompress
"\x1f\x8b\x08": "gz",
"\x42\x5a\x68": "bz2",
"\x50\x4b\x03\x04": "zip"
'''
def isGZ(fn):
"""
Tests whether a file is gz-compressed.
:param fn: a filename
:type fn: str
:returns: True if fn is gz-compressed otherwise False
"""
assert os.path.exists(fn)
with open(fn, 'rb') as fi:
b1, b2 = fi.read(1), fi.read(1)
return b1 == b'\x1f' and b2 == b'\x8b'
def isBZ2(fn):
"""
Tests whether a file is bz2-compressed.
:param fn: a filename
:type fn: str
:returns: True if fn is bz2-compressed otherwise False
"""
assert os.path.exists(fn)
with open(fn, 'rb') as fi:
return fi.read(10) == b'BZh91AY&SY'
def openFile(fn, fmt=None, mode='rt'):
"""
Opens an uncompressed, gz-compressed or bz2-compressed file.
:param fn: a filename
:type fn: str
:param fmt: a file compression format
:type fmt: str {None, 'gz', or 'bz2'}
:param mode: a file mode (append, read, write)
:type mode: str {'a', 'ab', 'r', 'rb', 'w', 'wb'}
:returns: a handle to fn
"""
file_exists = os.path.exists(fn)
assert mode in ('a', 'r', 'w', 'rt', 'wt', 'at')
assert file_exists or mode in ('w', 'a', 'wt', 'at')
assert fmt in (None, 'gz', 'bz2')
if fmt == 'gz' or (fmt is None and (file_exists and isGZ(fn))):
_file = gzip.open(fn, mode)
elif fmt == 'bz2' or (fmt is None and (file_exists and isBZ2(fn))):
# _file = bz2.BZ2File(fn, mode)
_file = bz2.open(fn, mode=mode)
else:
_file = open(fn, mode)
return _file
def nRecords(fn, fmt='fq'):
assert fmt in ('fa', 'fq')
assert os.path.exists(fn)
nlines = sum(1 for line in openFile)
return nlines / (4 if fmt == 'fq' else 2)
def isPreCassava18(string):
return string.endswith('/1') or string.endswith('/2')
def getFastqIdentifier(string):
return string[1:-2] if isPreCassava18(string) else string.split()[0][1:]
def getFastaIdentifier(string):
string = string.split()[0][1:]
return string[:-2] if isPreCassava18(string) else string
def verifyFileFormat(fn, fileFormat):
firstChar = openFile(fn).read(1)
print("fc:", firstChar)
verifiedFastq = firstChar == '@' and fileFormat == 'fq'
verifiedFasta = firstChar == '>' and fileFormat == 'fa'
return verifiedFastq or verifiedFasta
def readFasta(_in):
if type(_in) is str:
return processFasta(openFile(_in))
else:
return processFasta(_in)
def processFasta(fi):
"""
Provides a generator to the contents of a Fasta file.
The function can handle single seq/multi seq, formatted/unformatted Fasta.
Input: a file/stream handle
Output: a generator object providing identifier and sequence information
for each record in the Fastq file
"""
seq, identifier = [], ''
for line in fi:
line = line.strip()
if not line:
# ignore empty lines
continue
if line.startswith('>'):
# new record starts with >identifier
if seq:
# if there is data in seq, return id, seq and empty seq
yield (identifier, ''.join(seq))
seq = []
identifier = line
else:
# current line contains sequence information
seq.append(line)
if seq:
# return last sequence record
yield (identifier, ''.join(seq))
def readFastq(_in):
if type(_in) is str:
return processFastq(openFile(_in))
else:
return processFastq(_in)
def processFastq(fi):
"""
Provides a generator to the contents of a Fastq file.
The function can handle single seq/multi seq, formatted/unformatted Fastq.
Input: a file/stream handle
Output: a generator object providing identifier, sequence, and quality information
for each record in the Fastq file
"""
block, identifier = [], ''
for line in fi:
line = line.strip()
if not line:
# ignore empty lines
continue
if line.startswith('@'):
# new record starts with @identifier
identifier = line
elif line.startswith('+'):
# if seq/qual separator occurs,
# read equal number of lines of quality information
seq = ''.join(block)
qual = ''.join([next(fi).strip() for row in block])
yield (identifier, seq, qual)
block, identifier = [], ''
else:
# current line contains sequence information
block.append(line)
def extractSequences(keepSequences, fileInfo, rejected=set()):
assert fileInfo.input_format in ('fq', 'fa')
if fileInfo.input_format == 'fq':
getID, getSeqs, nlines, outfmt = getFastqIdentifier, readFastq, 4, '%s\n%s\n+\n%s\n'
else:
getID, getSeqs, nlines, outfmt = getFastaIdentifier, readFasta, 2, '%s\n%s\n'
if 'gz_output' in fileInfo and fileInfo.gz_output:
ffmt = 'gz'
elif 'bz2_output' in fileInfo and fileInfo.bz2_output:
ffmt = 'bz2'
else:
ffmt = None
fwdOut, fwdGen = openFile(fileInfo.outR1, mode='wt', fmt=ffmt), getSeqs(fileInfo.inR1)
revOut, revGen = None, None
if fileInfo.outR2 and fileInfo.inR2:
revOut, revGen = openFile(fileInfo.outR2, mode='wt', fmt=ffmt), getSeqs(fileInfo.inR2)
fxid1, fxid2 = None, None
while 1:
try:
# fwdRecord = fwdGen.next()
fwdRecord = next(fwdGen)
#print(fwdRecord)
except:
break
fxid1 = getID(fwdRecord[0])
if revGen is not None:
try:
revRecord = next(revGen)
# revRecord = revGen.next()
except:
break
fxid2 = getID(revRecord[0])
assert fxid1 == fxid2 or fxid2 is None
if fxid1 in keepSequences or (rejected and fxid1 not in rejected): # the rejected-set allows extracting unclassified sequences if they are not in the kraken-output
fwdOut.write(outfmt % fwdRecord)
if revOut is not None:
revOut.write(outfmt % revRecord)
else:
pass
fwdOut.close()
if revOut is not None:
revOut.close()
pass
"""
# Experimental code
class ktFastxIterator(object):
def __init__(self, **kwargs):
fmt = kwargs.get('fmt', 'fq')
assert fmt in ('fq', 'fa')
if fmt == 'fa':
self.getID = getFastaIdentifier
self.getSeqs = readFasta
else:
self.getID = getFastqIdentifier
self.getSeqs = readFastq
assert 'in1' in kwargs and 'out1' in kwargs
self.in1, self.out1 = self.getSeqs(kwargs['in1']), open(kwargs['out1'], 'wb')
self.in2, self.out2 = kwargs.get('in2', None), kwargs.get('out2', None)
if self.in2 is not None and self.out2 is not None:
self.in2, self.out2 = self.getSeqs(self.in2), open(self.out2, 'wb')
pass
def next(self):
assert self.in1
y1, y2 = self.in1.next(), (self.in2.next() if self.in2 is not None else None)
print 'y1', y1, type(y1)
if y1:
yield y1, y2
#while 1:
# try:
# yield self.in1.next(), (self.in2.next() if self.in2 is not None else None)
# except:
# break
pass
"""
"""
Unit tests
"""
GZ_TESTFILE = 'testdata/ktoolu_test.R1.fq.gz'
BZ2_TESTFILE = 'testdata/ktoolu_test.R1.fq.bz2'
FASTQ_TESTFILE_R1 = 'testdata/ktoolu_test.R1.fq'
FASTQ_TESTRECORDS = [22, 27, 32]
FASTA_TESTFILE = 'testdata/ktoolu_test.fa'
FASTA_TESTRECORDS = [22, 27, 32]
class compressedFileTest(unittest.TestCase):
def test(self):
self.assertTrue(isGZ(GZ_TESTFILE))
self.assertFalse(isBZ2(GZ_TESTFILE))
self.assertTrue(isBZ2(BZ2_TESTFILE))
self.assertFalse(isGZ(BZ2_TESTFILE))
self.assertFalse(isGZ(FASTA_TESTFILE))
self.assertFalse(isGZ(FASTQ_TESTFILE_R1))
class readFastqTest(unittest.TestCase):
def test(self):
data = list(readFastq(FASTQ_TESTFILE_R1))
self.assertEqual(len(data), len(FASTQ_TESTRECORDS))
self.assertEqual(len(data[0][1]), FASTQ_TESTRECORDS[0])
self.assertEqual(len(data[1][1]), FASTQ_TESTRECORDS[1])
self.assertEqual(len(data[2][1]), FASTQ_TESTRECORDS[2])
class readFastaTest(unittest.TestCase):
def test(self):
data = list(readFasta(FASTA_TESTFILE))
self.assertEqual(len(data), len(FASTA_TESTRECORDS))
self.assertEqual(len(data[0][1]), FASTQ_TESTRECORDS[0])
self.assertEqual(len(data[1][1]), FASTQ_TESTRECORDS[1])
self.assertEqual(len(data[2][1]), FASTQ_TESTRECORDS[2])
class getFastqIdentifierTest(unittest.TestCase):
def test(self):
self.assertEqual(getFastqIdentifier('@HWUSI-EAS100R:6:73:941:1973#0/1'), 'HWUSI-EAS100R:6:73:941:1973#0')
self.assertEqual(getFastqIdentifier('@EAS139:136:FC706VJ:2:2104:15343:197393 1:Y:18:ATCACG'), 'EAS139:136:FC706VJ:2:2104:15343:197393')
if __name__ == '__main__':
unittest.main()
pass
__author__ = "Christian Schudoma"
__copyright__ = "Copyright 2014-2016, Christian Schudoma, The Sainsbury Laboratory"
__credits__ = ["Pirasteh Pahlavan", "Agathe Jouet", "Yogesh Gupta"]
__license__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Christian Schudoma"
__email__ = "cschu1981@gmail.com"
__status__ = "Development"