File: PIRFileParser.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 3,324 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 16
file content (299 lines) | stat: -rw-r--r-- 7,930 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
/**
 * 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 "PIRFileParser.h"

namespace clustalw
{

/**
 * PIRFileParser constructor sets up the chartab array.
 * @param filePath 
 */
PIRFileParser::PIRFileParser(string filePath)
{
    fileName = filePath; 
    fillCharTab();
}


/*
 * get range of sequences
 */
    vector<Sequence> PIRFileParser::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 function getSeq finds the sequence 'seqNum' in the file and returns it.
 * @param seqNum The number of the sequence to get from the file.
 * @return The 'seqNum' sequence from the file.
 */
    Sequence PIRFileParser::getSeq(int seqNum, string *offendingSeq)
{
    char _line[MAXLINE + 1];
    char _sname[MAXNAMES + 1];
    char _title[MAXTITLES + 1];
    string characterSeq = "";
    string name = "";
    string title = "";
    string blank = "";
    
    _line[0] = EOS;
    int i;
    unsigned char c;
    int _currentSeqNum = 0;
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg);
        
        // Read in lines until we get to the begining of sequence seqNum.
        while (_currentSeqNum != seqNum)
        {
            while(*_line != '>')
            {
                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);
        }        
        
        // line contains the name of the sequence
        for (i = 4; i <= (int)strlen(_line); i++)
        {
            if (_line[i] != ' ')
            {
                break;
            }
        }
        
        strncpy(_sname, _line + i, MAXNAMES); // remember entryname 
        _sname[MAXNAMES] = EOS;
        utilityObject->rTrim(_sname);
        utilityObject->blankToUnderscore(_sname); // replace blanks with '_'
        name = string(_sname);
        
        _fileIn->getline(_line, MAXLINE + 1);
        strncpy(_title, _line, MAXTITLES);
        _title[MAXTITLES] = EOS;
        i = strlen(_title);
        if (_title[i - 1] == '\n')
        {
            _title[i - 1] = EOS;
        }
        title = string(_title);
        
        while (_fileIn->getline(_line, MAXLINE + 1))
        {
            for (i = 0; i <= MAXLINE; i++)
            {
                c = _line[i];
                if (c == '\n' || c == EOS || c == '*')
                {
                    break;
                }

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

/**
 * The function countSeqs finds the number of sequences in the file and returns it.
 * @return The number of sequences in the file.
 */
int PIRFileParser::countSeqs()
{
    char line[MAXLINE + 1], c;
    line[0] = EOS;
    int numSeqs, i;
    bool seqOk;
    
    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
    
        if(!_fileIn->is_open())
        {
            return 0; // No sequences found!
        }
    
        // Get to begining of sequences!
        while (_fileIn->getline(line, MAXLINE + 1))
        {
            if (!utilityObject->blankLine(line))
            {
                break;
            }
        }
    
        // Now check the 1st sequence to make sure it ends with *
        seqOk = false;
        while (_fileIn->getline(line, MAXLINE + 1))
        {
             // Look for end of first seq
            if (*line == '>')
            {
                break;
            }
            for (i = 0; seqOk == false; i++)
            {
                c = line[i];
                if (c == '*')
                {
                    seqOk = true; // ok - end of sequence found
                    break;
                } // EOL 
                if (c == '\n' || c == EOS)
                {
                    break;
                }
                // EOL
            }
            if (seqOk == true)
            {
                break;
            }
        }
        if (seqOk == false)
        {
            _fileIn->close();
            utilityObject->error("PIR format sequence end marker '*'\nmissing for one or more sequences.\n");     
            return 0; // funny format
        }

        numSeqs = 1;
    
        while (_fileIn->getline(line, MAXLINE + 1))
        {
            if (*line == '>')
            {
                // Look for start of next seq 
                seqOk = false;
                while (_fileIn->getline(line, MAXLINE + 1))
                {
                    // Look for end of seq
                    if (*line == '>')
                    {
                        _fileIn->close();
                        utilityObject->error("PIR format sequence end marker '*'\nmissing for one or more sequences.\n");     
                        return 0; // funny format
                    }
                    for (i = 0; seqOk == false; i++)
                    {
                        c = line[i];
                        if (c == '*')
                        {
                            seqOk = true; // ok - sequence found
                            break;
                        }
                        if (c == '\n' || c == EOS)
                        {
                            break;
                        }
                    }
                    if (seqOk == true)
                    {
                        numSeqs++;
                        break;
                    }
                }
            }
        }
    
        _fileIn->close();
    
        return numSeqs;
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "An exception has occurred in the function PIRFileParser::countSeqs()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }
}

/**
 * There is no secondary structure information in PIR files!
 * @param gapPenaltyMask 
 * @param secStructMask 
 * @param secStructName 
 * @param structPenalties 
 * @param length 
 */
void PIRFileParser::getSecStructure(vector<char>& gapPenaltyMask, vector<char>& secStructMask,
                                    string& secStructName, int &structPenalties, int length)
{
    structPenalties = NONE;
}

}