-
Notifications
You must be signed in to change notification settings - Fork 10
/
annotation.cpp
381 lines (274 loc) · 8.12 KB
/
annotation.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
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
#include "annotation.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string.h>
#include <zlib.h>
using namespace std;
// Turn a range or segment string into a start/stop and segment list
// Return false on failure and true on success
void GeneAnnotation::range(const string &m_range)
{
// Clear any existing segments
seg_list.clear();
const unsigned int len = m_range.size();
unsigned int i = 0;
// Skip to the start of the range entry.
while( (i < len) && isspace(m_range[i]) ){
i++;
}
if(i == len){
throw "Range: Empty range string";
}
while(i < len){
list<char> number;
if( !isdigit(m_range[i]) ){
seg_list.clear();
throw "Range: Could not read start";
}
while( (i < len) && isdigit(m_range[i]) ){
number.push_back(m_range[i] - '0');
i++;
}
const int start = list_to_int(number);
while( (i < len) && isspace(m_range[i]) ){
i++;
}
if( (i == len) || (m_range[i] != '.') ){
seg_list.clear();
throw "Range: Could not read \"..\"";
}
i++;
if( (i == len) || (m_range[i] != '.') ){
seg_list.clear();
throw "Range: Could not read \"..\"";
}
i++;
while( (i < len) && isspace(m_range[i]) ){
i++;
}
if(i == len){
seg_list.clear();
throw "Range: Could not read stop";
}
if( !isdigit(m_range[i]) ){
seg_list.clear();
throw "Range: Could not read stop";
}
while( (i < len) && isdigit(m_range[i]) ){
number.push_back(m_range[i] - '0');
i++;
}
const int stop = list_to_int(number);
while( (i < len) && ( isspace(m_range[i]) || (m_range[i] == ',') ) ){
i++;
}
seg_list.push_back( make_pair(start, stop) );
}
seg_list.sort();
start( seg_list.front().first );
stop( seg_list.back().second );
// Only keep the segment list around if it stores more than 1 element
if(seg_list.size() == 1){
seg_list.clear();
}
}
// Using the start/stop and segment list, return a valid range string
string GeneAnnotation::range() const
{
stringstream ss;
if(is_segmented() == true){
list< pair<unsigned int, unsigned int> >::const_iterator iter =
seg_list.begin();
while( iter != seg_list.end() ){
ss << iter->first << ".." << iter->second;
iter++;
if( iter != seg_list.end() ){
ss << ", ";
}
}
}
else{
ss << start() << ".." << stop();
}
return ss.str();
}
bool DNAMol::load(gzFile m_fin, const unsigned int &m_type, size_t &m_pos)
{
// Return true if there are multiple genomes to read in this file
bool ret = false;
switch(m_type){
case GBK:
ret = loadGBK(m_fin, m_pos);
break;
case EMBL:
ret = loadEMBL(m_fin, m_pos);
break;
default:
throw "Unknown file format!";
};
return ret;
}
void DNAMol::processGeneList(const bool &m_loading /* = false */)
{
list<GeneAnnotation>::iterator gene_iter;
bool annot_overlaps_origin = false;
for(gene_iter = gene_list.begin();gene_iter != gene_list.end();gene_iter++){
// Handle the special case of a gene that overlaps the genome start site
if( gene_iter->handle_gene_start_overlap(seq_len) == true){
annot_overlaps_origin = true;
}
}
// Sort genes
gene_list.sort();
if( m_loading && gene_list.empty() ){
// No annotations were found! Is there any sequence to be read?
// If so, then it will be classified as intergenic space
if(seq_len != 0){
GeneAnnotation inter_genic_space;
inter_genic_space.start(0);
inter_genic_space.stop(seq_len - 1);
gene_list.push_back(inter_genic_space);
}
}
else if(m_loading == true){
unsigned int last_stop_plus_1;
unsigned int first_start;
bool annot_is_entire_mol = false;
gene_iter = gene_list.begin();
// Assume a linear genome
last_stop_plus_1 = 0;
first_start = 0;
// Only add intergenic space if there is room -- i.e. no
// molecule spanning single annotation!
if(annot_is_entire_mol == false){
// Add intergenic spaces to the gene list.
for(/* Already initialized */;gene_iter != gene_list.end();gene_iter++){
// Only consider annotations that do not overlap the origin
// (i.e. only consider cases where start <= stop).
if( ( gene_iter->start() <= gene_iter->stop() ) &&
(gene_iter->start() > last_stop_plus_1) ){
GeneAnnotation inter_genic_space;
inter_genic_space.start(last_stop_plus_1);
inter_genic_space.stop(gene_iter->start() - 1);
gene_list.insert(gene_iter, inter_genic_space);
}
last_stop_plus_1 = max(last_stop_plus_1, gene_iter->stop() + 1);
}
// Do we need to add intergenic space to the tail?
last_stop_plus_1 = seq_len;
first_start = seq_len - 1;
// Only add intergenic space if no annotation overlaps the origin.
// Also, when last_stop_plus_1 == 0 and first_start == seq_len - 1, there is
// no intergenic space (these points are right next to each other on the
// circle).
if( !( (last_stop_plus_1 == 0) && (first_start == seq_len - 1) ) &&
(last_stop_plus_1 - 1 != first_start) && (annot_overlaps_origin == false)){
GeneAnnotation inter_genic_space;
if(last_stop_plus_1 <= first_start){
inter_genic_space.start(last_stop_plus_1);
inter_genic_space.stop(first_start);
}
else{
inter_genic_space.start(last_stop_plus_1);
inter_genic_space.stop(seq_len + first_start);
}
gene_list.push_back(inter_genic_space);
}
}
}
}
bool DNAMol::import(const DNAMol &m_mol, const unsigned int &m_type)
{
// Import all annotations from m_mol that are within the sequence bounds of
// mol.
// Delete all intergenic space prior to the addition of new annotation
list<GeneAnnotation>::iterator iter;
list<list<GeneAnnotation>::iterator> reaper;
for(iter = gene_list.begin();iter != gene_list.end();iter++){
if(iter->is_intergenic() == true){
reaper.push_back(iter);
}
}
while(reaper.empty() == false){
gene_list.erase( reaper.back() );
reaper.pop_back();
}
list<GeneAnnotation>::const_iterator m_iter;
for(m_iter = m_mol.gene_list.begin();m_iter != m_mol.gene_list.end();m_iter++){
// If this annotation *starts* before the sequence end, then we will include
// it (to allow for annotations that overlap the oriC).
if(m_iter->start() < seq_len){
gene_list.push_back(*m_iter);
if(m_iter->stop() > seq_len){
// This annotation needs to wrap around the oriC
gene_list.back().stop(m_iter->stop() - seq_len);
}
}
}
processGeneList(true /* recomputing intergenic space */);
return true;
}
int file_type(const std::string &m_filename)
{
// Open the file and read until we can determine the file type
gzFile fin = gzopen(m_filename.c_str(), "r");
int ret = -1; // return -1 on error
if(fin == NULL){
return ret;
}
const unsigned int buffer_size = 512;
char buffer[buffer_size];
// Clear the array of bytes
memset(buffer, '\0', buffer_size);
// Read buffer_size - 1 bytes to make sure that the resulting buffer is a
// valid '\0' terminated C-string
if(gzread(fin, buffer, buffer_size - 1) < 0){
return ret;
}
gzclose(fin);
bool GBK_hint = false;
int first_non_space_char = -1;
for(unsigned int i = 0;i < buffer_size;i++){
if( isspace(buffer[i]) ){
continue;
}
if(first_non_space_char < 0){
first_non_space_char = i;
}
if(buffer[i] == '>'){
ret = DNAMol::FASTA;
break;
}
if(isupper(buffer[i])){
// Genbank flat files typically start with upper case string,
// i.e. "LOCUS".
GBK_hint = true;
// This is only a hint, so don't break -- keep reading!
}
}
// No valid characters detected! This is an error!
if(first_non_space_char == -1){
return ret;
}
if(buffer[first_non_space_char] == '@'){
return DNAMol::FASTQ;
}
// Quick test for GBK file
if( strstr(buffer, "LOCUS") && strstr(buffer, "DEFINITION") ){
return DNAMol::GBK;
}
if((ret == -1) && GBK_hint){
// Does this file start with the string "ID"?
if(first_non_space_char < (int)buffer_size - 1){
if( ( (buffer[first_non_space_char] == 'I') &&
(buffer[first_non_space_char + 1] == 'D') ) ||
strstr(buffer, "\nFT") ){
return DNAMol::EMBL;
}
}
ret = DNAMol::GBK;
}
return ret;
}