File: ClustalFileParser.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 (613 lines) | stat: -rw-r--r-- 18,135 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/**
 * 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.
 *
 * 27-4-2007, Mark Larkin (UCD): Made 2 small changes to getSecStructure function. There 
 * was a problem with the secondary structure info in windows.
 */

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

namespace clustalw
{

ClustalFileParser::ClustalFileParser(string filePath)
{
    fileName = filePath; 
    fillCharTab();
    _fileIn = 0;
}
        
ClustalFileParser::~ClustalFileParser()
{
    
}


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


Sequence ClustalFileParser::getSeq(int seqNum, string *offendingSeq)
{
    char line[MAXLINE + 1];
    line[0] = EOS;
    char tseq[MAXLINE + 1];
    tseq[0] = '\0';
    char sname[MAXNAMES + 150];

    for(int i = 1; i < MAXNAMES + 1; i++)
    {
        line[i] = tseq[i] = sname[i] = '0';
    }
    string characterSeq = "";
    string name = "";
    string title = ""; // Nothing happens it here!!!

    int i, j;
    unsigned char c;
    string blank = "";
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg); // start at the beginning
             
        _fileIn->getline(line, MAXLINE + 1); // read the title line...ignore it
    
        while (_fileIn->getline(line, MAXLINE + 1))  //nige
        {
            if (!clustalBlankline(line))
            {

                for (i = 1; i < seqNum; i++)
                {
                    _fileIn->getline(line, MAXLINE + 1);  //nige
                }
                for (j = 0; j <= (int)strlen(line); j++)
                    if (line[j] != ' ')
                    {
                        break;
                    }
                string _tempStr = string(line);

                sscanf(_tempStr.c_str(), "%s%s", sname, tseq);
                for (j = 0; j < MAXNAMES; j++)
                {
                    if (sname[j] == ' ')
                    {
                        break;
                    }
                }
                sname[j] = EOS;
                utilityObject->rTrim(sname);
                utilityObject->blankToUnderscore(sname); // replace blanks with '_'
                name = string(sname);           
               
                for (i = 0; i <= MAXLINE; i++)
                {
                    c = tseq[i];
                    if (isspace(c) || c == EOS)
                    {
                        break;
                    }
                    // EOL 
                    c = chartab[c];
                    if (c)
                    {
                        characterSeq += c; // Add the character to the sequence
                    }
                }

                for (i = 0;; i++)
                {
                    if (!_fileIn->getline(line, MAXLINE + 1)) // If we cant get another line!
                    {
                        freeFileResources(_fileIn);
                        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);
                    }
                    if (clustalBlankline(line))
                    {
                        break;
                    }
                }
            }
        }
        freeFileResources(_fileIn);

	// getSecStructure(vector<char>& gapPenaltyMask, 
	//      vector<char>& secStructMask, string& secStructName, int &structPenalties, int length)

        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(...)
    {
        freeFileResources(_fileIn);
        cerr << "An exception has occurred in the function ClustalFileParser::getSeq()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);
    }
}
        
/*
 * The function countSeqs tells us how many sequences are in a clustal format file.
 * Need to check if the file is open!
 */
int ClustalFileParser::countSeqs()
{
    char line[MAXLINE + 1];
    int _nseqs;
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
    
        if(!_fileIn->is_open())
        {
            freeFileResources(_fileIn);            
            return 0; // No sequences found!
        }


        while (_fileIn->getline(line, MAXLINE + 1))
        {
            if (!utilityObject->blankLine(line))
            {
                break;
            }
        }
    
        // This gets us to the begining of the sequence lines!
        while (_fileIn->getline(line, MAXLINE + 1))
        {
            if (!clustalBlankline(line))
            {
                break;
            }
        }
        _nseqs = 1;

        while (_fileIn->getline(line, MAXLINE + 1))
        {
            if (clustalBlankline(line))
            {
                freeFileResources(_fileIn);
                return _nseqs;
            }
            _nseqs++;
        }
        freeFileResources(_fileIn);
        return (int)0; // if you got to here-funny format/no seqs.
    }
    catch(...)
    {
        freeFileResources(_fileIn);
        cerr << "An exception has occurred in the function ClustalFileParser::countSeqs()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }    
}

