There can be only one (filename convention)
[mussa.git] / gui / ConnWindow.cpp
1 //  This file is part of the Mussa source distribution.
2 //  http://mussa.caltech.edu/
3 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
4
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
9
10
11 #include "gui/ConnWindow.hpp"
12 #include "mussa_exceptions.hpp"
13
14 #include <iostream>
15 using namespace std;
16
17 void
18 load_ana_cb(Fl_Button* o, void* v)
19 {
20   ConnWindow* T=(ConnWindow*)v;
21   T->real_load_ana_cb();
22 }
23
24
25 void
26 ConnWindow::real_load_ana_cb()
27 {
28   char *picked_file;
29   string a_file_path;
30   string err_msg;
31
32   cout << "Load!  Load!  Load, ye yellow-bellied seadogs!\n";
33   //picked_file = fl_file_chooser("Find an Analysis", "", "", 1);
34   picked_file = fl_dir_chooser("Find an Analysis", "", 1);
35   if (picked_file != NULL)
36   {
37     a_file_path = picked_file;
38     cout << "doo wah diddy diddy dum diddy doo\n";
39     cout << a_file_path << endl;
40
41     // load the analysis
42     an_analysis = new Mussa();
43     try {
44       an_analysis->load(a_file_path);
45     } catch (mussa_load_error e) {
46       fl_alert(e.what());
47       return;
48     }
49     //.substr(0,a_file_path.find(".mu")));
50
51     // relabel window with the analysis name
52     window_name = "Mussa: " + an_analysis->get_name();
53     label((const char*)window_name.c_str());
54     // show the user the analysis
55     conn_box->setup(an_analysis->get_name(), an_analysis->size(), 
56                     an_analysis->get_window(), &(an_analysis->the_seqs), 
57                     &(an_analysis->the_paths));
58     conn_box->scale_paths();
59   }
60 }
61
62
63 void
64 do_ana_cb(Fl_Button* o, void* v)
65 {
66   ConnWindow* T=(ConnWindow*)v;
67   T->real_do_ana_cb();
68 }
69
70
71 void
72 ConnWindow::real_do_ana_cb()
73 {
74   char *picked_file;
75   string a_file_path;
76   string err_msg;
77
78   picked_file = fl_file_chooser("Analysis Config File", "", "", 1);
79   if (picked_file != NULL)
80   {
81     a_file_path = picked_file;
82
83     an_analysis = new Mussa();
84     try {
85       an_analysis->load_mupa_file(a_file_path);
86     } catch (mussa_load_error e) {
87       fl_alert(e.what());
88       return;
89     }
90
91     try {
92       an_analysis->analyze(0, 0, Mussa::TransitiveNway, 0.0);
93       // relabel window with the analysis name
94       window_name = "Mussa: " + an_analysis->get_name();
95       label((const char*)window_name.c_str());
96       // show the user the analysis
97       conn_box->setup(an_analysis->get_name(), an_analysis->size(), 
98                       an_analysis->get_window(), &(an_analysis->the_seqs), 
99                       &(an_analysis->the_paths));
100       conn_box->scale_paths();
101     } catch (mussa_analysis_error e) {
102       fl_alert(e.what());
103     }
104   }
105   conn_box->redraw();
106 }
107
108
109 void
110 setup_ana_cb(Fl_Button* o, void* v)
111 {
112   ConnWindow* T=(ConnWindow*)v;
113   T->real_setup_ana_cb();
114 }
115
116 void
117 ConnWindow::real_setup_ana_cb()
118 {
119   an_analysis = new Mussa();
120   setup_win = new SetupWindow(650,700,"Analysis Setup", an_analysis);
121
122   while(setup_win->visible())
123     Fl::wait(.1);
124   cout << "Like a warm summer day\n";
125
126   if (setup_win->done())
127   {
128     // relabel window with the analysis name
129     window_name = "Mussa: " + an_analysis->get_name();
130     label((const char*)window_name.c_str());
131     // is it this easy?  what if setup window encounters an error...??
132     conn_box->setup(an_analysis->get_name(), an_analysis->size(), 
133                     an_analysis->get_window(), &(an_analysis->the_seqs), 
134                     &(an_analysis->the_paths));
135
136     conn_box->scale_paths();
137   }
138 }
139
140
141 void
142 subana_cb(Fl_Button* o, void* v)
143 {
144   ConnWindow* T=(ConnWindow*)v;
145   T->real_subana_cb();
146 }
147
148 void
149 ConnWindow::real_subana_cb()
150 {
151   string subana_name;
152
153   if (an_analysis == 0 || an_analysis->the_seqs.size() == 0) {
154     fl_message("please load an analysis before creating a subanalysis");
155     return;
156   }
157     
158   sub_analysis = new Mussa();
159   subana_win = new SubAnalysisWindow(500,400,"Sub Analysis Setup", sub_analysis,
160                                 an_analysis->the_seqs);
161
162   while(subana_win->visible())
163     Fl::wait(.1);
164   cout << "Like a warm summer day\n";
165
166   if (subana_win->done())
167   {
168     subana_name = "SubMussa: " + sub_analysis->get_name(); 
169     sub_conn_win = new ConnWindow(w(), h(), (const char*) subana_name.c_str());
170     cout << "Like a warm day in may\n";
171     sub_conn_win->add_ana(sub_analysis);
172     //Fl::visual(FL_DOUBLE|FL_INDEX);
173     //sub_conn_box->show();
174   }
175 }
176
177
178 void
179 seq_show_cb(Fl_Button* o, void* v)
180 {
181   ConnWindow* T=(ConnWindow*)v;
182   T->real_seq_show_cb();
183 }
184
185 void
186 ConnWindow::real_seq_show_cb()
187 {
188   show_seq_win = new SeqTextWindow(500,400,"Seq Show", an_analysis->the_seqs);
189 }
190
191 void
192 seq_win_spawn_cb(Fl_Button* o, void* v)
193 {
194   ConnWindow* T=(ConnWindow*)v;
195   T->real_seq_win_spawn_cb();
196 }
197
198 void
199 ConnWindow::real_seq_win_spawn_cb()
200 {
201   conn_box->spawnSeq();
202 }
203
204 void
205 motif_find_cb(Fl_Button* o, void* v)
206 {
207   ConnWindow* T=(ConnWindow*)v;
208   T->real_motif_find_cb();
209 }
210
211 void
212 ConnWindow::real_motif_find_cb()
213 {
214   conn_box->find_motifs();
215 }
216
217 void
218 annot_win_cb(Fl_Button* o, void* v)
219 {
220   ConnWindow* T=(ConnWindow*)v;
221   T->real_annot_win_cb();
222 }
223
224 void
225 ConnWindow::real_annot_win_cb()
226 {
227   conn_box->annot_win();
228 }
229
230
231 // all the crap needed for dealing with the scale bars
232 void
233 toggle_scale_bars_cb(Fl_Button* o, void* v)
234 {
235   ConnWindow* T=(ConnWindow*)v;
236   T->real_toggle_bars_cb();
237 }
238
239 void
240 ConnWindow::real_toggle_bars_cb()
241 {
242   conn_box->toggle_bars();
243 }
244
245 void
246 set_bar_len_cb(Fl_Input* o, void* v)
247 {
248   ConnWindow* T=(ConnWindow*)v;
249   cout << "WAAAAAAAAA\n";
250   T->real_set_bar_len_cb(o);
251 }
252
253 void
254 ConnWindow::real_set_bar_len_cb(Fl_Input* o)
255 {
256   int new_bar_len;
257  
258   new_bar_len = atoi(o->value());
259   cout << "new bar len = " << new_bar_len << endl;
260   conn_box->set_bar_interval(new_bar_len);
261 }
262
263
264 // all the crap needed for dealing with the scale lines
265 void
266 toggle_scale_lines_cb(Fl_Button* o, void* v)
267 {
268   ConnWindow* T=(ConnWindow*)v;
269   T->real_toggle_lines_cb();
270 }
271
272 void
273 ConnWindow::real_toggle_lines_cb()
274 {
275   conn_box->toggle_lines();
276 }
277
278 void
279 set_line_len_cb(Fl_Input* o, void* v)
280 {
281   ConnWindow* T=(ConnWindow*)v;
282   cout << "WAAAAAAAAA\n";
283   T->real_set_line_len_cb(o);
284 }
285
286 void
287 ConnWindow::real_set_line_len_cb(Fl_Input* o)
288 {
289   int new_line_len;
290  
291   new_line_len = atoi(o->value());
292   cout << "new line len = " << new_line_len << endl;
293   conn_box->set_line_interval(new_line_len);
294 }
295
296
297 // setting new soft threshold
298
299 void
300 set_soft_thres_cb(Fl_Input* o, void* v)
301 {
302   ConnWindow* T=(ConnWindow*)v;
303   cout << "WAAAAAAAAA\n";
304   T->real_set_soft_thres_cb(o);
305 }
306
307 void
308 ConnWindow::real_set_soft_thres_cb(Fl_Input* o)
309 {
310   int new_soft_thres;
311  
312   new_soft_thres = atoi(o->value());
313   cout << "new soft thres = " << new_soft_thres << endl;
314
315   an_analysis->set_soft_thres(new_soft_thres);
316   an_analysis->set_analysis_mode(Mussa::TransitiveNway);
317   an_analysis->nway();
318   conn_box->scale_paths();
319   conn_box->redraw();
320
321   // hacked in stuff to save muway files at different thresholds
322   string save_path;
323   ostringstream append_info;
324
325   save_path = an_analysis->get_name() + "/"
326     + an_analysis->get_name().substr(0,an_analysis->get_name().find("_t"));
327   append_info.str("");
328   append_info << "_t" << new_soft_thres << ".muway";
329   //<<  "_w" << an_analysis->window
330   save_path += append_info.str();
331   cout << "saving as: " << save_path << endl;
332   an_analysis->save_muway(save_path);
333 }
334
335
336
337
338 ConnWindow::ConnWindow(int w, int h, const char* title):
339   Fl_Double_Window(w,h,title)
340 {
341   int button_len = 120;
342   Fl_Color a_color = fl_rgb_color(150, 200, 255);
343
344   padding = 5;
345
346   begin();
347   //fl_color(150,200,255);
348   color(FL_WHITE);
349   resizable(this);
350
351  
352   // create file menu button
353   file_menu = new Fl_Menu_Button(padding, 2, button_len, 30, "Analysis");
354   file_menu->color(FL_WHITE,a_color);
355   file_menu->box(FL_BORDER_BOX);
356   file_menu->clear();
357
358   // add menu items
359   file_menu->add("Do Analysis", 0, (Fl_Callback *) do_ana_cb, this);
360   file_menu->add("Load Analysis", 0, (Fl_Callback *) load_ana_cb, this);
361   file_menu->add("Setup Analysis", 0, (Fl_Callback *) setup_ana_cb, this);
362   file_menu->add("Sub Analysis", 0, (Fl_Callback *) subana_cb, this);
363
364
365   view_menu = new Fl_Menu_Button(padding+button_len,2, button_len, 30, "View");
366   view_menu->color(FL_WHITE,a_color);
367   view_menu->box(FL_BORDER_BOX);
368   view_menu->clear();
369   view_menu->add("Sequence Zoom", 0, (Fl_Callback*) seq_win_spawn_cb, this);
370   view_menu->add("Motif Finder", 0, (Fl_Callback*) motif_find_cb, this);
371   view_menu->add("Annotations", 0, (Fl_Callback*) annot_win_cb, this);
372   view_menu->add("Copy Seq", 0, (Fl_Callback*) seq_show_cb, this);
373   view_menu->add("Toggle Bars", 0, (Fl_Callback*) toggle_scale_bars_cb, this, 
374                  FL_MENU_TOGGLE|FL_MENU_VALUE);
375   view_menu->add("Toggle Lines", 0, (Fl_Callback*) toggle_scale_lines_cb, this,
376                  FL_MENU_TOGGLE|FL_MENU_VALUE);
377
378   bar_input = new Fl_Input(padding+3*button_len, 2, button_len, 30, "Bar Length (bp)");
379   bar_input->value("");
380   bar_input->when(FL_WHEN_ENTER_KEY);
381   bar_input->callback((Fl_Callback*)set_bar_len_cb, this);
382
383   line_input = new Fl_Input(padding+5*button_len, 2, button_len, 30, "Line Interval (bp)");
384   line_input->value("");
385   line_input->when(FL_WHEN_ENTER_KEY);
386   line_input->callback((Fl_Callback*)set_line_len_cb, this);
387
388
389   thres_input = new Fl_Input(padding+7*button_len, 2, button_len, 30, "Threshold");
390   thres_input->value("");
391   thres_input->when(FL_WHEN_ENTER_KEY);
392   thres_input->callback((Fl_Callback*)set_soft_thres_cb, this);
393
394
395   // create the connections box
396   conn_box = new ConnView(padding, padding + 30, w-2*padding, h-2*padding-30);
397   //conn_box = new ConnView(padding, padding, w-2*padding, h-2*padding);
398
399   end();
400   show();
401 }
402
403
404 ConnWindow::~ConnWindow()
405 {}
406
407
408 void
409 ConnWindow::add_ana(Mussa *the_ana)
410 {
411   an_analysis = the_ana;
412
413   // relabel window with the analysis name
414   window_name = "Mussa: " + an_analysis->get_name();
415   label((const char*)window_name.c_str());
416
417   conn_box->setup(an_analysis->get_name(), an_analysis->size(), 
418                   an_analysis->get_window(), &(an_analysis->the_seqs), 
419                   &(an_analysis->the_paths));
420   conn_box->scale_paths();
421 }