source: tags/cvs_2_svn/NTREE/NT_validNames.cxx

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