1 // This file is part of the Mussa source distribution.
2 // http://mussa.caltech.edu/
3 // Contact author: Tristan De Buysscher, tristan@caltech.edu
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.
11 #include "ConnView.hh"
17 #include <FL/fl_draw.H>
22 ConnView::setup(string name, int sq_num, int win_len,
23 vector<Sequence> *some_seqs,
24 Nway_Paths *some_paths)
26 int i, i2, seq_length;
29 annot_color new_annot;
38 cout << "num of paths = " << some_paths->refined_pathz.size() << endl;
40 cout << "x()=" << x() << " y()=" << y() << " w()=" << w() << " h()=" << h();
44 for(i = 0; i < seq_num; ++i)
47 cout << a_seq.len() << endl;
48 seq_length = a_seq.len();
49 seq_lens.push_back(seq_length);
50 if (seq_length > max_seq_len)
51 max_seq_len = seq_length;
52 if (seq_length < 1000)
53 seq_scales.push_back(1);
54 else if (seq_length < 1000000)
55 seq_scales.push_back(2);
57 seq_scales.push_back(3);
63 x_scale_factor = (float) max_seq_len / (w() - name_pad);
64 cout << "scale factor is " << x_scale_factor << endl;
65 y_seq_incre = (h()-(y_pad*2)) / (seq_num - 1);
78 bar_interval = max_seq_len / 20;
79 line_interval = max_seq_len / 20;
84 for(i = 0; i < 5; ++i)
86 blank_motif = new_blank_motif();
87 some_motifs.push_back(blank_motif);
90 // find all unique annotation type ids
92 vector<annot_color>::iterator annot_type_i;
93 list<annot>::iterator annot_i;
95 // loop thru all sequences
96 for(i2 = 0; i2 < seq_num; i2++)
98 cout << "ConnView: annotation loop\n";
99 // loop thru all annotation entries in this sequence
100 for(annot_i = (*S)[i2].annots.begin(); annot_i != (*S)[i2].annots.end(); ++annot_i)
102 cout << annot_i->start << ", " << annot_i->end << " : ";
103 cout << annot_i->name << " " << annot_i->type << endl;
104 // loop thru annotation types
105 annot_type_i = some_annots.begin();
106 bool type_unfound = true;
107 while ((annot_type_i != some_annots.end()) && type_unfound)
109 if (annot_i->type == annot_type_i->type)
110 type_unfound = false; // stop search if found
113 //if unfound, then add to list
116 new_annot = new_blank_annot();
117 new_annot.type = annot_i->type;
118 some_annots.push_back(new_annot);
126 ConnView::scale_paths()
129 list<vector<int> >::iterator pathz_i;
133 scaled_pathz.clear();
135 for(pathz_i = P->refined_pathz.begin(); pathz_i != P->refined_pathz.end(); ++pathz_i)
138 for(i2 = 0; i2 <= seq_num; i2++)
139 a_path[i2] = (int) (a_path[i2] / x_scale_factor);
140 scaled_pathz.push_back(a_path);
145 ConnView::toggle_bars()
147 show_bars = !show_bars;
152 ConnView::set_bar_interval(int new_bar_len)
154 bar_interval = new_bar_len;
155 cout << "bar interval = " << bar_interval << endl;
160 ConnView::toggle_lines()
162 show_lines = !show_lines;
167 ConnView::set_line_interval(int new_line_len)
169 line_interval = new_line_len;
170 cout << "line interval = " << line_interval << endl;
178 // this is temporary - check if new motifs have been added
181 // clear drawing area and set background to white
183 fl_rectf(x(), y(), w(), h());
186 // draw the scale indicators if they are on
187 // put into own method soon...
188 int i, div_num, x_loc;
189 float div_len_scaled;
193 div_num = max_seq_len / bar_interval;
194 div_len_scaled = ((float) bar_interval) / x_scale_factor;
195 fl_color(230,230,230);
196 for(i = 0; i <= div_num; i+=2)
198 x_loc = (int)(i * div_len_scaled+x());
199 fl_rectf(x_loc, y(), (int) div_len_scaled, h());
205 div_num = max_seq_len / line_interval;
206 div_len_scaled = ((float) line_interval) / x_scale_factor;
208 fl_line_style(FL_SOLID, 1, NULL);
209 for(i = 0; i <= div_num; i++)
211 x_loc = (int)(i * div_len_scaled+x());
212 fl_line(x_loc, y(), x_loc, h()+y());
217 // divide up the space with some light blue lines
218 fl_color(150,200,255);
219 fl_line_style(FL_SOLID, 2, NULL);
220 fl_rect(x(), y(), w(), h());
221 fl_line(w()+x()-name_pad, y(), w()+x()-name_pad, h()+y());
225 // white out any overdraw from path area into name/info area
227 fl_rectf(w()+x()-name_pad, y(), w()+x(), h());
229 draw_sequence_stuff();
231 // draw selection box
236 fl_line_style(FL_SOLID, 1, NULL);
237 fl_rect(drag_start,(ref_seq_num*y_seq_incre)+y(),drag_end-drag_start,
245 ConnView::draw_paths()
247 list<vector<int> >::iterator i;
248 int i2, i3, y_loc, x_loc, x_start, x_end;
250 list<bool>::iterator highlight_i;
253 int path_start, path_end;
257 // determine which paths to highlight
259 for(i = scaled_pathz.begin(); i != scaled_pathz.end(); ++i)
262 // determine if path falls within the selected region and mark it for
264 path_start = abs(a_path[ref_seq_num+1]);
265 path_end = path_start + a_path[0];
266 if ( ( (path_start >= drag_start-x()) && (path_end <= drag_end-x()) ) ||
267 ( (path_start < drag_start-x()) && (path_end > drag_end-x()) ) ||
268 ( (path_start < drag_start-x()) && (path_end > drag_start-x()) ) ||
269 ( (path_start < drag_end-x()) && (path_end > drag_end-x()) ) )
270 highlight.push_back(true);
272 highlight.push_back(false);
275 fl_line_style(FL_SOLID, 1, NULL);
276 // draw non-highlight paths (ie not in the selection box)
277 highlight_i = highlight.begin();
278 for(i = scaled_pathz.begin(); i != scaled_pathz.end(); ++i)
283 // get window size to determine line width
284 window_size = a_path[0];
285 // make sure width is at least 1 - might be zero to my slack rounding
286 if (window_size == 0)
290 for(i2 = 1; i2 < seq_num; i2++)
293 // ugh, an xor...only want blue if one of the nodes is rc
294 if ( ((a_path[i2] < 0) || (a_path[i2+1] < 0)) &&
295 !((a_path[i2] < 0) && (a_path[i2+1] < 0)) )
296 fl_color(200,200,255);
298 fl_color(255,200,200);
300 fl_line_style(FL_SOLID, 1, NULL);
301 fl_polygon((int)abs(a_path[i2])+x(), y_loc + 3,
302 (int)abs(a_path[i2])+window_size+x(), y_loc + 3,
303 (int)abs(a_path[i2+1])+window_size+x(), y_loc + y_seq_incre - 3,
304 (int)abs(a_path[i2+1])+x(), y_loc + y_seq_incre - 3);
306 y_loc += y_seq_incre;
311 // draw highlighted paths (ie in or partially in selection)
312 // drawing these separately and after other paths so they are on top
313 highlight_i = highlight.begin();
314 for(i = scaled_pathz.begin(); i != scaled_pathz.end(); ++i)
319 // get window size to determine line width
320 window_size = a_path[0];
321 // make sure width is at least 1 - might be zero to my slack rounding
322 if (window_size == 0)
326 for(i2 = 1; i2 < seq_num; i2++)
329 // ugh, an xor...only want blue if one of the nodes is rc
330 if ( ((a_path[i2] < 0) || (a_path[i2+1] < 0)) &&
331 !((a_path[i2] < 0) && (a_path[i2+1] < 0)) )
336 fl_polygon((int)abs(a_path[i2])+x(), y_loc + 3,
337 (int)abs(a_path[i2])+window_size+x(), y_loc + 3,
338 (int)abs(a_path[i2+1])+window_size+x(), y_loc + y_seq_incre - 3,
339 (int)abs(a_path[i2+1])+x(), y_loc + y_seq_incre - 3);
341 y_loc += y_seq_incre;
348 list<vector<int> >::iterator i;
351 int path_start, path_end;
357 ConnView::draw_sequence_stuff()
359 int i2, i3, y_loc, y_offset, x_loc, x_start, x_end, mv_offset;
360 list<annot>::iterator annot_i;
361 string species_name, seq_length;
362 stringstream raw_length;
365 // draw sequence representation lines
366 fl_font(FL_COURIER, 14);
367 //fl_color(FL_BLACK);
368 fl_color(100,100,100);
369 // normally size 7, adjust for various screenshotage
370 fl_line_style(FL_SOLID, 11, NULL);
373 for(i2 = 0; i2 < seq_num; i2++)
375 if (i2 == seq_num - 1)
377 x_loc = (int)(seq_lens[i2] / x_scale_factor) + x();
378 //report_float("seq scaled len", x_loc);
379 fl_line(x(),y_loc,x_loc,y_loc);
381 species_name = (*S)[i2].species;
382 fl_draw(species_name.c_str(), x()+w()-name_pad+5, y_loc+y_offset);
384 // funkiness to figure out which genomic scale to report size in
385 if (seq_scales[i2] == 1)
386 raw_length << setprecision(3) << seq_lens[i2] << " bp";
387 else if (seq_scales[i2] == 2)
388 raw_length << setprecision(3) << seq_lens[i2] / 1000.0 << " Kb";
389 else if (seq_scales[i2] == 3)
390 raw_length << setprecision(3) << seq_lens[i2] /1000000.0<< " Mb";
391 seq_length = raw_length.str();
392 fl_draw(seq_length.c_str(), x()+w()-name_pad+5, y_loc+y_offset+15);
395 y_loc += y_seq_incre;
398 //fl_line(x(),y()+y_pad/2,w()-name_pad,y()+y_pad/2);
399 //fl_line(x(),y()+h()-y_pad/2,w()-name_pad,y()+h()-y_pad/2);
402 vector<annot_color>::iterator annot_type_i;
405 fl_line_style(FL_SOLID, 3, NULL);
407 for(i2 = 0; i2 < seq_num; i2++)
409 // loop thru all annotation entries
410 for(annot_i = (*S)[i2].annots.begin(); annot_i != (*S)[i2].annots.end(); ++annot_i)
412 fl_line_style(FL_SOLID, 7, NULL);
415 // loop thru annotation types to determine color
416 annot_type_i = some_annots.begin();
417 bool type_unfound = true;
418 while ((annot_type_i != some_annots.end()) && type_unfound)
420 if (annot_i->type == annot_type_i->type)
422 fl_color(annot_type_i->color);
423 type_unfound = false;
430 // calculate scaled start and end, and draw
431 x_start = (int)(annot_i->start / x_scale_factor) + x();
432 x_end = (int)(annot_i->end / x_scale_factor) + x();
433 fl_line(x_start,y_loc+mv_offset,x_end,y_loc+mv_offset);
435 y_loc += y_seq_incre;
440 vector<motif>::iterator motif_i;
441 vector<int> some_motif_locs;
442 vector<int>::iterator i_locs;
443 int scale_len, motif_len;
446 fl_line_style(FL_SOLID, 9, NULL);
447 motif_i = some_motifs.begin();
448 while (motif_i != some_motifs.end())
450 fl_color(motif_i->color);
451 motif_len = motif_i->seq.length();
452 scale_len = (int) (motif_len / x_scale_factor);
456 if (!motif_i->locations.empty())
457 for(i2 = 0; i2 < seq_num; i2++)
459 some_motif_locs = (*motif_i).locations[i2];
461 i_locs = some_motif_locs.begin();
462 while (i_locs != some_motif_locs.end())
464 x_start = (int)(*i_locs / x_scale_factor) + x();
465 fl_line(x_start,y_loc,x_start+scale_len,y_loc);
468 y_loc += y_seq_incre;
476 ConnView::resize(int new_x, int new_y, int new_w, int new_h)
484 x_scale_factor = (float) max_seq_len / (w() - name_pad);
485 cout << "scale factor is " << x_scale_factor << endl;
486 y_seq_incre = (h()-(2*y_pad)) / (seq_num - 1);
493 ConnView::reporter(string var, int value)
495 cout << var << " : " << value << endl;
500 ConnView::report_float(string var, float value)
502 cout << var << " : " << value << endl;
506 ConnView::handle(int e)
511 // this empty string needs to be put on cout, otherwise the -O optimize
512 // compile option seems to throw this function away with the following:
513 // gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
516 // why do I need a -O for this module? Well, originally I just used the
517 // same compile flags for all modules. I changed that. However, on debian
518 // systems I need the -O otherwise the behavior I observe on redhat machines
519 // with above compiler happens if I don't have that empty cout. WTF
524 if (Fl::event_button3())
529 else if (Fl::event_button2())
538 drag_start = Fl::event_x();
539 y_drag_start = (float) Fl::event_y();
540 ref_seq_num = (int) (round ( (y_drag_start/y_seq_incre) ) );
545 fl_line_style(FL_SOLID, 1, NULL);
546 fl_overlay_rect(drag_start, (ref_seq_num*y_seq_incre)+y(),
547 Fl::event_x()-drag_start, 16);
553 drag_end = Fl::event_x();
561 return_value = Fl_Widget::handle(e);
570 ConnView::check_new_motifs()
572 vector<motif>::iterator i;
574 vector<int> some_motif_locs;
575 vector<int>::iterator i_locs;
577 i = some_motifs.begin();
578 while (i != some_motifs.end())
582 cout << i->seq << " is new\n";
583 i->locations.clear();
584 //i_locs = i->locations;
585 for(i2 = 0; i2 < seq_num; i2++)
587 some_motif_locs = (*S)[i2].find_motif(i->seq);
588 (*i).locations.push_back(some_motif_locs);
591 i_locs = some_motif_locs.begin();
592 while (i_locs != some_motif_locs.end())
594 cout << *i_locs << " ";
611 list<vector<int> > selected_paths;
612 list<vector<int> >::iterator pathz_i;
613 int i2, i3, y_loc, x_loc, x_start, x_end;
615 list<bool>::iterator highlight_i;
622 // make new list of connections that are highlighted
623 selected_paths.clear();
624 highlight_i = highlight.begin();
625 for(pathz_i = P->refined_pathz.begin(); pathz_i != P->refined_pathz.end(); ++pathz_i)
630 selected_paths.push_back(a_path);
635 // give 50 pixels of height per sequence
636 y_max = seq_num * 50;
637 window_name = "Mussa Sequence: " + ana_name;
639 a_seq_win = new SeqWindow(800, y_max, (const char*) window_name.c_str(),
641 S, selected_paths, seq_lens, &some_motifs);
647 ConnView::find_motifs()
649 motif_find_window = new MotifWindow(300, 300, "Motifs", &some_motifs);
653 ConnView::annot_win()
655 annot_color_window = new AnnotWindow(300, 300, "Annotations", &some_annots);
658 // @!@! special hacked in color coding for the myf5/6 region annots
661 if (annot_i->type == "mvista")
664 fl_line_style(FL_SOLID, 8, NULL);
667 else if (annot_i->type == "reg")
669 fl_line_style(FL_SOLID, 10, NULL);
672 if (annot_i->name == "CNS")
674 else if (annot_i->name == "ELA")
676 else if (annot_i->name == "ES")
678 else if (annot_i->name == "NA")
679 fl_color(120,120,120);
680 else if (annot_i->name == "EA")
682 fl_line_style(FL_SOLID, 7, NULL);
686 else if (annot_i->name == "VS")
688 fl_line_style(FL_SOLID, 7, NULL);
692 else if (annot_i->name == "SP")
694 fl_line_style(FL_SOLID, 7, NULL);
696 fl_color(255,230,150);
698 else if (annot_i->name == "L")
700 fl_line_style(FL_SOLID, 7, NULL);
704 else if (annot_i->name == "TCM")