source: tags/arb-6.0/NTREE/NT_validNameParser.cxx

Last change on this file was 11492, checked in by westram, 10 years ago
  • fix several doxygen issues (doxygen 1.6.3)
    • force macro expansion for IF_ASSERTION_USED
    • fixed changed param names
    • named some anonymous (nested) structs
    • remove/quote several '#' (triggering unknown commands)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1/*
2 * Definition of all objects belonging to this version of
3 * the valid names text file
4 *
5 * 29. November 2002
6 *
7 * coded by Lothar Richter
8 *
9 * Copyright (C) 2002 Department of Microbiology (Technical University Munich)
10 */
11
12#if defined(DEVEL_LOTHAR)
13#define DUMP
14#endif // DEVEL_LOTHAR
15
16#include "NT_validNameParser.h"
17#include "NT_local.h"
18
19#include <arb_assert.h>
20
21#include <cstdlib>
22#include <cstdlib>
23#include <iostream>
24#include <fstream>
25
26using namespace std;
27
28namespace validNames {
29
30
31    TokLPtr tokenize(const std::string& description, TokLPtr tokenLP) {
32        size_t tokenEnd = 0;
33        size_t tokenBegin = 0;
34
35        while (tokenEnd != description.size()) { // CC : warning: comparison between signed and unsigned (tokenEnd sollte nicht 'int' sondern 'unsigned' sein)
36            tokenEnd = description.find_first_of(' ', tokenBegin);
37            if (tokenEnd == string::npos) tokenEnd = description.size();
38            int tokLength = tokenEnd - tokenBegin;
39            if (tokLength != 0) {
40                tokenLP->push_back(description.substr(tokenBegin, tokenEnd - tokenBegin));
41            }
42            tokenBegin = tokenEnd + 1;
43
44        }
45        return tokenLP;
46    }
47
48
49
50
51
52    Desco determineType(const string& descriptionString)
53    { // begin determineType
54
55        DESCT actType = NOTYPE;
56        TokLPtr tokenLP =  new TokL;
57        tokenLP = tokenize(descriptionString, tokenLP);
58        // remove all tokens in parentheses
59        {
60            TokL::iterator it = tokenLP->begin();
61            while (it != tokenLP->end()) {
62                if (((*it).at(0) == '(') && *it != string("(corrig.)")) it = tokenLP->erase(it);
63                else ++it;
64            }
65        }
66
67        // check first word for upper case letters
68        string descNames[6]; // first the valid genus, species, subsp. then the other names
69        // stores occurrence of subsp. which is needed to retrieve the right tokens later on and status flags
70        int sspPos[2] = { 0, 0 }; // token subsp. occurs maximum twice
71        int ssp = 0;
72        bool isValid = true;
73        bool isRenamed = false;
74        bool isHetero = false;
75        bool isHomo = false;
76        bool isGenus = false;
77        // bool isSee = false;
78        bool isCorr = false;
79
80
81
82        for (TokL::iterator it = tokenLP->begin(); it != tokenLP->end(); ++it, ++ssp) {
83            if (isUpperCase(*it)) {
84                isGenus = true;
85#if defined(DUMP)
86                std::cout << "genus detected" << std::endl;
87#endif // DUMP
88            }
89
90
91            else { // begin operators
92                if (*it == string("->")) {
93                    nt_assert(!isHetero);
94                    nt_assert(!isHomo);
95                    nt_assert(isValid); // only one operator per line allowed
96                    isRenamed = true;
97                    isValid   = false;
98#if defined(DUMP)
99                    std::cout << "renaming detected" << std::endl;
100#endif // DUMP
101                }
102                else {
103                    if (*it == string("=>")) {
104                        nt_assert(!isRenamed);
105                        nt_assert(!isHomo);
106                        nt_assert(isValid);
107                        isHetero = true;
108                        isValid = false;
109#if defined(DUMP)
110                        std::cout << "heteronym detected" << std::endl;
111#endif // DUMP
112                    }
113                    else {
114                        if (*it == string("=")) {
115                            nt_assert(!isRenamed);
116                            nt_assert(!isHetero);
117                            nt_assert(isValid);
118                            isHomo = true;
119                            isValid = false;
120#if defined(DUMP)
121                            std::cout << "homonym detected" << std::endl;
122#endif // DUMP
123                        }
124                        else {
125                            if (*it == string("(corrig.)")) {
126                                isCorr = true;
127#if defined(DUMP)
128                                std::cout << "correction" << std::endl;
129#endif // DUMP
130                            }
131                            else {
132                                if (*it == string("see:")) {
133                                    // isSee = true;
134                                    isValid = false;
135#if defined(DUMP)
136                                    std::cout << "reference" << std::endl;
137#endif // DUMP
138                                }
139                                else {
140                                    if (*it == string("subsp.")) {
141#if defined(DUMP)
142                                        std::cout << "subspecies detected at position: >>>" << ssp << "<<<" << std::endl;
143#endif // DUMP
144                                        ssp == 2 ? sspPos[0] = ssp : sspPos[1] = ssp;
145                                        // max. one subsp. on each operator side
146#if defined(DUMP)
147                                        std::cout << "position of subsp.: " << sspPos[0] << "\tand: " << sspPos[1] << std::endl;
148#endif // DUMP
149                                    }
150                                }
151                            }
152                        }
153                    }
154                }
155            }
156        }
157
158
159
160        if (isGenus) {
161#if defined(DUMP)
162            std::cout << " GENUS description found " << std::endl;
163#endif // DUMP
164            if (isValid) {
165                descNames[0] = (*tokenLP)[0];
166                actType      = VALGEN;
167#if defined(DUMP)
168                std::cout << "VALIDGEN type set to: " << actType << std::endl;
169#endif// DUMP
170            }
171            else {
172                if (isHetero) {
173                    descNames[0] = (*tokenLP)[2];
174                    descNames[3] = (*tokenLP)[0];
175                    actType = HETGEN;
176#if defined(DUMP)
177                    std::cout << "HETERONYMGEN type set to: " << actType << std::endl;
178#endif // DUMP
179                }
180                else {
181                    if (isHomo) {
182                        descNames[0] = (*tokenLP)[2];
183                        descNames[3] = (*tokenLP)[0];
184                        actType = HOMGEN;
185#if defined(DUMP)
186                        std::cout << "HOMONYMGEN type set to: " << actType << std::endl;
187#endif // DUMP
188
189                    }
190                    else {
191
192                        if (isRenamed) {
193                            descNames[0] = (*tokenLP)[2];
194                            descNames[3] = (*tokenLP)[0];
195                            actType = RENGEN;
196#if defined(DUMP)
197                            std::cout << "RENAMEDGEN type set to: " << actType << std::endl;
198#endif // DUMP
199                        }
200                        else {
201#if defined(DUMP)
202                            std::cout << "no meaningful combination of conditions reached" << std::endl
203                                      << "for line: " << descriptionString << std::endl;
204                            std::cout << "description type is set to NOTYPE: " << NOTYPE << std::endl;
205#endif // DUMP
206                            isValid = false;
207#if defined(DUMP)
208                            std::cout << "isValid set to false "  << std::endl;
209#endif // DUMP
210                            actType = NOTYPE;
211                        }
212                    }
213                }
214            }
215        }
216        else {
217
218            //       just fancy experimental , maybe not 100% correct but looks good
219            if (!(((sspPos[0] == 0) || (sspPos[0] == 2)) && (((sspPos[1] > 4)&&(sspPos[1]< 9))||(sspPos[1]==0))))
220            {
221#if defined(DUMP)
222                std::cout << "subsp. at strange position found in line:" << std::endl << descriptionString << endl;
223                std::cout << "description type is set to NOTYPE: " << NOTYPE << std::endl;
224#endif // DUMP
225                isValid = false;
226#if defined(DUMP)
227                std::cout << "isValid set to false "  << std::endl;
228#endif // DUMP
229                actType = NOTYPE;
230            }
231
232            if (isValid) {
233                descNames[0] = (*tokenLP)[0];
234                descNames[1] = (*tokenLP)[1];
235                if (sspPos[0] != 0) { descNames[2] = (*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
236                actType = VALSPEC;
237            }
238            else { // begin else isHetero
239                if (isHetero) {
240                    descNames[0] = (*tokenLP)[3 + sspPos[0]];
241                    descNames[1] = (*tokenLP)[4 + sspPos[0]];
242                    if (sspPos[1]!=0) { descNames[2]=(*tokenLP)[6 + sspPos[0]]; } // only if subsp. exists
243
244                    descNames[3] = (*tokenLP)[0];
245                    descNames[4] = (*tokenLP)[1];
246                    if (sspPos[0]!=0) { descNames[5]=(*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
247
248                    actType = HETSPEC;
249                }
250                else {
251                    if (isHomo) {
252                        descNames[0] = (*tokenLP)[3 + sspPos[0]];
253                        descNames[1] = (*tokenLP)[4 + sspPos[0]];
254                        if (sspPos[1]!=0) { descNames[2]=(*tokenLP)[6 + sspPos[0]]; } // only if subsp. exists
255
256                        descNames[3] = (*tokenLP)[0];
257                        descNames[4] = (*tokenLP)[1];
258                        if (sspPos[0]!=0) { descNames[5]=(*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
259
260                        actType = HOMSPEC;
261
262                    }
263                    else { // else branch isHomo
264                        if (isRenamed) {
265                            descNames[0] = (*tokenLP)[3 + sspPos[0]];
266                            descNames[1] = (*tokenLP)[4 + sspPos[0]];
267                            if (sspPos[1]!=0) { descNames[2]=(*tokenLP)[6 + sspPos[0]]; } // only if subsp. exists
268
269                            descNames[3] = (*tokenLP)[0];
270                            descNames[4] = (*tokenLP)[1];
271                            if (sspPos[0]!=0) { descNames[5]=(*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
272
273                            actType = RENSPEC;
274
275                        }
276                        else { // species remaining cases
277#if defined(DUMP)
278                            std::cout << "not a valid description line detected" << std::endl;
279                            std::cout << "isValid: " << isValid << std::endl;
280                            std::cout << "isRenamed: " << isRenamed << std::endl;
281                            std::cout << "isHetero: " << isHetero << std::endl;
282                            std::cout << "isHomo: " << isHomo << std::endl;
283                            std::cout << "isGenus: " << isGenus << std::endl;
284                            std::cout << "isSee: " << isSee << std::endl;
285                            std::cout << "isCorr: " << isCorr << std::endl;
286                            std::cout << "sspPos: " << sspPos[0] << " and " << sspPos[1] << std::endl;
287                            std::cout << descriptionString << std::endl;
288#endif // DUMP
289                            actType = NOTYPE;
290                        }
291
292                    }
293                }
294            }
295        }
296
297
298#if defined(DUMP)
299        std::cout << descriptionString << std::endl;
300        std::cout << "classified as " << actType << std::endl;
301#endif // DUMP
302
303        Desco actDesc(actType, isCorr, descNames[0], descNames[1], descNames[2], descNames[3], descNames[4], descNames[5]);
304        delete tokenLP;
305        return actDesc;
306    }
307
308
309    string Desco::getFirstName() {
310        string tmp = firstgen;
311        if (!firstspec.empty()) {
312            tmp = tmp + " " + firstspec;
313            if (!firstsub.empty()) {
314                tmp = tmp + " " + "subsp." + " " + firstsub;
315            }
316        }
317
318
319        return tmp;
320    }
321
322    string Desco::getSecondName() {
323        string tmp = secondgen;
324        if (!secondspec.empty()) {
325            tmp = tmp + " " + firstspec;
326            if (!secondsub.empty()) {
327                tmp = tmp + " " + "subsp." + " " + secondsub;
328            }
329        }
330        return tmp;
331    }
332
333
334    bool isUpperCase(const string& input)
335    {
336        for (size_t i=0; i<input.length(); ++i)
337        {
338            if (input[i]<'A' || input[i]>'Z') { return false; }
339        }
340        return true;
341    }
342}
Note: See TracBrowser for help on using the repository browser.