| 1 | // ================================================================ // |
|---|
| 2 | // // |
|---|
| 3 | // File : AWTC_submission.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ================================================================ // |
|---|
| 10 | |
|---|
| 11 | #include <aw_window.hxx> |
|---|
| 12 | #include <aw_awars.hxx> |
|---|
| 13 | #include <aw_edit.hxx> |
|---|
| 14 | #include <aw_root.hxx> |
|---|
| 15 | #include <aw_msg.hxx> |
|---|
| 16 | |
|---|
| 17 | #include <arbdbt.h> |
|---|
| 18 | #include <arb_strbuf.h> |
|---|
| 19 | #include <arb_strarray.h> |
|---|
| 20 | |
|---|
| 21 | #define awtc_assert(bed) arb_assert(bed) |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #define AWAR_PARSER "tmp/submission/parser" |
|---|
| 25 | #define AWAR_PARSED "tmp/submission/parsed" |
|---|
| 26 | |
|---|
| 27 | void AWTC_create_submission_variables(AW_root *root, AW_default db1) |
|---|
| 28 | { |
|---|
| 29 | root->awar_string("submission/source", "", db1); |
|---|
| 30 | root->awar_string(AWAR_PARSER, "press READ INFO", db1); |
|---|
| 31 | root->awar_string(AWAR_PARSED, "", db1); |
|---|
| 32 | root->awar_string("submission/file", "", db1); |
|---|
| 33 | root->awar_string("submission/privat", "$(ADDRESS, db1)=TUM Munich:\n"); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | static void ed_calltexe_event(AW_window *aww, char *varname) |
|---|
| 37 | { |
|---|
| 38 | AW_root *aw_root = aww->get_root(); |
|---|
| 39 | char *file; |
|---|
| 40 | file = aw_root->awar(varname)->read_string(); |
|---|
| 41 | AW_edit(file); |
|---|
| 42 | free(file); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | static AW_window *create_calltexe_window(AW_root *root, char *varname) { |
|---|
| 46 | AW_window_simple *aws = new AW_window_simple; |
|---|
| 47 | { |
|---|
| 48 | char *var_id = GBS_string_2_key(varname); |
|---|
| 49 | char *window_id = GBS_global_string_copy("SUBM_TEXTEDIT_%s", var_id); |
|---|
| 50 | aws->init(root, window_id, "START TEXT EDITOR"); |
|---|
| 51 | free(window_id); |
|---|
| 52 | free(var_id); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | aws->load_xfig("calltexe.fig"); |
|---|
| 56 | aws->label_length(18); |
|---|
| 57 | aws->button_length(8); |
|---|
| 58 | |
|---|
| 59 | aws->at("close"); |
|---|
| 60 | aws->callback ((AW_CB0)AW_POPDOWN); |
|---|
| 61 | aws->create_button("CLOSE", "CLOSE", "O"); |
|---|
| 62 | |
|---|
| 63 | aws->at("file"); |
|---|
| 64 | aws->create_input_field(varname, 34); |
|---|
| 65 | |
|---|
| 66 | aws->at("edit"); |
|---|
| 67 | aws->callback((AW_CB1)ed_calltexe_event, (AW_CL)varname); |
|---|
| 68 | aws->create_button("EDIT", "EDIT", "E"); |
|---|
| 69 | |
|---|
| 70 | return (AW_window *)aws; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | static void ed_submit_info_event_rm(char *string) |
|---|
| 74 | { |
|---|
| 75 | char *p = string; |
|---|
| 76 | int c; |
|---|
| 77 | while ((c=*p) != 0) { |
|---|
| 78 | if (c==':') *p = ';'; |
|---|
| 79 | if (c=='=') *p = '-'; |
|---|
| 80 | p++; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | static void ed_submit_info_event(AW_window *aww, AW_CL cl_gbmain) { |
|---|
| 85 | GBDATA *gb_main = (GBDATA*)cl_gbmain; |
|---|
| 86 | AW_root *aw_root = aww->get_root(); |
|---|
| 87 | |
|---|
| 88 | GB_transaction ta(gb_main); |
|---|
| 89 | char *species_name = aw_root->awar(AWAR_SPECIES_NAME)->read_string(); |
|---|
| 90 | GBDATA *gb_species = GBT_find_species(gb_main, species_name); |
|---|
| 91 | GBS_strstruct *strstruct = GBS_stropen(1000); |
|---|
| 92 | |
|---|
| 93 | if (gb_species) { |
|---|
| 94 | for (GBDATA *gb_entry = GB_child(gb_species); gb_entry; gb_entry = GB_nextChild(gb_entry)) { |
|---|
| 95 | int type = GB_read_type(gb_entry); |
|---|
| 96 | switch (type) { |
|---|
| 97 | case GB_STRING: |
|---|
| 98 | case GB_INT: |
|---|
| 99 | case GB_BITS: |
|---|
| 100 | case GB_FLOAT: { |
|---|
| 101 | char *key = GB_read_key(gb_entry); |
|---|
| 102 | |
|---|
| 103 | GBS_strcat(strstruct, "$("); |
|---|
| 104 | GBS_strcat(strstruct, key); |
|---|
| 105 | GBS_strcat(strstruct, ")="); |
|---|
| 106 | free(key); |
|---|
| 107 | |
|---|
| 108 | key = GB_read_as_string(gb_entry); |
|---|
| 109 | ed_submit_info_event_rm(key); |
|---|
| 110 | GBS_strcat(strstruct, key); |
|---|
| 111 | free(key); |
|---|
| 112 | |
|---|
| 113 | GBS_strcat(strstruct, ":\n"); |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | default: |
|---|
| 117 | break; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | char *seq_info = GBS_string_eval(" ", "*=" |
|---|
| 121 | "$(SEQ_LEN)\\=*(|sequence|len(.-))\\:\n" |
|---|
| 122 | "$(SEQ_A)\\=*(|sequence|count(aA))\\:\n" |
|---|
| 123 | "$(SEQ_C)\\=*(|sequence|count(cC))\\:\n" |
|---|
| 124 | "$(SEQ_G)\\=*(|sequence|count(gG))\\:\n" |
|---|
| 125 | "$(SEQ_T)\\=*(|sequence|count(tT))\\:\n" |
|---|
| 126 | "$(SEQ_U)\\=*(|sequence|count(uU))\\:\n" |
|---|
| 127 | "$(SEQUENCE)\\=*(|sequence|remove(.-)|format_sequence(firsttab\\=12;tab\\=12;width\\=60;numleft;gap\\=10))\\:\n" |
|---|
| 128 | , gb_species); |
|---|
| 129 | if (seq_info) { |
|---|
| 130 | GBS_strcat(strstruct, seq_info); |
|---|
| 131 | free(seq_info); |
|---|
| 132 | } |
|---|
| 133 | else { |
|---|
| 134 | aw_message(GB_await_error()); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | else { |
|---|
| 138 | GBS_strcat(strstruct, "Species not found"); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | char *parser = GBS_strclose(strstruct); |
|---|
| 142 | aw_root->awar(AWAR_PARSER)->write_string(parser); |
|---|
| 143 | |
|---|
| 144 | free(parser); |
|---|
| 145 | free(species_name); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static void ed_save_var_to_file(AW_window *aww, char *data_var, char *file_var) { |
|---|
| 149 | AW_root *aw_root = aww->get_root(); |
|---|
| 150 | char *data = aw_root->awar(data_var)->read_string(); |
|---|
| 151 | char *file_name = aw_root->awar(file_var)->read_string(); |
|---|
| 152 | FILE *out = fopen(file_name, "w"); |
|---|
| 153 | |
|---|
| 154 | if (out) { |
|---|
| 155 | fprintf(out, "%s", data); |
|---|
| 156 | fclose(out); |
|---|
| 157 | } |
|---|
| 158 | else { |
|---|
| 159 | GB_export_IO_error("saving info", file_name); |
|---|
| 160 | aw_message(GB_await_error()); |
|---|
| 161 | } |
|---|
| 162 | free(file_name); |
|---|
| 163 | free(data); |
|---|
| 164 | } |
|---|
| 165 | static void ed_submit_parse_event(AW_window *aww) |
|---|
| 166 | { |
|---|
| 167 | AW_root *aw_root = aww->get_root(); |
|---|
| 168 | char *parser = aw_root->awar(AWAR_PARSER)->read_string(); |
|---|
| 169 | char *dest; |
|---|
| 170 | char *dest2; |
|---|
| 171 | char *privat; |
|---|
| 172 | char *p, *d; |
|---|
| 173 | int c; |
|---|
| 174 | |
|---|
| 175 | for (d = p = parser; *p; p++) { |
|---|
| 176 | if ((c=*p)==':') { |
|---|
| 177 | if (p[1] == '\n') p++; // skip newline |
|---|
| 178 | } |
|---|
| 179 | *(d++) = c; |
|---|
| 180 | } |
|---|
| 181 | *d = 0; |
|---|
| 182 | char *sub_file = aw_root->awar("submission/source")->read_string(); |
|---|
| 183 | char *source = GB_read_file(sub_file); |
|---|
| 184 | if (source) { |
|---|
| 185 | dest = GBS_string_eval(source, parser, 0); |
|---|
| 186 | if (!dest) dest = strdup(GB_await_error()); |
|---|
| 187 | } |
|---|
| 188 | else { |
|---|
| 189 | dest = GBS_global_string_copy("submission form not found\n(Reason: %s)", GB_await_error()); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | awtc_assert(dest); // should contain partly filled form or error message |
|---|
| 193 | |
|---|
| 194 | privat = aw_root->awar("submission/privat")->read_string(); |
|---|
| 195 | for (d = p = privat; *p; p++) { |
|---|
| 196 | if ((c=*p)==':') { |
|---|
| 197 | if (p[1] == '\n') p++; // skip newline |
|---|
| 198 | } |
|---|
| 199 | *(d++) = c; |
|---|
| 200 | } |
|---|
| 201 | *d = 0; |
|---|
| 202 | |
|---|
| 203 | dest2 = GBS_string_eval(dest, privat, 0); |
|---|
| 204 | if (!dest2) dest2 = strdup(GB_await_error()); |
|---|
| 205 | |
|---|
| 206 | aw_root->awar(AWAR_PARSED)->write_string(dest2); |
|---|
| 207 | |
|---|
| 208 | free(dest); |
|---|
| 209 | free(dest2); |
|---|
| 210 | free(privat); |
|---|
| 211 | free(source); |
|---|
| 212 | free (sub_file); |
|---|
| 213 | free(parser); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | static void ed_submit_gen_event(AW_window *aww) |
|---|
| 217 | { |
|---|
| 218 | AW_root *aw_root = aww->get_root(); |
|---|
| 219 | char buffer[256]; |
|---|
| 220 | char *name = aw_root->awar(AWAR_SPECIES_NAME)->read_string(); |
|---|
| 221 | sprintf(buffer, "%s.submit", name); |
|---|
| 222 | free(name); |
|---|
| 223 | aw_root->awar("submission/file")->write_string(buffer); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | AW_window *AWTC_create_submission_window(AW_root *root, GBDATA *gb_main) { |
|---|
| 228 | AW_window_simple *aws = new AW_window_simple; |
|---|
| 229 | aws->init(root, "SUBMISSION", "SUBMISSION"); |
|---|
| 230 | |
|---|
| 231 | aws->load_xfig("submiss.fig"); |
|---|
| 232 | aws->label_length(18); |
|---|
| 233 | aws->button_length(8); |
|---|
| 234 | |
|---|
| 235 | aws->at("close"); |
|---|
| 236 | aws->callback ((AW_CB0)AW_POPDOWN); |
|---|
| 237 | aws->create_button("CLOSE", "CLOSE", "O"); |
|---|
| 238 | |
|---|
| 239 | aws->callback(makeHelpCallback("submission.hlp")); |
|---|
| 240 | aws->at("help"); |
|---|
| 241 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 242 | |
|---|
| 243 | aws->button_length(15); |
|---|
| 244 | |
|---|
| 245 | aws->at("privat"); |
|---|
| 246 | aws->create_text_field("submission/privat", 80, 5); |
|---|
| 247 | |
|---|
| 248 | aws->at("parsed_info"); |
|---|
| 249 | aws->create_text_field(AWAR_PARSER, 80, 6); |
|---|
| 250 | |
|---|
| 251 | aws->at("parsed"); |
|---|
| 252 | aws->create_text_field(AWAR_PARSED, 80, 13); |
|---|
| 253 | |
|---|
| 254 | aws->at("species"); |
|---|
| 255 | aws->label("Species Name:"); |
|---|
| 256 | aws->create_input_field(AWAR_SPECIES_NAME, 12); |
|---|
| 257 | |
|---|
| 258 | aws->at("submission"); |
|---|
| 259 | { |
|---|
| 260 | StrArray submits; |
|---|
| 261 | GBS_read_dir(submits, GB_path_in_ARBLIB("submit"), NULL); |
|---|
| 262 | |
|---|
| 263 | if (!submits.empty()) { |
|---|
| 264 | aws->create_option_menu("submission/source", "Select a Form", "s"); |
|---|
| 265 | for (int i = 0; submits[i]; ++i) { |
|---|
| 266 | aws->insert_option(submits[i], "", submits[i]); |
|---|
| 267 | } |
|---|
| 268 | aws->insert_default_option("default", "d", "default"); |
|---|
| 269 | aws->update_option_menu(); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | aws->at("gen"); |
|---|
| 274 | aws->label("Gen File Name"); |
|---|
| 275 | aws->callback(ed_submit_gen_event); |
|---|
| 276 | aws->create_button("GEN_FILE_NAME", "CREATE NAME", "C"); |
|---|
| 277 | |
|---|
| 278 | aws->at("file"); |
|---|
| 279 | aws->label("or enter"); |
|---|
| 280 | aws->create_input_field("submission/file", 30); |
|---|
| 281 | |
|---|
| 282 | aws->at("info"); |
|---|
| 283 | aws->callback(ed_submit_info_event, (AW_CL)gb_main); |
|---|
| 284 | aws->create_button("READ_INFO", "READ INFO", "R"); |
|---|
| 285 | |
|---|
| 286 | aws->at("parse"); |
|---|
| 287 | aws->callback((AW_CB0)ed_submit_parse_event); |
|---|
| 288 | aws->create_button("FILL_OUT_FORM", "FILL THE FORM", "F"); |
|---|
| 289 | |
|---|
| 290 | aws->at("write"); |
|---|
| 291 | aws->callback((AW_CB)ed_save_var_to_file, (AW_CL)AWAR_PARSED, (AW_CL)"submission/file"); |
|---|
| 292 | aws->create_button("SAVE", "SAVE TO FILE", "S"); |
|---|
| 293 | |
|---|
| 294 | aws->button_length(20); |
|---|
| 295 | aws->at("edit"); |
|---|
| 296 | aws->callback(AW_POPUP, (AW_CL)create_calltexe_window, (AW_CL)"submission/source"); |
|---|
| 297 | aws->create_button("EDIT_FORM", "EDIT FORM", "R"); |
|---|
| 298 | |
|---|
| 299 | aws->at("editresult"); |
|---|
| 300 | aws->callback(AW_POPUP, (AW_CL)create_calltexe_window, (AW_CL)"submission/file"); |
|---|
| 301 | aws->create_button("EDIT_SAVED", "EDIT SAVED", "S"); |
|---|
| 302 | |
|---|
| 303 | aws->at("privatlabel"); |
|---|
| 304 | aws->create_button(0, "Your private data"); |
|---|
| 305 | |
|---|
| 306 | return aws; |
|---|
| 307 | } |
|---|