-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathReadFormatter.hpp
More file actions
425 lines (376 loc) · 10.4 KB
/
ReadFormatter.hpp
File metadata and controls
425 lines (376 loc) · 10.4 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifndef _MOURISL_READ_FORMATTER
#define _MOURISL_READ_FORMATTER
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BufferManager.hpp"
enum {
FORMAT_READ1,
FORMAT_READ2,
FORMAT_BARCODE,
FORMAT_UMI,
FORMAT_CATEGORY_COUNT
} ;
struct _segInfo
{
int start ;
int end ;
int strand ; // -1: minus, 1:posiive
bool inComment ;
int field ; // -1: field tag is a string. Otherwise, the field is segments separated by space or tab
char *fieldPrefix ;
bool operator<( const struct _segInfo &b ) const
{
return start < b.start ;
}
} ;
// Parse for read1, read2, barcode and UMI.
// Current implementation is not thread-safe.
class ReadFormatter
{
private:
BufferManager<char> _buffers ;
char _compChar[256] ;
std::vector<struct _segInfo> _segs[FORMAT_CATEGORY_COUNT] ;
bool _areSegmentsSorted[FORMAT_CATEGORY_COUNT] ; // Whether the segments for this category is sorted or not. Sorted segments may be parsed quicker in certain functions.
// Return false if it fails to parse the format string.
bool ParseFormatStringAndAppendEffectiveRange(const char *s, int len) {
int i;
int j = 0; // start, end, strand section
char buffer[20];
int blen = 0;
int start;
struct _segInfo seg ;
if (s[2] != ':')
return false ;
int category = 0 ;
if (s[0] == 'r' && s[1] == '1') {
category = FORMAT_READ1 ;
} else if (s[0] == 'r' && s[1] == '2') {
category = FORMAT_READ2 ;
} else if (s[0] == 'b' && s[1] == 'c') {
category = FORMAT_BARCODE ;
} else if (s[0] == 'u' && s[1] == 'm') {
category = FORMAT_UMI ;
} else {
return false ;
}
//Specification like: bc:hd:XX:YY. (hd is for header comment field, XX is the number, YY is the conventional segment specification string)
//Also support bc:hd:SSS:YY for searching the tag with SSS as the prefix
start = 3 ;
seg.inComment = false ;
if (len >= 6 && s[3] == 'h' && s[4] == 'd' && s[5] == ':')
{
seg.inComment = true ;
blen = 0 ;
start = 6 ;
for (i = start ; i <= len ; ++i)
{
if (i == len || s[i] == ':')
{
buffer[blen] = '\0' ;
int l ;
for (l = 0 ; l < blen ; ++l)
if (buffer[l] < '0' || buffer[l] > '9')
break ;
if (l == blen)
{
seg.field = atoi(buffer) ;
seg.fieldPrefix = NULL ;
}
else
{
seg.field = -1 ;
seg.fieldPrefix = strdup(buffer) ;
}
break ;
}
buffer[blen] = s[i] ;
++blen ;
}
start = i + 1 ;
}
seg.strand = 1 ;
blen = 0 ;
for (i = start; i <= len; ++i) {
if (i == len || s[i] == ':') {
buffer[blen] = '\0';
if (j == 0) {
seg.start = atoi(buffer) ;
} else if (j == 1) {
seg.end = atoi(buffer) ;
} else {
seg.strand = (buffer[0] == '+' ? 1 : -1);
}
blen = 0;
if (i < len && s[i] == ':') {
++j;
}
} else {
buffer[blen] = s[i];
++blen;
}
}
if (j >= 3 || j < 1) {
return false;
}
_segs[category].push_back(seg) ;
return true;
}
bool AreSegmentsSorted(int category)
{
int size = _segs[category].size() ;
int i ;
for (i = 1 ; i < size ; ++i)
if (_segs[category][i].start <= _segs[category][i - 1].end)
return false ;
return true ;
}
void ReverseBuffer(char *buffer, int len)
{
int i, j ;
for (i = 0, j = len - 1 ; i < j ; ++i, --j )
{
char tmp = buffer[i] ;
buffer[i] = buffer[j] ;
buffer[j] = tmp ;
}
}
void ComplementBuffer(char *buffer, int len)
{
int i ;
for (i = 0 ; i < len ; ++i)
buffer[i] = _compChar[ (int)buffer[i] ] ;
}
public:
ReadFormatter() {
int i ;
for (i = 0 ; i < 256 ; ++i)
_compChar[i] = 'N' ;
_compChar['A'] = 'T' ;
_compChar['C'] = 'G' ;
_compChar['G'] = 'C' ;
_compChar['T'] = 'A' ;
for (i = 0 ; i < FORMAT_CATEGORY_COUNT ; ++i)
_areSegmentsSorted[i] = true ;
}
~ReadFormatter()
{
int i, j ;
for (i = 0 ; i < FORMAT_CATEGORY_COUNT ; ++i)
{
if (!IsInComment(i))
continue ;
int segCnt = _segs[i].size() ;
for (j = 0 ; j < segCnt ; ++j)
if (_segs[i][j].inComment && _segs[i][j].field == -1)
free(_segs[i][j].fieldPrefix) ;
}
}
void AllocateBuffers(int bufferCnt)
{
_buffers.Init(bufferCnt) ;
}
void Init(const char *formatStr) {
int i, j;
if (_buffers.GetBufferCount() == 0)
AllocateBuffers(2) ;
for (i = 0 ; formatStr[i] ; ) {
for (j = i ; formatStr[j] && formatStr[j] != ';' && formatStr[j] != ',' ; ++j)
;
if (!ParseFormatStringAndAppendEffectiveRange(formatStr + i, j - i))
{
fprintf(stderr, "Format description error in %s\n", formatStr) ;
exit(1) ;
}
if (formatStr[j])
i = j + 1 ;
else
i = j ;
}
// Sort the order in each specification
// It seems there are applications
//for (i = 0 ; i < FORMAT_CATEGORY_COUNT ; ++i)
// std::sort(_segs[i].begin(), _segs[i].end()) ;
for (i = 0 ; i < FORMAT_CATEGORY_COUNT ; ++i)
_areSegmentsSorted[i] = AreSegmentsSorted(i) ;
}
void AddSegment(int start, int end, int strand, int category)
{
struct _segInfo ns ;
ns.start = start ;
ns.end = end ;
ns.strand = strand ;
_segs[category].push_back(ns) ;
//std::sort(_segs[ category ].begin(), _segs[ category ].end()) ;
if (_buffers.GetBufferCount() == 0)
AllocateBuffers(2) ;
}
// category == FORMAT_CATEGORY_COUNT would be total segment count
int GetSegmentCount(int category)
{
int i ;
int ret = 0 ;
if (category == FORMAT_CATEGORY_COUNT)
{
for (i = 0 ; i < FORMAT_CATEGORY_COUNT ; ++i)
ret += _segs[i].size() ;
}
else
{
return _segs[category].size() ;
}
return ret ;
}
int NeedExtract(int category)
{
int size = _segs[category].size() ;
if (size == 0)
return 0 ;
else if (size == 1)
{
if (_segs[category][0].start == 0
&& _segs[category][0].end == -1
&& _segs[category][0].strand == 1
&& _segs[category][0].inComment == false)
return 0 ;
}
return 1 ;
}
bool IsInComment(int category)
{
if (_segs[category].size() > 0 && _segs[category][0].inComment)
return true ;
return false ;
}
// needComplement=true: reverse complement. Otherwise, just reverse
// retSeqWhenNoExtraction: when needextract==false, return seq instead of buffer
// bufferId -1 for inplace (a super set of retSeqWhenNoExtraction)
// The outside program can modify the buffer.
char* Extract(char *seq, int category, bool needComplement, bool retSeqWhenNoExtraction, int bufferId = 0)
{
// Even when input is NULL string, we return a valid string, so downstream algorithm don't need to
// worry extract NULL.
if (seq == NULL)
{
char *buffer = _buffers.Get(bufferId, 1) ;
buffer[0] = '\0' ;
return buffer ;
}
int len = strlen(seq) ;
int i, j, k ;
const std::vector<_segInfo> &seg = _segs[category] ;
int segSize = seg.size() ;
int strand = 1 ;
if (!NeedExtract(category))
{
if (retSeqWhenNoExtraction || bufferId == -1) // this implictly require no _buffers initalization
return seq ;
else
{
char *buffer = _buffers.Get(bufferId, len + 1) ;
strcpy(buffer, seq) ;
return buffer ;
}
}
char *buffer = seq ;
if (bufferId >= 0)
buffer = _buffers.Get(bufferId, len + 1) ;
i = 0 ;
for (k = 0 ; k < segSize ; ++k)
{
int start = seg[k].start ;
int end = seg[k].end ;
int lenk = len ; // For pattern not in the comment field, this is the length of the seuqnece. For pattern in the comment, this represents the length of the field.
if (IsInComment(category))
{
// Move seq to the appropriate section and adjust start, end, lenk
// Assume seq is the comment
int f = 0 ;
int fstart = 0, fend = 0 ; // where the field starts and ends
if (seg[k].field >= 0)
{
for (j = 0 ; j <= len ; ++j)
{
if (seq[j] == ' ' || seq[j] == '\t' || seq[j] == '\0')
{
++f ;
if (f == seg[k].field)
fstart = j + 1 ;
else if (f == seg[k].field + 1)
{
fend = j - 1 ;
break ;
}
}
}
if (f <= seg[k].field) // Field is not found
{
fstart = len ;
fend = len - 1 ;
}
}
else
{
char *p = strstr(seq, seg[k].fieldPrefix) ;
if (p != NULL)
{
fstart = p - seq ;
for (; *p != ' ' && *p != '\t' && *p != '\0' ; ++p)
;
fend = p - seq - 1 ;
}
else // No pattern hit.
{
fstart = len ; // Make sure actual start begins later than fend
fend = len - 1 ;
}
}
if (start >= 0)
start += fstart ;
if (end >= 0)
end += fstart ;
lenk = fend + 1 ;
}
if (start < 0)
start = lenk + start ;
if (end >= lenk)
end = lenk - 1 ;
else if (end < 0)
end = lenk + end ;
for (j = start ; j <= end ; ++j)
{
buffer[i] = seq[j] ;
++i ;
}
if (seg[k].strand == -1)
strand = -1 ;
}
buffer[i] = '\0' ;
if (strand == -1)
{
ReverseBuffer(buffer, i) ;
if (needComplement)
ComplementBuffer(buffer, i) ;
}
return buffer ;
}
// Directly change the content of seq and qual
void InplaceExtractSeqAndQual(char *seq, char *qual, int category, int bufferId = 0)
{
char *buffer = Extract(seq, category, true, true,
_areSegmentsSorted[category] ? -1 : bufferId) ;
if (buffer != seq)
strcpy(seq, buffer) ;
if (qual != NULL)
{
buffer = Extract(qual, category, false, true,
_areSegmentsSorted[category] ? -1 : bufferId) ;
if (buffer != qual)
strcpy(qual, buffer) ;
}
}
} ;
#endif