Removed 'create subanalysis' from menu for now (until implemented)
[mussa.git] / gui / SetupWindow.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/SetupWindow.hpp"
12 #include "alg/mussa.hpp"
13 #include "mussa_exceptions.hpp"
14 #include <iostream>
15 #include <FL/Fl_File_Chooser.H>
16
17 using namespace std;
18
19 bool
20 SetupWindow::done()
21 {
22   return all_done;
23 }
24
25
26 void
27 do_analysis_cb(Fl_Button* o, void* v)
28 {
29   SetupWindow* T=(SetupWindow*)v;
30   T->real_do_analysis();
31 }
32
33
34 // passes the parameters to the mussa class object and orders it to run
35 void
36 SetupWindow::real_do_analysis()
37 {
38   int i;
39   string err_msg;
40
41   // <-- need a check here to make sure all vars have a valid value
42
43   an_analysis->clear();
44   an_analysis->set_name(analysis_name);
45   an_analysis->set_window(window);
46   an_analysis->set_threshold(threshold);
47
48   for(i=0; i < seq_num; i++) {
49     an_analysis->load_sequence(seq_files[i], annot_files[i], fasta_indices[i], 
50                                sub_seq_starts[i], sub_seq_ends[i]);
51   }
52
53   try {
54     an_analysis->analyze();
55   } catch (mussa_analysis_error e) {
56     fl_alert(e.what());
57   }
58
59   all_done = true;
60
61   hide();
62 }
63
64
65 void
66 choose_seq_file_cb(Fl_Button* o, void* v)
67 {
68   seq_data_instance * blah = (seq_data_instance *) v;
69   SetupWindow* T= blah->sw_ptr;
70   T->real_choose_seq_file_cb(blah->index);
71 }
72
73
74 void
75 SetupWindow::real_choose_seq_file_cb(int i)
76 {
77   char *picked_file;
78   string a_file_path;
79
80   picked_file = fl_file_chooser("Find an Analysis", "", "", 1);
81   a_file_path = picked_file;
82   seq_files[i] = a_file_path;
83   seq_inputs[i]->value((const char *)picked_file);
84 }
85
86
87 void
88 seq_file_in_cb(Fl_Input* o, void* v)
89
90   seq_data_instance * blah = (seq_data_instance *) v;
91   SetupWindow* T= blah->sw_ptr;
92   T->real_seq_file_in_cb(o,blah->index);
93 }
94
95
96 void
97 SetupWindow::real_seq_file_in_cb(Fl_Input* o, int i)
98 {
99   seq_files[i] = o->value();
100   // leaving this as a reminder that bools will need to be set to determine
101   // if all needed fields have been filled with some value before an analysis
102   // is attempted
103   //(*the_motifs)[i].dirty = true;
104 }
105
106 //char *picked_file;
107 //picked_file = fl_file_chooser("Find an Analysis", "", "", 1);
108
109 // *** Annot file selection
110
111 void
112 choose_annot_file_cb(Fl_Button* o, void* v)
113 {
114   seq_data_instance * blah = (seq_data_instance *) v;
115   SetupWindow* T= blah->sw_ptr;
116   T->real_choose_annot_file_cb(blah->index);
117 }
118
119
120 void
121 SetupWindow::real_choose_annot_file_cb(int i)
122 {
123   char *picked_file;
124   string a_file_path;
125
126   picked_file = fl_file_chooser("Find an Analysis", "", "", 1);
127   a_file_path = picked_file;
128   annot_files[i] = a_file_path;
129   annot_inputs[i]->value((const char *)picked_file);
130 }
131
132 void
133 annot_file_in_cb(Fl_Input* o, void* v)
134
135   seq_data_instance * blah = (seq_data_instance *) v;
136   SetupWindow* T= blah->sw_ptr;
137   T->real_annot_file_in_cb(o,blah->index);
138 }
139
140 void
141 SetupWindow::real_annot_file_in_cb(Fl_Input* o, int i)
142 {
143   annot_files[i] = o->value();
144 }
145
146
147 // *** fasta index selection
148
149 void
150 fa_index_in_cb(Fl_Input* o, void* v)
151
152   seq_data_instance * blah = (seq_data_instance *) v;
153   SetupWindow* T= blah->sw_ptr;
154   T->real_fa_index_in_cb(o,blah->index);
155 }
156
157 void
158 SetupWindow::real_fa_index_in_cb(Fl_Input* o, int i)
159 {
160   fasta_indices[i] = atoi(o->value());
161 }
162
163 // *** subsequence start select
164
165 void
166 sub_start_in_cb(Fl_Input* o, void* v)
167
168   seq_data_instance * blah = (seq_data_instance *) v;
169   SetupWindow* T= blah->sw_ptr;
170   T->real_sub_start_in_cb(o,blah->index);
171 }
172
173 void
174 SetupWindow::real_sub_start_in_cb(Fl_Input* o, int i)
175 {
176   sub_seq_starts[i] = atoi(o->value());
177 }
178
179 // *** subsequence end select
180
181 void
182 sub_end_in_cb(Fl_Input* o, void* v)
183
184   seq_data_instance * blah = (seq_data_instance *) v;
185   SetupWindow* T= blah->sw_ptr;
186   T->real_sub_end_in_cb(o,blah->index);
187 }
188
189 void
190 SetupWindow::real_sub_end_in_cb(Fl_Input* o, int i)
191 {
192   sub_seq_ends[i] = atoi(o->value());
193
194 }
195
196 // *** input analysis name
197
198 void
199 set_ana_name_cb(Fl_Input* o, void* v)
200 {
201   SetupWindow* T=(SetupWindow*)v;
202   T->real_set_ana_name_cb(o);
203 }
204
205 void
206 SetupWindow::real_set_ana_name_cb(Fl_Input* o)
207 {
208   analysis_name = o->value();
209 }
210
211 // *** input window size
212
213 void
214 set_win_size_cb(Fl_Input* o, void* v)
215 {
216   SetupWindow* T=(SetupWindow*)v;
217   T->real_set_win_size_cb(o);
218 }
219
220 void
221 SetupWindow::real_set_win_size_cb(Fl_Input* o)
222 {
223   window = atoi(o->value());
224 }
225
226 // *** input threshold
227
228 void
229 set_threshold_cb(Fl_Input* o, void* v)
230 {
231   SetupWindow* T=(SetupWindow*)v;
232   T->real_set_threshold_cb(o);
233 }
234
235 void
236 SetupWindow::real_set_threshold_cb(Fl_Input* o)
237 {
238   threshold = atoi(o->value());
239 }
240
241 void
242 set_seq_num_cb(Fl_Input* o, void* v)
243 {
244   SetupWindow* T=(SetupWindow*)v;
245   T->real_set_seq_num_cb(o);
246 }
247
248 // *** input number of sequences, add appropriate inputs for each sequence
249
250 void
251 SetupWindow::real_set_seq_num_cb(Fl_Input* o)
252 {
253   int i;
254
255   seq_num = atoi(o->value());
256   seq_inputs.clear();
257   annot_inputs.clear();
258
259   seq_scroll = new Fl_Scroll(5,80,w()-10,h()-115);
260   seq_scroll->color(FL_WHITE);
261
262   for (i = 0; i < seq_num; i++)
263   {
264     seq_files.push_back("");
265     annot_files.push_back("");
266     fasta_indices.push_back(1);
267     sub_seq_starts.push_back(0);
268     sub_seq_ends.push_back(0);
269     add_seq_input(i);
270   }
271   seq_scroll->box(FL_DOWN_FRAME);
272   add(seq_scroll);
273   redraw();
274 }
275
276
277 SetupWindow::SetupWindow(int w, int h, const char* title, Mussa *the_analysis):Fl_Window(w,h,title)
278 {
279   color(FL_WHITE);
280
281   an_analysis = the_analysis;
282   all_done = false;
283
284   begin();
285
286   name_input = new Fl_Input(110, 10, w-120, 30, "Analysis Name ");
287   name_input->value("");
288   //name_input->when(FL_WHEN_ENTER_KEY);
289   name_input->box(FL_BORDER_BOX);
290   name_input->callback((Fl_Callback*)set_ana_name_cb, this);
291
292   // add in radio buttons for win_append and thres_append eventually
293   win_append = true;
294   thres_append = true;
295
296   win_input = new Fl_Input(65,45,100,30, "Window ");
297   win_input->value("");
298   win_input->callback((Fl_Callback*)set_win_size_cb, this);
299
300   thres_input = new Fl_Input(250,45,100,30, "Threshold ");
301   thres_input->value("");
302   thres_input->callback((Fl_Callback*)set_threshold_cb, this);
303
304   seq_num_input = new Fl_Input(480,45,100, 30, "Number of Seqs");
305   seq_num_input->value("");
306   seq_num_input->callback((Fl_Callback*)set_seq_num_cb, this);
307
308   // button to launch the analysis
309   test = new Fl_Button(w-100,h-30,100,30,"Do Analysis");
310   test->callback((Fl_Callback*)do_analysis_cb, this);
311
312   // test button to make sure setup data is being stored
313   test = new Fl_Button(w-200,h-30,100,30,"show setup");
314   test->callback((Fl_Callback*)print_cb, this);
315
316   // generic input, keeping around as template for future additions
317   //_input = new Fl_Input(0, , 30, "");
318   //_input->value("");
319   //_input->when(FL_WHEN_ENTER_KEY);
320   //_input->callback((Fl_Callback*)set__cb, this);
321
322   end();
323   //resizable(this);
324   show();
325 }
326
327
328 SetupWindow::~SetupWindow(){}
329
330
331 void
332 SetupWindow::add_seq_input(int i)
333 {
334   Fl_Input *fasta_input, *start_input, *end_input;
335   Fl_Button *seq_browse, *annot_browse;
336
337
338   // this remembers what seq index each input is associated with
339   seq_data_instance * something;
340   something = new seq_data_instance;
341   something->sw_ptr = this;
342   something->index = i;
343
344
345   // setup sequence file input for text input or file browsing
346   seq_inputs.push_back(new Fl_Input(55, 85+(i*105), w()-165, 30, "seq "));
347   seq_inputs[i]->value("");
348   seq_inputs[i]->callback((Fl_Callback*)seq_file_in_cb, (void*) something);
349
350   seq_browse = new Fl_Button(w()-105, 85+(i*105), 80, 30, "Browse");
351   seq_browse->callback((Fl_Callback*)choose_seq_file_cb, (void*) something);
352
353   // setup annotation file input for text input or file browsing
354   annot_inputs.push_back(new Fl_Input(55, 120+(i*105), w()-165, 30, "annot "));
355   annot_inputs[i]->value("");
356   annot_inputs[i]->callback((Fl_Callback*)annot_file_in_cb,(void*)something);
357
358   annot_browse = new Fl_Button(w()-105, 120+(i*105), 80, 30, "Browse");
359   annot_browse->callback((Fl_Callback*)choose_annot_file_cb, (void*)something);
360
361   // index of fasta seq wanted if more than 1 seq in file
362   fasta_input = new Fl_Input(90, 155+(i*105), 50, 30, "fasta index");
363   fasta_input->value("");
364   fasta_input->callback((Fl_Callback*)fa_index_in_cb, (void*) something);
365
366   // subsequence select, index of first bp wanted
367   start_input = new Fl_Input(225, 155+(i*105), 50, 30, "start index");
368   start_input->value("");
369   start_input->callback((Fl_Callback*)sub_start_in_cb, (void*) something);
370
371   // subsequence select, index of last bp wanted
372   end_input = new Fl_Input(355, 155+(i*105), 50, 30, "end index");
373   end_input->value("");
374   end_input->callback((Fl_Callback*)sub_end_in_cb, (void*) something);
375
376   seq_scroll->add(seq_inputs[i]);
377   seq_scroll->add(seq_browse);
378   seq_scroll->add(annot_inputs[i]);
379   seq_scroll->add(annot_browse);
380   seq_scroll->add(fasta_input);
381   seq_scroll->add(start_input);
382   seq_scroll->add(end_input);
383 }
384
385 /*
386   cout << "fee\n";
387   cout << "fie\n";
388   cout << "foe\n";
389   cout << "fum\n";
390 */
391
392 void SetupWindow::print_cb(Fl_Button* o, void* v)
393 {
394   SetupWindow* T=(SetupWindow*)v;
395   T->print_cb_real(o,v);
396 }
397
398
399 void SetupWindow::print_cb_real(Fl_Button* , void*)
400 {
401   cout << "analysis_name: " << analysis_name << endl;
402   cout << "win: " << window << endl;
403   cout << "thres: " << threshold << endl;
404   cout << "seq_num: " << seq_num << endl;
405   //cout << "win_add: " << win_append << end;
406   //cout << "thres_add: " << thres_append << endl;
407
408   int i;
409
410   
411
412   for (i = 0; i < seq_num; i++)
413   {
414     cout << seq_files[i] << endl; 
415     cout << annot_files[i] << endl;
416     cout << "fasta: " << fasta_indices[i];
417     cout << " start: " << sub_seq_starts[i];
418     cout << " end: " << sub_seq_ends[i] << endl;
419     cout << "blargle!\n";
420   }
421   cout << "end of inputs\n" << endl;
422 }