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