1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : ED4_no_class.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include <ed4_extern.hxx> |
---|
12 | |
---|
13 | #include "ed4_awars.hxx" |
---|
14 | #include "ed4_class.hxx" |
---|
15 | #include "ed4_edit_string.hxx" |
---|
16 | #include "ed4_nds.hxx" |
---|
17 | #include "ed4_list.hxx" |
---|
18 | #include "ed4_seq_colors.hxx" |
---|
19 | #include "ed4_flags.hxx" |
---|
20 | |
---|
21 | #include <iupac.h> |
---|
22 | #include <consensus_config.h> |
---|
23 | #include <item_sel_list.h> |
---|
24 | #include <macros.hxx> |
---|
25 | |
---|
26 | #include <awt.hxx> |
---|
27 | #include <awt_config_manager.hxx> |
---|
28 | #include <awt_misc.hxx> |
---|
29 | #include <awt_sel_boxes.hxx> |
---|
30 | |
---|
31 | #include <aw_awars.hxx> |
---|
32 | #include <AW_helix.hxx> |
---|
33 | #include <aw_msg.hxx> |
---|
34 | #include <AW_rename.hxx> |
---|
35 | #include <aw_root.hxx> |
---|
36 | |
---|
37 | #include <ad_config.h> |
---|
38 | |
---|
39 | #include <arb_defs.h> |
---|
40 | #include <arb_global_defs.h> |
---|
41 | #include <arb_progress.h> |
---|
42 | |
---|
43 | #include <cctype> |
---|
44 | #include <limits.h> |
---|
45 | |
---|
46 | #include <vector> |
---|
47 | |
---|
48 | using namespace std; |
---|
49 | |
---|
50 | struct group_folding { |
---|
51 | int max_depth; // maximum group level (root-group has level 0) |
---|
52 | int max_visible; // max visible group level (even if folded) |
---|
53 | int max_unfolded; // max visible unfolded group level |
---|
54 | |
---|
55 | group_folding() : |
---|
56 | max_depth(0), |
---|
57 | max_visible(0), |
---|
58 | max_unfolded(0) |
---|
59 | {} |
---|
60 | }; |
---|
61 | |
---|
62 | static ARB_ERROR update_group_folding(ED4_base *base, group_folding *folding) { |
---|
63 | if (base->is_group_manager()) { |
---|
64 | int group_level = base->calc_group_depth()+1; |
---|
65 | folding->max_depth = std::max(folding->max_depth, group_level); |
---|
66 | |
---|
67 | bool is_folded = base->has_property(PROP_IS_FOLDED); |
---|
68 | if (group_level>folding->max_visible || (group_level>folding->max_unfolded && !is_folded)) { // may change other maxima? |
---|
69 | bool is_visible = !base->is_hidden(); |
---|
70 | if (is_visible) { |
---|
71 | folding->max_visible = std::max(folding->max_visible, group_level); |
---|
72 | if (!is_folded) { |
---|
73 | folding->max_unfolded = std::max(folding->max_unfolded, group_level); |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | return NULp; |
---|
79 | } |
---|
80 | |
---|
81 | static void calculate_group_folding(group_folding& folding) { |
---|
82 | if (ED4_ROOT->main_manager) { |
---|
83 | ED4_ROOT->main_manager->route_down_hierarchy(makeED4_route_cb(update_group_folding, &folding)).expect_no_error(); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void ED4_calc_terminal_extentions() { |
---|
88 | ED4_ROOT->recalc_font_group(); |
---|
89 | |
---|
90 | AW_device *device = ED4_ROOT->first_window->get_device(); // any device |
---|
91 | |
---|
92 | const AW_font_group& font_group = ED4_ROOT->font_group; |
---|
93 | const AW_font_limits& seq_font_limits = font_group.get_limits(ED4_G_SEQUENCES); |
---|
94 | const AW_font_limits& seq_equal_limits = device->get_font_limits(ED4_G_SEQUENCES, '='); |
---|
95 | const AW_font_limits& info_font_limits = font_group.get_limits(ED4_G_STANDARD); |
---|
96 | |
---|
97 | int info_char_width = info_font_limits.width; |
---|
98 | int seq_term_descent; |
---|
99 | |
---|
100 | if (ED4_ROOT->helix->is_enabled() || ED4_ROOT->protstruct) { // display helix ? |
---|
101 | ED4_ROOT->helix_spacing = |
---|
102 | seq_equal_limits.ascent // the ascent of '=' |
---|
103 | + ED4_ROOT->helix_add_spacing; // xtra user-defined spacing |
---|
104 | |
---|
105 | seq_term_descent = ED4_ROOT->helix_spacing; |
---|
106 | } |
---|
107 | else { |
---|
108 | ED4_ROOT->helix_spacing = 0; |
---|
109 | seq_term_descent = seq_font_limits.descent; |
---|
110 | } |
---|
111 | |
---|
112 | // for wanted_seq_term_height ignore descent, because it additionally allocates 'ED4_ROOT->helix_spacing' space: |
---|
113 | int wanted_seq_term_height = seq_font_limits.ascent + seq_term_descent + ED4_ROOT->terminal_add_spacing; |
---|
114 | int wanted_seq_info_height = info_font_limits.get_height() + ED4_ROOT->terminal_add_spacing; |
---|
115 | |
---|
116 | TERMINAL_HEIGHT = (wanted_seq_term_height>wanted_seq_info_height) ? wanted_seq_term_height : wanted_seq_info_height; |
---|
117 | |
---|
118 | { |
---|
119 | group_folding folding; |
---|
120 | calculate_group_folding(folding); |
---|
121 | |
---|
122 | int maxbrackets = folding.max_unfolded; |
---|
123 | int maxchars = ED4_get_NDS_width(); |
---|
124 | |
---|
125 | #if defined(DEBUG) |
---|
126 | fprintf(stderr, "maxbrackets=%i\n", maxbrackets); |
---|
127 | #endif |
---|
128 | |
---|
129 | MAXNAME_WIDTH = |
---|
130 | (maxchars+1+1)*info_char_width + // width defined in NDS window (+ 1 char for marked-box; + 1 extra char to avoid truncation) |
---|
131 | maxbrackets*BRACKET_WIDTH; // brackets defined in NDS window |
---|
132 | } |
---|
133 | |
---|
134 | { |
---|
135 | SpeciesFlags& flags = SpeciesFlags::mutable_instance(); |
---|
136 | int headerlen = flags.get_header_length(); |
---|
137 | |
---|
138 | if (headerlen) { |
---|
139 | flags.calculate_header_dimensions(device, ED4_G_FLAG_INFO); |
---|
140 | FLAG_WIDTH = flags.get_pixel_width(); |
---|
141 | } |
---|
142 | else { |
---|
143 | FLAG_WIDTH = 0; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | MAXINFO_WIDTH = |
---|
148 | CHARACTEROFFSET + |
---|
149 | info_char_width*ED4_ROOT->aw_root->awar(ED4_AWAR_NDS_INFO_WIDTH)->read_int() + |
---|
150 | 1; // + 1 extra char to avoid truncation |
---|
151 | |
---|
152 | INFO_TERM_TEXT_YOFFSET = info_font_limits.ascent - 1; |
---|
153 | SEQ_TERM_TEXT_YOFFSET = seq_font_limits.ascent - 1; |
---|
154 | |
---|
155 | if (INFO_TERM_TEXT_YOFFSET<SEQ_TERM_TEXT_YOFFSET) INFO_TERM_TEXT_YOFFSET = SEQ_TERM_TEXT_YOFFSET; |
---|
156 | |
---|
157 | #if defined(DEBUG) && 0 |
---|
158 | printf("seq_term_descent = %i\n", seq_term_descent); |
---|
159 | printf("TERMINAL_HEIGHT = %i\n", TERMINAL_HEIGHT); |
---|
160 | printf("MAXNAME_WIDTH = %i\n", MAXNAME_WIDTH); |
---|
161 | printf("FLAG_WIDTH = %i\n", FLAG_WIDTH); |
---|
162 | printf("MAXINFO_WIDTH = %i\n", MAXINFO_WIDTH); |
---|
163 | printf("INFO_TERM_TEXT_YOFFSET= %i\n", INFO_TERM_TEXT_YOFFSET); |
---|
164 | printf(" SEQ_TERM_TEXT_YOFFSET= %i\n", SEQ_TERM_TEXT_YOFFSET); |
---|
165 | #endif // DEBUG |
---|
166 | } |
---|
167 | |
---|
168 | bool ED4_flag_header_terminal::set_dynamic_size() { |
---|
169 | return extension.set_size_does_change(WIDTH, FLAG_WIDTH); |
---|
170 | } |
---|
171 | bool ED4_flag_terminal::set_dynamic_size() { |
---|
172 | return extension.set_size_does_change(WIDTH, FLAG_WIDTH); |
---|
173 | } |
---|
174 | bool ED4_species_name_terminal::set_dynamic_size() { |
---|
175 | return extension.set_size_does_change(WIDTH, MAXNAME_WIDTH - BRACKET_WIDTH * calc_group_depth()); |
---|
176 | } |
---|
177 | bool ED4_sequence_info_terminal::set_dynamic_size() { |
---|
178 | return extension.set_size_does_change(WIDTH, MAXINFO_WIDTH); |
---|
179 | } |
---|
180 | bool ED4_line_terminal::set_dynamic_size() { |
---|
181 | // dynamically adapt to ref_terminals |
---|
182 | |
---|
183 | AW_pos overall_width = |
---|
184 | TREE_TERMINAL_WIDTH + |
---|
185 | MAXNAME_WIDTH + |
---|
186 | ED4_ROOT->ref_terminals.sequence_info()->extension.size[WIDTH] + |
---|
187 | ED4_ROOT->ref_terminals.sequence()->extension.size[WIDTH]; |
---|
188 | |
---|
189 | return extension.set_size_does_change(WIDTH, overall_width); |
---|
190 | } |
---|
191 | bool ED4_spacer_terminal::set_dynamic_size() { |
---|
192 | if (!has_property(PROP_DYNA_RESIZE)) return false; // some spacer terminals never change their size (eg. bottom-spacer) |
---|
193 | |
---|
194 | AW_pos new_height = SPACER_HEIGHT; |
---|
195 | |
---|
196 | if (parent->is_device_manager()) { |
---|
197 | if (this == ED4_ROOT->main_manager->get_top_middle_spacer_terminal()) { |
---|
198 | ED4_terminal *top_middle_line_terminal = ED4_ROOT->main_manager->get_top_middle_line_terminal(); |
---|
199 | |
---|
200 | // top-middle spacer + top-middle line >= font-size (otherwise scrolling relicts!) |
---|
201 | new_height = TERMINAL_HEIGHT - top_middle_line_terminal->extension.size[HEIGHT]; |
---|
202 | } |
---|
203 | else { |
---|
204 | new_height = 1; // use minimal height for other top-group spacers |
---|
205 | } |
---|
206 | } |
---|
207 | else { |
---|
208 | ED4_manager *grandpa = parent->parent; |
---|
209 | e4_assert(grandpa); |
---|
210 | |
---|
211 | if (grandpa->is_group_manager()) { |
---|
212 | ED4_group_manager *group_man = grandpa->to_group_manager(); |
---|
213 | if (group_man->has_property(PROP_IS_FOLDED)) { |
---|
214 | if (!AW_root::SINGLETON->awar(ED4_AWAR_CONSENSUS_SHOW)->read_int()) { |
---|
215 | new_height = SPACER_NOCONS_HEIGHT; |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | return extension.set_size_does_change(HEIGHT, new_height); |
---|
222 | } |
---|
223 | |
---|
224 | static ARB_ERROR update_extension_size(ED4_base *base) { |
---|
225 | base->resize_dynamic(); |
---|
226 | return NULp; // doesn't fail |
---|
227 | } |
---|
228 | |
---|
229 | void ED4_resize_all_extensions() { // @@@ pass flag to force resize-request? (eg. for initial-call?) |
---|
230 | ED4_calc_terminal_extentions(); |
---|
231 | |
---|
232 | // @@@ below calculations have to be done at startup as well (are they done somewhere else or not done?) |
---|
233 | |
---|
234 | ED4_ROOT->ref_terminals.sequence()->extension.size[HEIGHT] = TERMINAL_HEIGHT; |
---|
235 | ED4_ROOT->ref_terminals.sequence_info()->extension.size[HEIGHT] = TERMINAL_HEIGHT; |
---|
236 | ED4_ROOT->ref_terminals.sequence_info()->extension.size[WIDTH] = MAXINFO_WIDTH; |
---|
237 | |
---|
238 | int screenwidth = ED4_ROOT->root_group_man->remap()->shown_sequence_to_screen(MAXSEQUENCECHARACTERLENGTH); |
---|
239 | while (1) { |
---|
240 | ED4_ROOT->ref_terminals.sequence()->extension.size[WIDTH] = ED4_ROOT->font_group.get_width(ED4_G_SEQUENCES) * (screenwidth+3); |
---|
241 | |
---|
242 | ED4_ROOT->main_manager->route_down_hierarchy(makeED4_route_cb(update_extension_size)).expect_no_error(); |
---|
243 | |
---|
244 | ED4_ROOT->resize_all_requesting_childs(); // Note: may change mapping |
---|
245 | |
---|
246 | int new_screenwidth = ED4_ROOT->root_group_man->remap()->shown_sequence_to_screen(MAXSEQUENCECHARACTERLENGTH); |
---|
247 | if (new_screenwidth == screenwidth) { // mapping did not change |
---|
248 | break; |
---|
249 | } |
---|
250 | screenwidth = new_screenwidth; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | static ARB_ERROR call_edit(ED4_base *object, const ED4_work_info *work_info) { |
---|
255 | // called after editing consensus to edit single sequences |
---|
256 | GB_ERROR error = NULp; |
---|
257 | |
---|
258 | if (object->is_species_seq_terminal()) { |
---|
259 | int expected_prop = PROP_CURSOR_ALLOWED|PROP_ALIGNMENT_DATA; |
---|
260 | |
---|
261 | if ((object->dynamic_prop & expected_prop) == expected_prop) { |
---|
262 | ED4_work_info new_work_info; |
---|
263 | |
---|
264 | new_work_info.event = work_info->event; |
---|
265 | new_work_info.char_position = work_info->char_position; |
---|
266 | new_work_info.out_seq_position = work_info->out_seq_position; |
---|
267 | new_work_info.refresh_needed = false; |
---|
268 | new_work_info.cursor_jump = ED4_JUMP_KEEP_VISIBLE; |
---|
269 | new_work_info.out_string = NULp; |
---|
270 | new_work_info.mode = work_info->mode; |
---|
271 | new_work_info.rightward = work_info->rightward; |
---|
272 | new_work_info.cannot_handle = false; |
---|
273 | new_work_info.is_sequence = work_info->is_sequence; |
---|
274 | new_work_info.working_terminal = object->to_terminal(); |
---|
275 | |
---|
276 | if (object->get_species_pointer()) { |
---|
277 | new_work_info.gb_data = object->get_species_pointer(); |
---|
278 | new_work_info.string = NULp; |
---|
279 | } |
---|
280 | else { |
---|
281 | new_work_info.gb_data = NULp; |
---|
282 | new_work_info.string = object->id; // @@@ looks obsolete (see [8402] for previous code) |
---|
283 | e4_assert(0); // assume we never come here |
---|
284 | } |
---|
285 | |
---|
286 | new_work_info.repeat_count = 1; |
---|
287 | |
---|
288 | { |
---|
289 | ED4_Edit_String editString; |
---|
290 | error = editString.edit(&new_work_info); |
---|
291 | } |
---|
292 | |
---|
293 | e4_assert(error || !new_work_info.out_string); |
---|
294 | |
---|
295 | if (new_work_info.refresh_needed) { |
---|
296 | object->request_refresh(); |
---|
297 | if (object->is_sequence_terminal()) { |
---|
298 | ED4_sequence_terminal *seq_term = object->to_sequence_terminal(); |
---|
299 | seq_term->results().searchAgain(); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | if (move_cursor) { |
---|
304 | current_cursor().jump_sequence_pos(new_work_info.out_seq_position, ED4_JUMP_KEEP_VISIBLE); |
---|
305 | move_cursor = 0; |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | return error; |
---|
310 | } |
---|
311 | |
---|
312 | static int get_max_slider_xpos() { |
---|
313 | const AW_screen_area& rect = current_device()->get_area_size(); |
---|
314 | |
---|
315 | AW_pos x, y; |
---|
316 | ED4_base *horizontal_link = ED4_ROOT->scroll_links.link_for_hor_slider; |
---|
317 | horizontal_link->calc_world_coords(&x, &y); |
---|
318 | |
---|
319 | AW_pos max_xpos = horizontal_link->extension.size[WIDTH] // overall width of virtual scrolling area |
---|
320 | - (rect.r - x); // minus width of visible scroll-area (== relative width of horizontal scrollbar) |
---|
321 | |
---|
322 | if (max_xpos<0) max_xpos = 0; // happens when window-content is smaller than window (e.g. if (folded) alignment is narrow) |
---|
323 | return int(max_xpos+0.5); |
---|
324 | } |
---|
325 | |
---|
326 | static int get_max_slider_ypos() { |
---|
327 | const AW_screen_area& rect = current_device()->get_area_size(); |
---|
328 | |
---|
329 | AW_pos x, y; |
---|
330 | ED4_base *vertical_link = ED4_ROOT->scroll_links.link_for_ver_slider; |
---|
331 | vertical_link->calc_world_coords(&x, &y); |
---|
332 | |
---|
333 | AW_pos max_ypos = vertical_link->extension.size[HEIGHT] // overall height of virtual scrolling area |
---|
334 | - (rect.b - y); // minus height of visible scroll-area (== relative height of vertical scrollbar) |
---|
335 | |
---|
336 | if (max_ypos<0) max_ypos = 0; // happens when window-content is smaller than window (e.g. if ARB_EDIT4 is not filled) |
---|
337 | return int(max_ypos+0.5); |
---|
338 | } |
---|
339 | |
---|
340 | static void ed4_scroll(AW_window *aww, int xdiff, int ydiff, bool use_scroll_speed) { |
---|
341 | if (use_scroll_speed) { |
---|
342 | xdiff = xdiff * ED4_ROOT->aw_root->awar(ED4_AWAR_SCROLL_SPEED_X)->read_int() / 10; |
---|
343 | ydiff = ydiff * ED4_ROOT->aw_root->awar(ED4_AWAR_SCROLL_SPEED_Y)->read_int() / 10; |
---|
344 | } |
---|
345 | |
---|
346 | int new_xpos = aww->slider_pos_horizontal + xdiff; |
---|
347 | int new_ypos = aww->slider_pos_vertical + ydiff; |
---|
348 | |
---|
349 | if (xdiff<0) { // scroll left |
---|
350 | if (new_xpos<0) new_xpos = 0; |
---|
351 | } |
---|
352 | else if (xdiff>0) { // scroll right |
---|
353 | int max_xpos = get_max_slider_xpos(); |
---|
354 | if (max_xpos<0) max_xpos = 0; |
---|
355 | if (new_xpos>max_xpos) new_xpos = max_xpos; |
---|
356 | } |
---|
357 | |
---|
358 | if (ydiff<0) { // scroll up |
---|
359 | if (new_ypos<0) new_ypos = 0; |
---|
360 | } |
---|
361 | else if (ydiff>0) { // scroll down |
---|
362 | int max_ypos = get_max_slider_ypos(); |
---|
363 | if (max_ypos<0) max_ypos = 0; |
---|
364 | if (new_ypos>max_ypos) new_ypos = max_ypos; |
---|
365 | } |
---|
366 | |
---|
367 | if (new_xpos!=aww->slider_pos_horizontal) { |
---|
368 | aww->set_horizontal_scrollbar_position(new_xpos); |
---|
369 | ED4_horizontal_change_cb(aww); |
---|
370 | } |
---|
371 | |
---|
372 | if (new_ypos!=aww->slider_pos_vertical) { |
---|
373 | aww->set_vertical_scrollbar_position(new_ypos); |
---|
374 | ED4_vertical_change_cb(aww); |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | static void scrollHalfPage(bool backwards, bool horizontal) { |
---|
379 | ED4_window *e4w = current_ed4w(); |
---|
380 | AW::Rectangle scrolledArea = e4w->scrolled_rect.get_window_rect(); |
---|
381 | |
---|
382 | double fdist = horizontal ? scrolledArea.width() : scrolledArea.height(); |
---|
383 | int dist = int(fdist/2 + 0.5); |
---|
384 | |
---|
385 | if (backwards) dist = -dist; |
---|
386 | |
---|
387 | ed4_scroll(e4w->aww, horizontal ? dist : 0, horizontal ? 0 : dist, false); |
---|
388 | } |
---|
389 | |
---|
390 | static void executeKeystroke(AW_event *event, int repeatCount) { |
---|
391 | e4_assert(repeatCount>0); |
---|
392 | |
---|
393 | if (event->keycode == AW_KEY_PGUP || event->keycode == AW_KEY_PGDN) { |
---|
394 | scrollHalfPage(event->keycode == AW_KEY_PGUP, event->keymodifier & AW_KEYMODE_ALT); |
---|
395 | } |
---|
396 | else if (event->keycode!=AW_KEY_NONE) { |
---|
397 | ED4_cursor *cursor = ¤t_cursor(); |
---|
398 | if (cursor->owner_of_cursor && !cursor->owner_of_cursor->flag.hidden) { |
---|
399 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
---|
400 | if (event->keycode == AW_KEY_UP || event->keycode == AW_KEY_DOWN || |
---|
401 | ((event->keymodifier & AW_KEYMODE_CONTROL) && |
---|
402 | (event->keycode == AW_KEY_HOME || event->keycode == AW_KEY_END))) |
---|
403 | { |
---|
404 | GB_transaction ta(gb_main); |
---|
405 | while (repeatCount--) { |
---|
406 | cursor->move_cursor(event); |
---|
407 | } |
---|
408 | } |
---|
409 | else { |
---|
410 | // @@@ move into separate function? |
---|
411 | |
---|
412 | ED4_work_info work_info; |
---|
413 | |
---|
414 | work_info.cannot_handle = false; |
---|
415 | work_info.event = *event; |
---|
416 | work_info.char_position = cursor->get_screen_pos(); |
---|
417 | work_info.out_seq_position = cursor->get_sequence_pos(); |
---|
418 | work_info.refresh_needed = false; |
---|
419 | work_info.cursor_jump = ED4_JUMP_KEEP_VISIBLE; |
---|
420 | work_info.out_string = NULp; // nur falls new malloc |
---|
421 | work_info.repeat_count = repeatCount; |
---|
422 | |
---|
423 | ED4_terminal *terminal = cursor->owner_of_cursor; |
---|
424 | e4_assert(terminal->is_text_terminal()); |
---|
425 | |
---|
426 | work_info.working_terminal = terminal; |
---|
427 | |
---|
428 | if (terminal->is_sequence_terminal()) { |
---|
429 | work_info.mode = awar_edit_mode; |
---|
430 | work_info.rightward = awar_edit_rightward; |
---|
431 | work_info.is_sequence = 1; |
---|
432 | } |
---|
433 | else { |
---|
434 | work_info.rightward = true; |
---|
435 | work_info.is_sequence = 0; |
---|
436 | |
---|
437 | if (terminal->is_pure_text_terminal()) { |
---|
438 | work_info.mode = awar_edit_mode; |
---|
439 | } |
---|
440 | else if (terminal->is_columnStat_terminal()) { |
---|
441 | work_info.mode = AD_NOWRITE; |
---|
442 | } |
---|
443 | else { |
---|
444 | e4_assert(0); |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | work_info.string = NULp; |
---|
449 | work_info.gb_data = NULp; |
---|
450 | |
---|
451 | if (terminal->get_species_pointer()) { |
---|
452 | work_info.gb_data = terminal->get_species_pointer(); |
---|
453 | } |
---|
454 | else if (terminal->is_columnStat_terminal()) { |
---|
455 | work_info.gb_data = terminal->to_columnStat_terminal()->corresponding_sequence_terminal()->get_species_pointer(); |
---|
456 | } |
---|
457 | else { |
---|
458 | e4_assert(terminal->is_consensus_terminal()); |
---|
459 | ED4_consensus_sequence_terminal *cterm = terminal->to_consensus_sequence_terminal(); |
---|
460 | work_info.string = cterm->temp_cons_seq; |
---|
461 | } |
---|
462 | |
---|
463 | ED4_Edit_String editString; |
---|
464 | ARB_ERROR error = NULp; |
---|
465 | |
---|
466 | GB_push_transaction(gb_main); |
---|
467 | |
---|
468 | if (terminal->is_consensus_terminal()) { |
---|
469 | ED4_consensus_sequence_terminal *cterm = terminal->to_consensus_sequence_terminal(); |
---|
470 | ED4_group_manager *group_manager = terminal->get_parent(LEV_GROUP)->to_group_manager(); |
---|
471 | |
---|
472 | e4_assert(!cterm->temp_cons_seq); |
---|
473 | work_info.string = cterm->temp_cons_seq = group_manager->build_consensus_string(); |
---|
474 | |
---|
475 | error = editString.edit(&work_info); |
---|
476 | |
---|
477 | cursor->jump_sequence_pos(work_info.out_seq_position, ED4_JUMP_KEEP_VISIBLE); |
---|
478 | |
---|
479 | work_info.string = NULp; |
---|
480 | |
---|
481 | if (work_info.cannot_handle) { |
---|
482 | e4_assert(!error); // see ED4_Edit_String::edit() |
---|
483 | move_cursor = 1; |
---|
484 | error = group_manager->route_down_hierarchy(makeED4_route_cb(call_edit, &work_info)); |
---|
485 | group_manager->rebuild_consensi(group_manager, ED4_U_UP_DOWN); |
---|
486 | } |
---|
487 | |
---|
488 | freenull(cterm->temp_cons_seq); |
---|
489 | } |
---|
490 | else { |
---|
491 | error = editString.edit(&work_info); |
---|
492 | cursor->jump_sequence_pos(work_info.out_seq_position, work_info.cursor_jump); |
---|
493 | } |
---|
494 | |
---|
495 | editString.finish_edit(); |
---|
496 | |
---|
497 | if (error) work_info.refresh_needed = true; |
---|
498 | |
---|
499 | GB_end_transaction_show_error(gb_main, error, aw_message); |
---|
500 | |
---|
501 | if (work_info.refresh_needed) { |
---|
502 | GB_transaction ta(gb_main); |
---|
503 | |
---|
504 | terminal->request_refresh(); |
---|
505 | if (terminal->is_sequence_terminal()) { |
---|
506 | ED4_sequence_terminal *seq_term = terminal->to_sequence_terminal(); |
---|
507 | seq_term->results().searchAgain(); |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | } |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | void ED4_remote_event(AW_event *faked_event) { // keystrokes forwarded from SECEDIT |
---|
516 | ED4_MostRecentWinContext context; |
---|
517 | executeKeystroke(faked_event, 1); |
---|
518 | } |
---|
519 | |
---|
520 | void ED4_input_cb(AW_window *aww) { |
---|
521 | AW_event event; |
---|
522 | static AW_event lastEvent; |
---|
523 | static int repeatCount; |
---|
524 | |
---|
525 | ED4_LocalWinContext uses(aww); |
---|
526 | |
---|
527 | aww->get_event(&event); |
---|
528 | |
---|
529 | |
---|
530 | #if defined(DEBUG) && 0 |
---|
531 | printf("event.type=%i event.keycode=%i event.character='%c' event.keymodifier=%i\n", event.type, event.keycode, event.character, event.keymodifier); |
---|
532 | #endif |
---|
533 | |
---|
534 | switch (event.type) { |
---|
535 | case AW_Keyboard_Press: { |
---|
536 | if (repeatCount==0) { // first key event? |
---|
537 | lastEvent = event; |
---|
538 | repeatCount = 1; |
---|
539 | } |
---|
540 | else { |
---|
541 | if (lastEvent.keycode==event.keycode && |
---|
542 | lastEvent.character==event.character && |
---|
543 | lastEvent.keymodifier==event.keymodifier) { // same key as last? |
---|
544 | repeatCount++; |
---|
545 | } |
---|
546 | else { // other key => execute now |
---|
547 | executeKeystroke(&lastEvent, repeatCount); |
---|
548 | lastEvent = event; |
---|
549 | repeatCount = 1; |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | if (repeatCount) { |
---|
554 | #if defined(DARWIN) || 1 |
---|
555 | // sth goes wrong with OSX -> always execute keystroke |
---|
556 | // Xfree 4.3 has problems as well, so repeat counting is disabled completely |
---|
557 | executeKeystroke(&lastEvent, repeatCount); |
---|
558 | repeatCount = 0; |
---|
559 | #else |
---|
560 | AW_ProcessEventType nextEventType = ED4_ROOT->aw_root->peek_key_event(aww); |
---|
561 | |
---|
562 | if (nextEventType!=KEY_RELEASED) { // no key waiting => execute now |
---|
563 | executeKeystroke(&lastEvent, repeatCount); |
---|
564 | repeatCount = 0; |
---|
565 | } |
---|
566 | #endif |
---|
567 | } |
---|
568 | break; |
---|
569 | } |
---|
570 | case AW_Keyboard_Release: { |
---|
571 | AW_ProcessEventType nextEventType = ED4_ROOT->aw_root->peek_key_event(aww); |
---|
572 | |
---|
573 | if (nextEventType!=KEY_PRESSED && repeatCount) { // no key follows => execute keystrokes (if any) |
---|
574 | executeKeystroke(&lastEvent, repeatCount); |
---|
575 | repeatCount = 0; |
---|
576 | } |
---|
577 | |
---|
578 | break; |
---|
579 | } |
---|
580 | default: { |
---|
581 | if (event.button == AW_WHEEL_UP || event.button == AW_WHEEL_DOWN) { |
---|
582 | if (event.type == AW_Mouse_Press) { |
---|
583 | bool horizontal = event.keymodifier & AW_KEYMODE_ALT; |
---|
584 | int direction = event.button == AW_WHEEL_UP ? -1 : 1; |
---|
585 | |
---|
586 | int dx = horizontal ? direction*ED4_ROOT->font_group.get_max_width() : 0; |
---|
587 | int dy = horizontal ? 0 : direction*ED4_ROOT->font_group.get_max_height(); |
---|
588 | |
---|
589 | ed4_scroll(aww, dx, dy, true); |
---|
590 | } |
---|
591 | return; |
---|
592 | } |
---|
593 | |
---|
594 | if (event.button == AW_BUTTON_MIDDLE) { |
---|
595 | if (event.type == AW_Mouse_Press) { |
---|
596 | ED4_ROOT->scroll_picture.scroll = 1; |
---|
597 | ED4_ROOT->scroll_picture.old_y = event.y; |
---|
598 | ED4_ROOT->scroll_picture.old_x = event.x; |
---|
599 | return; |
---|
600 | } |
---|
601 | if (event.type == AW_Mouse_Release) { |
---|
602 | ED4_ROOT->scroll_picture.scroll = 0; |
---|
603 | return; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | #if defined(DEBUG) && 0 |
---|
608 | if (event.button==AW_BUTTON_LEFT) { |
---|
609 | printf("[ED4_input_cb] type=%i x=%i y=%i ", (int)event.type, (int)event.x, (int)event.y); |
---|
610 | } |
---|
611 | #endif |
---|
612 | |
---|
613 | AW_pos win_x = event.x; |
---|
614 | AW_pos win_y = event.y; |
---|
615 | current_ed4w()->win_to_world_coords(&(win_x), &(win_y)); |
---|
616 | event.x = (int) win_x; |
---|
617 | event.y = (int) win_y; |
---|
618 | |
---|
619 | #if defined(DEBUG) && 0 |
---|
620 | if (event.button==AW_BUTTON_LEFT) { |
---|
621 | printf("-> x=%i y=%i\n", (int)event.type, (int)event.x, (int)event.y); |
---|
622 | } |
---|
623 | #endif |
---|
624 | |
---|
625 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
626 | ED4_ROOT->main_manager->event_sent_by_parent(&event, aww); |
---|
627 | break; |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | ED4_trigger_instant_refresh(); |
---|
632 | } |
---|
633 | |
---|
634 | void ED4_vertical_change_cb(AW_window *aww) { |
---|
635 | ED4_LocalWinContext uses(aww); |
---|
636 | |
---|
637 | ED4_window *win = current_ed4w(); |
---|
638 | { |
---|
639 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
640 | |
---|
641 | int old_slider_pos = win->slider_pos_vertical; |
---|
642 | |
---|
643 | { // correct slider_pos if necessary |
---|
644 | int max_slider_ypos = get_max_slider_ypos(); |
---|
645 | |
---|
646 | if (aww->slider_pos_vertical>max_slider_ypos) aww->set_vertical_scrollbar_position(max_slider_ypos); |
---|
647 | if (aww->slider_pos_vertical<0) aww->set_vertical_scrollbar_position(0); |
---|
648 | } |
---|
649 | |
---|
650 | int slider_diff = aww->slider_pos_vertical - old_slider_pos; |
---|
651 | |
---|
652 | win->coords.window_upper_clip_point += slider_diff; |
---|
653 | win->coords.window_lower_clip_point += slider_diff; |
---|
654 | |
---|
655 | win->scroll_rectangle(0, -slider_diff); |
---|
656 | win->slider_pos_vertical = aww->slider_pos_vertical; |
---|
657 | } |
---|
658 | win->update_window_coords(); |
---|
659 | } |
---|
660 | |
---|
661 | void ED4_horizontal_change_cb(AW_window *aww) { |
---|
662 | ED4_LocalWinContext uses(aww); |
---|
663 | |
---|
664 | ED4_window *win = current_ed4w(); |
---|
665 | { |
---|
666 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
667 | |
---|
668 | int old_slider_pos = win->slider_pos_horizontal; |
---|
669 | |
---|
670 | { // correct slider_pos if necessary |
---|
671 | int max_slider_xpos = get_max_slider_xpos(); |
---|
672 | |
---|
673 | if (aww->slider_pos_horizontal>max_slider_xpos) aww->set_horizontal_scrollbar_position(max_slider_xpos); |
---|
674 | if (aww->slider_pos_horizontal<0) aww->set_horizontal_scrollbar_position(0); |
---|
675 | } |
---|
676 | |
---|
677 | int slider_diff = aww->slider_pos_horizontal - old_slider_pos; |
---|
678 | |
---|
679 | win->coords.window_left_clip_point += slider_diff; |
---|
680 | win->coords.window_right_clip_point += slider_diff; |
---|
681 | |
---|
682 | win->scroll_rectangle(-slider_diff, 0); |
---|
683 | win->slider_pos_horizontal = aww->slider_pos_horizontal; |
---|
684 | } |
---|
685 | win->update_window_coords(); |
---|
686 | } |
---|
687 | |
---|
688 | void ED4_scrollbar_change_cb(AW_window *aww) { |
---|
689 | ED4_LocalWinContext uses(aww); |
---|
690 | |
---|
691 | ED4_window *win = current_ed4w(); |
---|
692 | { |
---|
693 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
694 | |
---|
695 | |
---|
696 | int old_hslider_pos = win->slider_pos_horizontal; |
---|
697 | int old_vslider_pos = win->slider_pos_vertical; |
---|
698 | |
---|
699 | { |
---|
700 | // correct slider_pos if necessary |
---|
701 | int max_slider_xpos = get_max_slider_xpos(); |
---|
702 | int max_slider_ypos = get_max_slider_ypos(); |
---|
703 | |
---|
704 | if (aww->slider_pos_horizontal>max_slider_xpos) aww->set_horizontal_scrollbar_position(max_slider_xpos); |
---|
705 | if (aww->slider_pos_horizontal<0) aww->set_horizontal_scrollbar_position(0); |
---|
706 | |
---|
707 | if (aww->slider_pos_vertical>max_slider_ypos) aww->set_vertical_scrollbar_position(max_slider_ypos); |
---|
708 | if (aww->slider_pos_vertical<0) aww->set_vertical_scrollbar_position(0); |
---|
709 | } |
---|
710 | |
---|
711 | int slider_hdiff = aww->slider_pos_horizontal - old_hslider_pos; |
---|
712 | int slider_vdiff = aww->slider_pos_vertical - old_vslider_pos; |
---|
713 | |
---|
714 | ED4_coords *coords = &win->coords; |
---|
715 | coords->window_left_clip_point += slider_hdiff; |
---|
716 | coords->window_right_clip_point += slider_hdiff; |
---|
717 | coords->window_upper_clip_point += slider_vdiff; |
---|
718 | coords->window_lower_clip_point += slider_vdiff; |
---|
719 | |
---|
720 | win->scroll_rectangle(-slider_hdiff, -slider_vdiff); |
---|
721 | |
---|
722 | win->slider_pos_vertical = aww->slider_pos_vertical; |
---|
723 | win->slider_pos_horizontal = aww->slider_pos_horizontal; |
---|
724 | } |
---|
725 | win->update_window_coords(); |
---|
726 | } |
---|
727 | |
---|
728 | void ED4_motion_cb(AW_window *aww) { |
---|
729 | AW_event event; |
---|
730 | |
---|
731 | ED4_LocalWinContext uses(aww); |
---|
732 | |
---|
733 | aww->get_event(&event); |
---|
734 | |
---|
735 | if (event.type == AW_Mouse_Drag && event.button == AW_BUTTON_MIDDLE) { |
---|
736 | if (ED4_ROOT->scroll_picture.scroll) { |
---|
737 | int xdiff = ED4_ROOT->scroll_picture.old_x - event.x; |
---|
738 | int ydiff = ED4_ROOT->scroll_picture.old_y - event.y; |
---|
739 | |
---|
740 | ed4_scroll(aww, xdiff, ydiff, true); |
---|
741 | |
---|
742 | ED4_ROOT->scroll_picture.old_x = event.x; |
---|
743 | ED4_ROOT->scroll_picture.old_y = event.y; |
---|
744 | } |
---|
745 | } |
---|
746 | else { |
---|
747 | |
---|
748 | #if defined(DEBUG) && 0 |
---|
749 | if (event.button==AW_BUTTON_LEFT) { |
---|
750 | printf("[ED4_motion_cb] type=%i x=%i y=%i ", (int)event.type, (int)event.x, (int)event.y); |
---|
751 | } |
---|
752 | #endif |
---|
753 | |
---|
754 | AW_pos win_x = event.x; |
---|
755 | AW_pos win_y = event.y; |
---|
756 | current_ed4w()->win_to_world_coords(&win_x, &win_y); |
---|
757 | event.x = (int) win_x; |
---|
758 | event.y = (int) win_y; |
---|
759 | |
---|
760 | #if defined(DEBUG) && 0 |
---|
761 | if (event.button==AW_BUTTON_LEFT) { |
---|
762 | printf("-> x=%i y=%i\n", (int)event.type, (int)event.x, (int)event.y); |
---|
763 | } |
---|
764 | #endif |
---|
765 | |
---|
766 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
767 | ED4_ROOT->main_manager->event_sent_by_parent(&event, aww); |
---|
768 | } |
---|
769 | } |
---|
770 | |
---|
771 | void ED4_remote_set_cursor_cb(AW_root *awr) { |
---|
772 | AW_awar *awar = awr->awar(AWAR_SET_CURSOR_POSITION); |
---|
773 | long pos = awar->read_int(); |
---|
774 | |
---|
775 | if (pos != -1) { |
---|
776 | ED4_MostRecentWinContext context; |
---|
777 | ED4_cursor *cursor = ¤t_cursor(); |
---|
778 | cursor->jump_sequence_pos(pos, ED4_JUMP_CENTERED); |
---|
779 | awar->write_int(-1); |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | void ED4_jump_to_cursor_position(AW_window *aww, const char *awar_name, PositionType posType) { |
---|
784 | ED4_LocalWinContext uses(aww); |
---|
785 | ED4_cursor *cursor = ¤t_cursor(); |
---|
786 | GB_ERROR error = NULp; |
---|
787 | |
---|
788 | long pos = aww->get_root()->awar(awar_name)->read_int(); |
---|
789 | |
---|
790 | if (pos>0) pos = bio2info(pos); |
---|
791 | else if (pos<0) { // jump negative (count from back) |
---|
792 | int last_pos = -1; // [1..] |
---|
793 | |
---|
794 | switch (posType) { |
---|
795 | case ED4_POS_SEQUENCE: { |
---|
796 | last_pos = MAXSEQUENCECHARACTERLENGTH; |
---|
797 | break; |
---|
798 | } |
---|
799 | case ED4_POS_ECOLI: { |
---|
800 | BI_ecoli_ref *ecoli = ED4_ROOT->ecoli_ref; |
---|
801 | if (ecoli->gotData()) { |
---|
802 | last_pos = ecoli->abs_2_rel(INT_MAX); |
---|
803 | } |
---|
804 | else { |
---|
805 | last_pos = 0; // doesnt matter (error below) |
---|
806 | } |
---|
807 | break; |
---|
808 | } |
---|
809 | case ED4_POS_BASE: { |
---|
810 | last_pos = cursor->sequence2base_position(INT_MAX); |
---|
811 | break; |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | e4_assert(last_pos != -1); |
---|
816 | pos = bio2info(last_pos+1+pos); |
---|
817 | } |
---|
818 | |
---|
819 | switch (posType) { |
---|
820 | case ED4_POS_SEQUENCE: { |
---|
821 | e4_assert(strcmp(awar_name, current_ed4w()->awar_path_for_cursor)==0); |
---|
822 | break; |
---|
823 | } |
---|
824 | case ED4_POS_ECOLI: { |
---|
825 | e4_assert(strcmp(awar_name, current_ed4w()->awar_path_for_Ecoli)==0); |
---|
826 | |
---|
827 | BI_ecoli_ref *ecoli = ED4_ROOT->ecoli_ref; |
---|
828 | if (ecoli->gotData()) pos = ecoli->rel_2_abs(pos); |
---|
829 | else error = "No ecoli reference"; |
---|
830 | break; |
---|
831 | } |
---|
832 | case ED4_POS_BASE: { |
---|
833 | e4_assert(strcmp(awar_name, current_ed4w()->awar_path_for_basePos)==0); |
---|
834 | pos = cursor->base2sequence_position(pos); |
---|
835 | break; |
---|
836 | } |
---|
837 | } |
---|
838 | |
---|
839 | // now position is absolute [0..N-1] |
---|
840 | |
---|
841 | // limit to screen |
---|
842 | { |
---|
843 | ED4_remap *remap = ED4_ROOT->root_group_man->remap(); |
---|
844 | long max = remap->screen_to_sequence(remap->get_max_screen_pos()); |
---|
845 | |
---|
846 | if (pos > max) pos = max; |
---|
847 | else if (pos<0) pos = 0; |
---|
848 | } |
---|
849 | |
---|
850 | if (error) { |
---|
851 | aw_message(error); |
---|
852 | } |
---|
853 | else { |
---|
854 | cursor->jump_sequence_pos(pos, ED4_JUMP_CENTERED); |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | void ED4_set_helixnr(AW_window *aww, const char *awar_name) { |
---|
859 | ED4_LocalWinContext uses(aww); |
---|
860 | ED4_cursor *cursor = ¤t_cursor(); |
---|
861 | |
---|
862 | if (cursor->owner_of_cursor) { |
---|
863 | AW_root *root = aww->get_root(); |
---|
864 | char *helix_nr = root->awar(awar_name)->read_string(); |
---|
865 | BI_helix *helix = ED4_ROOT->helix; |
---|
866 | |
---|
867 | if (helix->has_entries()) { |
---|
868 | long pos = helix->first_position(helix_nr); |
---|
869 | |
---|
870 | if (pos == -1) { |
---|
871 | aw_message(GBS_global_string("No helix '%s' found", helix_nr)); |
---|
872 | } |
---|
873 | else { |
---|
874 | cursor->jump_sequence_pos(pos, ED4_JUMP_CENTERED); |
---|
875 | } |
---|
876 | } |
---|
877 | else { |
---|
878 | aw_message("Got no helix information"); |
---|
879 | } |
---|
880 | free(helix_nr); |
---|
881 | } |
---|
882 | } |
---|
883 | |
---|
884 | void ED4_set_iupac(AW_window *aww, const char *awar_name) { |
---|
885 | ED4_LocalWinContext uses(aww); |
---|
886 | ED4_cursor *cursor = ¤t_cursor(); |
---|
887 | |
---|
888 | if (cursor->owner_of_cursor) { |
---|
889 | if (cursor->in_consensus_terminal()) { |
---|
890 | aw_message("You cannot change the consensus"); |
---|
891 | } |
---|
892 | else { |
---|
893 | int len; |
---|
894 | char *seq = cursor->owner_of_cursor->resolve_pointer_to_string_copy(&len); |
---|
895 | int seq_pos = cursor->get_sequence_pos(); |
---|
896 | |
---|
897 | e4_assert(seq); |
---|
898 | |
---|
899 | if (seq_pos<len) { |
---|
900 | char *iupac = ED4_ROOT->aw_root->awar(awar_name)->read_string(); |
---|
901 | char new_char = iupac::encode(iupac, ED4_ROOT->alignment_type); |
---|
902 | |
---|
903 | seq[seq_pos] = new_char; |
---|
904 | cursor->owner_of_cursor->write_sequence(seq, len); |
---|
905 | |
---|
906 | free(iupac); |
---|
907 | } |
---|
908 | |
---|
909 | free(seq); |
---|
910 | } |
---|
911 | } |
---|
912 | } |
---|
913 | |
---|
914 | void ED4_exit() { |
---|
915 | // @@@ most of the code here could be placed in ED4_ROOT-dtor |
---|
916 | |
---|
917 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
---|
918 | ED4_ROOT->aw_root->unlink_awars_from_DB(gb_main); |
---|
919 | |
---|
920 | ED4_window *ed4w = ED4_ROOT->first_window; |
---|
921 | |
---|
922 | while (ed4w) { |
---|
923 | ed4w->aww->hide(); |
---|
924 | ed4w->cursor.prepare_shutdown(); // removes any callbacks |
---|
925 | ed4w = ed4w->next; |
---|
926 | } |
---|
927 | |
---|
928 | while (ED4_ROOT->first_window) { |
---|
929 | ED4_ROOT->first_window->delete_window(ED4_ROOT->first_window); |
---|
930 | } |
---|
931 | |
---|
932 | shutdown_macro_recording(ED4_ROOT->aw_root); |
---|
933 | delete ED4_ROOT; |
---|
934 | |
---|
935 | #if defined(DEBUG) |
---|
936 | AWT_browser_forget_db(gb_main); |
---|
937 | #endif // DEBUG |
---|
938 | GB_close(gb_main); |
---|
939 | |
---|
940 | ::exit(EXIT_SUCCESS); |
---|
941 | } |
---|
942 | |
---|
943 | void ED4_quit_editor(AW_window *aww) { |
---|
944 | ED4_LocalWinContext uses(aww); // @@@ dont use context here |
---|
945 | |
---|
946 | if (ED4_ROOT->first_window == current_ed4w()) { // quit button has been pressed in first window |
---|
947 | ED4_exit(); |
---|
948 | } |
---|
949 | // case : in another window close has been pressed |
---|
950 | current_aww()->hide(); |
---|
951 | current_ed4w()->is_hidden = true; |
---|
952 | } |
---|
953 | |
---|
954 | static int timer_calls = 0; |
---|
955 | static int timer_calls_triggered = 0; |
---|
956 | |
---|
957 | static unsigned ED4_timer(AW_root *) { |
---|
958 | timer_calls++; |
---|
959 | |
---|
960 | #if defined(TRACE_REFRESH) |
---|
961 | fprintf(stderr, "ED4_timer\n"); fflush(stderr); |
---|
962 | #endif |
---|
963 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
---|
964 | |
---|
965 | // get all changes from server |
---|
966 | GB_begin_transaction(gb_main); |
---|
967 | GB_tell_server_dont_wait(gb_main); |
---|
968 | GB_commit_transaction(gb_main); |
---|
969 | |
---|
970 | ED4_ROOT->refresh_all_windows(0); |
---|
971 | |
---|
972 | if (timer_calls == timer_calls_triggered) { |
---|
973 | timer_calls_triggered++; |
---|
974 | return 2000; // trigger callback after 2s |
---|
975 | } |
---|
976 | return 0; // do not trigger callback |
---|
977 | } |
---|
978 | |
---|
979 | void ED4_trigger_instant_refresh() { |
---|
980 | #if defined(TRACE_REFRESH) |
---|
981 | fprintf(stderr, "ED4_trigger_instant_refresh\n"); fflush(stderr); |
---|
982 | #endif |
---|
983 | timer_calls_triggered++; |
---|
984 | ED4_ROOT->aw_root->add_timed_callback(1, makeTimedCallback(ED4_timer)); // trigger instant callback |
---|
985 | } |
---|
986 | void ED4_request_full_refresh() { |
---|
987 | ED4_ROOT->main_manager->request_refresh(); |
---|
988 | } |
---|
989 | void ED4_request_full_instant_refresh() { |
---|
990 | ED4_request_full_refresh(); |
---|
991 | ED4_trigger_instant_refresh(); |
---|
992 | } |
---|
993 | |
---|
994 | void ED4_request_relayout() { |
---|
995 | ED4_resize_all_extensions(); |
---|
996 | ED4_ROOT->main_manager->request_resize(); |
---|
997 | ED4_trigger_instant_refresh(); |
---|
998 | } |
---|
999 | |
---|
1000 | #define SIGNIFICANT_FIELD_CHARS 30 // length used to compare field contents (in createGroupFromSelected) |
---|
1001 | |
---|
1002 | static void createGroupFromSelected(GB_CSTR group_name, GB_CSTR field_name, GB_CSTR field_content) { |
---|
1003 | // creates a new group named group_name |
---|
1004 | // if field_name==0 -> all selected species & subgroups are moved to this new group |
---|
1005 | // if field_name!=0 -> all selected species containing field_content in field field_name are moved to this new group |
---|
1006 | |
---|
1007 | ED4_multi_species_manager *top_multi_species_manager = ED4_ROOT->top_area_man->get_multi_species_manager(); |
---|
1008 | ED4_multi_species_manager *group_content_manager; |
---|
1009 | ED4_group_manager *new_group_manager = ED4_build_group_manager_start(top_multi_species_manager, group_name, 1, false, ED4_ROOT->ref_terminals, group_content_manager); |
---|
1010 | ED4_build_group_manager_end(group_content_manager); |
---|
1011 | |
---|
1012 | group_content_manager->update_requested_by_child(); |
---|
1013 | |
---|
1014 | ED4_counter++; |
---|
1015 | ED4_base::touch_world_cache(); |
---|
1016 | |
---|
1017 | bool lookingForNoContent = !field_content || field_content[0]==0; |
---|
1018 | |
---|
1019 | ED4_selected_elem *list_elem = ED4_ROOT->selected_objects->head(); |
---|
1020 | while (list_elem) { |
---|
1021 | ED4_base *object = list_elem->elem()->object; |
---|
1022 | object = object->get_parent(LEV_SPECIES); |
---|
1023 | |
---|
1024 | bool move_object = true; |
---|
1025 | if (object->is_consensus_manager()) { |
---|
1026 | object = object->get_parent(LEV_GROUP); |
---|
1027 | if (field_name) move_object = false; // don't move groups if moving by field_name |
---|
1028 | } |
---|
1029 | else { |
---|
1030 | e4_assert(object->is_species_manager()); |
---|
1031 | if (field_name) { |
---|
1032 | GBDATA *gb_species = object->get_species_pointer(); |
---|
1033 | GBDATA *gb_field = GB_search(gb_species, field_name, GB_FIND); |
---|
1034 | |
---|
1035 | move_object = lookingForNoContent; |
---|
1036 | if (gb_field) { // field was found |
---|
1037 | char *found_content = GB_read_as_string(gb_field); |
---|
1038 | if (found_content) { |
---|
1039 | move_object = strncmp(found_content, field_content, SIGNIFICANT_FIELD_CHARS)==0; |
---|
1040 | free(found_content); |
---|
1041 | } |
---|
1042 | } |
---|
1043 | } |
---|
1044 | } |
---|
1045 | |
---|
1046 | if (move_object) { |
---|
1047 | ED4_base *base = object->get_parent(LEV_MULTI_SPECIES); |
---|
1048 | if (base && base->is_multi_species_manager()) { |
---|
1049 | ED4_multi_species_manager *old_multi = base->to_multi_species_manager(); |
---|
1050 | old_multi->invalidate_species_counters(); |
---|
1051 | } |
---|
1052 | |
---|
1053 | object->parent->remove_member(object); |
---|
1054 | group_content_manager->append_member(object); |
---|
1055 | |
---|
1056 | object->parent = group_content_manager; |
---|
1057 | object->set_width(); |
---|
1058 | } |
---|
1059 | |
---|
1060 | list_elem = list_elem->next(); |
---|
1061 | } |
---|
1062 | |
---|
1063 | new_group_manager->create_consensus(new_group_manager, NULp); |
---|
1064 | group_content_manager->invalidate_species_counters(); |
---|
1065 | |
---|
1066 | new_group_manager->fold(); |
---|
1067 | |
---|
1068 | group_content_manager->resize_requested_by_child(); |
---|
1069 | } |
---|
1070 | |
---|
1071 | static void group_species(bool use_field, AW_window *use_as_main_window) { |
---|
1072 | GB_ERROR error = NULp; |
---|
1073 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
---|
1074 | GB_push_transaction(gb_main); |
---|
1075 | |
---|
1076 | ED4_LocalWinContext uses(use_as_main_window); |
---|
1077 | |
---|
1078 | if (!use_field) { |
---|
1079 | char *group_name = aw_input("Enter name for new group:"); |
---|
1080 | |
---|
1081 | if (group_name) { |
---|
1082 | if (strlen(group_name)>GB_GROUP_NAME_MAX) { |
---|
1083 | group_name[GB_GROUP_NAME_MAX] = 0; |
---|
1084 | aw_message("Truncated overlong group name"); |
---|
1085 | } |
---|
1086 | createGroupFromSelected(group_name, NULp, NULp); |
---|
1087 | free(group_name); |
---|
1088 | } |
---|
1089 | } |
---|
1090 | else { |
---|
1091 | char *field_name = ED4_ROOT->aw_root->awar(AWAR_FIELD_CHOSEN)->read_string(); |
---|
1092 | char *doneContents = ARB_strdup(";"); |
---|
1093 | size_t doneLen = 1; |
---|
1094 | |
---|
1095 | bool tryAgain = true; |
---|
1096 | bool foundField = false; |
---|
1097 | bool foundSpecies = false; |
---|
1098 | |
---|
1099 | if (strcmp(field_name, NO_FIELD_SELECTED) == 0) { |
---|
1100 | error = "Please select a field to use for grouping."; |
---|
1101 | } |
---|
1102 | |
---|
1103 | while (tryAgain && !error) { |
---|
1104 | tryAgain = false; |
---|
1105 | ED4_selected_elem *list_elem = ED4_ROOT->selected_objects->head(); |
---|
1106 | while (list_elem && !error) { |
---|
1107 | ED4_base *object = list_elem->elem()->object; |
---|
1108 | object = object->get_parent(LEV_SPECIES); |
---|
1109 | if (!object->is_consensus_manager()) { |
---|
1110 | GBDATA *gb_species = object->get_species_pointer(); |
---|
1111 | GBDATA *gb_field = NULp; |
---|
1112 | |
---|
1113 | if (gb_species) { |
---|
1114 | foundSpecies = true; |
---|
1115 | gb_field = GB_search(gb_species, field_name, GB_FIND); |
---|
1116 | } |
---|
1117 | |
---|
1118 | error = GB_incur_error_if(!gb_field); |
---|
1119 | if (!error) { |
---|
1120 | e4_assert(gb_field); |
---|
1121 | char *field_content = GB_read_as_string(gb_field); |
---|
1122 | if (field_content) { |
---|
1123 | size_t field_content_len = strlen(field_content); |
---|
1124 | |
---|
1125 | foundField = true; |
---|
1126 | if (field_content_len>SIGNIFICANT_FIELD_CHARS) { |
---|
1127 | field_content[SIGNIFICANT_FIELD_CHARS] = 0; |
---|
1128 | field_content_len = SIGNIFICANT_FIELD_CHARS; |
---|
1129 | } |
---|
1130 | |
---|
1131 | char with_semi[SIGNIFICANT_FIELD_CHARS+2+1]; |
---|
1132 | sprintf(with_semi, ";%s;", field_content); |
---|
1133 | |
---|
1134 | if (!strstr(doneContents, with_semi)) { // field_content was not used yet |
---|
1135 | createGroupFromSelected(field_content, field_name, field_content); |
---|
1136 | tryAgain = true; |
---|
1137 | |
---|
1138 | int newlen = doneLen + field_content_len + 1; |
---|
1139 | char *newDone = ARB_alloc<char>(newlen+1); |
---|
1140 | |
---|
1141 | GBS_global_string_to_buffer(newDone, newlen+1, "%s%s;", doneContents, field_content); |
---|
1142 | freeset(doneContents, newDone); |
---|
1143 | doneLen = newlen; |
---|
1144 | } |
---|
1145 | free(field_content); |
---|
1146 | } |
---|
1147 | else { |
---|
1148 | error = "Incompatible field type"; |
---|
1149 | } |
---|
1150 | } |
---|
1151 | } |
---|
1152 | list_elem = list_elem->next(); |
---|
1153 | } |
---|
1154 | } |
---|
1155 | |
---|
1156 | if (!error) { |
---|
1157 | if (!foundSpecies) error = "Please select some species in order to insert them into new groups"; |
---|
1158 | else if (!foundField) error = GBS_global_string("Field not found: '%s'%s", field_name, error ? GBS_global_string(" (Reason: %s)", error) : ""); |
---|
1159 | } |
---|
1160 | |
---|
1161 | free(doneContents); |
---|
1162 | free(field_name); |
---|
1163 | } |
---|
1164 | |
---|
1165 | GB_end_transaction_show_error(gb_main, error, aw_message); |
---|
1166 | } |
---|
1167 | |
---|
1168 | static void group_species_by_field_content(AW_window*, AW_window *use_as_main_window, AW_window *window_to_hide) { |
---|
1169 | group_species(true, use_as_main_window); |
---|
1170 | window_to_hide->hide(); |
---|
1171 | } |
---|
1172 | |
---|
1173 | static AW_window *create_group_species_by_field_window(AW_root *aw_root, AW_window *use_as_main_window) { |
---|
1174 | AW_window_simple *aws = new AW_window_simple; |
---|
1175 | |
---|
1176 | aws->init(aw_root, "CREATE_GROUP_USING_FIELD_CONTENT", "Create groups using field"); |
---|
1177 | aws->auto_space(10, 10); |
---|
1178 | |
---|
1179 | aws->button_length(10); |
---|
1180 | aws->at_newline(); |
---|
1181 | |
---|
1182 | aws->callback(AW_POPDOWN); |
---|
1183 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
1184 | |
---|
1185 | aws->callback(makeHelpCallback("group_by_field.hlp")); |
---|
1186 | aws->create_button("HELP", "HELP", "H"); |
---|
1187 | |
---|
1188 | aws->at_newline(); |
---|
1189 | aws->label("Use content of field"); |
---|
1190 | create_itemfield_selection_button(aws, FieldSelDef(AWAR_FIELD_CHOSEN, ED4_ROOT->get_gb_main(), SPECIES_get_selector(), FIELD_FILTER_STRING_READABLE, "group-field"), NULp); |
---|
1191 | |
---|
1192 | aws->at_newline(); |
---|
1193 | aws->callback(makeWindowCallback(group_species_by_field_content, use_as_main_window, static_cast<AW_window*>(aws))); |
---|
1194 | aws->create_autosize_button("USE_FIELD", "Group selected species by content", ""); |
---|
1195 | |
---|
1196 | return aws; |
---|
1197 | } |
---|
1198 | |
---|
1199 | void group_species_cb(AW_window *aww, bool use_fields) { |
---|
1200 | if (!use_fields) { |
---|
1201 | group_species(false, aww); |
---|
1202 | } |
---|
1203 | else { |
---|
1204 | static AW_window *ask_field_window; |
---|
1205 | |
---|
1206 | if (!ask_field_window) ask_field_window = create_group_species_by_field_window(ED4_ROOT->aw_root, aww); |
---|
1207 | ask_field_window->activate(); |
---|
1208 | } |
---|
1209 | } |
---|
1210 | |
---|
1211 | static GB_ERROR ED4_load_new_config(char *name) { |
---|
1212 | GB_ERROR error; |
---|
1213 | GBT_config cfg(ED4_ROOT->get_gb_main(), name, error); |
---|
1214 | if (cfg.exists()) { |
---|
1215 | ED4_ROOT->main_manager->clear_whole_background(); |
---|
1216 | |
---|
1217 | max_seq_terminal_length = 0; |
---|
1218 | |
---|
1219 | ED4_init_notFoundMessage(); |
---|
1220 | |
---|
1221 | if (ED4_ROOT->selected_objects->size() > 0) { |
---|
1222 | ED4_ROOT->deselect_all(); |
---|
1223 | } |
---|
1224 | |
---|
1225 | ED4_ROOT->remove_all_callbacks(); |
---|
1226 | |
---|
1227 | ED4_ROOT->scroll_picture.scroll = 0; |
---|
1228 | ED4_ROOT->scroll_picture.old_x = 0; |
---|
1229 | ED4_ROOT->scroll_picture.old_y = 0; |
---|
1230 | |
---|
1231 | ED4_ROOT->ref_terminals.clear(); |
---|
1232 | |
---|
1233 | for (ED4_window *window = ED4_ROOT->first_window; window; window=window->next) { |
---|
1234 | window->cursor.init(); |
---|
1235 | window->aww->set_horizontal_scrollbar_position (0); |
---|
1236 | window->aww->set_vertical_scrollbar_position (0); |
---|
1237 | } |
---|
1238 | |
---|
1239 | ED4_ROOT->scroll_links.link_for_hor_slider = NULp; |
---|
1240 | ED4_ROOT->scroll_links.link_for_ver_slider = NULp; |
---|
1241 | ED4_ROOT->middle_area_man = NULp; |
---|
1242 | ED4_ROOT->top_area_man = NULp; |
---|
1243 | |
---|
1244 | delete ED4_ROOT->main_manager; |
---|
1245 | ED4_ROOT->main_manager = NULp; |
---|
1246 | delete ED4_ROOT->ecoli_ref; |
---|
1247 | |
---|
1248 | ED4_ROOT->first_window->reset_all_for_new_config(); |
---|
1249 | ED4_ROOT->create_hierarchy(cfg.get_definition(GBT_config::MIDDLE_AREA), cfg.get_definition(GBT_config::TOP_AREA)); |
---|
1250 | } |
---|
1251 | |
---|
1252 | return error; |
---|
1253 | } |
---|
1254 | |
---|
1255 | static ED4_EDITMODE ED4_get_edit_mode(AW_root *root) { |
---|
1256 | if (!root->awar(AWAR_EDIT_MODE)->read_int()) return AD_ALIGN; |
---|
1257 | return root->awar(AWAR_INSERT_MODE)->read_int() ? AD_INSERT : AD_REPLACE; |
---|
1258 | } |
---|
1259 | |
---|
1260 | void ed4_changesecurity(AW_root *root) { |
---|
1261 | ED4_EDITMODE mode = ED4_get_edit_mode(root); |
---|
1262 | const char *awar_name = NULp; |
---|
1263 | |
---|
1264 | switch (mode) { |
---|
1265 | case AD_ALIGN: |
---|
1266 | awar_name = AWAR_EDIT_SECURITY_LEVEL_ALIGN; |
---|
1267 | break; |
---|
1268 | default: |
---|
1269 | awar_name = AWAR_EDIT_SECURITY_LEVEL_CHANGE; |
---|
1270 | } |
---|
1271 | |
---|
1272 | ED4_ROOT->aw_root->awar(AWAR_EDIT_SECURITY_LEVEL)->map(awar_name); |
---|
1273 | |
---|
1274 | long level = ED4_ROOT->aw_root->awar(awar_name)->read_int(); |
---|
1275 | GB_change_my_security(ED4_ROOT->get_gb_main(), level); // change user security in EDIT4 |
---|
1276 | } |
---|
1277 | |
---|
1278 | void ed4_change_edit_mode(AW_root *root) { |
---|
1279 | awar_edit_mode = ED4_get_edit_mode(root); |
---|
1280 | ed4_changesecurity(root); |
---|
1281 | } |
---|
1282 | |
---|
1283 | ARB_ERROR rebuild_consensus(ED4_base *object) { |
---|
1284 | if (object->is_consensus_manager()) { |
---|
1285 | ED4_species_manager *spec_man = object->to_species_manager(); |
---|
1286 | spec_man->do_callbacks(); |
---|
1287 | |
---|
1288 | ED4_base *sequence_data_terminal = spec_man->search_spec_child_rek(LEV_SEQUENCE_STRING); |
---|
1289 | sequence_data_terminal->request_refresh(); |
---|
1290 | } |
---|
1291 | return NULp; // needed by route_down_hierarchy |
---|
1292 | } |
---|
1293 | |
---|
1294 | void ED4_new_editor_window(AW_window *aww) { |
---|
1295 | ED4_LocalWinContext uses(aww); |
---|
1296 | |
---|
1297 | AW_device *device; |
---|
1298 | ED4_window *new_window = NULp; |
---|
1299 | |
---|
1300 | if (ED4_ROOT->generate_window(&device, &new_window) != ED4_R_BREAK) { |
---|
1301 | ED4_LocalWinContext now_uses(new_window); |
---|
1302 | |
---|
1303 | new_window->set_scrolled_rectangle(ED4_ROOT->scroll_links.link_for_hor_slider, |
---|
1304 | ED4_ROOT->scroll_links.link_for_ver_slider, |
---|
1305 | ED4_ROOT->scroll_links.link_for_hor_slider, |
---|
1306 | ED4_ROOT->scroll_links.link_for_ver_slider); |
---|
1307 | |
---|
1308 | new_window->aww->show(); |
---|
1309 | new_window->update_scrolled_rectangle(); |
---|
1310 | } |
---|
1311 | } |
---|
1312 | |
---|
1313 | |
---|
1314 | |
---|
1315 | static void ED4_start_editor_on_configuration(AW_window *aww) { |
---|
1316 | aww->hide(); |
---|
1317 | |
---|
1318 | GB_ERROR error; |
---|
1319 | { |
---|
1320 | char *name = aww->get_root()->awar(AWAR_EDIT_CONFIGURATION)->read_string(); |
---|
1321 | error = ED4_load_new_config(name); |
---|
1322 | free(name); |
---|
1323 | } |
---|
1324 | |
---|
1325 | if (error) { |
---|
1326 | aw_message(error); |
---|
1327 | aww->show(); // show old window |
---|
1328 | } |
---|
1329 | } |
---|
1330 | |
---|
1331 | struct cursorpos { |
---|
1332 | RefPtr<ED4_cursor> cursor; |
---|
1333 | int screen_rel; |
---|
1334 | int seq; |
---|
1335 | |
---|
1336 | cursorpos(ED4_window *win) |
---|
1337 | : cursor(&win->cursor), |
---|
1338 | screen_rel(cursor->get_screen_relative_pos()), |
---|
1339 | seq(cursor->get_sequence_pos()) |
---|
1340 | {} |
---|
1341 | }; |
---|
1342 | |
---|
1343 | |
---|
1344 | void ED4_compression_changed_cb(AW_root *awr) { |
---|
1345 | ED4_remap_mode mode = (ED4_remap_mode)awr->awar(ED4_AWAR_COMPRESS_SEQUENCE_TYPE)->read_int(); |
---|
1346 | int percent = awr->awar(ED4_AWAR_COMPRESS_SEQUENCE_PERCENT)->read_int(); |
---|
1347 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
1348 | |
---|
1349 | if (ED4_ROOT->root_group_man) { |
---|
1350 | vector<cursorpos> pos; |
---|
1351 | |
---|
1352 | for (ED4_window *win = ED4_ROOT->first_window; win; win = win->next) { |
---|
1353 | pos.push_back(cursorpos(win)); |
---|
1354 | } |
---|
1355 | |
---|
1356 | ED4_ROOT->root_group_man->remap()->set_mode(mode, percent); |
---|
1357 | ED4_resize_all_extensions(); |
---|
1358 | |
---|
1359 | for (vector<cursorpos>::iterator i = pos.begin(); i != pos.end(); ++i) { |
---|
1360 | ED4_cursor *cursor = i->cursor; |
---|
1361 | ED4_window *win = cursor->window(); |
---|
1362 | |
---|
1363 | win->update_scrolled_rectangle(); // @@@ needed ? |
---|
1364 | |
---|
1365 | cursor->jump_sequence_pos(i->seq, ED4_JUMP_KEEP_POSITION); |
---|
1366 | cursor->set_screen_relative_pos(i->screen_rel); |
---|
1367 | } |
---|
1368 | |
---|
1369 | ED4_request_full_instant_refresh(); |
---|
1370 | } |
---|
1371 | } |
---|
1372 | |
---|
1373 | void ED4_compression_toggle_changed_cb(AW_root *root, bool hideChanged) { |
---|
1374 | int gaps = root->awar(ED4_AWAR_COMPRESS_SEQUENCE_GAPS)->read_int(); |
---|
1375 | int hide = root->awar(ED4_AWAR_COMPRESS_SEQUENCE_HIDE)->read_int(); |
---|
1376 | |
---|
1377 | ED4_remap_mode mode = ED4_remap_mode(root->awar(ED4_AWAR_COMPRESS_SEQUENCE_TYPE)->read_int()); // @@@ mode is overwritten below |
---|
1378 | |
---|
1379 | if (hideChanged) { |
---|
1380 | // ED4_AWAR_COMPRESS_SEQUENCE_HIDE changed |
---|
1381 | if (hide!=0 && gaps!=2) { |
---|
1382 | root->awar(ED4_AWAR_COMPRESS_SEQUENCE_GAPS)->write_int(2); |
---|
1383 | return; |
---|
1384 | } |
---|
1385 | } |
---|
1386 | else { |
---|
1387 | // ED4_AWAR_COMPRESS_SEQUENCE_GAPS changed |
---|
1388 | if (gaps!=2 && hide!=0) { |
---|
1389 | root->awar(ED4_AWAR_COMPRESS_SEQUENCE_HIDE)->write_int(0); |
---|
1390 | return; |
---|
1391 | } |
---|
1392 | } |
---|
1393 | |
---|
1394 | mode = ED4_RM_NONE; |
---|
1395 | switch (gaps) { |
---|
1396 | case 0: mode = ED4_RM_NONE; break; |
---|
1397 | case 1: mode = ED4_RM_DYNAMIC_GAPS; break; |
---|
1398 | case 2: { |
---|
1399 | switch (hide) { |
---|
1400 | case 0: mode = ED4_RM_MAX_ALIGN; break; |
---|
1401 | case 1: mode = ED4_RM_SHOW_ABOVE; break; |
---|
1402 | default: e4_assert(0); break; |
---|
1403 | } |
---|
1404 | break; |
---|
1405 | } |
---|
1406 | default: e4_assert(0); break; |
---|
1407 | } |
---|
1408 | |
---|
1409 | root->awar(ED4_AWAR_COMPRESS_SEQUENCE_TYPE)->write_int(int(mode)); |
---|
1410 | } |
---|
1411 | |
---|
1412 | static AWT_config_mapping_def editor_options_config_mapping[] = { |
---|
1413 | { ED4_AWAR_COMPRESS_SEQUENCE_GAPS, "compressgaps" }, |
---|
1414 | { ED4_AWAR_COMPRESS_SEQUENCE_HIDE, "hidenucs" }, |
---|
1415 | { ED4_AWAR_COMPRESS_SEQUENCE_PERCENT, "hidepercent" }, |
---|
1416 | { AWAR_EDIT_HELIX_SPACING, "helixspacing" }, |
---|
1417 | { AWAR_EDIT_TERMINAL_SPACING, "terminalspacing" }, |
---|
1418 | { ED4_AWAR_SCROLL_SPEED_X, "scrollspeedx" }, |
---|
1419 | { ED4_AWAR_SCROLL_SPEED_Y, "scrollspeedy" }, |
---|
1420 | { ED4_AWAR_SCROLL_MARGIN, "scrollmargin" }, |
---|
1421 | { ED4_AWAR_GAP_CHARS, "gapchars" }, |
---|
1422 | { ED4_AWAR_DIGITS_AS_REPEAT, "digitsasrepeat" }, |
---|
1423 | { ED4_AWAR_FAST_CURSOR_JUMP, "fastcursorjump" }, |
---|
1424 | { ED4_AWAR_ANNOUNCE_CHECKSUM_CHANGES, "announcechecksumchanges" }, |
---|
1425 | |
---|
1426 | { NULp, NULp } |
---|
1427 | }; |
---|
1428 | |
---|
1429 | AW_window *ED4_create_editor_options_window(AW_root *root) { |
---|
1430 | AW_window_simple *aws = new AW_window_simple; |
---|
1431 | |
---|
1432 | aws->init(root, "EDIT4_PROPS", "EDIT4 Options"); |
---|
1433 | aws->load_xfig("edit4/options.fig"); |
---|
1434 | |
---|
1435 | aws->auto_space(5, 5); |
---|
1436 | |
---|
1437 | const int SCALEDCOLUMNS = 4; |
---|
1438 | const int SCALERLEN = 200; |
---|
1439 | |
---|
1440 | aws->callback(AW_POPDOWN); |
---|
1441 | aws->at("close"); |
---|
1442 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
1443 | |
---|
1444 | aws->callback(makeHelpCallback("e4_options.hlp")); |
---|
1445 | aws->at("help"); |
---|
1446 | aws->create_button("HELP", "HELP", "H"); |
---|
1447 | |
---|
1448 | // ----------------------------------- |
---|
1449 | // Online Sequence Compression |
---|
1450 | |
---|
1451 | aws->at("gaps"); |
---|
1452 | aws->create_toggle_field(ED4_AWAR_COMPRESS_SEQUENCE_GAPS); |
---|
1453 | aws->insert_default_toggle("Show all gaps", "A", 0); |
---|
1454 | aws->insert_toggle("Show some gaps", "S", 1); |
---|
1455 | aws->insert_toggle("Hide all gaps", "H", 2); |
---|
1456 | aws->update_toggle_field(); |
---|
1457 | |
---|
1458 | aws->at("hide"); |
---|
1459 | aws->create_toggle_field(ED4_AWAR_COMPRESS_SEQUENCE_HIDE); |
---|
1460 | aws->insert_default_toggle("Hide no Nucleotides", "0", 0); |
---|
1461 | aws->insert_toggle("Hide columns with less than...", "1", 1); |
---|
1462 | aws->update_toggle_field(); |
---|
1463 | |
---|
1464 | aws->at("percent"); |
---|
1465 | aws->create_input_field_with_scaler(ED4_AWAR_COMPRESS_SEQUENCE_PERCENT, SCALEDCOLUMNS, SCALERLEN, AW_SCALER_LINEAR); |
---|
1466 | |
---|
1467 | // -------------- |
---|
1468 | // Layout |
---|
1469 | |
---|
1470 | aws->at("seq_helix"); aws->create_input_field_with_scaler(AWAR_EDIT_HELIX_SPACING, SCALEDCOLUMNS, SCALERLEN, AW_SCALER_EXP_CENTER); |
---|
1471 | aws->at("seq_seq"); aws->create_input_field_with_scaler(AWAR_EDIT_TERMINAL_SPACING, SCALEDCOLUMNS, SCALERLEN, AW_SCALER_EXP_CENTER); |
---|
1472 | |
---|
1473 | // -------------------- |
---|
1474 | // Scroll-Speed |
---|
1475 | |
---|
1476 | aws->at("scroll_x"); |
---|
1477 | aws->create_input_field(ED4_AWAR_SCROLL_SPEED_X); |
---|
1478 | |
---|
1479 | aws->at("scroll_y"); |
---|
1480 | aws->create_input_field(ED4_AWAR_SCROLL_SPEED_Y); |
---|
1481 | |
---|
1482 | aws->at("margin"); |
---|
1483 | aws->create_input_field(ED4_AWAR_SCROLL_MARGIN); |
---|
1484 | |
---|
1485 | // --------------- |
---|
1486 | // Editing |
---|
1487 | |
---|
1488 | aws->at("gapchars"); |
---|
1489 | aws->create_input_field(ED4_AWAR_GAP_CHARS); |
---|
1490 | |
---|
1491 | aws->at("repeat"); |
---|
1492 | aws->create_toggle(ED4_AWAR_DIGITS_AS_REPEAT); |
---|
1493 | |
---|
1494 | aws->at("fast"); |
---|
1495 | aws->create_toggle(ED4_AWAR_FAST_CURSOR_JUMP); |
---|
1496 | |
---|
1497 | aws->at("checksum"); |
---|
1498 | aws->create_toggle(ED4_AWAR_ANNOUNCE_CHECKSUM_CHANGES); |
---|
1499 | |
---|
1500 | aws->at("config"); |
---|
1501 | AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "options", editor_options_config_mapping); |
---|
1502 | |
---|
1503 | return aws; |
---|
1504 | } |
---|
1505 | |
---|
1506 | static AWT_config_mapping_def consensus_config_mapping[] = { |
---|
1507 | { ED4_AWAR_CONSENSUS_COUNTGAPS, CONSENSUS_CONFIG_COUNTGAPS }, |
---|
1508 | { ED4_AWAR_CONSENSUS_GAPBOUND, CONSENSUS_CONFIG_GAPBOUND }, |
---|
1509 | { ED4_AWAR_CONSENSUS_GROUP, CONSENSUS_CONFIG_GROUP }, |
---|
1510 | { ED4_AWAR_CONSENSUS_CONSIDBOUND, CONSENSUS_CONFIG_CONSIDBOUND }, |
---|
1511 | { ED4_AWAR_CONSENSUS_UPPER, CONSENSUS_CONFIG_UPPER }, |
---|
1512 | { ED4_AWAR_CONSENSUS_LOWER, CONSENSUS_CONFIG_LOWER }, |
---|
1513 | |
---|
1514 | { NULp, NULp } |
---|
1515 | }; |
---|
1516 | |
---|
1517 | static AWT_predefined_config predefined_consensus_config[] = { |
---|
1518 | { "*create_species_from_consensus", "Consensus settings to create a new species \nfrom group consensus using\n\"Create/Create new species from consensus\"", "considbound='30';countgaps='0';gapbound='60';group='0';lower='0';upper='0'" }, |
---|
1519 | { NULp, NULp, NULp } |
---|
1520 | }; |
---|
1521 | |
---|
1522 | AW_window *ED4_create_consensus_definition_window(AW_root *root) { |
---|
1523 | // keep in sync with ../NTREE/AP_consensus.cxx@AP_create_con_expert_window |
---|
1524 | |
---|
1525 | static AW_window_simple *aws = NULp; |
---|
1526 | |
---|
1527 | if (!aws) { |
---|
1528 | aws = new AW_window_simple; |
---|
1529 | |
---|
1530 | aws->init(root, "EDIT4_CONSENSUS_DEFm", "EDIT4 Consensus Definition"); |
---|
1531 | aws->load_xfig("edit4/consensus.fig"); |
---|
1532 | |
---|
1533 | aws->auto_space(5, 5); |
---|
1534 | |
---|
1535 | const int SCALEDCOLUMNS = 3; |
---|
1536 | const int SCALERSIZE = 150; |
---|
1537 | |
---|
1538 | // top part of window: |
---|
1539 | aws->button_length(9); |
---|
1540 | |
---|
1541 | aws->callback(AW_POPDOWN); |
---|
1542 | aws->at("close"); |
---|
1543 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
1544 | |
---|
1545 | aws->callback(makeHelpCallback("e4_consensus.hlp")); |
---|
1546 | aws->at("help"); |
---|
1547 | aws->create_button("HELP", "HELP", "H"); |
---|
1548 | |
---|
1549 | // center part of window (same as in NTREE): |
---|
1550 | aws->at("countgaps"); |
---|
1551 | aws->create_toggle_field(ED4_AWAR_CONSENSUS_COUNTGAPS); |
---|
1552 | aws->insert_toggle("on", "1", 1); |
---|
1553 | aws->insert_default_toggle("off", "0", 0); |
---|
1554 | aws->update_toggle_field(); |
---|
1555 | |
---|
1556 | aws->at("gapbound"); |
---|
1557 | aws->create_input_field_with_scaler(ED4_AWAR_CONSENSUS_GAPBOUND, SCALEDCOLUMNS, SCALERSIZE, AW_SCALER_LINEAR); |
---|
1558 | |
---|
1559 | aws->at("group"); |
---|
1560 | aws->create_toggle_field(ED4_AWAR_CONSENSUS_GROUP); |
---|
1561 | aws->insert_toggle("on", "1", 1); |
---|
1562 | aws->insert_default_toggle("off", "0", 0); |
---|
1563 | aws->update_toggle_field(); |
---|
1564 | |
---|
1565 | aws->at("considbound"); |
---|
1566 | aws->create_input_field_with_scaler(ED4_AWAR_CONSENSUS_CONSIDBOUND, SCALEDCOLUMNS, SCALERSIZE, AW_SCALER_LINEAR); |
---|
1567 | |
---|
1568 | aws->at("showgroups"); |
---|
1569 | aws->callback(AWT_create_IUPAC_info_window); |
---|
1570 | aws->create_autosize_button("SHOW_IUPAC", "Show IUPAC groups", "S"); |
---|
1571 | |
---|
1572 | aws->at("upper"); |
---|
1573 | aws->create_input_field_with_scaler(ED4_AWAR_CONSENSUS_UPPER, SCALEDCOLUMNS, SCALERSIZE, AW_SCALER_LINEAR); |
---|
1574 | |
---|
1575 | aws->at("lower"); |
---|
1576 | aws->create_input_field_with_scaler(ED4_AWAR_CONSENSUS_LOWER, SCALEDCOLUMNS, SCALERSIZE, AW_SCALER_LINEAR); |
---|
1577 | |
---|
1578 | // bottom part of window: |
---|
1579 | aws->at("show"); |
---|
1580 | aws->label("Display consensus?"); |
---|
1581 | aws->create_toggle(ED4_AWAR_CONSENSUS_SHOW); |
---|
1582 | |
---|
1583 | aws->at("config"); |
---|
1584 | AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, CONSENSUS_CONFIG_ID, consensus_config_mapping, NULp, predefined_consensus_config); |
---|
1585 | } |
---|
1586 | |
---|
1587 | return aws; |
---|
1588 | } |
---|
1589 | |
---|
1590 | static void consensus_upper_lower_changed_cb(AW_root *awr, bool upper_changed) { |
---|
1591 | AW_awar *awar_lower = awr->awar(ED4_AWAR_CONSENSUS_LOWER); |
---|
1592 | AW_awar *awar_upper = awr->awar(ED4_AWAR_CONSENSUS_UPPER); |
---|
1593 | |
---|
1594 | int lower = awar_lower->read_int(); |
---|
1595 | int upper = awar_upper->read_int(); |
---|
1596 | |
---|
1597 | if (upper<lower) { |
---|
1598 | if (upper_changed) awar_lower->write_int(upper); |
---|
1599 | else awar_upper->write_int(lower); |
---|
1600 | } |
---|
1601 | ED4_consensus_definition_changed(awr); |
---|
1602 | } |
---|
1603 | |
---|
1604 | void ED4_create_consensus_awars(AW_root *aw_root) { |
---|
1605 | GB_transaction ta(ED4_ROOT->get_gb_main()); |
---|
1606 | |
---|
1607 | aw_root->awar_int(ED4_AWAR_CONSENSUS_COUNTGAPS, 1) ->add_callback(ED4_consensus_definition_changed); |
---|
1608 | aw_root->awar_int(ED4_AWAR_CONSENSUS_GROUP, 1) ->add_callback(ED4_consensus_definition_changed); |
---|
1609 | aw_root->awar_int(ED4_AWAR_CONSENSUS_GAPBOUND, 60)->set_minmax(0, 100)->add_callback(ED4_consensus_definition_changed); |
---|
1610 | aw_root->awar_int(ED4_AWAR_CONSENSUS_CONSIDBOUND, 30)->set_minmax(0, 100)->add_callback(ED4_consensus_definition_changed); |
---|
1611 | aw_root->awar_int(ED4_AWAR_CONSENSUS_UPPER, 95)->set_minmax(0, 100)->add_callback(makeRootCallback(consensus_upper_lower_changed_cb, true)); |
---|
1612 | aw_root->awar_int(ED4_AWAR_CONSENSUS_LOWER, 70)->set_minmax(0, 100)->add_callback(makeRootCallback(consensus_upper_lower_changed_cb, false)); |
---|
1613 | |
---|
1614 | AW_awar *cons_show = aw_root->awar_int(ED4_AWAR_CONSENSUS_SHOW, 1); |
---|
1615 | |
---|
1616 | cons_show->write_int(1); |
---|
1617 | cons_show->add_callback(ED4_consensus_display_changed); |
---|
1618 | } |
---|
1619 | |
---|
1620 | void ED4_reloadConfiguration(AW_window *aww) { |
---|
1621 | ED4_start_editor_on_configuration(aww); |
---|
1622 | } |
---|
1623 | |
---|
1624 | AW_window *ED4_create_loadConfiguration_window(AW_root *awr) { |
---|
1625 | static AW_window_simple *aws = NULp; |
---|
1626 | if (!aws) { |
---|
1627 | aws = new AW_window_simple; |
---|
1628 | aws->init(awr, "LOAD_CONFIGURATION", "Load existing configuration"); |
---|
1629 | aws->load_xfig("edit4/load_config.fig"); |
---|
1630 | |
---|
1631 | aws->at("close"); |
---|
1632 | aws->callback(AW_POPDOWN); |
---|
1633 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
1634 | |
---|
1635 | aws->at("help"); |
---|
1636 | aws->callback(makeHelpCallback("species_configs_saveload.hlp")); |
---|
1637 | aws->create_button("HELP", "HELP"); |
---|
1638 | |
---|
1639 | aws->at("confs"); |
---|
1640 | awt_create_CONFIG_selection_list(ED4_ROOT->get_gb_main(), aws, AWAR_EDIT_CONFIGURATION, false); |
---|
1641 | |
---|
1642 | aws->at("go"); |
---|
1643 | aws->callback(ED4_start_editor_on_configuration); |
---|
1644 | aws->create_button("LOAD", "LOAD"); |
---|
1645 | |
---|
1646 | aws->window_fit(); |
---|
1647 | } |
---|
1648 | return aws; |
---|
1649 | } |
---|
1650 | |
---|
1651 | void ED4_saveConfiguration(AW_window *aww, bool hide_aww) { |
---|
1652 | if (hide_aww) aww->hide(); |
---|
1653 | |
---|
1654 | char *name = aww->get_root()->awar(AWAR_EDIT_CONFIGURATION)->read_string(); |
---|
1655 | EDB_root_bact::save_current_config(name); |
---|
1656 | free(name); |
---|
1657 | } |
---|
1658 | |
---|
1659 | AW_window *ED4_create_saveConfigurationAs_window(AW_root *awr) { |
---|
1660 | static AW_window_simple *aws = NULp; |
---|
1661 | if (!aws) { |
---|
1662 | aws = new AW_window_simple; |
---|
1663 | aws->init(awr, "SAVE_CONFIGURATION", "Save current configuration as"); |
---|
1664 | aws->load_xfig("edit4/save_config.fig"); |
---|
1665 | |
---|
1666 | aws->at("close"); |
---|
1667 | aws->callback(AW_POPDOWN); |
---|
1668 | aws->create_button("CLOSE", "CLOSE"); |
---|
1669 | |
---|
1670 | aws->at("help"); |
---|
1671 | aws->callback(makeHelpCallback("species_configs_saveload.hlp")); |
---|
1672 | aws->create_button("HELP", "HELP"); |
---|
1673 | |
---|
1674 | aws->at("save"); |
---|
1675 | aws->create_input_field(AWAR_EDIT_CONFIGURATION); |
---|
1676 | |
---|
1677 | aws->at("confs"); |
---|
1678 | awt_create_CONFIG_selection_list(ED4_ROOT->get_gb_main(), aws, AWAR_EDIT_CONFIGURATION, false); |
---|
1679 | |
---|
1680 | aws->at("go"); |
---|
1681 | aws->callback(makeWindowCallback(ED4_saveConfiguration, true)); |
---|
1682 | aws->create_button("SAVE", "SAVE"); |
---|
1683 | } |
---|
1684 | return aws; |
---|
1685 | } |
---|
1686 | |
---|
1687 | static char *filter_loadable_SAIs(GBDATA *gb_sai) { |
---|
1688 | GBDATA *gb_ali = GB_search(gb_sai, ED4_ROOT->alignment_name, GB_FIND); |
---|
1689 | if (gb_ali) { |
---|
1690 | GBDATA *gb_data = GB_search(gb_ali, "data", GB_FIND); |
---|
1691 | if (gb_data) { |
---|
1692 | const char *sai_name = GBT_get_name(gb_sai); |
---|
1693 | if (sai_name && !ED4_find_SAI_name_terminal(sai_name)) { // if not loaded yet |
---|
1694 | return ARB_strdup(sai_name); |
---|
1695 | } |
---|
1696 | } |
---|
1697 | } |
---|
1698 | return NULp; |
---|
1699 | } |
---|
1700 | |
---|
1701 | AW_window *ED4_create_loadSAI_window(AW_root *awr) { |
---|
1702 | static AW_window_simple *aws = NULp; |
---|
1703 | if (!aws) { |
---|
1704 | aws = new AW_window_simple; |
---|
1705 | aws->init(awr, "LOAD_SAI", "Load additional SAI"); |
---|
1706 | aws->load_xfig("edit4/load_sai.fig"); |
---|
1707 | |
---|
1708 | aws->at("close"); |
---|
1709 | aws->callback(AW_POPDOWN); |
---|
1710 | aws->create_button("CLOSE", "CLOSE"); |
---|
1711 | |
---|
1712 | aws->at("help"); |
---|
1713 | aws->callback(makeHelpCallback("e4_get_species.hlp")); |
---|
1714 | aws->create_button("HELP", "HELP"); |
---|
1715 | |
---|
1716 | aws->at("sai"); |
---|
1717 | awt_create_SAI_selection_list(ED4_ROOT->get_gb_main(), aws, AWAR_SAI_NAME, false, makeSaiSelectionlistFilterCallback(filter_loadable_SAIs)); |
---|
1718 | ED4_ROOT->loadable_SAIs = LSAI_UPTODATE; |
---|
1719 | |
---|
1720 | aws->at("go"); |
---|
1721 | aws->callback(ED4_get_and_jump_to_selected_SAI); |
---|
1722 | aws->create_button("LOAD", "LOAD"); |
---|
1723 | } |
---|
1724 | return aws; |
---|
1725 | } |
---|
1726 | |
---|
1727 | static GB_ERROR createDataFromConsensus(GBDATA *gb_species, ED4_group_manager *group_man) { |
---|
1728 | GB_ERROR error = NULp; |
---|
1729 | |
---|
1730 | int len; |
---|
1731 | char *consensus = group_man->build_consensus_string(&len); |
---|
1732 | |
---|
1733 | char *equal_to = ED4_ROOT->aw_root->awar(ED4_AWAR_CREATE_FROM_CONS_REPL_EQUAL)->read_string(); |
---|
1734 | char *point_to = ED4_ROOT->aw_root->awar(ED4_AWAR_CREATE_FROM_CONS_REPL_POINT)->read_string(); |
---|
1735 | int allUpper = ED4_ROOT->aw_root->awar(ED4_AWAR_CREATE_FROM_CONS_ALL_UPPER)->read_int(); |
---|
1736 | |
---|
1737 | for (int p=0; p<len; p++) { |
---|
1738 | switch (consensus[p]) { |
---|
1739 | case '=': consensus[p] = equal_to[0]; break; |
---|
1740 | case '.': consensus[p] = point_to[0]; break; |
---|
1741 | default: { |
---|
1742 | if (allUpper) { |
---|
1743 | consensus[p] = toupper(consensus[p]); |
---|
1744 | } |
---|
1745 | break; |
---|
1746 | } |
---|
1747 | } |
---|
1748 | } |
---|
1749 | |
---|
1750 | if (ED4_ROOT->aw_root->awar(ED4_AWAR_CREATE_FROM_CONS_CREATE_POINTS)) { // points at start & end of sequence? |
---|
1751 | for (int p=0; p<len; p++) { |
---|
1752 | if (!ED4_is_gap_character(consensus[p])) break; |
---|
1753 | consensus[p] = '.'; |
---|
1754 | } |
---|
1755 | for (int p=len-1; p>=0; p--) { |
---|
1756 | if (!ED4_is_gap_character(consensus[p])) break; |
---|
1757 | consensus[p] = '.'; |
---|
1758 | } |
---|
1759 | } |
---|
1760 | |
---|
1761 | GB_CSTR ali = GBT_get_default_alignment(ED4_ROOT->get_gb_main()); |
---|
1762 | GBDATA *gb_ali = GB_search(gb_species, ali, GB_DB); |
---|
1763 | if (gb_ali) { |
---|
1764 | GBDATA *gb_data = GB_search(gb_ali, "data", GB_STRING); |
---|
1765 | error = GB_write_pntr(gb_data, consensus, len+1, len); |
---|
1766 | } |
---|
1767 | else { |
---|
1768 | error = GB_export_errorf("Can't find alignment '%s'", ali); |
---|
1769 | } |
---|
1770 | free(consensus); |
---|
1771 | return error; |
---|
1772 | } |
---|
1773 | |
---|
1774 | // -------------------------------------------------------------------------------- |
---|
1775 | |
---|
1776 | struct SpeciesMergeList { |
---|
1777 | GBDATA *species; |
---|
1778 | char *species_name; |
---|
1779 | |
---|
1780 | SpeciesMergeList *next; |
---|
1781 | }; |
---|
1782 | |
---|
1783 | static ARB_ERROR add_species_to_merge_list(ED4_base *base, SpeciesMergeList **smlp, GBDATA *gb_species_data) { |
---|
1784 | GB_ERROR error = NULp; |
---|
1785 | |
---|
1786 | if (base->is_species_name_terminal()) { |
---|
1787 | ED4_species_name_terminal *name_term = base->to_species_name_terminal(); |
---|
1788 | |
---|
1789 | if (!name_term->inside_consensus_manager()) { |
---|
1790 | char *species_name = name_term->resolve_pointer_to_string_copy(); |
---|
1791 | GBDATA *gb_species = GBT_find_species_rel_species_data(gb_species_data, species_name); |
---|
1792 | |
---|
1793 | if (gb_species) { |
---|
1794 | SpeciesMergeList *sml = new SpeciesMergeList; |
---|
1795 | |
---|
1796 | sml->species = gb_species; |
---|
1797 | sml->species_name = ARB_strdup(species_name); |
---|
1798 | sml->next = *smlp; |
---|
1799 | *smlp = sml; |
---|
1800 | } |
---|
1801 | else { |
---|
1802 | error = GB_append_exportedError(GBS_global_string("can't find species '%s'", species_name)); |
---|
1803 | } |
---|
1804 | |
---|
1805 | free(species_name); |
---|
1806 | } |
---|
1807 | } |
---|
1808 | return error; |
---|
1809 | } |
---|
1810 | static int SpeciesMergeListLength(SpeciesMergeList *sml) { |
---|
1811 | int length = 0; |
---|
1812 | |
---|
1813 | while (sml) { |
---|
1814 | length++; |
---|
1815 | sml = sml->next; |
---|
1816 | } |
---|
1817 | |
---|
1818 | return length; |
---|
1819 | } |
---|
1820 | static void freeSpeciesMergeList(SpeciesMergeList *sml) { |
---|
1821 | while (sml) { |
---|
1822 | free(sml->species_name); |
---|
1823 | freeset(sml, sml->next); |
---|
1824 | } |
---|
1825 | } |
---|
1826 | |
---|
1827 | // -------------------------------------------------------------------------------- |
---|
1828 | |
---|
1829 | inline bool nameIsUnique(const char *short_name, GBDATA *gb_species_data) { |
---|
1830 | return !GBT_find_species_rel_species_data(gb_species_data, short_name); |
---|
1831 | } |
---|
1832 | |
---|
1833 | |
---|
1834 | static void create_new_species(AW_window *, SpeciesCreationMode creation_mode) { |
---|
1835 | char *new_species_full_name = ED4_ROOT->aw_root->awar(ED4_AWAR_SPECIES_TO_CREATE)->read_string(); // this contains the full_name now! |
---|
1836 | ARB_ERROR error = NULp; |
---|
1837 | |
---|
1838 | e4_assert(creation_mode>=0 && creation_mode<=2); |
---|
1839 | |
---|
1840 | if (!new_species_full_name || new_species_full_name[0]==0) { |
---|
1841 | error = "Please enter a full_name for the new species"; |
---|
1842 | } |
---|
1843 | else { |
---|
1844 | ED4_MostRecentWinContext context; |
---|
1845 | |
---|
1846 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
---|
1847 | error = GB_begin_transaction(gb_main); |
---|
1848 | |
---|
1849 | GBDATA *gb_species_data = GBT_get_species_data(gb_main); |
---|
1850 | char *new_species_name = NULp; |
---|
1851 | char *acc = NULp; |
---|
1852 | char *addid = NULp; |
---|
1853 | |
---|
1854 | enum e_dataSource { MERGE_FIELDS, COPY_FIELDS } dataSource = (enum e_dataSource)ED4_ROOT->aw_root ->awar(ED4_AWAR_CREATE_FROM_CONS_DATA_SOURCE)->read_int(); |
---|
1855 | enum { NOWHERE, ON_SPECIES, ON_CONSENSUS } where_we_are = NOWHERE; |
---|
1856 | ED4_terminal *cursor_terminal = NULp; |
---|
1857 | |
---|
1858 | if (!error) { |
---|
1859 | if (creation_mode==CREATE_FROM_CONSENSUS || creation_mode==COPY_SPECIES) { |
---|
1860 | ED4_cursor *cursor = ¤t_cursor(); |
---|
1861 | |
---|
1862 | if (cursor->owner_of_cursor) { |
---|
1863 | cursor_terminal = cursor->owner_of_cursor; |
---|
1864 | where_we_are = cursor_terminal->is_consensus_terminal() ? ON_CONSENSUS : ON_SPECIES; |
---|
1865 | } |
---|
1866 | } |
---|
1867 | |
---|
1868 | if (creation_mode==COPY_SPECIES || (creation_mode==CREATE_FROM_CONSENSUS && dataSource==COPY_FIELDS)) { |
---|
1869 | if (where_we_are==ON_SPECIES) { |
---|
1870 | ED4_species_name_terminal *spec_name = cursor_terminal->to_sequence_terminal()->corresponding_species_name_terminal(); |
---|
1871 | const char *source_name = spec_name->resolve_pointer_to_char_pntr(); |
---|
1872 | GBDATA *gb_source = GBT_find_species_rel_species_data(gb_species_data, source_name); |
---|
1873 | |
---|
1874 | if (!gb_source) error = GBS_global_string("No such species: '%s'", source_name); |
---|
1875 | else { |
---|
1876 | GBDATA *gb_acc = GB_search(gb_source, "acc", GB_FIND); |
---|
1877 | if (gb_acc) acc = GB_read_string(gb_acc); // if has accession |
---|
1878 | |
---|
1879 | const char *add_field = AW_get_nameserver_addid(gb_main); |
---|
1880 | GBDATA *gb_addid = add_field[0] ? GB_search(gb_source, add_field, GB_FIND) : NULp; |
---|
1881 | if (gb_addid) addid = GB_read_as_string(gb_addid); |
---|
1882 | } |
---|
1883 | } |
---|
1884 | else { |
---|
1885 | error = "Please place cursor on a species"; |
---|
1886 | } |
---|
1887 | } |
---|
1888 | } |
---|
1889 | |
---|
1890 | if (!error) { |
---|
1891 | UniqueNameDetector *existingNames = NULp; |
---|
1892 | |
---|
1893 | if (creation_mode==0) { |
---|
1894 | error = "It's no good idea to create the short-name for a new species using the nameserver! (has no acc yet)"; |
---|
1895 | } |
---|
1896 | else { |
---|
1897 | error = AWTC_generate_one_name(gb_main, new_species_full_name, acc, addid, new_species_name); |
---|
1898 | if (!error) { // name was created |
---|
1899 | if (!nameIsUnique(new_species_name, gb_species_data)) { |
---|
1900 | if (!existingNames) existingNames = new UniqueNameDetector(gb_species_data); |
---|
1901 | freeset(new_species_name, AWTC_makeUniqueShortName(new_species_name, *existingNames)); |
---|
1902 | if (!new_species_name) error = GB_await_error(); |
---|
1903 | } |
---|
1904 | } |
---|
1905 | } |
---|
1906 | |
---|
1907 | if (error) { // try to make a random name |
---|
1908 | const char *msg = GBS_global_string("%s\nGenerating a random name instead.", error.deliver()); |
---|
1909 | aw_message(msg); |
---|
1910 | error = NULp; |
---|
1911 | |
---|
1912 | if (!existingNames) existingNames = new UniqueNameDetector(gb_species_data); |
---|
1913 | new_species_name = AWTC_generate_random_name(*existingNames); |
---|
1914 | |
---|
1915 | if (!new_species_name) { |
---|
1916 | error = GBS_global_string("Failed to create a new name for '%s'", new_species_full_name); |
---|
1917 | } |
---|
1918 | } |
---|
1919 | |
---|
1920 | if (existingNames) delete existingNames; |
---|
1921 | } |
---|
1922 | |
---|
1923 | if (!error) { |
---|
1924 | if (!error) { |
---|
1925 | if (creation_mode==CREATE_NEW_SPECIES) { |
---|
1926 | GBDATA *gb_created_species = GBT_find_or_create_species(gb_main, new_species_name, true); |
---|
1927 | if (!gb_created_species) { |
---|
1928 | error = GBS_global_string("Failed to create new species '%s'", new_species_name); |
---|
1929 | } |
---|
1930 | else { |
---|
1931 | GB_CSTR ali = GBT_get_default_alignment(gb_main); |
---|
1932 | GBDATA *gb_ali = GB_search(gb_created_species, ali, GB_DB); |
---|
1933 | |
---|
1934 | if (gb_ali) error = GBT_write_string(gb_ali, "data", "......."); |
---|
1935 | else error = GBS_global_string("Can't create alignment '%s' (Reason: %s)", ali, GB_await_error()); |
---|
1936 | } |
---|
1937 | if (!error) error = GBT_write_string(gb_created_species, "full_name", new_species_full_name); |
---|
1938 | } |
---|
1939 | else if (creation_mode==CREATE_FROM_CONSENSUS && dataSource==MERGE_FIELDS) { |
---|
1940 | // create from consensus (merge fields from all species in container) |
---|
1941 | if (where_we_are==NOWHERE) { |
---|
1942 | error = "Please place cursor on any sequence/consensus of group"; |
---|
1943 | } |
---|
1944 | else { |
---|
1945 | ED4_group_manager *group_man = cursor_terminal->get_parent(LEV_GROUP)->to_group_manager(); |
---|
1946 | SpeciesMergeList *sml = NULp; // list of species in group |
---|
1947 | |
---|
1948 | error = group_man->route_down_hierarchy(makeED4_route_cb(add_species_to_merge_list, &sml, gb_species_data)); |
---|
1949 | if (!error && !sml) { |
---|
1950 | error = "Please choose a none empty group!"; |
---|
1951 | } |
---|
1952 | |
---|
1953 | GBDATA *gb_new_species = NULp; |
---|
1954 | if (!error) { |
---|
1955 | GBDATA *gb_source = sml->species; |
---|
1956 | gb_new_species = GB_create_container(gb_species_data, "species"); |
---|
1957 | error = GB_copy_dropProtectMarksAndTempstate(gb_new_species, gb_source); // copy first found species to create a new species |
---|
1958 | } |
---|
1959 | if (!error) error = GBT_write_string(gb_new_species, "name", new_species_name); // insert new 'name' |
---|
1960 | if (!error) error = GBT_write_string(gb_new_species, "full_name", new_species_full_name); // insert new 'full_name' |
---|
1961 | if (!error) error = createDataFromConsensus(gb_new_species, group_man); // insert consensus as 'data' |
---|
1962 | |
---|
1963 | if (!error) { |
---|
1964 | char *doneFields = ARB_strdup(";name;full_name;"); // all fields which are already merged |
---|
1965 | int doneLen = strlen(doneFields); |
---|
1966 | SpeciesMergeList *sl = sml; |
---|
1967 | int sl_length = SpeciesMergeListLength(sml); |
---|
1968 | int *fieldStat = new int[sl_length]; // 0 = not used yet ; -1 = don't has field ; 1..n = field content, same number means same content |
---|
1969 | |
---|
1970 | arb_progress progress("Merging fields", long(sl_length)); |
---|
1971 | |
---|
1972 | while (sl && !error) { // with all species do.. |
---|
1973 | char *newFields = GB_get_subfields(sl->species); |
---|
1974 | char *fieldStart = newFields; // points to ; before next field |
---|
1975 | |
---|
1976 | while (fieldStart[1] && !error) { // with all subfields of the species do.. |
---|
1977 | char *fieldEnd = strchr(fieldStart+1, ';'); |
---|
1978 | |
---|
1979 | e4_assert(fieldEnd); |
---|
1980 | char behind = fieldEnd[1]; |
---|
1981 | fieldEnd[1] = 0; |
---|
1982 | |
---|
1983 | if (!strstr(doneFields, fieldStart)) { // field is not merged yet |
---|
1984 | char *fieldName = fieldStart+1; |
---|
1985 | int fieldLen = int(fieldEnd-fieldName); |
---|
1986 | |
---|
1987 | e4_assert(fieldEnd[0]==';'); |
---|
1988 | fieldEnd[0] = 0; |
---|
1989 | |
---|
1990 | GBDATA *gb_field = GB_search(sl->species, fieldName, GB_FIND); |
---|
1991 | e4_assert(gb_field); // field has to exist, cause it was found before |
---|
1992 | |
---|
1993 | GB_TYPES type = GB_read_type(gb_field); |
---|
1994 | if (type==GB_STRING) { // we only merge string fields |
---|
1995 | int i; |
---|
1996 | int doneSpecies = 0; |
---|
1997 | int nextStat = 1; |
---|
1998 | |
---|
1999 | for (i=0; i<sl_length; i++) { // clear field status |
---|
2000 | fieldStat[i] = 0; |
---|
2001 | } |
---|
2002 | |
---|
2003 | while (doneSpecies<sl_length) { // since all species in list were handled |
---|
2004 | SpeciesMergeList *sl2 = sml; |
---|
2005 | i = 0; |
---|
2006 | |
---|
2007 | while (sl2) { |
---|
2008 | if (fieldStat[i]==0) { |
---|
2009 | gb_field = GB_search(sl2->species, fieldName, GB_FIND); |
---|
2010 | if (gb_field) { |
---|
2011 | char *content = GB_read_as_string(gb_field); |
---|
2012 | SpeciesMergeList *sl3 = sl2->next; |
---|
2013 | |
---|
2014 | fieldStat[i] = nextStat; |
---|
2015 | doneSpecies++; |
---|
2016 | int j = i+1; |
---|
2017 | while (sl3) { |
---|
2018 | if (fieldStat[j]==0) { |
---|
2019 | gb_field = GB_search(sl3->species, fieldName, GB_FIND); |
---|
2020 | if (gb_field) { |
---|
2021 | char *content2 = GB_read_as_string(gb_field); |
---|
2022 | |
---|
2023 | if (strcmp(content, content2)==0) { // if contents are the same, they get the same status |
---|
2024 | fieldStat[j] = nextStat; |
---|
2025 | doneSpecies++; |
---|
2026 | } |
---|
2027 | free(content2); |
---|
2028 | } |
---|
2029 | else { |
---|
2030 | fieldStat[j] = -1; |
---|
2031 | doneSpecies++; |
---|
2032 | } |
---|
2033 | } |
---|
2034 | sl3 = sl3->next; |
---|
2035 | j++; |
---|
2036 | } |
---|
2037 | |
---|
2038 | free(content); |
---|
2039 | nextStat++; |
---|
2040 | } |
---|
2041 | else { |
---|
2042 | fieldStat[i] = -1; // field does not exist here |
---|
2043 | doneSpecies++; |
---|
2044 | } |
---|
2045 | } |
---|
2046 | sl2 = sl2->next; |
---|
2047 | i++; |
---|
2048 | } |
---|
2049 | } |
---|
2050 | |
---|
2051 | e4_assert(nextStat!=1); // this would mean that none of the species contained the field |
---|
2052 | |
---|
2053 | { |
---|
2054 | char *new_content = NULp; |
---|
2055 | int new_content_len = 0; |
---|
2056 | |
---|
2057 | if (nextStat==2) { // all species contain same field content or do not have the field |
---|
2058 | SpeciesMergeList *sl2 = sml; |
---|
2059 | |
---|
2060 | while (sl2) { |
---|
2061 | gb_field = GB_search(sl2->species, fieldName, GB_FIND); |
---|
2062 | if (gb_field) { |
---|
2063 | new_content = GB_read_as_string(gb_field); |
---|
2064 | new_content_len = strlen(new_content); // @@@ new_content_len never used |
---|
2065 | break; |
---|
2066 | } |
---|
2067 | sl2 = sl2->next; |
---|
2068 | } |
---|
2069 | } |
---|
2070 | else { // different field contents |
---|
2071 | int currStat; |
---|
2072 | for (currStat=1; currStat<nextStat; currStat++) { |
---|
2073 | int names_len = 1; // open bracket |
---|
2074 | SpeciesMergeList *sl2 = sml; |
---|
2075 | i = 0; |
---|
2076 | char *content = NULp; |
---|
2077 | |
---|
2078 | while (sl2) { |
---|
2079 | if (fieldStat[i]==currStat) { |
---|
2080 | names_len += strlen(sl2->species_name)+1; |
---|
2081 | if (!content) { |
---|
2082 | gb_field = GB_search(sl2->species, fieldName, GB_FIND); |
---|
2083 | e4_assert(gb_field); |
---|
2084 | content = GB_read_as_string(gb_field); |
---|
2085 | } |
---|
2086 | } |
---|
2087 | sl2 = sl2->next; |
---|
2088 | i++; |
---|
2089 | } |
---|
2090 | |
---|
2091 | e4_assert(content); |
---|
2092 | int add_len = names_len+1+strlen(content); |
---|
2093 | char *whole = ARB_alloc<char>(new_content_len+1+add_len+1); |
---|
2094 | e4_assert(whole); |
---|
2095 | char *add = new_content ? whole+sprintf(whole, "%s ", new_content) : whole; |
---|
2096 | sl2 = sml; |
---|
2097 | i = 0; |
---|
2098 | int first = 1; |
---|
2099 | while (sl2) { |
---|
2100 | if (fieldStat[i]==currStat) { |
---|
2101 | add += sprintf(add, "%c%s", first ? '{' : ';', sl2->species_name); |
---|
2102 | first = 0; |
---|
2103 | } |
---|
2104 | sl2 = sl2->next; |
---|
2105 | i++; |
---|
2106 | } |
---|
2107 | add += sprintf(add, "} %s", content); |
---|
2108 | |
---|
2109 | free(content); |
---|
2110 | |
---|
2111 | freeset(new_content, whole); |
---|
2112 | new_content_len = strlen(new_content); |
---|
2113 | } |
---|
2114 | } |
---|
2115 | |
---|
2116 | if (new_content) { |
---|
2117 | error = GBT_write_string(gb_new_species, fieldName, new_content); |
---|
2118 | free(new_content); |
---|
2119 | } |
---|
2120 | } |
---|
2121 | } |
---|
2122 | |
---|
2123 | // mark field as done: |
---|
2124 | char *new_doneFields = ARB_alloc<char>(doneLen+fieldLen+1+1); |
---|
2125 | sprintf(new_doneFields, "%s%s;", doneFields, fieldName); |
---|
2126 | doneLen += fieldLen+1; |
---|
2127 | freeset(doneFields, new_doneFields); |
---|
2128 | |
---|
2129 | fieldEnd[0] = ';'; |
---|
2130 | } |
---|
2131 | |
---|
2132 | fieldEnd[1] = behind; |
---|
2133 | fieldStart = fieldEnd; |
---|
2134 | } |
---|
2135 | free(newFields); |
---|
2136 | sl = sl->next; |
---|
2137 | progress.inc_and_check_user_abort(error); |
---|
2138 | } |
---|
2139 | free(doneFields); |
---|
2140 | delete [] fieldStat; |
---|
2141 | } |
---|
2142 | freeSpeciesMergeList(sml); sml = NULp; |
---|
2143 | } |
---|
2144 | } |
---|
2145 | else { // copy species or create from consensus (copy fields from one species) |
---|
2146 | e4_assert(where_we_are==ON_SPECIES); |
---|
2147 | |
---|
2148 | ED4_species_name_terminal *spec_name = cursor_terminal->to_sequence_terminal()->corresponding_species_name_terminal(); |
---|
2149 | const char *source_name = spec_name->resolve_pointer_to_char_pntr(); |
---|
2150 | GBDATA *gb_source = GBT_find_species_rel_species_data(gb_species_data, source_name); |
---|
2151 | |
---|
2152 | if (gb_source) { |
---|
2153 | GBDATA *gb_new_species = GB_create_container(gb_species_data, "species"); |
---|
2154 | error = GB_copy_dropProtectMarksAndTempstate(gb_new_species, gb_source); |
---|
2155 | if (!error) error = GBT_write_string(gb_new_species, "name", new_species_name); |
---|
2156 | if (!error) error = GBT_write_string(gb_new_species, "full_name", new_species_full_name); // insert new 'full_name' |
---|
2157 | if (!error && creation_mode==CREATE_FROM_CONSENSUS) { |
---|
2158 | ED4_group_manager *group_man = cursor_terminal->get_parent(LEV_GROUP)->to_group_manager(); |
---|
2159 | error = createDataFromConsensus(gb_new_species, group_man); |
---|
2160 | } |
---|
2161 | } |
---|
2162 | else { |
---|
2163 | error = GBS_global_string("Can't find species '%s'", source_name); |
---|
2164 | } |
---|
2165 | } |
---|
2166 | } |
---|
2167 | |
---|
2168 | error = GB_end_transaction(gb_main, error); |
---|
2169 | if (!error) ED4_get_and_jump_to_species(new_species_name); |
---|
2170 | } |
---|
2171 | else { |
---|
2172 | GB_abort_transaction(gb_main); |
---|
2173 | } |
---|
2174 | |
---|
2175 | free(addid); |
---|
2176 | free(acc); |
---|
2177 | free(new_species_name); |
---|
2178 | } |
---|
2179 | |
---|
2180 | aw_message_if(error); |
---|
2181 | free(new_species_full_name); |
---|
2182 | } |
---|
2183 | |
---|
2184 | AW_window *ED4_create_new_seq_window(AW_root *root, SpeciesCreationMode creation_mode) { |
---|
2185 | e4_assert(valid(creation_mode)); |
---|
2186 | |
---|
2187 | AW_window_simple *aws = new AW_window_simple; |
---|
2188 | switch (creation_mode) { |
---|
2189 | case CREATE_NEW_SPECIES: aws->init(root, "create_species", "Create species"); break; |
---|
2190 | case CREATE_FROM_CONSENSUS: aws->init(root, "create_species_from_consensus", "Create species from consensus"); break; |
---|
2191 | case COPY_SPECIES: aws->init(root, "copy_species", "Copy current species"); break; |
---|
2192 | } |
---|
2193 | |
---|
2194 | if (creation_mode==CREATE_FROM_CONSENSUS) { |
---|
2195 | aws->load_xfig("edit4/create_seq_fc.fig"); |
---|
2196 | } |
---|
2197 | else { |
---|
2198 | aws->load_xfig("edit4/create_seq.fig"); |
---|
2199 | } |
---|
2200 | |
---|
2201 | aws->callback(AW_POPDOWN); |
---|
2202 | aws->at("close"); |
---|
2203 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
2204 | |
---|
2205 | aws->at("label"); |
---|
2206 | aws->create_autosize_button(NULp, "Please enter the FULL_NAME\nof the new species"); |
---|
2207 | |
---|
2208 | aws->at("input"); |
---|
2209 | aws->create_input_field(ED4_AWAR_SPECIES_TO_CREATE, 30); |
---|
2210 | |
---|
2211 | aws->at("ok"); |
---|
2212 | aws->callback(makeWindowCallback(create_new_species, creation_mode)); |
---|
2213 | aws->create_button("GO", "GO", "g"); |
---|
2214 | |
---|
2215 | if (creation_mode==CREATE_FROM_CONSENSUS) { |
---|
2216 | aws->at("replace_equal"); |
---|
2217 | aws->label("Replace '=' by "); |
---|
2218 | aws->create_input_field(ED4_AWAR_CREATE_FROM_CONS_REPL_EQUAL, 1); |
---|
2219 | |
---|
2220 | aws->at("replace_point"); |
---|
2221 | aws->label("Replace '.' by "); |
---|
2222 | aws->create_input_field(ED4_AWAR_CREATE_FROM_CONS_REPL_POINT, 1); |
---|
2223 | |
---|
2224 | aws->at("replace_start_end"); |
---|
2225 | aws->label("Create ... at ends of sequence?"); |
---|
2226 | aws->create_toggle(ED4_AWAR_CREATE_FROM_CONS_CREATE_POINTS); |
---|
2227 | |
---|
2228 | aws->at("upper"); |
---|
2229 | aws->label("Convert all chars to upper?"); |
---|
2230 | aws->create_toggle(ED4_AWAR_CREATE_FROM_CONS_ALL_UPPER); |
---|
2231 | |
---|
2232 | aws->at("data"); |
---|
2233 | aws->label("Other fields"); |
---|
2234 | aws->create_option_menu(ED4_AWAR_CREATE_FROM_CONS_DATA_SOURCE, true); |
---|
2235 | aws->insert_default_option("Merge from all in group", "", 0); |
---|
2236 | aws->insert_option("Copy from current species", "", 1); |
---|
2237 | aws->update_option_menu(); |
---|
2238 | } |
---|
2239 | |
---|
2240 | return aws; |
---|
2241 | } |
---|
2242 | |
---|