source: tags/svn.1.5.4/NTREE/NT_validNames.cxx

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