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