source: tags/ms_ra2q56/NTREE/NT_validNames.cxx

Last change on this file was 16763, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : NT_validNames.cxx                                 //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "NT_local.h"
12#include "NT_validNameParser.h"
13
14#include <arbdbt.h>
15#include <aw_msg.hxx>
16#include <awt_www.hxx>
17
18#include <fstream>
19#include <iostream>
20#include <iterator>
21
22using namespace std;
23
24#if defined(DEVEL_LOTHAR)
25#define DUMP
26#endif // DEVEL_LOTHAR
27
28void NT_deleteValidNames(AW_window*) {
29    GB_ERROR error;
30
31    GB_begin_transaction(GLOBAL.gb_main);
32    GBDATA* namesCont = GB_search(GLOBAL.gb_main, "VALID_NAMES", GB_CREATE_CONTAINER);
33    GB_write_security_delete(namesCont, 6);
34    error = GB_delete(namesCont);
35    if (error) {
36        aw_message("Valid Names container was not removed from database\nProtection level 6 needed");
37    }
38    else {
39        aw_message("Valid Names container was removed from database\nThink again before saving");
40    }
41    GB_commit_transaction(GLOBAL.gb_main);
42
43#if defined(DUMP)
44    std::cout << "DeleteValidNames was selected" << std::endl;
45#endif // DUMP
46
47}
48
49void NT_importValidNames(AW_window*) {
50    using namespace        std;
51    using                  validNames::Desco;
52    typedef vector<string> StrL;
53    typedef vector<Desco>  DescList;
54    string                 tmpString;
55    StrL                   fileContent;
56
57    // Load LoVPBN (List of Validly Published Bacterial Names)
58    const char *fileName = GB_path_in_ARBLIB("LoVPBN.txt");
59
60    DescList myDescs;
61
62    // file select dialog goes here
63    try {
64        ifstream namesFile(fileName);
65        if (!namesFile.is_open()) {
66            throw string("cannot open file \"") + fileName + "\" to read";
67        }
68        namesFile.unsetf(ios::skipws); // keep white spaces
69        // undefined iterator theEnd denotes end of stream
70        istream_iterator<char> inIter(namesFile), theEnd;
71
72        std::cout << "Reading valid names from '" << fileName << "'\n";
73
74        for (; inIter != theEnd; ++inIter) {
75            if (*inIter == '\r') continue; // remove empty lines due to dos \r
76            if (*inIter == '\n') {
77                if (!tmpString.empty()) {  // check for newline
78                    fileContent.push_back(tmpString);
79                    tmpString = "";
80                }
81            }
82            else {
83                tmpString += *inIter;
84            }
85        }
86        if (!tmpString.empty()) fileContent.push_back(tmpString); // if last line doesn't end with \n
87
88        StrL::iterator it;
89        bool isHeader = true;
90        for (it = fileContent.begin(); it != fileContent.end(); ++it) {
91            if (isHeader) {
92                string nameStart ("ABIOTROPHIA");
93                if (it->find(nameStart.c_str(), 0, 11) != string::npos) {
94                    isHeader = false;
95                    Desco myDesc =  validNames::determineType(*it);
96#if defined(DUMP)
97                    std::cout << string("valid name: ") << myDesc.getFirstName() << std::endl
98                              << string("other name: \t\t") << myDesc.getSecondName() << std::endl;
99#endif // DUMP
100                    myDescs.push_back(myDesc);
101                }
102            }
103            else {
104                Desco myDesc =  validNames::determineType(*it);
105#if defined(DUMP)
106                std::cout << string("valid name: ") << myDesc.getFirstName() << std::endl
107                          << string("other name: \t\t") << myDesc.getSecondName() << std::endl;
108#endif // DUMP
109                myDescs.push_back(myDesc);
110            }
111
112        }
113        // continue here with database interaction
114        GB_ERROR error        = GB_begin_transaction(GLOBAL.gb_main);
115        GBDATA*  gb_namesCont = GB_entry(GLOBAL.gb_main, "VALID_NAMES");
116
117        if (gb_namesCont) {
118            error = "Container for Valid Names already exists\n Please delete old Valid Names first";
119        }
120        else {
121            gb_namesCont             = GB_create_container(GLOBAL.gb_main, "VALID_NAMES");
122            if (!gb_namesCont) error = GB_await_error();
123            else {
124                DescList::iterator di;
125                for (di = myDescs.begin(); di != myDescs.end() && !error; ++di) {
126                    if (di->getType() < 10) {
127                        GBDATA* gb_pair     = GB_create_container(gb_namesCont, "pair");
128                        if (!gb_pair) error = GB_await_error();
129                        else {
130                            error             = GBT_write_string(gb_pair, "OLDNAME", di->getSecondName().c_str());
131                            if (!error) error = GBT_write_string(gb_pair, "NEWNAME", di->getFirstName().c_str());
132                            if (!error) {
133                                const char* typeStr  = NULp;
134                                switch (di->getType()) {
135                                    case 0: typeStr = "VALGEN"; break;
136                                    case 1: typeStr = "HETGEN"; break;
137                                    case 2: typeStr = "HOMGEN"; break;
138                                    case 3: typeStr = "RENGEN"; break;
139                                    case 4: typeStr = "CORGEN"; break;
140                                    case 5: typeStr = "VALSPEC"; break;
141                                    case 6: typeStr = "HETSPEC"; break;
142                                    case 7: typeStr = "HOMSPEC"; break;
143                                    case 8: typeStr = "RENSPEC"; break;
144                                    case 9: typeStr = "CORSPEC"; break;
145                                    default: nt_assert(0); break;
146                                }
147                                error = GBT_write_string(gb_pair, "DESCTYPE", typeStr);
148                            }
149                        }
150                    }
151                }
152            }
153        }
154        error = GB_end_transaction(GLOBAL.gb_main, error);
155        if (error) aw_message(error);
156    }
157    catch (string& err) { aw_message(err.c_str()); }
158    catch (...) { aw_message("Unknown exception"); }
159}
160
161
162void NT_suggestValidNames(AW_window*) {
163    GB_begin_transaction(GLOBAL.gb_main);
164
165    GBDATA*  gb_validNamesCont = GB_entry(GLOBAL.gb_main, "VALID_NAMES");
166    GB_ERROR err               = NULp;
167
168    if (!gb_validNamesCont) err = "No valid names imported yet";
169
170    for (GBDATA *gb_species = GBT_first_species(GLOBAL.gb_main);
171         !err && gb_species;
172         gb_species = GBT_next_species(gb_species))
173    {
174        // retrieve species names
175        GBDATA*  gb_fullName = GB_entry(gb_species, "full_name"); // gb_fullname
176        char    *fullName     = gb_fullName ? GB_read_string(gb_fullName) : NULp;
177        if (!fullName) err    = "Species has no fullname";
178
179        // search validNames
180
181        for (GBDATA *gb_validNamePair = GB_entry(gb_validNamesCont, "pair");
182             gb_validNamePair && !err;
183             gb_validNamePair = GB_nextEntry(gb_validNamePair))
184        {
185            // retrieve list of all species names
186
187            GBDATA* gb_actDesc = GB_entry(gb_validNamePair, "DESCTYPE");
188            char*   typeString = GB_read_string(gb_actDesc);
189
190            nt_assert(strcmp(typeString, "NOTYPE") != 0);
191
192            char *validName = GBT_read_string(gb_validNamePair, "NEWNAME");
193            char *depName   = GBT_read_string(gb_validNamePair, "OLDNAME");
194
195            if (!validName || !depName) err = GB_await_error();
196            else {
197                // now compare all names
198                if (!err && ((strcmp(fullName, validName) == 0)||(strcmp(fullName, depName) == 0))) {
199                    // insert new database fields if necessary
200                    GBDATA* gb_validNameCont   = GB_search(gb_species, "Valid_Name", GB_CREATE_CONTAINER);
201                    if (!gb_validNameCont) err = GB_await_error();
202                    else {
203                        err           = GBT_write_string(gb_validNameCont, "NameString", validName);
204                        if (!err) err = GBT_write_string(gb_validNameCont, "DescType", typeString);
205                    }
206                }
207            }
208
209            free(validName);
210            free(depName);
211            free(typeString);
212        }
213
214        free(fullName);
215    }
216
217    GB_end_transaction_show_error(GLOBAL.gb_main, err, aw_message);
218}
Note: See TracBrowser for help on using the repository browser.