File: EMBLFileParser.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,320 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 16
file content (491 lines) | stat: -rw-r--r-- 13,870 bytes parent folder | download | duplicates (6)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
/**
 * Changes:
 *
 * 10-02-07,Nigel Brown(EMBL): changed ifstream to InFileStream to handle
 * cross-platform end-of-lines.
 */

#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include "EMBLFileParser.h"

namespace clustalw
{

/*
 * constructor sets up chartab array.
 */
EMBLFileParser::EMBLFileParser(string filePath)
{
    fileName = filePath; 
    fillCharTab();
}

/*
 * dont need to destruct anything.
 */
EMBLFileParser::~EMBLFileParser()
{

}


/*
 * get range of sequences
 */
    vector<Sequence> EMBLFileParser::getSeqRange(int firstSeq, int no, string *offendingSeq)
{
    vector<Sequence> seqRangeVector;
    int i;

    for (i=0; i<no; i++)
    { 
        Sequence tempSeq = getSeq(firstSeq + i, offendingSeq);
        if (parseExitCode!=OK) {
            seqRangeVector.clear();
            return seqRangeVector;
        }
        seqRangeVector.push_back(tempSeq);
    }
    return seqRangeVector;
}


/*
 * get the sequence seqNum in the file.
 */
    Sequence EMBLFileParser::getSeq(int seqNum, string *offendingSeq)
{
    char _line[MAXLINE + 1];
    //char _tseq[MAXLINE + 1];
    char _sname[MAXNAMES + 1];
    //char _title[MAXTITLES + 1];
    string characterSeq = "";
    string name = "";
    string title = "";
    
    _line[0] = EOS;
    int i;
    //int j;
    bool gotSeq = false;
    unsigned char c;
    int _currentSeqNum = 0;
    string blank = "";

    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg); // start at the beginning

        // Read in lines until we get to the begining of sequence seqNum.
        while (_currentSeqNum != seqNum)
        {
            while(!utilityObject->lineType(_line, "ID"))
            {
                if(!_fileIn->getline(_line, MAXLINE + 1)) // If we cannot get anymore!
                {
                    _fileIn->close();
                    return Sequence(blank, blank, blank);
                }
            }
            ++_currentSeqNum;
            if(_currentSeqNum == seqNum) // Found the sequence
            {
                break;
            }
            // Get next line so that we are past the '>' line
            _fileIn->getline(_line, MAXLINE + 1);
        }

        for (i = 5; i <= (int)strlen(_line); i++)
        {
            if (_line[i] != ' ')
            {
                break;
            }
        }
        strncpy(_sname, _line + i, MAXNAMES); // remember entryname 
        for (i = 0; i <= (int)strlen(_sname); i++)
        {
            if (_sname[i] == ' ')
            {
                _sname[i] = EOS;
                break;
            }
        }

        _sname[MAXNAMES] = EOS;
        utilityObject->rTrim(_sname);
        utilityObject->blankToUnderscore(_sname);
        name = string(_sname);
        // Andreas Wilm (UCD): why cout here? cout << name << "\n"; 
        
        while (!utilityObject->lineType(_line, "SQ"))
        {
            if(!_fileIn->getline(_line, MAXLINE + 1)) // If we cannot get anymore!
            {
                _fileIn->close();
                // FIXME AW: why return with a name but otherwise empty seq?
                return Sequence(blank, name, blank);
            }
        }

        while (_fileIn->getline(_line, MAXLINE + 1))
        {
            if (gotSeq && utilityObject->blankLine(_line))
            {
                break;
            }
            
            // NOTE I changed this to -1 and -2 because the getline doesnt return the \n
            if (strlen(_line) > 2 && _line[strlen(_line) - 1] == '.' &&
                    _line[strlen(_line) - 2] == '.')
            {
                continue;
            }

            for (i = 0; i <= MAXLINE; i++)
            {
                c = _line[i];
                if (c == '\n' || c == EOS || c == '/')
                {
                    break;
                }
                // EOL     
                c = chartab[c];
                if (c)
                {
                    gotSeq = true;
                    characterSeq += c;
                }
            }
            if (c == '/')
            {
                break;
            }
        }
        _fileIn->close();
        
        if ((int)characterSeq.length() > userParameters->getMaxAllowedSeqLength())
        {
            parseExitCode=SEQUENCETOOBIG;
            if (offendingSeq!=NULL)
                offendingSeq->assign(name);
            // return empty seq
            return Sequence(blank, blank, blank);
        }
        return Sequence(characterSeq, name, title);
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "There was an exception in the EMBLFileParser::getSeq function.\n"
             << "Need to end program\n";
        exit(1);    
    }            
}

/*
 * count the number of sequences in the file and return the number
 */
int EMBLFileParser::countSeqs()
{
    char line[MAXLINE + 1];
    // char c;
    line[0] = EOS;
    int numSeqs;
    // int i;
    //bool seqOk;
    numSeqs = 0;
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
    
        if(!_fileIn->is_open())
        {
            return 0; // No sequences found!
        }       
                 
        while (_fileIn->getline(line, MAXLINE + 1))
        {
            if (utilityObject->lineType(line, "ID"))
            {
                numSeqs++;
            }
        }
        
        _fileIn->close();
        return numSeqs;
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "An exception has occurred in the function EMBLFileParser::countSeqs()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);     
    }
}

/*
 * get secondary structure information from the file.
 */
