1 | #include <awti_imp_local.hxx> |
---|
2 | |
---|
3 | #include <awt.hxx> |
---|
4 | #include <awt_advice.hxx> |
---|
5 | #include <awt_sel_boxes.hxx> |
---|
6 | #include <awt_item_sel_list.hxx> |
---|
7 | |
---|
8 | #include <aw_global.hxx> |
---|
9 | #include <AW_rename.hxx> |
---|
10 | |
---|
11 | #include <GenomeImport.h> |
---|
12 | #include <GEN.hxx> |
---|
13 | |
---|
14 | #include <climits> |
---|
15 | |
---|
16 | using namespace std; |
---|
17 | |
---|
18 | static awtcig_struct awtcig; |
---|
19 | #define MAX_COMMENT_LINES 2000 |
---|
20 | |
---|
21 | inline const char *name_only(const char *fullpath) { |
---|
22 | char *lslash = strrchr(fullpath, '/'); |
---|
23 | return lslash ? lslash+1 : fullpath; |
---|
24 | } |
---|
25 | |
---|
26 | static char *awtc_fgets(char *s, int size, FILE *stream) { |
---|
27 | // same as fgets but also works with file in MACOS format |
---|
28 | int i; |
---|
29 | for (i = 0; i<(size-1); ++i) { |
---|
30 | int byte = fgetc(stream); |
---|
31 | if (byte == EOF) { |
---|
32 | if (i == 0) return 0; |
---|
33 | break; |
---|
34 | } |
---|
35 | |
---|
36 | s[i] = byte; |
---|
37 | if (byte == '\n' || byte == '\r') break; |
---|
38 | } |
---|
39 | s[i] = 0; |
---|
40 | |
---|
41 | return s; |
---|
42 | } |
---|
43 | |
---|
44 | bool awtc_read_string_pair(FILE *in, char *&s1, char *&s2, size_t& lineNr) { |
---|
45 | // helper function to read import/export filters. |
---|
46 | // returns true if sucessfully read |
---|
47 | // |
---|
48 | // 's1' is set to a heap-copy of the first token on line |
---|
49 | // 's2' is set to a heap-copy of the rest of the line (or NULL if only one token is present) |
---|
50 | // 'lineNr' is incremented with each line read |
---|
51 | |
---|
52 | s1 = 0; |
---|
53 | s2 = 0; |
---|
54 | |
---|
55 | const int BUFSIZE = 8000; |
---|
56 | char buffer[BUFSIZE]; |
---|
57 | char *res; |
---|
58 | |
---|
59 | do { |
---|
60 | res = awtc_fgets(&buffer[0], BUFSIZE-1, in); |
---|
61 | if (res) { |
---|
62 | char *p = buffer; |
---|
63 | |
---|
64 | lineNr++; |
---|
65 | |
---|
66 | while (*p == ' ' || *p == '\t') p++; |
---|
67 | if (*p != '#') { |
---|
68 | int len = strlen(p)-1; |
---|
69 | while (len >= 0 && strchr("\t \n\r", p[len])) { |
---|
70 | p[len--] = 0; |
---|
71 | } |
---|
72 | |
---|
73 | if (*p) { |
---|
74 | char *e = strpbrk(p," \t"); |
---|
75 | |
---|
76 | if (e) { |
---|
77 | *(e++) = 0; |
---|
78 | s1 = strdup(p); |
---|
79 | |
---|
80 | e += strspn(e, " \t"); // skip whitespace |
---|
81 | |
---|
82 | if (*e == '"') { |
---|
83 | char *k = strrchr(e,'"'); |
---|
84 | if (k!=e) { |
---|
85 | e++; |
---|
86 | *k=0; |
---|
87 | } |
---|
88 | } |
---|
89 | s2 = strdup(e); |
---|
90 | } |
---|
91 | else { |
---|
92 | s1 = strdup(p); |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | } while (res && !s1); // read until EOF or something found |
---|
98 | |
---|
99 | awti_assert(!res == !s1); |
---|
100 | |
---|
101 | return res; |
---|
102 | } |
---|
103 | |
---|
104 | static GB_ERROR not_in_match_error(const char *cmd) { |
---|
105 | return GBS_global_string("Command '%s' may only appear after 'MATCH'", cmd); |
---|
106 | } |
---|
107 | |
---|
108 | static GB_ERROR read_import_format(const char *fullfile, input_format_struct *ifo, bool *var_set, bool included) { |
---|
109 | GB_ERROR error = 0; |
---|
110 | FILE *in = fopen(fullfile,"rt"); |
---|
111 | |
---|
112 | input_format_per_line *pl = ifo->pl; |
---|
113 | |
---|
114 | if (!in) { |
---|
115 | const char *name = name_only(fullfile); |
---|
116 | |
---|
117 | if (included) { |
---|
118 | error = GB_export_IO_error("including", name); |
---|
119 | } |
---|
120 | else { |
---|
121 | error = strchr(name, '*') |
---|
122 | ? "Please use 'AUTO DETECT' or manually select an import format" |
---|
123 | : GB_export_IO_error("loading import filter", name); |
---|
124 | } |
---|
125 | } |
---|
126 | else { |
---|
127 | char *s1, *s2; |
---|
128 | size_t lineNumber = 0; |
---|
129 | bool include_error = false; |
---|
130 | |
---|
131 | while (!error && awtc_read_string_pair(in, s1, s2, lineNumber)) { |
---|
132 | |
---|
133 | #define GLOBAL_COMMAND(cmd) (!error && strcmp(s1, cmd) == 0) |
---|
134 | #define MATCH_COMMAND(cmd) (!error && strcmp(s1, cmd) == 0 && (pl || !(error = not_in_match_error(cmd)))) |
---|
135 | |
---|
136 | if (GLOBAL_COMMAND("MATCH")) { |
---|
137 | pl = new input_format_per_line; |
---|
138 | |
---|
139 | pl->defined_at = GBS_global_string_copy("%zi,%s", lineNumber, name_only(fullfile)); |
---|
140 | pl->next = ifo->pl; // this concatenates the filters to the front -> the list is reversed below |
---|
141 | ifo->pl = pl; |
---|
142 | pl->match = GBS_remove_escape(s2); |
---|
143 | pl->type = GB_STRING; |
---|
144 | |
---|
145 | if (ifo->autotag) pl->mtag = strdup(ifo->autotag); // will be overwritten by TAG command |
---|
146 | } |
---|
147 | else if (MATCH_COMMAND("SRT")) { reassign(pl->srt, s2); } |
---|
148 | else if (MATCH_COMMAND("ACI")) { reassign(pl->aci, s2); } |
---|
149 | else if (MATCH_COMMAND("WRITE")) { reassign(pl->write, s2); } |
---|
150 | else if (MATCH_COMMAND("WRITE_INT")) { reassign(pl->write, s2); pl->type = GB_INT; } |
---|
151 | else if (MATCH_COMMAND("WRITE_FLOAT")) { reassign(pl->write, s2); pl->type = GB_FLOAT; } |
---|
152 | else if (MATCH_COMMAND("APPEND")) { reassign(pl->append, s2); } |
---|
153 | else if (MATCH_COMMAND("SETVAR")) { |
---|
154 | if (strlen(s2) != 1 || s2[0]<'a' || s2[0]>'z') { |
---|
155 | error = "Allowed variable names are a-z"; |
---|
156 | } |
---|
157 | else { |
---|
158 | var_set[s2[0]-'a'] = true; |
---|
159 | reassign(pl->setvar, s2); |
---|
160 | } |
---|
161 | } |
---|
162 | else if (MATCH_COMMAND("TAG")) { |
---|
163 | if (s2[0]) reassign(pl->mtag, s2); |
---|
164 | else error = "Empty TAG is not allowed"; |
---|
165 | } |
---|
166 | else if (GLOBAL_COMMAND("AUTODETECT")) { reassign(ifo->autodetect, s2); } |
---|
167 | else if (GLOBAL_COMMAND("SYSTEM")) { reassign(ifo->system, s2); } |
---|
168 | else if (GLOBAL_COMMAND("NEW_FORMAT")) { reassign(ifo->new_format, s2); ifo->new_format_lineno = lineNumber; } |
---|
169 | else if (GLOBAL_COMMAND("BEGIN")) { reassign(ifo->begin, s2); } |
---|
170 | else if (GLOBAL_COMMAND("FILETAG")) { reassign(ifo->filetag, s2); } |
---|
171 | else if (GLOBAL_COMMAND("SEQUENCESRT")) { reassign(ifo->sequencesrt, s2); } |
---|
172 | else if (GLOBAL_COMMAND("SEQUENCEACI")) { reassign(ifo->sequenceaci, s2); } |
---|
173 | else if (GLOBAL_COMMAND("SEQUENCEEND")) { reassign(ifo->sequenceend, s2); } |
---|
174 | else if (GLOBAL_COMMAND("END")) { reassign(ifo->end, s2); } |
---|
175 | else if (GLOBAL_COMMAND("AUTOTAG")) { reassign(ifo->autotag, s2); } |
---|
176 | else if (GLOBAL_COMMAND("SEQUENCESTART")) { reassign(ifo->sequencestart, s2); ifo->read_this_sequence_line_too = 1; } |
---|
177 | else if (GLOBAL_COMMAND("SEQUENCEAFTER")) { reassign(ifo->sequencestart, s2); ifo->read_this_sequence_line_too = 0; } |
---|
178 | else if (GLOBAL_COMMAND("KEYWIDTH")) { ifo->tab = atoi(s2); } |
---|
179 | else if (GLOBAL_COMMAND("SEQUENCECOLUMN")) { ifo->sequencecolumn = atoi(s2); } |
---|
180 | else if (GLOBAL_COMMAND("CREATE_ACC_FROM_SEQUENCE")) { ifo->autocreateacc = 1; } |
---|
181 | else if (GLOBAL_COMMAND("DONT_GEN_NAMES")) { ifo->noautonames = 1; } |
---|
182 | else if (GLOBAL_COMMAND("INCLUDE")) { |
---|
183 | char *dir = AWT_extract_directory(fullfile); |
---|
184 | char *includeFile = GBS_global_string_copy("%s/%s", dir, s2); |
---|
185 | |
---|
186 | error = read_import_format(includeFile, ifo, var_set, true); |
---|
187 | if (error) include_error = true; |
---|
188 | |
---|
189 | free(includeFile); |
---|
190 | free(dir); |
---|
191 | } |
---|
192 | else { |
---|
193 | bool ifnotset = GLOBAL_COMMAND("IFNOTSET"); |
---|
194 | bool setglobal = GLOBAL_COMMAND("SETGLOBAL"); |
---|
195 | |
---|
196 | if (ifnotset || setglobal) { |
---|
197 | if (s2[0]<'a' || s2[0]>'z') { |
---|
198 | error = "Allowed variable names are a-z"; |
---|
199 | } |
---|
200 | else { |
---|
201 | int off = 1; |
---|
202 | while (isblank(s2[off])) off++; |
---|
203 | if (!s2[off]) error = GBS_global_string("Expected two arguments in '%s'", s2); |
---|
204 | else { |
---|
205 | char varname = s2[0]; |
---|
206 | const char *arg2 = s2+off; |
---|
207 | |
---|
208 | if (ifnotset) { |
---|
209 | if (ifo->variable_errors.get(varname)) { |
---|
210 | error = GBS_global_string("Redefinition of IFNOTSET %c", varname); |
---|
211 | } |
---|
212 | else { |
---|
213 | ifo->variable_errors.set(varname, arg2); |
---|
214 | } |
---|
215 | } |
---|
216 | else { |
---|
217 | awti_assert(setglobal); |
---|
218 | ifo->global_variables.set(varname, arg2); |
---|
219 | var_set[varname-'a'] = true; |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | else if (!error) { |
---|
225 | error = GBS_global_string("Unknown command '%s'", s1); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | free(s1); |
---|
230 | free(s2); |
---|
231 | } |
---|
232 | |
---|
233 | if (error) { |
---|
234 | error = GBS_global_string("%sin line %zi of %s '%s':\n%s", |
---|
235 | include_error ? "included " : "", |
---|
236 | lineNumber, |
---|
237 | included ? "file" : "import format", |
---|
238 | name_only(fullfile), |
---|
239 | error); |
---|
240 | } |
---|
241 | |
---|
242 | fclose(in); |
---|
243 | |
---|
244 | #undef MATCH_COMMAND |
---|
245 | #undef GLOBAL_COMMAND |
---|
246 | } |
---|
247 | |
---|
248 | return error; |
---|
249 | } |
---|
250 | |
---|
251 | GB_ERROR awtc_read_import_format(const char *file) { |
---|
252 | char *fullfile = AWT_unfold_path(file,"ARBHOME"); |
---|
253 | |
---|
254 | delete awtcig.ifo; |
---|
255 | awtcig.ifo = new input_format_struct; |
---|
256 | |
---|
257 | bool var_set[IFS_VARIABLES]; |
---|
258 | for (int i = 0; i<IFS_VARIABLES; i++) var_set[i] = false; |
---|
259 | |
---|
260 | GB_ERROR error = read_import_format(fullfile, awtcig.ifo, var_set, false); |
---|
261 | |
---|
262 | |
---|
263 | for (int i = 0; i<IFS_VARIABLES && !error; i++) { |
---|
264 | bool ifnotset = awtcig.ifo->variable_errors.get(i+'a'); |
---|
265 | if (var_set[i]) { |
---|
266 | bool isglobal = awtcig.ifo->global_variables.get(i+'a'); |
---|
267 | if (!ifnotset && !isglobal) { // don't warn if variable is global |
---|
268 | error = GBS_global_string("Warning: missing IFNOTSET for variable '%c'", 'a'+i); |
---|
269 | } |
---|
270 | } |
---|
271 | else { |
---|
272 | if (ifnotset) { |
---|
273 | error = GBS_global_string("Warning: useless IFNOTSET for unused variable '%c'", 'a'+i); |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | // reverse order of match list (was appended backwards during creation) |
---|
279 | if (awtcig.ifo->pl) awtcig.ifo->pl = awtcig.ifo->pl->reverse(0); |
---|
280 | |
---|
281 | free(fullfile); |
---|
282 | |
---|
283 | return error; |
---|
284 | } |
---|
285 | |
---|
286 | input_format_per_line::input_format_per_line() { |
---|
287 | memset((char *)this,0,sizeof(*this)); |
---|
288 | } |
---|
289 | |
---|
290 | input_format_per_line::~input_format_per_line() { |
---|
291 | free(match); |
---|
292 | free(aci); |
---|
293 | free(srt); |
---|
294 | free(mtag); |
---|
295 | free(append); |
---|
296 | free(write); |
---|
297 | free(setvar); |
---|
298 | free(defined_at); |
---|
299 | |
---|
300 | delete next; |
---|
301 | } |
---|
302 | |
---|
303 | input_format_struct::input_format_struct() { |
---|
304 | memset((char *)this,0,sizeof(*this)); |
---|
305 | } |
---|
306 | |
---|
307 | input_format_struct::~input_format_struct() { |
---|
308 | free(autodetect); |
---|
309 | free(system); |
---|
310 | free(new_format); |
---|
311 | free(begin); |
---|
312 | free(sequencestart); |
---|
313 | free(sequenceend); |
---|
314 | free(sequencesrt); |
---|
315 | free(sequenceaci); |
---|
316 | free(filetag); |
---|
317 | free(autotag); |
---|
318 | free(end); |
---|
319 | free(b1); |
---|
320 | free(b2); |
---|
321 | |
---|
322 | delete pl; |
---|
323 | } |
---|
324 | |
---|
325 | void awtc_check_input_format(AW_window *aww) { |
---|
326 | AW_root *root = aww->get_root(); |
---|
327 | char **files = GBS_read_dir(GB_path_in_ARBLIB("import", NULL), "*.ift"); |
---|
328 | char buffer[AWTC_IMPORT_CHECK_BUFFER_SIZE+10]; |
---|
329 | GB_ERROR error = 0; |
---|
330 | |
---|
331 | int matched = -1; |
---|
332 | int first = -1; |
---|
333 | int matched_count = 0; |
---|
334 | |
---|
335 | AW_awar *awar_filter = root->awar(AWAR_FORM"/file_name"); |
---|
336 | char *prev_selected = awar_filter->read_string(); |
---|
337 | |
---|
338 | for (int idx = 0; files[idx] && !error; ++idx) { |
---|
339 | char *filtername = files[idx]; |
---|
340 | awar_filter->write_string(filtername); |
---|
341 | |
---|
342 | GB_ERROR form_err = awtc_read_import_format(filtername); |
---|
343 | if (form_err) { |
---|
344 | aw_message(form_err); |
---|
345 | } |
---|
346 | else { |
---|
347 | if (awtcig.ifo->autodetect) { // detectable |
---|
348 | char *autodetect = GBS_remove_escape(awtcig.ifo->autodetect); |
---|
349 | delete awtcig.ifo; awtcig.ifo = 0; |
---|
350 | |
---|
351 | FILE *in = 0; |
---|
352 | { |
---|
353 | char *f = root->awar(AWAR_FILE)->read_string(); |
---|
354 | |
---|
355 | if (f[0]) { |
---|
356 | const char *com = GBS_global_string("cat %s 2>/dev/null", f); |
---|
357 | in = popen(com,"r"); |
---|
358 | } |
---|
359 | free(f); |
---|
360 | if (!in) error = "Cannot open any input file"; |
---|
361 | } |
---|
362 | if (in) { |
---|
363 | int size = fread(buffer,1,AWTC_IMPORT_CHECK_BUFFER_SIZE,in); |
---|
364 | pclose(in); |
---|
365 | if (size>=0){ |
---|
366 | buffer[size] = 0; |
---|
367 | if (GBS_string_matches(buffer,autodetect,GB_MIND_CASE)) { |
---|
368 | // format found! |
---|
369 | matched_count++; |
---|
370 | if (matched == -1) matched = idx; // remember first/next found format |
---|
371 | if (first == -1) first = idx; // remember first found format |
---|
372 | |
---|
373 | if (strcmp(filtername, prev_selected) == 0) { // previously selected filter |
---|
374 | matched = -1; // select next (or first.. see below) |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | free(autodetect); |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | const char *select = 0; |
---|
385 | switch (matched_count) { |
---|
386 | case 0: |
---|
387 | AWT_advice("Not all formats can be auto-detected.\n" |
---|
388 | "Some need to be selected manually.", |
---|
389 | AWT_ADVICE_TOGGLE, |
---|
390 | "No format auto-detected", |
---|
391 | "arb_import.hlp"); |
---|
392 | |
---|
393 | select = "unknown.ift"; |
---|
394 | break; |
---|
395 | |
---|
396 | default: |
---|
397 | AWT_advice("Several import filters matched during auto-detection.\n" |
---|
398 | "Click 'AUTO DETECT' again to select next matching import-filter.", |
---|
399 | AWT_ADVICE_TOGGLE, |
---|
400 | "Several formats detected", |
---|
401 | "arb_import.hlp"); |
---|
402 | |
---|
403 | // fall-through |
---|
404 | case 1: |
---|
405 | if (matched == -1) { |
---|
406 | // wrap around to top (while searching next matching filter) |
---|
407 | // or re-select the one matching filter (if it was previously selected) |
---|
408 | awti_assert(first != -1); |
---|
409 | matched = first; |
---|
410 | } |
---|
411 | awti_assert(matched != -1); |
---|
412 | select = files[matched]; // select 1st matching filter |
---|
413 | break; |
---|
414 | } |
---|
415 | |
---|
416 | if (error) { |
---|
417 | select = "unknown.ift"; |
---|
418 | aw_message(error); |
---|
419 | } |
---|
420 | |
---|
421 | free(prev_selected); |
---|
422 | awar_filter->write_string(select); |
---|
423 | GBT_free_names(files); |
---|
424 | } |
---|
425 | |
---|
426 | static int awtc_next_file(void) { |
---|
427 | if (awtcig.in) fclose(awtcig.in); |
---|
428 | if (!awtcig.current_file) awtcig.current_file = awtcig.filenames; |
---|
429 | |
---|
430 | int result = 1; |
---|
431 | while (result == 1 && *awtcig.current_file) { |
---|
432 | const char *origin_file_name = *(awtcig.current_file++); |
---|
433 | const char *sorigin = strrchr(origin_file_name,'/'); |
---|
434 | if (!sorigin) sorigin = origin_file_name; |
---|
435 | else sorigin++; |
---|
436 | |
---|
437 | GB_ERROR error = 0; |
---|
438 | char *mid_file_name = 0; |
---|
439 | char *dest_file_name = 0; |
---|
440 | |
---|
441 | if (awtcig.ifo2 && awtcig.ifo2->system) { |
---|
442 | { |
---|
443 | const char *sorigin_nameonly = strrchr(sorigin, '/'); |
---|
444 | if (!sorigin_nameonly) sorigin_nameonly = sorigin; |
---|
445 | |
---|
446 | char *mid_name = GB_unique_filename(sorigin_nameonly, "tmp"); |
---|
447 | mid_file_name = GB_create_tempfile(mid_name); |
---|
448 | free(mid_name); |
---|
449 | |
---|
450 | if (!mid_file_name) error = GB_await_error(); |
---|
451 | } |
---|
452 | |
---|
453 | if (!error) { |
---|
454 | char *srt = GBS_global_string_copy("$<=%s:$>=%s", origin_file_name, mid_file_name); |
---|
455 | char *sys = GBS_string_eval(awtcig.ifo2->system, srt, 0); |
---|
456 | |
---|
457 | aw_status(GBS_global_string("exec '%s'", awtcig.ifo2->system)); |
---|
458 | |
---|
459 | error = GB_system(sys); |
---|
460 | if (!error) origin_file_name = mid_file_name; |
---|
461 | |
---|
462 | free(sys); |
---|
463 | free(srt); |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | if (!error && awtcig.ifo->system) { |
---|
468 | { |
---|
469 | const char *sorigin_nameonly = strrchr(sorigin, '/'); |
---|
470 | if (!sorigin_nameonly) sorigin_nameonly = sorigin; |
---|
471 | |
---|
472 | char *dest_name = GB_unique_filename(sorigin_nameonly, "tmp"); |
---|
473 | dest_file_name = GB_create_tempfile(dest_name); |
---|
474 | free(dest_name); |
---|
475 | |
---|
476 | if (!dest_file_name) error = GB_await_error(); |
---|
477 | } |
---|
478 | |
---|
479 | if (!error) { |
---|
480 | char *srt = GBS_global_string_copy("$<=%s:$>=%s", origin_file_name, dest_file_name); |
---|
481 | char *sys = GBS_string_eval(awtcig.ifo->system, srt, 0); |
---|
482 | |
---|
483 | aw_status(GBS_global_string("Converting File %s", awtcig.ifo->system)); |
---|
484 | |
---|
485 | error = GB_system(sys); |
---|
486 | if (!error) origin_file_name = dest_file_name; |
---|
487 | |
---|
488 | free(sys); |
---|
489 | free(srt); |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | if (!error) { |
---|
494 | awtcig.in = fopen(origin_file_name,"r"); |
---|
495 | |
---|
496 | if (awtcig.in) { |
---|
497 | result = 0; |
---|
498 | } |
---|
499 | else { |
---|
500 | error = GBS_global_string("Error: Cannot open file %s\n", awtcig.current_file[-1]); |
---|
501 | } |
---|
502 | } |
---|
503 | |
---|
504 | if (mid_file_name) { |
---|
505 | gb_assert(GB_is_privatefile(mid_file_name, GB_FALSE)); |
---|
506 | GB_unlink_or_warn(mid_file_name, &error); |
---|
507 | free(mid_file_name); |
---|
508 | } |
---|
509 | if (dest_file_name) { |
---|
510 | GB_unlink_or_warn(dest_file_name, &error); |
---|
511 | free(dest_file_name); |
---|
512 | } |
---|
513 | |
---|
514 | if (error) aw_message(error); |
---|
515 | } |
---|
516 | |
---|
517 | return result; |
---|
518 | } |
---|
519 | char *awtc_read_line(int tab,char *sequencestart, char *sequenceend){ |
---|
520 | /* two modes: tab == 0 -> read single lines, |
---|
521 | different files are seperated by sequenceend, |
---|
522 | tab != 0 join lines that start after position tab, |
---|
523 | joined lines are seperated by '|' |
---|
524 | except lines that match sequencestart |
---|
525 | (they may be part of sequence if read_this_sequence_line_too = 1 */ |
---|
526 | |
---|
527 | static char *in_queue = 0; // read data |
---|
528 | static int b2offset = 0; |
---|
529 | const int BUFSIZE = 8000; |
---|
530 | const char *SEPERATOR = "|"; // line seperator |
---|
531 | struct input_format_struct *ifo; |
---|
532 | ifo = awtcig.ifo; |
---|
533 | char *p; |
---|
534 | |
---|
535 | if (!ifo->b1) ifo->b1 = (char*)calloc(BUFSIZE,1); |
---|
536 | if (!ifo->b2) ifo->b2 = (char*)calloc(BUFSIZE,1); |
---|
537 | if (!awtcig.in){ |
---|
538 | if (awtc_next_file()) { |
---|
539 | if (in_queue) { |
---|
540 | char *s = in_queue; |
---|
541 | in_queue = 0; |
---|
542 | return s; |
---|
543 | } |
---|
544 | return 0; |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | |
---|
549 | if (!tab) { |
---|
550 | if (in_queue) { |
---|
551 | char *s = in_queue; |
---|
552 | in_queue = 0; |
---|
553 | return s; |
---|
554 | } |
---|
555 | p = awtc_fgets(ifo->b1, BUFSIZE-3, awtcig.in); |
---|
556 | if (!p){ |
---|
557 | sprintf(ifo->b1,"%s",sequenceend); |
---|
558 | if (awtcig.in) fclose(awtcig.in);awtcig.in= 0; |
---|
559 | p = ifo->b1; |
---|
560 | } |
---|
561 | int len = strlen(p)-1; |
---|
562 | while (len>=0){ |
---|
563 | if (p[len] =='\n' || p[len] == 13) p[len--] = 0; |
---|
564 | else break; |
---|
565 | } |
---|
566 | return ifo->b1; |
---|
567 | } |
---|
568 | |
---|
569 | b2offset = 0; |
---|
570 | ifo->b2[0] = 0; |
---|
571 | |
---|
572 | if (in_queue) { |
---|
573 | b2offset = 0; |
---|
574 | strncpy(ifo->b2+b2offset, in_queue, BUFSIZE - 4- b2offset); |
---|
575 | b2offset += strlen(ifo->b2+b2offset); |
---|
576 | in_queue = 0; |
---|
577 | if (GBS_string_matches(ifo->b2,sequencestart,GB_MIND_CASE)) return ifo->b2; |
---|
578 | } |
---|
579 | while (1) { |
---|
580 | p = awtc_fgets(ifo->b1, BUFSIZE-3, awtcig.in); |
---|
581 | if (!p){ |
---|
582 | if (awtcig.in) fclose(awtcig.in);awtcig.in= 0; |
---|
583 | break; |
---|
584 | } |
---|
585 | int len = strlen(p)-1; |
---|
586 | while (len>=0){ |
---|
587 | if (p[len] =='\n' || p[len] == 13) p[len--] = 0; |
---|
588 | else break; |
---|
589 | } |
---|
590 | |
---|
591 | |
---|
592 | if (GBS_string_matches(ifo->b1,sequencestart,GB_MIND_CASE)){ |
---|
593 | in_queue = ifo->b1; |
---|
594 | return ifo->b2; |
---|
595 | } |
---|
596 | |
---|
597 | int i; |
---|
598 | for (i=0;i<=tab;i++) if (ifo->b1[i] != ' ') break; |
---|
599 | |
---|
600 | if (i < tab) { |
---|
601 | in_queue = ifo->b1; |
---|
602 | return ifo->b2; |
---|
603 | } |
---|
604 | strncpy(ifo->b2+b2offset, SEPERATOR, BUFSIZE - 4- b2offset); |
---|
605 | b2offset += strlen(ifo->b2+b2offset); |
---|
606 | |
---|
607 | p = ifo->b1; |
---|
608 | if (b2offset>0) while (*p==' ') p++; // delete spaces in second line |
---|
609 | |
---|
610 | strncpy(ifo->b2+b2offset, p, BUFSIZE - 4- b2offset); |
---|
611 | b2offset += strlen(ifo->b2+b2offset); |
---|
612 | } |
---|
613 | in_queue = 0; |
---|
614 | return ifo->b2; |
---|
615 | } |
---|
616 | |
---|
617 | static void awtc_write_entry(GBDATA *gbd,const char *key,char *str,const char *tag,int append, GB_TYPES type = GB_STRING) |
---|
618 | { |
---|
619 | GBDATA *gbk; |
---|
620 | int len, taglen; |
---|
621 | char *buf; |
---|
622 | if (!gbd) return; |
---|
623 | while (str[0] == ' '|| str[0] == '\t'|| str[0] == '|') str++; |
---|
624 | len = strlen(str) -1; |
---|
625 | while (len >=0 && |
---|
626 | (str[len] == ' ' || str[len] == '\t' || str[len] == '|' || str[len] == 13)) |
---|
627 | { |
---|
628 | len--; |
---|
629 | } |
---|
630 | str[len+1] = 0; |
---|
631 | if (!str[0]) return; |
---|
632 | |
---|
633 | gbk = GB_entry(gbd,key); |
---|
634 | |
---|
635 | if (type != GB_STRING) { |
---|
636 | if (!gbk) gbk=GB_create(gbd,key,type); |
---|
637 | switch(type) { |
---|
638 | case GB_INT: |
---|
639 | GB_write_int(gbk, atoi(str)); |
---|
640 | break; |
---|
641 | case GB_FLOAT: |
---|
642 | GB_write_float(gbk, atof(str)); |
---|
643 | break; |
---|
644 | default: |
---|
645 | awti_assert(0); |
---|
646 | break; |
---|
647 | } |
---|
648 | return; |
---|
649 | } |
---|
650 | |
---|
651 | if (!gbk || !append){ |
---|
652 | if (!gbk) gbk=GB_create(gbd,key,GB_STRING); |
---|
653 | |
---|
654 | if (tag){ |
---|
655 | GBS_strstruct *s = GBS_stropen(10000); |
---|
656 | GBS_chrcat(s,'['); |
---|
657 | GBS_strcat(s,tag); |
---|
658 | GBS_strcat(s,"] "); |
---|
659 | GBS_strcat(s,str); |
---|
660 | char *val = GBS_strclose(s); |
---|
661 | GB_write_string(gbk,val); |
---|
662 | free(val); |
---|
663 | } |
---|
664 | else { |
---|
665 | if (strcmp(key,"name") == 0) { |
---|
666 | char *nstr = GBT_create_unique_species_name(awtcig.gb_main,str); |
---|
667 | GB_write_string(gbk,nstr); |
---|
668 | free(nstr); |
---|
669 | } |
---|
670 | else { |
---|
671 | GB_write_string(gbk,str); |
---|
672 | } |
---|
673 | } |
---|
674 | return; |
---|
675 | } |
---|
676 | |
---|
677 | const char *strin = GB_read_char_pntr(gbk); |
---|
678 | len = strlen(str) + strlen(strin); |
---|
679 | taglen = tag ? (strlen(tag)+2) : 0; |
---|
680 | buf = (char *)GB_calloc(sizeof(char),len+2+taglen+1); |
---|
681 | |
---|
682 | if (tag) { |
---|
683 | char *regexp = (char*)GB_calloc(sizeof(char), taglen+3); |
---|
684 | sprintf(regexp, "*[%s]*", tag); |
---|
685 | |
---|
686 | if (!GBS_string_matches(strin, regexp, GB_IGNORE_CASE)) { // if tag does not exist yet |
---|
687 | sprintf(buf,"%s [%s] %s", strin, tag, str); // prefix with tag |
---|
688 | } |
---|
689 | free(regexp); |
---|
690 | } |
---|
691 | |
---|
692 | if (buf[0] == 0) { |
---|
693 | sprintf(buf,"%s %s",strin,str); |
---|
694 | } |
---|
695 | |
---|
696 | GB_write_string(gbk,buf); |
---|
697 | free(buf); |
---|
698 | return; |
---|
699 | } |
---|
700 | |
---|
701 | static string expandSetVariables(const SetVariables& variables, const string& source, bool& error_occurred, const input_format_struct *ifo) { |
---|
702 | string dest; |
---|
703 | string::const_iterator norm_start = source.begin(); |
---|
704 | string::const_iterator p = norm_start; |
---|
705 | error_occurred = false; |
---|
706 | |
---|
707 | while (p != source.end()) { |
---|
708 | if (*p == '$') { |
---|
709 | ++p; |
---|
710 | if (*p == '$') { // '$$' -> '$' |
---|
711 | dest.append(1, *p); |
---|
712 | } |
---|
713 | else { // real variable |
---|
714 | const string *value = variables.get(*p); |
---|
715 | if (!value) { |
---|
716 | const string *user_error = ifo->variable_errors.get(*p); |
---|
717 | |
---|
718 | char *error = 0; |
---|
719 | if (user_error) { |
---|
720 | error = GBS_global_string_copy("%s (variable '$%c' not set yet)", user_error->c_str(), *p); |
---|
721 | } |
---|
722 | else { |
---|
723 | error = GBS_global_string_copy("Variable '$%c' not set (missing SETVAR or SETGLOBAL?)", *p); |
---|
724 | } |
---|
725 | |
---|
726 | dest.append(GBS_global_string("<%s>", error)); |
---|
727 | GB_export_error(error); |
---|
728 | free(error); |
---|
729 | error_occurred = true; |
---|
730 | } |
---|
731 | else { |
---|
732 | dest.append(*value); |
---|
733 | } |
---|
734 | } |
---|
735 | ++p; |
---|
736 | } |
---|
737 | else { |
---|
738 | dest.append(1, *p); |
---|
739 | ++p; |
---|
740 | } |
---|
741 | } |
---|
742 | return dest; |
---|
743 | } |
---|
744 | |
---|
745 | GB_ERROR awtc_read_data(char *ali_name, int security_write) |
---|
746 | { |
---|
747 | char num[6]; |
---|
748 | char text[100]; |
---|
749 | static int counter = 0; |
---|
750 | GBDATA *gb_species_data = GB_search(GB_MAIN,"species_data",GB_CREATE_CONTAINER); |
---|
751 | GBDATA *gb_species; |
---|
752 | char *p; |
---|
753 | |
---|
754 | input_format_struct *ifo = awtcig.ifo; |
---|
755 | input_format_per_line *pl = 0; |
---|
756 | |
---|
757 | while (1){ // go to the start |
---|
758 | p = awtc_read_line(0,ifo->sequencestart,ifo->sequenceend); |
---|
759 | if (!p || !ifo->begin || GBS_string_matches(p,ifo->begin,GB_MIND_CASE)) break; |
---|
760 | } |
---|
761 | if (!p) return "Cannot find start of file: Wrong format or empty file"; |
---|
762 | |
---|
763 | // lets start !!!!! |
---|
764 | while(p) { |
---|
765 | SetVariables variables(ifo->global_variables); |
---|
766 | |
---|
767 | counter++; |
---|
768 | sprintf(text,"Reading species %i",counter); |
---|
769 | if (counter %10 == 0){ |
---|
770 | if (aw_status(text)) break; |
---|
771 | } |
---|
772 | |
---|
773 | gb_species = GB_create_container(gb_species_data,"species"); |
---|
774 | sprintf(text,"spec%i",counter); |
---|
775 | GBT_readOrCreate_char_pntr(gb_species,"name",text); // set default if missing |
---|
776 | if ( awtcig.filenames[1] ) { // multiple files !!! |
---|
777 | char *f= strrchr(awtcig.current_file[-1],'/'); |
---|
778 | if (!f) f= awtcig.current_file[-1]; |
---|
779 | else f++; |
---|
780 | awtc_write_entry(gb_species,"file",f,ifo->filetag,0); |
---|
781 | } |
---|
782 | int line; |
---|
783 | static bool never_warn = false; |
---|
784 | int max_line = never_warn ? INT_MAX : MAX_COMMENT_LINES; |
---|
785 | |
---|
786 | for(line=0;line<=max_line;line++){ |
---|
787 | sprintf(num,"%i ",line); |
---|
788 | if (line == max_line){ |
---|
789 | char *file = 0; |
---|
790 | if (awtcig.current_file[0]) file = awtcig.current_file[0]; |
---|
791 | GB_ERROR msg = GB_export_errorf("A database entry in file '%s' is longer than %i lines.\n" |
---|
792 | " This might be the result of a wrong input format\n" |
---|
793 | " or a long comment in a sequence\n",file,line); |
---|
794 | |
---|
795 | switch (aw_question(msg,"Continue Reading,Continue Reading (Never ask again),Abort")) { |
---|
796 | case 0: |
---|
797 | max_line *= 2; |
---|
798 | break; |
---|
799 | case 1: |
---|
800 | max_line = INT_MAX; |
---|
801 | never_warn = true; |
---|
802 | break; |
---|
803 | case 2: |
---|
804 | break; |
---|
805 | } |
---|
806 | } |
---|
807 | GB_ERROR error = 0; |
---|
808 | if (strlen(p) > ifo->tab){ |
---|
809 | for (pl = ifo->pl; !error && pl; pl=pl->next) { |
---|
810 | const char *what_error = 0; |
---|
811 | if (GBS_string_matches(p,pl->match,GB_MIND_CASE)){ |
---|
812 | char *dup = p+ifo->tab; |
---|
813 | while (*dup == ' ' || *dup == '\t') dup++; |
---|
814 | |
---|
815 | char *s = 0; |
---|
816 | char *dele = 0; |
---|
817 | |
---|
818 | if (pl->srt){ |
---|
819 | bool err_flag; |
---|
820 | string expanded = expandSetVariables(variables, pl->srt, err_flag, ifo); |
---|
821 | if (err_flag) error = GB_await_error(); |
---|
822 | else { |
---|
823 | dele = s = GBS_string_eval(dup,expanded.c_str(),gb_species); |
---|
824 | if (!s) error = GB_await_error(); |
---|
825 | } |
---|
826 | if (error) what_error = "SRT"; |
---|
827 | } |
---|
828 | else { |
---|
829 | s = dup; |
---|
830 | } |
---|
831 | |
---|
832 | if (!error && pl->aci){ |
---|
833 | bool err_flag; |
---|
834 | string expanded = expandSetVariables(variables, pl->aci, err_flag, ifo); |
---|
835 | if (err_flag) error = GB_await_error(); |
---|
836 | else { |
---|
837 | dup = dele; |
---|
838 | dele = s = GB_command_interpreter(GB_MAIN, s,expanded.c_str(),gb_species, 0); |
---|
839 | if (!s) error = GB_await_error(); |
---|
840 | free(dup); |
---|
841 | } |
---|
842 | if (error) what_error = "ACI"; |
---|
843 | } |
---|
844 | |
---|
845 | if (!error && (pl->append || pl->write)) { |
---|
846 | char *field = 0; |
---|
847 | char *tag = 0; |
---|
848 | |
---|
849 | { |
---|
850 | bool err_flag; |
---|
851 | string expanded_field = expandSetVariables(variables, string(pl->append ? pl->append : pl->write), err_flag, ifo); |
---|
852 | if (err_flag) error = GB_await_error(); |
---|
853 | else field = GBS_string_2_key(expanded_field.c_str()); |
---|
854 | if (error) what_error = "APPEND or WRITE"; |
---|
855 | } |
---|
856 | |
---|
857 | if (!error && pl->mtag) { |
---|
858 | bool err_flag; |
---|
859 | string expanded_tag = expandSetVariables(variables, string(pl->mtag), err_flag, ifo); |
---|
860 | if (err_flag) error = GB_await_error(); |
---|
861 | else tag = GBS_string_2_key(expanded_tag.c_str()); |
---|
862 | if (error) what_error = "TAG"; |
---|
863 | } |
---|
864 | |
---|
865 | if (!error) { |
---|
866 | awtc_write_entry(gb_species, field, s, tag, pl->append != 0, pl->type); |
---|
867 | } |
---|
868 | free(tag); |
---|
869 | free(field); |
---|
870 | } |
---|
871 | |
---|
872 | if (!error && pl->setvar) variables.set(pl->setvar[0], s); |
---|
873 | free(dele); |
---|
874 | } |
---|
875 | |
---|
876 | if (error) { |
---|
877 | error = GBS_global_string("'%s'\nin %s of MATCH (defined at #%s)", error, what_error, pl->defined_at); |
---|
878 | } |
---|
879 | } |
---|
880 | } |
---|
881 | |
---|
882 | if (error) { |
---|
883 | return GBS_global_string("%s\nwhile parsing line #%i of species #%i", error, line, counter); |
---|
884 | } |
---|
885 | |
---|
886 | if (GBS_string_matches(p,ifo->sequencestart,GB_MIND_CASE)) goto read_sequence; |
---|
887 | |
---|
888 | p = awtc_read_line(ifo->tab,ifo->sequencestart,ifo->sequenceend); |
---|
889 | if (!p) break; |
---|
890 | } |
---|
891 | return GB_export_errorf("No Start of Sequence found (%i lines read)", max_line); |
---|
892 | |
---|
893 | read_sequence: |
---|
894 | { |
---|
895 | char *sequence; |
---|
896 | GBS_strstruct *strstruct = GBS_stropen(5000); |
---|
897 | int linecnt; |
---|
898 | |
---|
899 | for(linecnt = 0;;linecnt++) { |
---|
900 | if (linecnt || !ifo->read_this_sequence_line_too){ |
---|
901 | p = awtc_read_line(0,ifo->sequencestart,ifo->sequenceend); |
---|
902 | } |
---|
903 | if (!p) break; |
---|
904 | if (ifo->sequenceend && GBS_string_matches(p,ifo->sequenceend,GB_MIND_CASE)) break; |
---|
905 | if (strlen(p) <= ifo->sequencecolumn) continue; |
---|
906 | GBS_strcat(strstruct,p+ifo->sequencecolumn); |
---|
907 | } |
---|
908 | sequence = GBS_strclose(strstruct); |
---|
909 | |
---|
910 | GBDATA *gb_data = GBT_create_sequence_data(gb_species, ali_name, "data", GB_STRING, security_write); |
---|
911 | if (ifo->sequencesrt) { |
---|
912 | char *h = GBS_string_eval(sequence,ifo->sequencesrt,gb_species); |
---|
913 | if (!h) return GB_await_error(); |
---|
914 | freeset(sequence, h); |
---|
915 | } |
---|
916 | |
---|
917 | if (ifo->sequenceaci) { |
---|
918 | char *h = GB_command_interpreter(GB_MAIN, sequence,ifo->sequenceaci,gb_species, 0); |
---|
919 | free(sequence); |
---|
920 | if (!h) return GB_await_error(); |
---|
921 | sequence = h; |
---|
922 | } |
---|
923 | |
---|
924 | GB_write_string(gb_data,sequence); |
---|
925 | |
---|
926 | GBDATA *gb_acc = GB_search(gb_species,"acc",GB_FIND); |
---|
927 | if (!gb_acc && ifo->autocreateacc) { |
---|
928 | char buf[100]; |
---|
929 | long id = GBS_checksum(sequence,1,".-"); |
---|
930 | sprintf(buf,"ARB_%lX",id); |
---|
931 | gb_acc = GB_search(gb_species,"acc",GB_STRING); |
---|
932 | GB_write_string(gb_acc,buf); |
---|
933 | } |
---|
934 | free(sequence); |
---|
935 | } |
---|
936 | while (1){ // go to the start of an species |
---|
937 | if (!p || !ifo->begin || GBS_string_matches(p,ifo->begin,GB_MIND_CASE)) break; |
---|
938 | p = awtc_read_line(0,ifo->sequencestart,ifo->sequenceend); |
---|
939 | } |
---|
940 | } |
---|
941 | return 0; |
---|
942 | } |
---|
943 | |
---|
944 | void AWTC_import_go_cb(AW_window *aww) // Import sequences into new or existing database |
---|
945 | { |
---|
946 | AW_root *awr = aww->get_root(); |
---|
947 | bool is_genom_db = false; |
---|
948 | bool delete_db_type_if_error = false; // delete db type (genome/normal) in case of error ? |
---|
949 | { |
---|
950 | bool read_genom_db = (awr->awar(AWAR_READ_GENOM_DB)->read_int() != IMP_PLAIN_SEQUENCE); |
---|
951 | GB_transaction ta(GB_MAIN); |
---|
952 | |
---|
953 | delete_db_type_if_error = (GB_entry(GB_MAIN, GENOM_DB_TYPE) == 0); |
---|
954 | is_genom_db = GEN_is_genome_db(GB_MAIN, read_genom_db); |
---|
955 | |
---|
956 | if (read_genom_db!=is_genom_db) { |
---|
957 | if (is_genom_db) { |
---|
958 | aw_message("You can only import whole genom sequences into a genom database."); |
---|
959 | } |
---|
960 | else { |
---|
961 | aw_message("You can't import whole genom sequences into a non-genom ARB database."); |
---|
962 | } |
---|
963 | return; |
---|
964 | } |
---|
965 | } |
---|
966 | |
---|
967 | GB_ERROR error = 0; |
---|
968 | |
---|
969 | GB_change_my_security(GB_MAIN,6,""); |
---|
970 | |
---|
971 | GB_begin_transaction(GB_MAIN); // first transaction start |
---|
972 | char *ali_name; |
---|
973 | { |
---|
974 | char *ali = awr->awar(AWAR_ALI)->read_string(); |
---|
975 | ali_name = GBS_string_eval(ali,"*=ali_*1:ali_ali_=ali_",0); |
---|
976 | free(ali); |
---|
977 | } |
---|
978 | |
---|
979 | error = GBT_check_alignment_name(ali_name); |
---|
980 | |
---|
981 | int ali_protection = awr->awar(AWAR_ALI_PROTECTION)->read_int(); |
---|
982 | if (!error) { |
---|
983 | char *ali_type; |
---|
984 | ali_type = awr->awar(AWAR_ALI_TYPE)->read_string(); |
---|
985 | |
---|
986 | if (is_genom_db && strcmp(ali_type, "dna")!=0) { |
---|
987 | error = "You must set the alignment type to dna when importing genom sequences."; |
---|
988 | } |
---|
989 | else { |
---|
990 | GBT_create_alignment(GB_MAIN,ali_name,2000,0,ali_protection,ali_type); |
---|
991 | } |
---|
992 | free(ali_type); |
---|
993 | } |
---|
994 | |
---|
995 | bool ask_generate_names = true; |
---|
996 | |
---|
997 | if (!error) { |
---|
998 | if (is_genom_db) { |
---|
999 | // import genome flatfile into ARB-genome database: |
---|
1000 | |
---|
1001 | char *mask = awr->awar(AWAR_FILE)->read_string(); |
---|
1002 | char **fnames = GBS_read_dir(mask, NULL); |
---|
1003 | |
---|
1004 | if (fnames[0] == 0) { |
---|
1005 | error = GB_export_error("Cannot find selected file"); |
---|
1006 | } |
---|
1007 | else { |
---|
1008 | int successfull_imports = 0; |
---|
1009 | int failed_imports = 0; |
---|
1010 | int count; |
---|
1011 | |
---|
1012 | for (count = 0; fnames[count]; ++count) ; // count filenames |
---|
1013 | |
---|
1014 | GBDATA *gb_species_data = GB_search(GB_MAIN, "species_data", GB_CREATE_CONTAINER); |
---|
1015 | ImportSession import_session(gb_species_data, count*10); |
---|
1016 | |
---|
1017 | // close the above transaction and do each importfile in separate transaction |
---|
1018 | // to avoid that all imports are undone by transaction abort happening in case of error |
---|
1019 | GB_commit_transaction(GB_MAIN); |
---|
1020 | |
---|
1021 | aw_openstatus("Reading input files"); |
---|
1022 | |
---|
1023 | for (int curr = 0; !error && fnames[curr]; ++curr) { |
---|
1024 | GB_ERROR error_this_file = 0; |
---|
1025 | |
---|
1026 | GB_begin_transaction(GB_MAIN); |
---|
1027 | { |
---|
1028 | const char *lslash = strrchr(fnames[curr], '/'); |
---|
1029 | aw_status(GBS_global_string("%i/%i: %s", curr+1, count, lslash ? lslash+1 : fnames[curr])); |
---|
1030 | } |
---|
1031 | |
---|
1032 | #if defined(DEBUG) |
---|
1033 | fprintf(stderr, "import of '%s'\n", fnames[curr]); |
---|
1034 | #endif // DEBUG |
---|
1035 | error_this_file = GI_importGenomeFile(import_session, fnames[curr], ali_name); |
---|
1036 | |
---|
1037 | if (!error_this_file) { |
---|
1038 | GB_commit_transaction(GB_MAIN); |
---|
1039 | // GB_warning("File '%s' successfully imported", fnames[curr]); |
---|
1040 | successfull_imports++; |
---|
1041 | delete_db_type_if_error = false; |
---|
1042 | } |
---|
1043 | else { // error occurred during import |
---|
1044 | error_this_file = GBS_global_string("'%s' not imported\nReason: %s", fnames[curr], error_this_file); |
---|
1045 | GB_warningf("Import error: %s", error_this_file); |
---|
1046 | GB_abort_transaction(GB_MAIN); |
---|
1047 | failed_imports++; |
---|
1048 | } |
---|
1049 | |
---|
1050 | aw_status((curr+1)/double(count)); |
---|
1051 | } |
---|
1052 | |
---|
1053 | if (!successfull_imports) { |
---|
1054 | error = "Nothing has been imported"; |
---|
1055 | } |
---|
1056 | else { |
---|
1057 | GB_warningf("%i of %i files were imported with success", successfull_imports, (successfull_imports+failed_imports)); |
---|
1058 | } |
---|
1059 | |
---|
1060 | aw_closestatus(); |
---|
1061 | |
---|
1062 | // now open another transaction to "undo" the transaction close above |
---|
1063 | GB_begin_transaction(GB_MAIN); |
---|
1064 | } |
---|
1065 | |
---|
1066 | GBT_free_names(fnames); |
---|
1067 | free(mask); |
---|
1068 | } |
---|
1069 | else { |
---|
1070 | // import to non-genome ARB-db : |
---|
1071 | |
---|
1072 | { |
---|
1073 | // load import filter: |
---|
1074 | char *file = awr->awar(AWAR_FORM"/file_name")->read_string(); |
---|
1075 | |
---|
1076 | if (!strlen(file)) { |
---|
1077 | error = "Please select a form"; |
---|
1078 | } |
---|
1079 | else { |
---|
1080 | error = awtc_read_import_format(file); |
---|
1081 | if (!error && awtcig.ifo->new_format) { |
---|
1082 | awtcig.ifo2 = awtcig.ifo; |
---|
1083 | awtcig.ifo = 0; |
---|
1084 | |
---|
1085 | error = awtc_read_import_format(awtcig.ifo2->new_format); |
---|
1086 | if (!error) { |
---|
1087 | if (awtcig.ifo->new_format) { |
---|
1088 | error = GBS_global_string("in line %zi of import filter '%s':\n" |
---|
1089 | "Only one level of form nesting (NEW_FORMAT) allowed", |
---|
1090 | awtcig.ifo->new_format_lineno, name_only(awtcig.ifo2->new_format)); |
---|
1091 | } |
---|
1092 | } |
---|
1093 | if (error) { |
---|
1094 | error = GBS_global_string("in format used in line %zi of '%s':\n%s", |
---|
1095 | awtcig.ifo2->new_format_lineno, name_only(file), error); |
---|
1096 | } |
---|
1097 | } |
---|
1098 | } |
---|
1099 | free(file); |
---|
1100 | } |
---|
1101 | |
---|
1102 | { |
---|
1103 | char *f = awr->awar(AWAR_FILE)->read_string(); |
---|
1104 | awtcig.filenames = GBS_read_dir(f,NULL); |
---|
1105 | free(f); |
---|
1106 | } |
---|
1107 | |
---|
1108 | if (awtcig.filenames[0] == 0){ |
---|
1109 | error = GB_export_error("Cannot find selected file(s)"); |
---|
1110 | } |
---|
1111 | |
---|
1112 | bool status_open = false; |
---|
1113 | if (!error) { |
---|
1114 | aw_openstatus("Reading input files"); |
---|
1115 | status_open = true; |
---|
1116 | |
---|
1117 | error = awtc_read_data(ali_name, ali_protection); |
---|
1118 | |
---|
1119 | if (error) { |
---|
1120 | error = GBS_global_string("Error: %s\nwhile reading file %s", error, awtcig.current_file[-1]); |
---|
1121 | } |
---|
1122 | else { |
---|
1123 | if (awtcig.ifo->noautonames || (awtcig.ifo2 && awtcig.ifo2->noautonames)) { |
---|
1124 | ask_generate_names = false; |
---|
1125 | } |
---|
1126 | else { |
---|
1127 | ask_generate_names = true; |
---|
1128 | } |
---|
1129 | } |
---|
1130 | } |
---|
1131 | |
---|
1132 | delete awtcig.ifo; awtcig.ifo = 0; |
---|
1133 | delete awtcig.ifo2; awtcig.ifo2 = 0; |
---|
1134 | |
---|
1135 | if (awtcig.in) { |
---|
1136 | fclose(awtcig.in); |
---|
1137 | awtcig.in = 0; |
---|
1138 | } |
---|
1139 | |
---|
1140 | GBT_free_names(awtcig.filenames); |
---|
1141 | awtcig.filenames = 0; |
---|
1142 | awtcig.current_file = 0; |
---|
1143 | |
---|
1144 | if (status_open) aw_closestatus(); |
---|
1145 | } |
---|
1146 | } |
---|
1147 | free(ali_name); |
---|
1148 | |
---|
1149 | bool call_func = true; // shall awtcig.func be called ? |
---|
1150 | if (error) { |
---|
1151 | GB_abort_transaction(GB_MAIN); |
---|
1152 | |
---|
1153 | if (delete_db_type_if_error) { |
---|
1154 | // delete db type, if it was initialized above |
---|
1155 | // (avoids 'can't import'-error, if file-type (genome-file/species-file) is changed after a failed try) |
---|
1156 | GB_transaction ta(GB_MAIN); |
---|
1157 | GBDATA *db_type = GB_entry(GB_MAIN, GENOM_DB_TYPE); |
---|
1158 | if (db_type) GB_delete(db_type); |
---|
1159 | } |
---|
1160 | |
---|
1161 | call_func = false; |
---|
1162 | } |
---|
1163 | else { |
---|
1164 | aww->hide(); // import window stays open in case of error |
---|
1165 | |
---|
1166 | aw_openstatus("Checking and Scanning database"); |
---|
1167 | aw_status("Pass 1: Check entries"); |
---|
1168 | |
---|
1169 | // scan for hidden/unknown fields : |
---|
1170 | awt_selection_list_rescan(GB_MAIN, AWT_NDS_FILTER, AWT_RS_UPDATE_FIELDS); |
---|
1171 | if (is_genom_db) awt_gene_field_selection_list_rescan(GB_MAIN, AWT_NDS_FILTER, AWT_RS_UPDATE_FIELDS); |
---|
1172 | |
---|
1173 | GBT_mark_all(GB_MAIN,1); |
---|
1174 | sleep(1); |
---|
1175 | aw_status("Pass 2: Check sequence lengths"); |
---|
1176 | GBT_check_data(GB_MAIN,0); |
---|
1177 | sleep(1); |
---|
1178 | |
---|
1179 | GB_commit_transaction(GB_MAIN); |
---|
1180 | |
---|
1181 | if (ask_generate_names) { |
---|
1182 | if (aw_question("You may generate short names using the full_name and accession entry of the species", |
---|
1183 | "Generate new short names (recommended),Use found names")==0) |
---|
1184 | { |
---|
1185 | aw_status("Pass 3: Generate unique names"); |
---|
1186 | error = AW_select_nameserver(GB_MAIN, awtcig.gb_other_main); |
---|
1187 | if (!error) { |
---|
1188 | error = AWTC_pars_names(GB_MAIN,1); |
---|
1189 | } |
---|
1190 | } |
---|
1191 | } |
---|
1192 | |
---|
1193 | aw_closestatus(); |
---|
1194 | } |
---|
1195 | |
---|
1196 | if (error) aw_message(error); |
---|
1197 | |
---|
1198 | GB_change_my_security(GB_MAIN,0,""); |
---|
1199 | |
---|
1200 | if (call_func) awtcig.func(awr, awtcig.cd1,awtcig.cd2); |
---|
1201 | } |
---|
1202 | |
---|
1203 | class AliNameAndType { |
---|
1204 | string name_, type_; |
---|
1205 | public: |
---|
1206 | AliNameAndType(const char *ali_name, const char *ali_type) : name_(ali_name), type_(ali_type) {} |
---|
1207 | AliNameAndType(const AliNameAndType& other) : name_(other.name_), type_(other.type_) {} |
---|
1208 | |
---|
1209 | const char *name() const { return name_.c_str(); } |
---|
1210 | const char *type() const { return type_.c_str(); } |
---|
1211 | }; |
---|
1212 | |
---|
1213 | static AliNameAndType last_ali("ali_new", "rna"); // last selected ali for plain import (aka non-flatfile import) |
---|
1214 | |
---|
1215 | |
---|
1216 | void AWTC_import_set_ali_and_type(AW_root *awr, const char *ali_name, const char *ali_type, GBDATA *gbmain) { |
---|
1217 | bool switching_to_GENOM_ALIGNMENT = strcmp(ali_name, GENOM_ALIGNMENT) == 0; |
---|
1218 | static GBDATA *last_valid_gbmain = 0; |
---|
1219 | |
---|
1220 | if (gbmain) last_valid_gbmain = gbmain; |
---|
1221 | |
---|
1222 | AW_awar *awar_name = awr->awar(AWAR_ALI); |
---|
1223 | AW_awar *awar_type = awr->awar(AWAR_ALI_TYPE); |
---|
1224 | |
---|
1225 | if (switching_to_GENOM_ALIGNMENT) { |
---|
1226 | // read and store current settings |
---|
1227 | char *curr_ali_name = awar_name->read_string(); |
---|
1228 | char *curr_ali_type = awar_type->read_string(); |
---|
1229 | |
---|
1230 | last_ali = AliNameAndType(curr_ali_name, curr_ali_type); |
---|
1231 | |
---|
1232 | free(curr_ali_name); |
---|
1233 | free(curr_ali_type); |
---|
1234 | } |
---|
1235 | |
---|
1236 | awar_name->write_string(ali_name); |
---|
1237 | awar_type->write_string(ali_type); |
---|
1238 | |
---|
1239 | if (last_valid_gbmain) { // detect default write protection for alignment |
---|
1240 | GB_transaction ta(last_valid_gbmain); |
---|
1241 | GBDATA *gb_ali = GBT_get_alignment(last_valid_gbmain, ali_name); |
---|
1242 | int protection_to_use = 4; // default protection |
---|
1243 | |
---|
1244 | if (gb_ali) { |
---|
1245 | GBDATA *gb_write_security = GB_entry(gb_ali,"alignment_write_security"); |
---|
1246 | if (gb_write_security) { |
---|
1247 | protection_to_use = GB_read_int(gb_write_security); |
---|
1248 | } |
---|
1249 | } |
---|
1250 | awr->awar(AWAR_ALI_PROTECTION)->write_int(protection_to_use); |
---|
1251 | } |
---|
1252 | } |
---|
1253 | |
---|
1254 | static void genom_flag_changed(AW_root *awr) { |
---|
1255 | if (awr->awar(AWAR_READ_GENOM_DB)->read_int() == IMP_PLAIN_SEQUENCE) { |
---|
1256 | AWTC_import_set_ali_and_type(awr, last_ali.name(), last_ali.type(), 0); |
---|
1257 | awr->awar_string(AWAR_FORM"/filter",".ift"); |
---|
1258 | } |
---|
1259 | else { |
---|
1260 | AWTC_import_set_ali_and_type(awr, GENOM_ALIGNMENT, "dna", 0); |
---|
1261 | awr->awar_string(AWAR_FORM"/filter",".fit"); // *hack* to hide normal import filters |
---|
1262 | } |
---|
1263 | } |
---|
1264 | |
---|
1265 | static void import_window_close_cb(AW_window *aww) { |
---|
1266 | if (awtcig.doExit) exit(EXIT_SUCCESS); |
---|
1267 | else AW_POPDOWN(aww); |
---|
1268 | } |
---|
1269 | |
---|
1270 | GBDATA *open_AWTC_import_window(AW_root *awr,const char *defname, bool do_exit, GBDATA *gb_main, AWTC_RCB(func), AW_CL cd1, AW_CL cd2) |
---|
1271 | { |
---|
1272 | static AW_window_simple *aws = 0; |
---|
1273 | |
---|
1274 | #if defined(DEVEL_RALF) |
---|
1275 | #warning where is awtcig.gb_main closed |
---|
1276 | // it is either (currently not) closed by merge tool |
---|
1277 | // or closed as main db in ARB_NTREE |
---|
1278 | #endif // DEVEL_RALF |
---|
1279 | awtcig.gb_main = GB_open("noname.arb","wc"); |
---|
1280 | awtcig.func = func; |
---|
1281 | awtcig.cd1 = cd1; |
---|
1282 | awtcig.cd2 = cd2; |
---|
1283 | |
---|
1284 | #if defined(DEBUG) |
---|
1285 | AWT_announce_db_to_browser(awtcig.gb_main, "New database (import)"); |
---|
1286 | #endif // DEBUG |
---|
1287 | |
---|
1288 | awtcig.gb_other_main = gb_main; |
---|
1289 | |
---|
1290 | awtcig.doExit = do_exit; // change/set behavior of CLOSE button |
---|
1291 | |
---|
1292 | aw_create_selection_box_awars(awr, AWAR_FILE_BASE, ".", "", defname); |
---|
1293 | aw_create_selection_box_awars(awr, AWAR_FORM, GB_path_in_ARBLIB("import", NULL), ".ift", "*"); |
---|
1294 | |
---|
1295 | awr->awar_string(AWAR_ALI,"dummy"); // these defaults are never used |
---|
1296 | awr->awar_string(AWAR_ALI_TYPE,"dummy"); // they are overwritten by AWTC_import_set_ali_and_type |
---|
1297 | awr->awar_int(AWAR_ALI_PROTECTION, 0); // which is called via genom_flag_changed() below |
---|
1298 | |
---|
1299 | awr->awar(AWAR_READ_GENOM_DB)->add_callback(genom_flag_changed); |
---|
1300 | genom_flag_changed(awr); |
---|
1301 | |
---|
1302 | if (!aws) { |
---|
1303 | aws = new AW_window_simple; |
---|
1304 | |
---|
1305 | aws->init( awr, "ARB_IMPORT","ARB IMPORT"); |
---|
1306 | aws->load_xfig("awt/import_db.fig"); |
---|
1307 | |
---|
1308 | aws->at("close"); |
---|
1309 | aws->callback(import_window_close_cb); |
---|
1310 | aws->create_button("CLOSE", "CLOSE","C"); |
---|
1311 | |
---|
1312 | aws->at("help"); |
---|
1313 | aws->callback(AW_POPUP_HELP,(AW_CL)"arb_import.hlp"); |
---|
1314 | aws->create_button("HELP", "HELP","H"); |
---|
1315 | |
---|
1316 | awt_create_selection_box(aws, AWAR_FILE_BASE, "imp_", "PWD", true, true); // select import filename |
---|
1317 | awt_create_selection_box(aws, AWAR_FORM, "", "ARBHOME", false, false); // select import filter |
---|
1318 | |
---|
1319 | aws->at("auto"); |
---|
1320 | aws->callback(awtc_check_input_format); |
---|
1321 | aws->create_autosize_button("AUTO_DETECT", "AUTO DETECT","A"); |
---|
1322 | |
---|
1323 | aws->at("ali"); |
---|
1324 | aws->create_input_field(AWAR_ALI,4); |
---|
1325 | |
---|
1326 | aws->at("type"); |
---|
1327 | aws->create_option_menu(AWAR_ALI_TYPE); |
---|
1328 | aws->insert_option("dna","d","dna"); |
---|
1329 | aws->insert_option("rna","r","rna"); |
---|
1330 | aws->insert_option("protein","p","ami"); |
---|
1331 | aws->update_option_menu(); |
---|
1332 | |
---|
1333 | aws->at("protect"); |
---|
1334 | aws->create_option_menu(AWAR_ALI_PROTECTION); |
---|
1335 | aws->insert_option("0", "0", 0); |
---|
1336 | aws->insert_option("1", "1", 1); |
---|
1337 | aws->insert_option("2", "2", 2); |
---|
1338 | aws->insert_option("3", "3", 3); |
---|
1339 | aws->insert_default_option("4", "4", 4); |
---|
1340 | aws->insert_option("5", "5", 5); |
---|
1341 | aws->insert_option("6", "6", 6); |
---|
1342 | aws->update_option_menu(); |
---|
1343 | |
---|
1344 | aws->at("genom"); |
---|
1345 | aws->create_toggle_field(AWAR_READ_GENOM_DB); |
---|
1346 | aws->sens_mask(AWM_EXP); |
---|
1347 | aws->insert_toggle("Import genome data in EMBL, GenBank and DDBJ format","e", IMP_GENOME_FLATFILE); |
---|
1348 | aws->sens_mask(AWM_ALL); |
---|
1349 | aws->insert_toggle("Import selected format","f",IMP_PLAIN_SEQUENCE); |
---|
1350 | aws->update_toggle_field(); |
---|
1351 | |
---|
1352 | aws->at("go"); |
---|
1353 | aws->callback(AWTC_import_go_cb); |
---|
1354 | aws->highlight(); |
---|
1355 | aws->create_button("GO", "GO","G"); |
---|
1356 | } |
---|
1357 | aws->activate(); |
---|
1358 | return GB_MAIN; |
---|
1359 | } |
---|