-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproxy.cpp
289 lines (274 loc) · 15.4 KB
/
proxy.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
/*
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 "proxy.h"
string to_string(const FormatKind& value) {
string result;
switch(value) {
case FormatKind::FASTQ: result.assign("FASTQ"); break;
case FormatKind::HTS: result.assign("HTS"); break;
case FormatKind::DEV_NULL: result.assign("DEV_NULL"); break;
default: result.assign("UNKNOWN"); break;
}
return result;
};
bool from_string(const char* value, FormatKind& result) {
if(value == NULL) result = FormatKind::UNKNOWN;
else if(!strcmp(value, "FASTQ")) result = FormatKind::FASTQ;
else if(!strcmp(value, "HTS")) result = FormatKind::HTS;
else if(!strcmp(value, "DEV_NULL")) result = FormatKind::DEV_NULL;
else result = FormatKind::UNKNOWN;
return (result == FormatKind::UNKNOWN ? false : true);
};
void to_kstring(const FormatKind& value, kstring_t& result) {
ks_clear(result);
string string_value(to_string(value));
ks_put_string(string_value.c_str(), string_value.size(), result);
};
bool from_string(const string& value, FormatKind& result) {
return from_string(value.c_str(), result);
};
ostream& operator<<(ostream& o, const FormatKind& value) {
o << to_string(value);
return o;
};
void encode_key_value(const string& key, const FormatKind& value, Value& container, Document& document) {
string string_value(to_string(value));
Value v(string_value.c_str(), string_value.length(), document.GetAllocator());
Value k(key.c_str(), key.size(), document.GetAllocator());
container.RemoveMember(key.c_str());
container.AddMember(k.Move(), v.Move(), document.GetAllocator());
};
template<> bool decode_value_by_key< FormatKind >(const Value::Ch* key, FormatKind& value, const Value& container) {
Value::ConstMemberIterator element = container.FindMember(key);
if(element != container.MemberEnd() && !element->value.IsNull()) {
if(element->value.IsString()) {
return from_string(element->value.GetString(), value);
} else { throw ConfigurationError(string(key) + " element must be a string"); }
}
return false;
};
FeedProxy::FeedProxy(const Value& ontology) try :
index(decode_value_by_key< int32_t >("index", ontology)),
url(decode_value_by_key< URL >("url", ontology)),
direction(decode_value_by_key< IoDirection >("direction", ontology)),
phred_offset(decode_value_by_key< uint8_t >("phred offset", ontology)),
capacity(decode_value_by_key< int32_t >("capacity", ontology)),
resolution(decode_value_by_key< int32_t >("resolution", ontology)),
platform(decode_value_by_key< Platform >("platform", ontology)),
hfile(NULL) {
} catch(ConfigurationError& error) {
throw ConfigurationError("FeedProxy :: " + error.message);
} catch(exception& error) {
throw InternalError("FeedProxy :: " + string(error.what()));
};
void FeedProxy::open() {
if(hfile == NULL) {
if(!is_dev_null()) {
int hopen_errono(0);
switch(direction) {
case IoDirection::IN: {
/* Use hfile to probe the input file and verify format */
if(url.is_readable()) {
hfile = hopen(url.hfile_name(), "r");
hopen_errono = errno;
if(hfile != NULL) {
if(url.type() == FormatType::UNKNOWN) {
ssize_t peeked(0);
unsigned char* buffer(NULL);
const ssize_t buffer_capacity(PEEK_BUFFER_CAPACITY);
if((buffer = static_cast< unsigned char* >(malloc(buffer_capacity))) == NULL) {
throw OutOfMemoryError();
}
/* detect input format with htslib and override the type encoded in the url */
htsFormat format;
if(!hts_detect_format(hfile, &format)) {
switch (format.format) {
case htsExactFormat::sam:
url.set_type(FormatType::SAM);
break;
case htsExactFormat::bam:
url.set_type(FormatType::BAM);
break;
case htsExactFormat::bai:
url.set_type(FormatType::BAI);
break;
case htsExactFormat::cram:
url.set_type(FormatType::CRAM);
break;
case htsExactFormat::crai:
url.set_type(FormatType::CRAI);
break;
case htsExactFormat::vcf:
url.set_type(FormatType::VCF);
break;
case htsExactFormat::bcf:
url.set_type(FormatType::BCF);
break;
case htsExactFormat::csi:
url.set_type(FormatType::CSI);
break;
case htsExactFormat::gzi:
url.set_type(FormatType::GZI);
break;
case htsExactFormat::tbi:
url.set_type(FormatType::TBI);
break;
case htsExactFormat::bed:
url.set_type(FormatType::BED);
break;
default:
url.set_type(FormatType::UNKNOWN);
break;
}
}
if(url.type() == FormatType::SAM) {
peeked = hpeek(hfile, buffer, buffer_capacity);
if(peeked > 0) {
switch (format.compression) {
case htsCompression::gzip:
case htsCompression::bgzf: {
unsigned char* decompressed_buffer(NULL);
if((decompressed_buffer = static_cast< unsigned char* >(malloc(buffer_capacity))) == NULL) {
throw OutOfMemoryError();
}
z_stream gz_stream;
gz_stream.zalloc = NULL;
gz_stream.zfree = NULL;
gz_stream.next_in = buffer;
gz_stream.avail_in = static_cast< unsigned >(peeked);
gz_stream.next_out = decompressed_buffer;
gz_stream.avail_out = buffer_capacity;
if(inflateInit2(&gz_stream, 31) == Z_OK) {
while(gz_stream.total_out < buffer_capacity) {
if(inflate(&gz_stream, Z_SYNC_FLUSH) != Z_OK) break;
}
inflateEnd(&gz_stream);
memcpy(buffer, decompressed_buffer, gz_stream.total_out);
peeked = gz_stream.total_out;
} else {
peeked = 0;
}
free(decompressed_buffer);
break;
};
case htsCompression::no_compression:
break;
default:
throw InternalError("unknown compression");
break;
}
}
if(peeked > 0) {
size_t state(0);
char* position(reinterpret_cast< char * >(buffer));
char* end(position + peeked);
while(position < end && position != NULL) {
if(state == 0) {
if(*position == '\n') {
++position;
} else {
if(*position == '@') {
state = 1;
} else { break; }
}
} else if(state == 1) {
if((*position >= 'A' && *position <= 'Z') || (*position >= 'a' && *position <= 'z')) {
state = 2;
} else { break; }
} else if(state == 2) {
if(*position == '+' && position < end && *(position + 1) == '\n') {
url.set_type(FormatType::FASTQ);
}
break;
}
if((position = strchr(position, '\n')) != NULL) ++position;
}
}
}
free(buffer);
}
} else { throw IOError("failed to open " + string(url.path()) + " for reading with error code " + to_string(hopen_errono)); }
} else { throw IOError("could not open " + string(url.path()) + " for reading"); }
break;
};
case IoDirection::OUT: {
if(url.is_writable()) {
hfile = hopen(url.hfile_name(), "wx");
hopen_errono = errno;
if(hfile != NULL) {
/* more testing */
} else { throw IOError("failed to open " + string(url.path()) + " for writing with error code " + to_string(hopen_errono)); }
} else { throw IOError("could not open " + string(url.path()) + " for writing"); }
break;
};
default:
break;
}
}
}
};
bool encode_value(const string& key, const FeedProxy& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
Value element(kObjectType);
encode_key_value("index", value.index, element, document);
encode_key_value("url", value.url, element, document);
encode_key_value("direction", value.direction, element, document);
encode_key_value("platform", value.platform, element, document);
encode_key_value("capacity", value.capacity, element, document);
encode_key_value("resolution", value.resolution, element, document);
encode_key_value("phred offset", value.phred_offset, element, document);
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), element.Move(), document.GetAllocator());
return true;
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
ostream& operator<<(ostream& o, const FeedProxy& proxy) {
o << "direction : " << proxy.direction << endl;
o << "index : " << proxy.index << endl;
o << "url : " << proxy.url << endl;
o << "platform : " << proxy.platform << endl;
o << "capacity : " << proxy.capacity << endl;
o << "resolution : " << proxy.resolution << endl;
o << "phred_offset : " << to_string(proxy.phred_offset) << endl;
o << proxy.url.description();
return o;
};
template<> FeedProxy decode_value< FeedProxy >(const Value& container) {
if(container.IsObject()) {
FeedProxy proxy(container);
return proxy;
} else { throw ConfigurationError("feed proxy element must be a dictionary"); }
};
template<> list< FeedProxy > decode_value_by_key< list< FeedProxy > >(const Value::Ch* key, const Value& container) {
if(container.IsObject()) {
Value::ConstMemberIterator reference = container.FindMember(key);
if(reference != container.MemberEnd()) {
if(!reference->value.IsNull()) {
if(reference->value.IsArray()) {
list< FeedProxy > value;
if(!reference->value.Empty()) {
for(auto& element : reference->value.GetArray()) {
value.emplace_back(element);
}
}
return value;
} else { throw ConfigurationError(string(key) + " is not an array"); }
} else { throw ConfigurationError(string(key) + " is null"); }
} else { throw ConfigurationError(string(key) + " not found"); }
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
};