Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / clustalw / src / fileInput / PIRFileParser.cpp
1 /**
2  * Author: Mark Larkin
3  * 
4  * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
5  */
6 /**
7  * Changes:
8  *
9  * 10-02-07,Nigel Brown(EMBL): changed ifstream to InFileStream to handle
10  * cross-platform end-of-lines.
11  */
12
13 #ifdef HAVE_CONFIG_H
14     #include "config.h"
15 #endif
16 #include "PIRFileParser.h"
17
18 namespace clustalw
19 {
20
21 /**
22  * PIRFileParser contructor sets up the chartab array.
23  * @param filePath 
24  */
25 PIRFileParser::PIRFileParser(string filePath)
26 {
27     fileName = filePath; 
28     fillCharTab();
29 }
30
31
32 /*
33  * get range of sequences
34  */
35     vector<Sequence> PIRFileParser::getSeqRange(int firstSeq, int no, string *offendingSeq)
36 {
37     vector<Sequence> seqRangeVector;
38     int i;
39
40     for (i=0; i<no; i++)
41     { 
42         Sequence tempSeq = getSeq(firstSeq + i, offendingSeq);
43         if (parseExitCode!=OK) {
44             seqRangeVector.clear();
45             return seqRangeVector;
46         }
47         seqRangeVector.push_back(tempSeq);
48     }
49     return seqRangeVector;
50 }
51
52
53
54 /**
55  * The function getSeq finds the sequence 'seqNum' in the file and returns it.
56  * @param seqNum The number of the sequence to get from the file.
57  * @return The 'seqNum' sequence from the file.
58  */
59     Sequence PIRFileParser::getSeq(int seqNum, string *offendingSeq)
60 {
61     char _line[MAXLINE + 1];
62     char _sname[MAXNAMES + 1];
63     char _title[MAXTITLES + 1];
64     string characterSeq = "";
65     string name = "";
66     string title = "";
67     string blank = "";
68     
69     _line[0] = EOS;
70     int i;
71     unsigned char c;
72     int _currentSeqNum = 0;
73     
74     try
75     {
76         _fileIn = new InFileStream;  //nige
77         _fileIn->open(fileName.c_str());  //nige
78         _fileIn->seekg(0, std::ios::beg);
79         
80         // Read in lines until we get to the begining of sequence seqNum.
81         while (_currentSeqNum != seqNum)
82         {
83             while(*_line != '>')
84             {
85                 if(!_fileIn->getline(_line, MAXLINE + 1)) // If we cannot get anymore!
86                 {
87                     _fileIn->close();
88                     return Sequence(blank, blank, blank);
89                 }
90             }
91             ++_currentSeqNum;
92             if(_currentSeqNum == seqNum) // Found the sequence
93             {
94                 break;
95             }
96             // Get next line so that we are past the '>' line
97             _fileIn->getline(_line, MAXLINE + 1);
98         }        
99         
100         // line contains the name of the sequence
101         for (i = 4; i <= (int)strlen(_line); i++)
102         {
103             if (_line[i] != ' ')
104             {
105                 break;
106             }
107         }
108         
109         strncpy(_sname, _line + i, MAXNAMES); // remember entryname 
110         _sname[MAXNAMES] = EOS;
111         utilityObject->rTrim(_sname);
112         utilityObject->blankToUnderscore(_sname); // replace blanks with '_'
113         name = string(_sname);
114         
115         _fileIn->getline(_line, MAXLINE + 1);
116         strncpy(_title, _line, MAXTITLES);
117         _title[MAXTITLES] = EOS;
118         i = strlen(_title);
119         if (_title[i - 1] == '\n')
120         {
121             _title[i - 1] = EOS;
122         }
123         title = string(_title);
124         
125         while (_fileIn->getline(_line, MAXLINE + 1))
126         {
127             for (i = 0; i <= MAXLINE; i++)
128             {
129                 c = _line[i];
130                 if (c == '\n' || c == EOS || c == '*')
131                 {
132                     break;
133                 }
134
135                 c = chartab[c];
136                 if (c)
137                 {
138                     characterSeq += c;
139                 }
140             }
141             if (c == '*')
142             {
143                 break;
144             }
145         }
146         _fileIn->close();
147
148         if ((int)characterSeq.length() > userParameters->getMaxAllowedSeqLength())
149         {
150             parseExitCode=SEQUENCETOOBIG;
151             if (offendingSeq!=NULL)
152                 offendingSeq->assign(name);
153             // return empty seq
154             return Sequence(blank, blank, blank);
155         }
156         return Sequence(characterSeq, name, title);
157     }
158     catch(...)
159     {
160         _fileIn->close();
161         cerr << "There was an exception in the PIRFileParser::getSeq function.\n"
162              << "Need to end program\n";
163         exit(1);    
164     }
165 }
166
167 /**
168  * The function countSeqs finds the number of sequences in the file and returns it.
169  * @return The number of sequences in the file.
170  */
171 int PIRFileParser::countSeqs()
172 {
173     char line[MAXLINE + 1], c;
174     line[0] = EOS;
175     int numSeqs, i;
176     bool seqOk;
177     
178     try
179     {
180         _fileIn = new InFileStream;  //nige
181         _fileIn->open(fileName.c_str());  //nige
182     
183         if(!_fileIn->is_open())
184         {
185             return 0; // No sequences found!
186         }
187     
188         // Get to begining of sequences!
189         while (_fileIn->getline(line, MAXLINE + 1))
190         {
191             if (!utilityObject->blankLine(line))
192             {
193                 break;
194             }
195         }
196     
197         // Now check the 1st sequence to make sure it ends with *
198         seqOk = false;
199         while (_fileIn->getline(line, MAXLINE + 1))
200         {
201              // Look for end of first seq
202             if (*line == '>')
203             {
204                 break;
205             }
206             for (i = 0; seqOk == false; i++)
207             {
208                 c = line[i];
209                 if (c == '*')
210                 {
211                     seqOk = true; // ok - end of sequence found
212                     break;
213                 } // EOL 
214                 if (c == '\n' || c == EOS)
215                 {
216                     break;
217                 }
218                 // EOL
219             }
220             if (seqOk == true)
221             {
222                 break;
223             }
224         }
225         if (seqOk == false)
226         {
227             _fileIn->close();
228             utilityObject->error("PIR format sequence end marker '*'\nmissing for one or more sequences.\n");     
229             return 0; // funny format
230         }
231
232         numSeqs = 1;
233     
234         while (_fileIn->getline(line, MAXLINE + 1))
235         {
236             if (*line == '>')
237             {
238                 // Look for start of next seq 
239                 seqOk = false;
240                 while (_fileIn->getline(line, MAXLINE + 1))
241                 {
242                     // Look for end of seq
243                     if (*line == '>')
244                     {
245                         _fileIn->close();
246                         utilityObject->error("PIR format sequence end marker '*'\nmissing for one or more sequences.\n");     
247                         return 0; // funny format
248                     }
249                     for (i = 0; seqOk == false; i++)
250                     {
251                         c = line[i];
252                         if (c == '*')
253                         {
254                             seqOk = true; // ok - sequence found
255                             break;
256                         }
257                         if (c == '\n' || c == EOS)
258                         {
259                             break;
260                         }
261                     }
262                     if (seqOk == true)
263                     {
264                         numSeqs++;
265                         break;
266                     }
267                 }
268             }
269         }
270     
271         _fileIn->close();
272     
273         return numSeqs;
274     }
275     catch(...)
276     {
277         _fileIn->close();
278         cerr << "An exception has occured in the function PIRFileParser::countSeqs()\n"
279              << "Program needs to terminate.\nPlease contact the Clustal developers\n";
280         exit(1);    
281     }
282 }
283
284 /**
285  * There is no secondary structure information in PIR files!
286  * @param gapPenaltyMask 
287  * @param secStructMask 
288  * @param secStructName 
289  * @param structPenalties 
290  * @param length 
291  */
292 void PIRFileParser::getSecStructure(vector<char>& gapPenaltyMask, vector<char>& secStructMask,
293                                     string& secStructName, int &structPenalties, int length)
294 {
295     structPenalties = NONE;
296 }
297
298 }
299