void EMBLFileParser::getSecStructure(vector<char>& gapPenaltyMask, vector<char>& secStructMask, string& secStructName, int &structPenalties, int length)
{
    bool guigetss = false;
    if(userParameters->getProfileNum() == 1 && userParameters->getStructPenalties1())
         guigetss = true;
    if(userParameters->getProfileNum() == 2 && userParameters->getStructPenalties2())
         guigetss = true;

    char _title[MAXLINE + 1];
    char _line[MAXLINE + 1];
    char _lin2[MAXLINE + 1];
    char _sname[MAXNAMES + 1];
    char _feature[MAXLINE + 1];
    int i;
    _line[0] = '\0';
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg);

        // clear out the masks
        gapPenaltyMask.clear();
        secStructMask.clear();
            
        // find the start of the sequence entry 
        for (;;)
        {
            while (!utilityObject->lineType(_line, "ID"))
            {
                if (!_fileIn->getline(_line, MAXLINE + 1))
                {
                    _fileIn->close();
                    return;
                }
            }
            
            for (i = 5; i <= (int)strlen(_line); i++)
            {
                if (_line[i] != ' ')
                {
                    break;
                }
            }
            strncpy(_sname, _line + i, MAXNAMES); // remember entryname
            for (i = 0; i <= (int)strlen(_sname); i++)
            {
                if (_sname[i] == ' ')
                {
                    _sname[i] = EOS;
                    break;
                }
            }
            _sname[MAXNAMES] = EOS;
            utilityObject->rTrim(_sname);
            utilityObject->blankToUnderscore(_sname);
            
            // look for secondary structure feature table / gap penalty mask 
            while (_fileIn->getline(_line, MAXLINE + 1))
            {
                if (utilityObject->lineType(_line, "FT"))
                {
                    sscanf(_line + 2, "%s", _feature);
                    if (strcmp(_feature, "HELIX") == 0 || strcmp(_feature, "STRAND") == 0)
                    {
                        if (userParameters->getInteractive() && !userParameters->getGui())
                        {
                            strcpy(_title,
                                "Found secondary structure in alignment file: ");
                            strcat(_title, _sname);
                            (*_lin2) = utilityObject->promptForYesNo(_title,
                                "Use it to set local gap penalties ");
                        }
                        else
                        {
                            (*_lin2) = 'y';
                        }
                        if (guigetss || ((*_lin2 != 'n') && (*_lin2 != 'N')))
                        {
                            structPenalties = SECST;
                            for (i = 0; i < length; i++)
                            {
                                secStructMask.push_back('.');
                            }
                            do
                            {
                                getSwissFeature(&_line[2], secStructMask, length);
                                _fileIn->getline(_line, MAXLINE + 1);
                            }
                            while (utilityObject->lineType(_line, "FT"));
                        }
                        else
                        {
                            do
                            {
                                _fileIn->getline(_line, MAXLINE + 1);
                            }
                            while (utilityObject->lineType(_line, "FT"));
                        }
                        secStructName = string(_sname);
                    }
                }
                else if (utilityObject->lineType(_line, "GM"))
                {
                    if (userParameters->getInteractive())
                    {
                        strcpy(_title, "Found gap penalty mask in alignment file: ");
                        strcat(_title, _sname);
                        (*_lin2) = utilityObject->promptForYesNo(_title,
                            "Use it to set local gap penalties ");
                    }
                    else
                    {
                        (*_lin2) = 'y';
                    }
                    if (guigetss || ((*_lin2 != 'n') && (*_lin2 != 'N')))
                    {
                        structPenalties = GMASK;
                        for (i = 0; i < length; i++)
                        {
                            gapPenaltyMask.push_back('1');
                        }
                        do
                        {
                            getSwissMask(&_line[2], gapPenaltyMask, length);
                            _fileIn->getline(_line, MAXLINE + 1);
                        }
                        while (utilityObject->lineType(_line, "GM"));
                    }
                    else
                    {
                        do
                        {
                            _fileIn->getline(_line, MAXLINE + 1);
                        }
                        while (utilityObject->lineType(_line, "GM"));
                    }
                    secStructName = string(_sname);
                }
                if (utilityObject->lineType(_line, "SQ"))
                {
                    break;
                }

                if (structPenalties != NONE)
                {
                    break;
                }
            }
        }
        _fileIn->close();
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "An exception has occurred in the function EMBLFileParser::getSecStructure()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }
    
}

/*
 * get the sec structure mask
 */
void EMBLFileParser::getSwissFeature(char* line, vector<char>& secStructMask, int length)
{
    char c, s, feature[MAXLINE + 1];
    int i, startPos, endPos;

    try
    {
        if (sscanf(line, "%s%d%d", feature, &startPos, &endPos) != 3)
        {
            return ;
        }

        if (strcmp(feature, "HELIX") == 0)
        {
            c = 'A';
            s = '$';
        }
        else if (strcmp(feature, "STRAND") == 0)
        {
            c = 'B';
            s = '%';
        }
        else
        {
            return ;
        }

        if (startPos >= length || endPos >= length)
        {
            return ;
        }

        secStructMask[startPos - 1] = s;
        for (i = startPos; i < endPos - 1; i++)
        {
            secStructMask[i] = c;
        }
        secStructMask[endPos - 1] = s;
    }
    catch(...)
    {
        cerr << "An exception has occurred in the function EMBLFileParser::getSwissFeature()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }
}

/*
 * get the gap penalty mask.
 */
void EMBLFileParser::getSwissMask(char* line, vector<char>& gapPenaltyMask, int length)
{
    int i, value, startPos, endPos;

    try
    {
        if (sscanf(line, "%d%d%d", &value, &startPos, &endPos) != 3)
        {
            return;
        }

        if (value < 1 || value > 9)
        {
            return ;
        }

        if (startPos >= length || endPos >= length)
        {
            return ;
        }
        for (i = startPos - 1; i < endPos; i++)
        {
            gapPenaltyMask[i] = value + '0';
        }
    }
    catch(...)
    {
        cerr << "An exception has occurred in the function EMBLFileParser::getSwissMask()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }    
}


}