File: GDEFileParser.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,868 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 15
file content (439 lines) | stat: -rw-r--r-- 12,673 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
/**
 * 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 "GDEFileParser.h"

namespace clustalw
{

/*
 * Constructor sets up the chartab array.
 *
 */
GDEFileParser::GDEFileParser(string filePath)
{
    fileName = filePath; 
    fillCharTab();
}

/*
 * Nothing to do in destruction of object.
 */
GDEFileParser::~GDEFileParser()
{

}


    vector<Sequence> GDEFileParser::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;
}


/*
 * The getSeq function is used to get sequence number seqNum from the file.
 */
    Sequence GDEFileParser::getSeq(int seqNum, string *offendingSeq)
{
    char _line[MAXLINE + 1];
    char _sname[MAXNAMES + 1];
    string characterSeq = "";
    string name = "";
    string title = "";
    string blank = "";
    
    _line[0] = EOS; 
    
    int i;
    unsigned char c;
    int _currentSeqNum = 0; // Not at any sequence yet!
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg);
                
        bool dnaFlagSet = userParameters->getDNAFlag();
        while (_currentSeqNum != seqNum)
        {
            while((*_line != '#' && dnaFlagSet) ||
                  (*_line != '%' && !dnaFlagSet))
            {
                if(!_fileIn->getline(_line, MAXLINE + 1)) 
                {
                    _fileIn->close();
                    return Sequence(blank, blank, blank);
                }
            }
            ++_currentSeqNum;
            if(_currentSeqNum == seqNum) // Found the sequence
            {
                break;
            }
            // Get next line so that we are past the '#' or '%' line
            _fileIn->getline(_line, MAXLINE + 1);  //nige
        }
        
        for (i = 1; i <= MAXNAMES; i++)
        {
            if (_line[i] == '(' || _line[i] == '\n' || _line[i] == '\r')
            {
                break;
            }
            _sname[i - 1] = _line[i];
        }
        i--;
        _sname[i] = EOS;

        for (i--; i > 0; i--)
        {
            if (isspace(_sname[i]))
            {
                _sname[i] = EOS;
            }
            else
            {
                break;
            }
        }
        utilityObject->blankToUnderscore(_sname);
        name = string(_sname);
        title = "";
        
        while (_fileIn->getline(_line, MAXLINE + 1))
        {
            if (*_line == '%' ||  *_line == '#' ||  *_line == '"')
            {
               break;
            }
            for (i = 0; i <= MAXLINE; i++)
            {
                c = _line[i];
                if (c == '\n' || c == EOS)
                {
                    break;
                }

                c = chartab[c];
                if (c)
                {
                    characterSeq += c;
                }
            }
        }
        
        _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 GDEnFileParser::getSeq function.\n"
             << "Need to end program\n";
        exit(1);    
    }

}

/*
 * The countSeqs function returns the number of sequences in the file. 
 */
int GDEFileParser::countSeqs()
{
    char line[MAXLINE + 1];
    int _nseqs = 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 ((*line == '%') && (userParameters->getDNAFlag() == false))
            {
                _nseqs++;
            }
            else if ((*line == '#') && (userParameters->getDNAFlag() == true))
            {
                _nseqs++;
            }
        }
        _fileIn->close();

        return _nseqs;
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "An exception has occurred in the function GDEFileParser::countSeqs()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }    
}

/*
 * getSecStructure gets the secondary structure from the file.
 */
void GDEFileParser::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];
    int i, len, offset = 0;
    unsigned char c;
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg);
    
        // NOTE I think I should empty the masks before pushing onto them!
        gapPenaltyMask.clear();
        secStructMask.clear();
    
        for (;;)
        {
            _line[0] = '\0';
            // search for the next comment line
            while (*_line != '"')
            {
                if (!_fileIn->getline(_line, MAXLINE + 1))
                {
                    _fileIn->close();
                    return;
                }
            }

            // is it a secondary structure entry? 
            if (strncmp(&_line[1], "SS_", 3) == 0)
            {
                for (i = 1; i <= MAXNAMES - 3; i++)
                {
                    if (_line[i + 3] == '(' || _line[i + 3] == '\n' || _line[i + 3] == '\r')
                    {
                        break;
                    }
                    _sname[i - 1] = _line[i + 3];
                }
                i--;
                _sname[i] = EOS;
            
                // NOTE NOTE NOTE
                // Is it possible for this to be executed????????????????
                // if _line contains ( then we break and dont put it into _sname
                // So how can sname have it???????
                if (_sname[i - 1] == '(')
                {
                    sscanf(&_line[i + 3], "%d", &offset);
                }
                else
                {
                    offset = 0;
                }
                for (i--; i > 0; i--)
                {
                    if (isspace(_sname[i]))
                    {
                        _sname[i] = EOS;
                    }
                    else
                    {
                        break;
                    }
                }

                utilityObject->blankToUnderscore(_sname);
                secStructName = string(_sname);
            
                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('.');
                    }
                    len = 0;
                    while (_fileIn->getline(_line, MAXLINE + 1))
                    {
                        if (*_line == '%' ||  *_line == '#' ||  *_line == '"')
                        {
                            break;
                        }
                        for (i = offset; i < length; i++)
                        {
                            c = _line[i];
                            if (c == '\n' || c == EOS)
                            {
                                break;
                            }
                            // EOL
                            secStructMask[len++] = c;
                        }
                        if (len >= length) // NOTE i put in >=
                        {
                            break;
                        }
                    }
                }
            }
        
            // or is it a gap penalty mask entry?
            else if (strncmp(&_line[1], "GM_", 3) == 0)
            {
                for (i = 1; i <= MAXNAMES - 3; i++)
                {
                    if (_line[i + 3] == '(' || _line[i + 3] == '\n')
                    {
                        break;
                    }
                    _sname[i - 1] = _line[i + 3];
                }
                i--;
                _sname[i] = EOS;
            
                // NOTE NOTE
                // Again I dont think it is possible for _sname to have ( !!!!
                if (_sname[i - 1] == '(')
                {
                    sscanf(&_line[i + 3], "%d", &offset);
                }
                else
                {
                    offset = 0;
                }
                for (i--; i > 0; i--)
                {
                    if (isspace(_sname[i]))
                    {
                        _sname[i] = EOS;
                    }
                    else
                    {
                        break;
                    }
                }
            
                utilityObject->blankToUnderscore(_sname);
                secStructName = string(_sname);

                if (userParameters->getInteractive() && !userParameters->getGui())
                {
                    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');
                    }
                    len = 0;
                    while (_fileIn->getline(_line, MAXLINE + 1))
                    {
                        if (*_line == '%' ||  *_line == '#' ||  *_line == '"')
                        {
                            break;
                        }
                        for (i = offset; i < length; i++)
                        {
                            c = _line[i];
                            if (c == '\n' || c == EOS)
                            {
                                break;
                            }
                            // EOL
                            gapPenaltyMask[len++] = c;
                        }
                        if (len >= length) // NOTE I put in >=
                        {
                            break;
                        }
                    }
                }
            }
            if (structPenalties != NONE)
            {
                break;
            }
        }
        _fileIn->close();
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "An exception has occurred in the function GDEFileParser::getSecStructure()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }
}

}