-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransform.cpp
331 lines (314 loc) · 14.8 KB
/
transform.cpp
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
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <lior.galanti@nyu.edu>
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/>.
*/
#include "transform.h"
ostream& operator<<(ostream& o, const LeftTokenOperator& value) {
switch (value) {
case LeftTokenOperator::NONE: o << "none"; break;
case LeftTokenOperator::REVERSE_COMPLEMENT: o << "reverse complement"; break;
}
return o;
};
static const regex template_token_regex = regex("^(s|c|m|[0-9]+):(-?[0-9]+)?:(-?[0-9]+)?$");
/* Token
input_segment_index can be a positive integer to reference an input segment index
the following negative integers have a special meaning:
-1 error corrected sample barcode
-2 error corrected cellular barcode
-3 error corrected molecular barcode
*/
Token::Token(
const int32_t& index,
const int32_t& input_segment_index,
const int32_t& start,
const int32_t& end,
const bool& end_terminated) :
index(index),
input_segment_index(input_segment_index),
start(start),
end(end),
end_terminated(end_terminated) {
};
Token::Token(const Token& other) :
index(other.index),
input_segment_index(other.input_segment_index),
start(other.start),
end(other.end),
end_terminated(other.end_terminated) {
};
string Token::description() const {
string o("cycles ");
o.append(to_string(start));
o.append(" to ");
o.append(end_terminated ? to_string(end) : "end");
if(input_segment_index < 0) {
o.append(" of decoded ");
if(input_segment_index == -1) {
o.append("sample");
} else if(input_segment_index == -2) {
o.append("cellular");
} else if(input_segment_index == -3) {
o.append("molecular");
}
o.append(" barcode");
} else {
o.append(" of input segment ");
o.append(to_string(input_segment_index));
}
return o;
};
Token::operator string() const {
string o;
if(input_segment_index < 0) {
if(input_segment_index == -1) {
o.push_back('s');
} else if(input_segment_index == -2) {
o.push_back('c');
} else if(input_segment_index == -3) {
o.push_back('m');
}
} else {
o.append(to_string(input_segment_index));
}
o.push_back(':');
if(start) o.append(to_string(start));
o.push_back(':');
if(end_terminated) o.append(to_string(end));
return o;
};
ostream& operator<<(ostream& o, const Token& token) {
if(token.input_segment_index < 0) {
if(token.input_segment_index == -1) {
o << "s";
} else if(token.input_segment_index == -2) {
o << "c";
} else if(token.input_segment_index == -3) {
o << "m";
}
} else {
o << token.input_segment_index;
}
o << ":";
if(token.start) o << token.start;
o << ":";
if(token.end_terminated) o << token.end;
return o;
};
template<> vector< Token > decode_value_by_key(const Value::Ch* key, const Value& container) {
Value::ConstMemberIterator reference = container.FindMember(key);
if(reference != container.MemberEnd()) {
if(!reference->value.IsNull()) {
if(reference->value.IsArray()) {
int32_t token_index(0);
vector< Token > value;
value.reserve(reference->value.Size());
for(auto& element : reference->value.GetArray()) {
if(!element.IsNull()) {
if(element.IsString()) {
string pattern(element.GetString(), element.GetStringLength());
int32_t input_segment_index(numeric_limits< int32_t >::max());
int32_t start(0);
int32_t end(0);
bool end_terminated(true);
smatch match;
if(regex_match(pattern, match, template_token_regex)) {
if(match[1] == "s") {
/* corrected sample barcode sequence */
input_segment_index = -1;
} else if(match[1] == "c") {
/* corrected cellular barcode sequence */
input_segment_index = -2;
} else if(match[1] == "m") {
/* corrected molecular barcode sequence */
input_segment_index = -3;
} else {
input_segment_index = stoi(match[1]);
}
if(match[2].length()) {
start = stoi(match[2]);
}
if(match[3].length()) {
end = stoi(match[3]);
} else {
end_terminated = false;
}
} else {
throw ConfigurationError("illegal token syntax " + pattern);
}
value.emplace_back(token_index, input_segment_index, start, end, end_terminated);
++token_index;
} else { throw ConfigurationError("token element must be a string"); }
} else { throw ConfigurationError("token element is null"); }
}
return value;
} else { throw ConfigurationError(string(key) + " element must be a dictionary"); }
} else { throw ConfigurationError(string(key) + " element is null"); }
} else { throw ConfigurationError(string(key) + " element not found"); }
};
/* Transform */
Transform::Transform(const Transform& other) :
Token(other),
output_segment_index(other.output_segment_index),
left(other.left) {
};
string Transform::description() const {
string o("Append ");
switch (left) {
case LeftTokenOperator::NONE:
o.append("token ");
break;
case LeftTokenOperator::REVERSE_COMPLEMENT:
o.append("reverse complemented token ");
break;
}
o.append(to_string(index));
if(input_segment_index < 0) {
o.append(" of decoded ");
if(input_segment_index == -1) {
o.append("sample");
} else if(input_segment_index == -2) {
o.append("cellular");
} else if(input_segment_index == -3) {
o.append("molecular");
}
o.append(" barcode");
} else {
o.append(" of input segment ");
o.append(to_string(input_segment_index));
}
o.append(" to output segment ");
o.append(to_string(output_segment_index));
return o;
};
Transform::operator string() const {
string o;
switch (left) {
case LeftTokenOperator::NONE:
break;
case LeftTokenOperator::REVERSE_COMPLEMENT:
o.push_back('~');
break;
}
o.append(to_string(index));
return o;
};
ostream& operator<<(ostream& o, const Transform& transform) {
o << string(transform);
return o;
};
bool encode_key_value(const string& key, const list< Transform >& value, Value& container, Document& document) {
if(!value.empty()) {
Value collection(kArrayType);
int32_t index(0);
string current;
for(auto& transform : value) {
if(transform.output_segment_index != index) {
collection.PushBack(Value(current.c_str(), current.size(), document.GetAllocator()).Move(), document.GetAllocator());
current.clear();
++index;
}
if(current.size() > 0) {
current.push_back(':');
}
current.append(string(transform));
}
collection.PushBack(Value(current.c_str(), current.size(), document.GetAllocator()).Move(), document.GetAllocator());
container.RemoveMember(key.c_str());
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), collection.Move(), document.GetAllocator());
return true;
}
return false;
};
/* Rule */
template<> Rule decode_value_by_key(const Value::Ch* key, const Value& container) {
Value::ConstMemberIterator reference = container.FindMember(key);
if(reference != container.MemberEnd()) {
const Value& rule_element(reference->value);
if(!rule_element.IsNull()) {
if(rule_element.IsObject()) {
vector< Token > token_array(decode_value_by_key< vector< Token > >("token", rule_element));
reference = rule_element.FindMember("knit");
if(reference != rule_element.MemberEnd()) {
const Value& observation_element(reference->value);
if(!observation_element.IsNull()) {
if(observation_element.IsArray()) {
vector< Transform > transform_array;
int32_t output_segment_cardinality(0);
for(auto& element : observation_element.GetArray()) {
if(!element.IsNull()) {
if(element.IsString()) {
int32_t position(0);
LeftTokenOperator left = LeftTokenOperator::NONE;
int32_t token_index(numeric_limits< int32_t >::max());
string pattern(element.GetString(), element.GetStringLength());
while(true) {
const char& c = pattern[position];
switch(c) {
case ':':
case '\0': {
if(token_index != numeric_limits< int32_t >::max()) {
if(token_index < static_cast< int32_t >(token_array.size())) {
transform_array.emplace_back(token_array[token_index], output_segment_cardinality, left);
token_index = numeric_limits< int32_t >::max();
left = LeftTokenOperator::NONE;
} else { throw ConfigurationError("invalid token reference " + to_string(token_index) + " in transform"); }
} else { throw ConfigurationError("transform must explicitly specify a token reference"); }
break;
};
case '~': {
if(token_index == numeric_limits< int32_t >::max()) {
left = LeftTokenOperator::REVERSE_COMPLEMENT;
} else {
throw ConfigurationError(string("illegal right hand side operator in transform ") + c);
}
break;
};
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
if(token_index == numeric_limits< int32_t >::max()) {
token_index = c - '0';
} else {
token_index = token_index * 10 + (c - '0');
}
break;
};
default:
throw ConfigurationError(string("illegal character in transform ") + c);
}
if(c == '\0') { break; }
++position;
}
++output_segment_cardinality;
} else { throw ConfigurationError("transform element must be a string"); }
} else { throw ConfigurationError("transform element can not be null"); }
}
Rule value(token_array, output_segment_cardinality, transform_array);
return value;
} else { throw ConfigurationError("rule observation element must be an array"); }
} else { throw ConfigurationError("rule observation element is null"); }
} else { throw ConfigurationError("rule must define an observation element"); }
} else { throw ConfigurationError("element " + string(key) + " must be a dictionary"); }
} else { throw ConfigurationError("element " + string(key) + " is null"); }
} else { throw ConfigurationError("no element " + string(key) + " found"); }
};