/*
 * This function is to get the secondary structure for a Clustal format file.
 * I am aware that I am using some C and some C++ here, and this may seem like
 * bad style, but I think it is better to use the C functions for processing the
 * strings as they are already working.
 */        
void ClustalFileParser::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];
    title[0] = '\0';
    char line[MAXLINE + 1];
    line[0] = '\0';
    char lin2[MAXLINE + 1];
    lin2[0] = '\0';
    char tseq[MAXLINE + 1];
    tseq[0] = '\0';
    char sname[MAXNAMES + 1];
    sname[0] = '\0';
    
    for(int i = 1; i < MAXNAMES + 1; i++)
    {
        title[i] = line[i] = lin2[i] = tseq[i] = sname[i] ='0';
    }
    
    int i, j, len, ix, struct_index = 0;
    char c;
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg);

        // NOTE clear out the masks
        gapPenaltyMask.clear();
        secStructMask.clear();
        
        len = 0; // initialise length to zero 
    
        if (!_fileIn->getline(line, MAXLINE + 1))
        {
            freeFileResources(_fileIn);
            return ;
        }
        // read the title line...ignore it 

        if (!_fileIn->getline(line, MAXLINE + 1))
        {
            freeFileResources(_fileIn);
            return ;
        }
        // read the next line...
        // skip any blank lines 
        for (;;)
        {
            if (!_fileIn->getline(line, MAXLINE + 1))
            {
                freeFileResources(_fileIn);   
                return ;
            }
            if (!utilityObject->blankLine(line))
            {
                break;
            }
        }

        // look for structure table lines
        ix =  -1;
        for (;;)
        {
            if (line[0] != '!')
            {
                break;
            }
            if (strncmp(line, "!SS", 3) == 0)
            {
                ix++;
                sscanf(line + 4, "%s%s", sname, tseq);
                for (j = 0; j < MAXNAMES; j++)
                {
                    if (sname[j] == ' ')
                    {
                        break;
                    }
                }
                sname[j] = EOS;
                utilityObject->rTrim(sname);
                utilityObject->blankToUnderscore(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;
                    struct_index = ix;
                    for (i = 0; i < length; i++)
                    {
                        secStructMask.push_back('.');
                        gapPenaltyMask.push_back('.');
                    }
                
                    secStructName = string(sname);
                
                    for (i = 0; len < length; i++)
                    {
                        c = tseq[i];
                        if (c == '\n' || c == EOS)
                        {
                            break;
                        }
                        // EOL
                        if (!isspace(c))
                        {
                            if(len < (int)secStructMask.size())
                            {
                                secStructMask[len++] = c; // NOTE array notation = BAD
                            }
                        }
                    }
                }
            }
            else if (strncmp(line, "!GM", 3) == 0)
            {
                ix++;
                sscanf(line + 4, "%s%s", sname, tseq);
                for (j = 0; j < MAXNAMES; j++)
                {
                    if (sname[j] == ' ')
                    {
                        break;
                    }
                }
                sname[j] = EOS;
                utilityObject->rTrim(sname);
                utilityObject->blankToUnderscore(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;
                    struct_index = ix;
                    for (i = 0; i < length; i++)
                    {
                        gapPenaltyMask.push_back('1');
                    }
                
                    secStructName = string(sname);
                
                    for (i = 0; len < length; i++)
                    {
                        c = tseq[i];
                        if (c == '\n' || c == EOS)
                        {
                            break;
                        }
                        // EOL
                        if (!isspace(c))
                        {
                            if(len < (int)gapPenaltyMask.size())
                            {                            
                                gapPenaltyMask[len++] = c;
                            }
                        }
                    }
                }
            }
            if (structPenalties != NONE)
            {
                break;
            }
            if (!_fileIn->getline(line, MAXLINE + 1))
            {
                freeFileResources(_fileIn);   
                return ;
            }
        }
        if (structPenalties == NONE)
        {
            freeFileResources(_fileIn);    
            return ;
        }

        // skip any more comment lines
        while (line[0] == '!')
        {
            if (!_fileIn->getline(line, MAXLINE + 1))
            {
                freeFileResources(_fileIn);   
                return ;
            }
        }

        // skip the sequence lines and any comments after the alignment 
        for (;;)
        {
            if (isspace(line[0]) || line[0] == '\0') // Mark change 27-4-2007
            {
                break;
            }
            if (!_fileIn->getline(line, MAXLINE + 1))
            {
                freeFileResources(_fileIn);   
                return ;
            }
        }


        // read the rest of the alignment

        for (;;)
        {
            // skip any blank lines
            for (;;)
            {
                if (!utilityObject->blankLine(line))
                {
                    break;
                }
                if (!_fileIn->getline(line, MAXLINE + 1))
                {
                    freeFileResources(_fileIn);    
                    return ;
                }
            }
            // get structure table line 
            for (ix = 0; ix < struct_index; ix++)
            {
                if (line[0] != '!')
                {
                    if (structPenalties == SECST)
                    {
                        utilityObject->error("bad secondary structure format\n");
                    }
                    else
                    {
                        utilityObject->error("bad gap penalty mask format\n");
                    }
                    structPenalties = NONE;
                    freeFileResources(_fileIn);    
                    return ;
                }
                if (!_fileIn->getline(line, MAXLINE + 1))
                {
                    freeFileResources(_fileIn);    
                    return ;
                }
            }
            if (structPenalties == SECST)
            {
                if (strncmp(line, "!SS", 3) != 0)
                {
                    utilityObject->error("bad secondary structure format\n");
                    structPenalties = NONE;
                    freeFileResources(_fileIn);    
                    return ;
                }
                sscanf(line + 4, "%s%s", sname, tseq);
                for (i = 0; len < length; i++)
                {
                    c = tseq[i];
                    if (c == '\n' || c == EOS)
                    {
                        break;
                    }
                    // EOL
                    if (!isspace(c))
                    {
                        secStructMask[len++] = c;
                    }
                }
            }
            else if (structPenalties == GMASK)
            {
                if (strncmp(line, "!GM", 3) != 0)
                {
                    utilityObject->error("bad gap penalty mask format\n");
                    structPenalties = NONE;
                    freeFileResources(_fileIn);    
                    return ;
                }
                sscanf(line + 4, "%s%s", sname, tseq);
                for (i = 0; len < length; i++)
                {
                    c = tseq[i];
                    if (c == '\n' || c == EOS)
                {
                    break;
                }
                // EOL
                if (!isspace(c))
                {
                    gapPenaltyMask[len++] = c;
                }
            }
        }

        // skip any more comment lines
        while (line[0] == '!')
        {
            if (!_fileIn->getline(line, MAXLINE + 1))
            {
                freeFileResources(_fileIn);   
                return ;
            }
        }

            // skip the sequence lines
            for (;;)
            {
                if (isspace(line[0]) || line[0] == '\0') // Mark change 27-4-2007
                {
                    break;
                }
                if (!_fileIn->getline(line, MAXLINE + 1))
                {
                    freeFileResources(_fileIn);
                    return ;
                }
            }
        }
        freeFileResources(_fileIn);
    }
    catch(...)
    {
        freeFileResources(_fileIn);
        cerr << "An exception has occurred in the function ClustalFileParser::getSecStructure()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }
}

bool ClustalFileParser::clustalBlankline(char* line)
{
    int i;

    if (line[0] == '!')
    {
        return true;
    }

    for (i = 0; line[i] != '\n' && line[i] != EOS; i++)
    {
        if (isdigit(line[i]) || isspace(line[i]) || (line[i] == '*') ||
            (line[i] == ':') || (line[i] == '.'))
            ;
        else
        {
            return false;
        }
    }
    return true;
}

}