-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauxiliary.h
292 lines (255 loc) · 10.3 KB
/
auxiliary.h
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
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PHENIQS_AUXILIARY_H
#define PHENIQS_AUXILIARY_H
#include "include.h"
#include "atom.h"
#include "barcode.h"
// int bam_aux_append(bam1_t *b, const char tag[2], char type, int len, const uint8_t *data)
/* 2 ASCII code SAM tag names
The enumeration provides a 16 bit integer representation of the two letter auxiliary tag name for fast decoding
tags defined in the SAM specification
http://samtools.github.io/hts-specs/SAMv1.pdf
or the SAM tag specification
http://samtools.github.io/hts-specs/SAMtags.pdf
FI i The index of segment in the template.
TC i The number of segments in the template.
FS Z Segment suffix.
RG Z Read group. Value matches the header RG-ID tag if @RG is present in the header.
PU Z Platform unit. Value to be consistent with the header RG-PU tag if @RG is present.
LB Z Library. Value to be consistent with the header RG-LB tag if @RG is present.
PG Z Program. Value matches the header PG-ID tag if @PG is present.
CO Z Free-text comments
Multiplex barcode
BC Z Raw uncorrected sample barcode sequence with any quality scores stored in the QT tag.
The BC tag should match the QT tag in length.
In the case of multiple sample barcode segments the recommended implementation
concatenates all the segments and places a hyphen (‘-’) seperator.
QT Z Phred quality of the sample barcode sequence in the BC tag. Phred score + 33 encoded.
In the case of multiple sample barcode segments the recommended implementation
concatenates all the segments and places a space (‘ ’) seperator.
XB f The probability that multiplexing barcode decoding is incorrect
Molecular Barcode
RX Z Sequence bases from the unique molecular identifier.
These could be either corrected or uncorrected. Unlike MI, the value may be non-unique in the file.
In the case of multiple unique molecular identifier segments the recommended implementation
concatenates all the segments and places a hyphen (‘-’) seperator.
If the bases represent corrected bases, the original sequence can be stored in OX.
QX Z Phred quality of the unique molecular identifier sequence in the RX tag. Phred score + 33 encoded.
The qualities here may have been corrected (Raw bases and qualities can be stored in OX and BZ respectively.)
In the case of multiple unique molecular identifiers (e.g., one on each end of the template) the recommended implementation concatenates all the quality strings with a space (‘ ’) between the different strings.
If the qualities represent corrected values, the original values can be stored in BZ.
OX Z Raw uncorrected unique molecular identifier bases, with any quality scores stored in the BZ tag.
In the case of multiple unique molecular identifier segments the recommended implementation
concatenates all the segments and places a hyphen (‘-’) seperator.
BZ Z Phred quality of the uncorrected unique molecular identifier sequence in the OX tag. Phred score + 33 encoded.
In the case of multiple molecular barcode segments the recommended implementation
concatenates all the segments and places a space (‘ ’) seperator.
MI Z Molecular Identifier. A unique ID within the SAM file for the source molecule from which this read is derived.
All reads with the same MI tag represent the group of reads derived from the same source molecule.
XM f The probability that molecular barcode decoding is incorrect
Cellular barcode
CB Z cell identifier
CR Z uncorrected cellular barcode sequence bases
CY Z phred quality of the cellular barcode sequence in the CR tag
XC f The probability that cellular barcode decoding is incorrect
XO f The overall probability that barcode based classification incorrect
Specification amendment recommendation
EE f Expected number of errors in the segment sequence
*/
enum class AuxiliaryTagType : uint8_t {
UNKNOWN,
PRINABLE_CHARACTER,
SIGNED_INTEGER,
FLOAT,
STRING,
BYTE_ARRAY,
NUMERIC_ARRAY,
};
string to_string(const AuxiliaryTagType& value);
bool from_string(const char* value, AuxiliaryTagType& result);
bool from_string(const string& value, AuxiliaryTagType& result);
ostream& operator<<(ostream& o, const AuxiliaryTagType& value);
enum class AuxiliaryArrayType : uint8_t {
UNKNOWN,
INT_8,
UNSIGNED_INT_8,
INT_16,
UNSIGNED_INT_16,
INT_32,
UNSIGNED_INT_32,
};
string to_string(const AuxiliaryArrayType& value);
bool from_string(const char* value, AuxiliaryArrayType& result);
bool from_string(const string& value, AuxiliaryArrayType& result);
ostream& operator<<(ostream& o, const AuxiliaryArrayType& value);
class AuxiliaryTag {
public:
uint8_t* data;
int32_t length;
int32_t capacity;
AuxiliaryTag() :
data(NULL),
length(0),
capacity(8) {
if((data = static_cast< uint8_t* >(malloc(capacity))) == NULL) {
throw OutOfMemoryError();
}
};
~AuxiliaryTag() {
free(data);
};
AuxiliaryTag(const AuxiliaryTag& other) :
length(other.length),
capacity(other.capacity) {
if((data = static_cast< uint8_t* >(malloc(capacity))) == NULL) {
throw OutOfMemoryError();
}
memcpy(data, other.data, length);
};
inline void increase_to_size(const int32_t& size) {
if(size > capacity) {
capacity = size;
kroundup32(capacity);
if((data = static_cast< uint8_t* >(realloc(data, capacity))) == NULL) {
throw OutOfMemoryError();
}
}
};
inline void increase_by_size(const int32_t& size) {
int32_t increased(length + size);
if(increased > capacity) {
kroundup32(increased);
if((data = static_cast< uint8_t* >(realloc(data, increased))) == NULL) {
throw OutOfMemoryError();
}
capacity = increased;
}
};
void inline assign(const uint8_t* value, const int32_t& size) {
if(size > 0) {
increase_to_size(size);
memcpy(this->data, value, size);
}
length = size;
};
void inline append(const uint8_t* value, const int32_t& size) {
if(size > 0) {
increase_by_size(size);
memcpy(this->data + length, value, size);
}
length = size;
};
inline const char& type() const {
return *(reinterpret_cast< char* >(data));
};
inline bool empty() const {
return length == 0;
};
inline void clear() {
length = 0;
};
AuxiliaryTag& operator=(const AuxiliaryTag& other) {
if(&other == this) {
return *this;
} else {
assign(other.data, other.length);
}
return *this;
};
};
class Auxiliary {
friend ostream& operator<<(ostream& o, const Auxiliary& auxiliary);
public:
void operator=(Auxiliary const &) = delete;
uint32_t FI;
uint32_t TC;
kstring_t FS;
kstring_t RG;
kstring_t PU;
kstring_t LB;
kstring_t PG;
kstring_t CO;
/* sample */
kstring_t BC;
kstring_t QT;
float XB;
/* molecular */
kstring_t RX;
kstring_t QX;
kstring_t OX;
kstring_t BZ;
kstring_t MI;
float XM;
/* cellular */
kstring_t CB;
kstring_t CR;
kstring_t CY;
float XC;
/* overall barcode classification */
float XO;
#if defined(PHENIQS_ILLUMINA_CONTROL_NUMBER)
uint16_t illumina_control_number;
#endif
#if defined(PHENIQS_EXTENDED_SAM_TAG)
unordered_map< uint16_t, AuxiliaryTag > extended;
#endif
/* expected error */
float EE;
Auxiliary();
Auxiliary(const Auxiliary& other);
~Auxiliary();
void decode(const bam1_t* bam1);
void encode(bam1_t* bam1) const;
inline void set_RG(const string& rg) {
if(!rg.empty()) ks_put_string(rg, RG);
};
inline void clear() {
/* FI and TC don't change during demultiplexing */
ks_clear(FS);
ks_clear(RG);
ks_clear(PU);
ks_clear(LB);
ks_clear(PG);
ks_clear(CO);
ks_clear(BC);
ks_clear(QT);
XB = 0;
ks_clear(RX);
ks_clear(QX);
ks_clear(OX);
ks_clear(BZ);
ks_clear(MI);
XM = 0;
ks_clear(CB);
ks_clear(CR);
ks_clear(CY);
XC = 0;
XO = 0;
#if defined(PHENIQS_ILLUMINA_CONTROL_NUMBER)
illumina_control_number = 0;
#endif
#if defined(PHENIQS_EXTENDED_SAM_TAG)
for(auto& record : extended) {
record.second.clear();
}
#endif
EE = 0;
};
};
ostream& operator<<(ostream& o, const Auxiliary& auxiliary);
#endif /* PHENIQS_AUXILIARY_H */