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