-
Notifications
You must be signed in to change notification settings - Fork 10
/
annotation_util.cpp
356 lines (258 loc) · 6.93 KB
/
annotation_util.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
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
#include "annotation.h"
#include <sstream>
#include <string.h>
using namespace std;
#define MAX_LINE_LEN 1024
unsigned long int line_number = 1;
bool is_eol(char m_c);
// Note that genbank entries are 1's based (not zero based like asn.1
// entires).
bool read_range(gzFile m_fin, pair<unsigned int, unsigned int> &m_range,
list< pair<unsigned int, unsigned int> > &m_seg_list)
{
unsigned int i = 0;
char buffer[MAX_LINE_LEN];
if( (gzgets(m_fin, buffer, MAX_LINE_LEN) == NULL) || !strip_eol(buffer, MAX_LINE_LEN) ){
throw __FILE__ ":read_range: Unable to read line";
}
++line_number;
unsigned int len = strlen(buffer);
// Make the range parsing robust to Mac/PC/Unix EOL differences
if((len > 0) && (buffer[len - 1] == '\r') ){
len--;
}
// Skip to the start of the range entry.
while(isspace(buffer[i])){
i++;
}
// For now, ignore "<" and ">"
if((buffer[i] == '<') || (buffer[i] == '>')){
i++;
}
// Is this a basic range (i.e. xxx..yyy) ?
if( isdigit(buffer[i]) ){
// Read a basic range: The first entry ...
list<char> number;
while( isdigit(buffer[i]) ){
number.push_back(buffer[i] - '0');
i++;
}
// Convert this list to an zero based base location
m_range.first = list_to_int(number) - 1;
if(i == len){
// We have found a single number entry for the
// range (i.e. NC_005816.gbk)
m_range.second = m_range.first;
// Not a complement
return false;
}
// The second entry. Read the spacers ".."
while((i != len) && !isdigit(buffer[i])){
i++;
}
while((i != len) && isdigit(buffer[i])){
number.push_back(buffer[i] - '0');
i++;
}
// Convert this list to an zero based base location
m_range.second = list_to_int(number) - 1;
// Not a complement
return false;
}
// Is this range a simple complement?
if(buffer[i] == 'c'){
unsigned int j = i + 11; // + strlen( "complement(" )
// For now, ignore "<" and ">"
if((buffer[j] == '<') || (buffer[j] == '>')){
j++;
}
// Look -- not checking the bounds on buffer for speed reasons
if( isdigit(buffer[j]) ){
// Read a basic range: The first entry ...
list<char> number;
while(isdigit(buffer[j])){
number.push_back(buffer[j] - '0');
j++;
}
// Convert this list to an zero based base location
m_range.first = list_to_int(number) - 1;
if(j == len - 1 /* skip the closing ')' of complement(...) */){
// We have found a single number entry for the
// range (i.e. NC_005816.gbk)
m_range.second = m_range.first;
// Is a complement
return true;
}
// The second entry. Read the spacers ".."
while( !isdigit(buffer[j]) ){
j++;
}
while( isdigit(buffer[j]) ){
number.push_back(buffer[j] - '0');
j++;
}
// Convert this list to an zero based base location
m_range.second = list_to_int(number) - 1;
// Is a complement
return true;
}
}
// We are now into the complicated ranges! i.e. join and complement(join( .. ) )
// First, make sure that we have the entire range!
unsigned int left_paren = 0;
unsigned int right_paren = 0;
unsigned int j;
for(j = i;buffer[j] != '\0';j++){
if(buffer[j] == '('){
left_paren ++;
}
if(buffer[j] == ')'){
right_paren ++;
}
}
while(left_paren != right_paren){
char tmp[MAX_LINE_LEN];
if( (gzgets(m_fin, tmp, MAX_LINE_LEN) == NULL) || !strip_eol(tmp, MAX_LINE_LEN) ){
throw __FILE__ ":read_range: Unable to read tmp line";
}
line_number ++;
if( ( strlen(buffer) + strlen(tmp) ) >= MAX_LINE_LEN){
throw __FILE__ ":read_range: Ran out of room to store range!";
}
strcat(buffer, tmp);
left_paren = right_paren = 0;
for(unsigned int j = i;buffer[j] != '\0';j++){
if(buffer[j] == '('){
left_paren ++;
}
if(buffer[j] == ')'){
right_paren ++;
}
}
}
bool is_complement = false;
//bool is_join = false;
// Now test for complement
if(buffer[i] == 'c'){
is_complement = true;
i += 11; // + strlen( "complement(" )
}
// Read the join info
if(buffer[i] == 'j'){
//is_join = true;
i += 5; // + strlen( "join(" )
}
else{
if(buffer[i] == 'o'){
//is_join = true;
i += 6; // + strlen( "order(" )
}
else{
throw error_msg(":read_range: Did not find expected \"join\"");
}
}
// Test for "complement" *again* to catch the extreamly rare "join(complement" -- see
// NC_005213.gbk for an example.
if(buffer[i] == 'c'){
is_complement = true;
i += 11; // + strlen( "complement(" )
}
// For now, ignore "<" and ">"
if((buffer[i] == '<') || (buffer[i] == '>')){
i++;
}
j = strlen(buffer);
while(i < j){
// Read a range: The first entry ...
list<char> number;
pair<unsigned int, unsigned int> tmp_range;
while((i < j) && isdigit(buffer[i])){
number.push_back(buffer[i] - '0');
i++;
}
// Convert this list to an zero based base location
tmp_range.first = list_to_int(number) - 1;
// Is there a second value in this range, or is it a single
// base entry?
bool is_single_base = false;
while((i < j) && !isdigit(buffer[i])){
if((buffer[i] == ',') || (buffer[i] == ')')){
is_single_base = true;
}
i++;
}
// Is there a second entry to read?
if(is_single_base){
tmp_range.second = tmp_range.first;
}
else{
// The second entry
while((i < j) && isdigit(buffer[i])){
number.push_back(buffer[i] - '0');
i++;
}
// Convert this list to an zero based base location
tmp_range.second = list_to_int(number) - 1;
}
m_seg_list.push_back(tmp_range);
// Skip all non-digit characters
while((i < j) && !isdigit(buffer[i])){
i++;
}
}
if(m_seg_list.empty()){
throw error_msg("read_range: Unable to read join(...)");
}
// Sort the seg list (important for annotations that span the origin)
m_seg_list.sort();
m_range.first = m_seg_list.front().first;
m_range.second = m_seg_list.back().second;
return is_complement;
}
// Convert the input list of char's into an unsinged integer.
// The list is exhausted in the process
unsigned int list_to_int(list<char> &m_number)
{
#ifdef _DEBUG
if(m_number.empty()){
throw error_msg("Empty number list!");
}
#endif // _DEBUG
unsigned int num = m_number.back();
m_number.pop_back();
int power = 10;
while(m_number.empty() == false){
num += power*m_number.back();
power *= 10;
m_number.pop_back();
}
return num;
}
char *error_msg(const char *m_error)
{
static char error[1024];
stringstream ssout;
ssout << m_error << " : line #" << line_number;
strcpy( error, ssout.str().c_str() );
return error;
}
bool is_eol(char m_c)
{
return (m_c == '\n') || (m_c == '\r');
}
// Return true if we successfully found and removed any trailing eol
// characters (i.e., '\n' or '\r')
bool strip_eol(char *m_buffer, const size_t &m_max_len)
{
size_t len = strnlen(m_buffer, m_max_len);
if(len == m_max_len){
return false;
}
bool found_eol = false;
while( (len > 0) && is_eol(m_buffer[len - 1]) ){
found_eol = true;
m_buffer[len - 1] = '\0';
--len;
}
return found_eol;
}