File: MSFFileParser.cpp

package info (click to toggle)
clustalw 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,620 kB
  • sloc: cpp: 27,857; sh: 3,805; xml: 822; makefile: 153
file content (254 lines) | stat: -rw-r--r-- 6,670 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
/**
 * 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 "MSFFileParser.h"

namespace clustalw
{

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


    
    vector<Sequence> MSFFileParser::getSeqRange(int firstSeq, int no, string *offendingSeq)
{
    vector<Sequence> seqRangeVector;
    int i;

    for (i=0; i<no; i++)
    { 
        Sequence tempSeq = getSeq(firstSeq + i);
        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 in the file to get.
 * @return A sequence object containing the seqNum'th sequence from the file.
 */
Sequence MSFFileParser::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, j, k;
    unsigned char c;

    try
    {
        _fileIn = new InFileStream;  //nige
        _fileIn->open(fileName.c_str());  //nige
        _fileIn->seekg(0, std::ios::beg);       
        
        for (i = 0;; i++)
        {
            if (!_fileIn->getline(_line, MAXLINE + 1))
            {
                _fileIn->close();
                return Sequence(blank, blank, blank);
            }
            // read the title
            if (utilityObject->lineType(_line, "//"))
            {
                break;
            }
            // lines...ignore
        }

        while (_fileIn->getline(_line, MAXLINE + 1))
        {
            if (!utilityObject->blankLine(_line))
            {
                for (i = 1; i < seqNum; i++)
                {
                    _fileIn->getline(_line, MAXLINE + 1);
                }
                for (j = 0; j <= (int)strlen(_line); j++)
                {
                    if (_line[j] != ' ')
                    {
                        break;
                    }
                }
                for (k = j; k <= (int)strlen(_line); k++)
                {
                    if (_line[k] == ' ')
                    {
                        break;
                    }
                }
                
                // Get the name of the sequence
                strncpy(_sname, _line + j, utilityObject->MIN(MAXNAMES, k - j));
                _sname[utilityObject->MIN(MAXNAMES, k - j)] = EOS;
                utilityObject->rTrim(_sname);
                utilityObject->blankToUnderscore(_sname);
                name = string(_sname);

                for (i = k; i <= MAXLINE; i++)
                {
                    c = _line[i];
                    if (c == '.' || c == '~')
                    {
                        c = '-';
                    }
                    if (c == '*')
                    {
                        c = 'X';
                    }
                    if (c == '\n' || c == EOS)
                    {
                        break;
                    }
                    // EOL 
                    c = chartab[c];
                    if (c)
                    {
                        characterSeq += c;
                    }
                }

                for (i = 0;; i++)
                {
                    if (!_fileIn->getline(_line, MAXLINE + 1))
                    {
                        _fileIn->close();
                        return Sequence(characterSeq, name, title);
                    }
                    if (utilityObject->blankLine(_line))
                    {
                        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 << "An exception has occurred in the function MSFFileParser::getSeq()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);
    }
}

/**
 * The function countSeqs counts the number of sequences in the file.
 * @return The number of sequences in the file.
 */
int MSFFileParser::countSeqs()
{
    char _line[MAXLINE + 1];
    int _numSeqs;
    
    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, "//"))
            {
                break;
            }
        }

        while (_fileIn->getline(_line, MAXLINE + 1))
        {
            if (!utilityObject->blankLine(_line))
            {
                break;
            }
            // Look for next non- blank line
        } 
        _numSeqs = 1;

        while (_fileIn->getline(_line, MAXLINE + 1))
        {
            if (utilityObject->blankLineNumericLabel(_line))
            {
                _fileIn->close();
                return _numSeqs;
            }
            _numSeqs++;
        }

        return 0; // if you got to here-funny format/no seqs.
    }
    catch(...)
    {
        _fileIn->close();
        cerr << "An exception has occurred in the function MSFFileParser::countSeqs()\n"
             << "Program needs to terminate.\nPlease contact the Clustal developers\n";
        exit(1);    
    }
}

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


}