threshold widget shows both base pair and percents
[mussa.git] / alg / glseqbrowser.cpp
1 #include "alg/glseqbrowser.hpp"
2 #include "mussa_exceptions.hpp"
3
4 #include <iostream>
5 #include <sstream>
6 #include <stdexcept>
7
8 using namespace std;
9
10 GlSeqBrowser::GlSeqBrowser()
11   : border_width(25),
12     cur_ortho(400.0, 0.0, 600.0, 0.0),
13     viewport_size(600, 400),
14     viewport_center((cur_ortho.right-cur_ortho.left)/2+cur_ortho.left),
15     zoom_level(2),
16     color_mapper(),
17     track_container()
18 {
19 }
20
21 GlSeqBrowser::GlSeqBrowser(const GlSeqBrowser& gt)
22   : border_width(gt.border_width),
23     cur_ortho(gt.cur_ortho),
24     viewport_size(gt.viewport_size),
25     viewport_center(gt.viewport_center),
26     zoom_level(gt.zoom_level),
27     color_mapper(gt.color_mapper),
28     track_container(gt.track_container)
29 {
30 }
31
32 void GlSeqBrowser::initializeGL()
33 {
34   glEnable(GL_DEPTH_TEST);
35   glClearColor(1.0, 1.0, 1.0, 0.0);
36   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
37   glShadeModel(GL_FLAT);
38 }
39
40 void GlSeqBrowser::resizeGL(int width, int height)
41 {
42   viewport_size.x = width;
43   viewport_size.y = height;
44   glViewport(0, 0, (GLsizei)width, (GLsizei)height);
45   update_viewport(viewport_center, zoom_level);
46 }
47
48 void GlSeqBrowser::paintGL() const
49 {
50   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
51
52   glPushMatrix();
53   glMatrixMode(GL_PROJECTION);
54   glLoadIdentity();
55   glOrtho(cur_ortho.left, cur_ortho.right,
56           cur_ortho.bottom, cur_ortho.top,
57           -50.0, 50);
58
59   draw();
60
61   glPopMatrix();
62   glFlush();
63 }
64
65 void GlSeqBrowser::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
66 {
67   GLuint *ptr;
68   GLuint names;
69   GLuint consumed_names = 0;
70   float z1;
71   float z2;
72   GLuint objtype;
73   GLuint objid;
74   GLuint path_index = 0;
75   GLuint pair_key_0 = 0;
76   GLuint pair_key_1 = 0;
77
78   selected_paths.clear();
79   selected_tracks.clear();
80
81   std::cout << "hits = " << hits << std::endl;
82   ptr = (GLuint *) buffer;
83   if (hits > 0)
84     selectedMode = true;
85   for (GLuint i=0; i < hits; ++i)
86   {
87     if ((i + 5) > bufsize) {
88       std::clog << "*** selection overflow***" << std::endl;
89     } else {
90       consumed_names = 0;
91       names = *ptr++;
92       z1 = ((float)*ptr++)/0x7fffffff;
93       z2 = ((float)*ptr++)/0x7fffffff;
94       objtype = *ptr++; ++consumed_names;
95       switch (objtype) {
96         case MussaSegment:
97           path_index = *ptr++; ++consumed_names;
98           pair_key_0 = *ptr++; ++consumed_names;
99           pair_key_1 = *ptr++; ++consumed_names;
100           if (path_index < path_segments.size()) {
101             segment_key k(pair_key_0, pair_key_1);
102             pair_segment_map::iterator psm_i;
103             psm_i = path_segments[path_index].find(k);
104             if (psm_i != path_segments[path_index].end()) {
105               Segment &seg = psm_i->second;
106               selected_paths.insert(seg.path_ids.begin(), seg.path_ids.end());
107             }
108             // else something else is wrong
109           } else {
110             // something wasn't right
111             clog << "invalid path_index " << path_index 
112                  << " should have been [0,"<<path_segments.size()
113                  << ") " << endl;
114           }
115           break;
116         case MussaTrack:
117           objid = *ptr++; ++consumed_names;
118           selected_tracks.insert(objid);
119         break;
120         default:
121           cout << "unknown type " << objtype << " ";
122           for(; consumed_names < names; ++consumed_names) {
123             cout << consumed_names << "," << *ptr++ << " ";
124           }
125           cout << endl;
126           break;
127       }
128     }
129   }
130 }
131
132 void GlSeqBrowser::selectRegion(int top, int left, int bottom, int right)
133 {
134   GLfloat x_scale = cur_ortho.width()/((float)viewport_size.x);
135   GLfloat y_scale = cur_ortho.height()/((float)viewport_size.y);
136   GLfloat x_left = cur_ortho.left + (left*x_scale);
137   GLfloat x_right = cur_ortho.left + (right * x_scale);
138
139   if (top > bottom) {
140     // woah, someone gave us a rectangle with the origin in the lower left
141     int temp = top;
142     bottom = top;
143     top = temp;
144   }
145   // swap the orientation of canvas coordinates
146   GLfloat y_top = cur_ortho.top-(bottom*y_scale);
147   GLfloat y_bottom = cur_ortho.top - top * y_scale;
148   selectedRegion = rect<float>(y_top, x_left, y_bottom, x_right);
149
150   // hopefully this will make a buffer big enough to receive 
151   // everything being selected
152   //const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
153   //const GLuint select_buf_size = 1 + 5 * (pathz_count + sequences.size());
154   const GLuint select_buf_size = 500000;
155   GLuint selectBuf[select_buf_size];
156   glSelectBuffer(select_buf_size, selectBuf);
157   GLint hits;
158
159   (void)glRenderMode(GL_SELECT);
160   glPushMatrix();
161   glMatrixMode(GL_PROJECTION);
162   glLoadIdentity();
163   glOrtho(x_left, x_right, y_top, y_bottom, -50.0, 50.0);
164   glMatrixMode(GL_MODELVIEW);
165   glLoadIdentity();
166
167   draw();
168
169   glFlush();
170   glPopMatrix();
171   hits = glRenderMode(GL_RENDER);
172   processSelection(hits, selectBuf, select_buf_size);
173 }
174
175 float GlSeqBrowser::border() const
176 {
177   return border_width;
178 }
179
180 float GlSeqBrowser::left() const
181
182   float left;
183   if (track_container.size() == 0)
184   {
185     return cur_ortho.left;
186   } else {
187     vector<GlSequence>::const_iterator track_i = track_container.begin();    
188     left = track_i->x();
189     for( ; track_i != track_container.end(); ++track_i)
190     {
191       if (track_i->x() < left) {
192         left = track_i->x();
193       }
194     }
195     return left-border_width;
196   }
197 }
198
199 float GlSeqBrowser::right() const
200
201   float right;
202   if (track_container.size() == 0) {
203     return cur_ortho.right;
204   } else {
205     vector<GlSequence>::const_iterator track_i = track_container.begin();
206     right = track_i->right();
207     for( ; track_i != track_container.end(); ++track_i) {
208       if (track_i->right() > right)
209         right = track_i->right();
210     }
211     return right+border_width;
212   }
213 }
214
215 void GlSeqBrowser::setViewportCenter(float x)
216 {
217   update_viewport(x, zoom_level);
218   viewport_center = x;
219 }
220
221 float GlSeqBrowser::viewportLeft() const
222
223   return cur_ortho.left; 
224 }
225
226 float GlSeqBrowser::viewportCenter() const
227 {
228   return viewport_center;
229 }
230
231 float GlSeqBrowser::viewportRight() const
232
233   return cur_ortho.right; 
234 }
235
236 float GlSeqBrowser::viewportHeight() const
237 {
238   return cur_ortho.top - cur_ortho.bottom;
239 }
240
241 float GlSeqBrowser::viewportWidth() const
242 {
243   return cur_ortho.right - cur_ortho.left;
244 }
245
246 int GlSeqBrowser::zoomOut()
247 {
248
249   if (right() - left() > 0) {
250     cur_ortho.left = left();
251     cur_ortho.right = right();
252     zoom_level = (int)(( (right() - left()) / viewport_size.x) * 100);
253     return zoom_level;
254   } else {
255     // made up number representing 50 bp / pixel
256     return 5000;
257   }
258 }
259 int GlSeqBrowser::zoomToSequence()
260 {
261   // (experimentally determined zoom level)
262   const int friendly_zoom = 7;
263   setZoom(friendly_zoom);
264   return friendly_zoom;
265 }
266 void GlSeqBrowser::setZoom(int new_zoom)
267 {
268   update_viewport(viewport_center, new_zoom);
269   zoom_level = new_zoom;
270 }
271
272 int GlSeqBrowser::zoom() const
273 {
274   return zoom_level;
275 }
276
277 void GlSeqBrowser::setColorMapper(AnnotationColors& cm)
278 {
279   color_mapper = cm;
280 }
281
282 AnnotationColors& GlSeqBrowser::colorMapper()
283 {
284   return color_mapper;
285 }
286
287 void GlSeqBrowser::clear()
288 {
289   clear_links();
290   path_segments.clear();
291   track_container.clear();
292 }
293
294 void GlSeqBrowser::push_sequence(const Sequence &s)
295 {
296   GlSequence gs(s, color_mapper);
297   push_sequence(gs);
298 }
299
300 void GlSeqBrowser::push_sequence(GlSequence &gs)
301 {
302   clear_links();
303   pathid = 0;
304   track_container.push_back(gs);
305   update_layout();
306   if (track_container.size() > 1)
307     path_segments.push_back(pair_segment_map());
308 }
309
310 const std::vector<GlSequence>& GlSeqBrowser::sequences() const
311 {
312   return track_container;
313 }
314
315 void GlSeqBrowser::clear_links()
316 {
317   path_segments.clear();
318   for (int i = track_container.size()-1; i > 0; --i)
319   {
320     path_segments.push_back(pair_segment_map());
321   }
322 }
323
324 void 
325 GlSeqBrowser::link(const vector<int>& path, const vector<bool>& rc, int )
326 {
327   if (path.size() < 2) {
328     // should i throw an error instead?
329     return;
330   }
331   if (path.size() != track_container.size() ) {
332     stringstream msg;
333     msg << "Path size [" << path.size() << "] and track size [" 
334         << track_container.size() << "] don't match" << endl;
335     throw mussa_error(msg.str());
336  }
337   if (path.size() != rc.size()) {
338     throw runtime_error("path and reverse compliment must be the same length");
339   }
340   vector<int>::const_iterator path_i = path.begin();
341   vector<bool>::const_iterator rc_i = rc.begin();
342   int track_i = 0;
343   int prev_x = *path_i; ++path_i;
344   bool prev_rc = *rc_i; ++rc_i;
345   while (path_i != path.end() and rc_i != rc.end())
346   {
347     segment_key p(prev_x, *path_i);
348     pair_segment_map::iterator found_segment = path_segments[track_i].find(p);
349     if (found_segment == path_segments[track_i].end()) {
350       // not already found
351       float y1 = track_container[track_i].y();
352             y1 -= track_container[track_i].height()/2;
353       float y2 = track_container[track_i+1].y();
354             y2 += track_container[track_i+1].height()/2;
355             
356       Segment s(prev_x, y1, *path_i, y2, prev_rc);
357       s.path_ids.insert(pathid);
358       path_segments[track_i][p] = s;
359     } else {
360       //found
361       found_segment->second.path_ids.insert(pathid);
362     }
363     prev_x = *path_i;
364     prev_rc = *rc_i;
365     ++track_i;
366     ++path_i;
367     ++rc_i;
368   }
369   // pathid is reset by push_sequence
370   ++pathid;
371 }
372
373 const set<int>& GlSeqBrowser::selectedPaths() const
374 {
375   return selected_paths;
376 }
377
378 void GlSeqBrowser::centerOnPath(const vector<int>& paths)
379 {
380   if (paths.size() != track_container.size()) {
381     throw mussa_error("Path length didn't match the number of sequences");
382   }
383
384   for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
385   {
386     // -15 = shift more to the left
387     track_container[track_i].setX((viewport_center-15) - paths[track_i]);
388   }
389 }
390
391 void GlSeqBrowser::update_viewport(float center, int new_zoom)
392 {
393   // limit how close we can get
394   if (new_zoom < 1) {
395     new_zoom = 1;
396   }
397   float new_width = (((float)new_zoom / 100.0) * (float)viewport_size.x);
398   cur_ortho.left = center-new_width/2.0;
399   cur_ortho.right = center+new_width/2.0;
400 }
401
402 void GlSeqBrowser::update_layout()
403 {
404   typedef std::vector<GlSequence>::iterator glseq_itor_type;
405   float available_height = (float)cur_ortho.top - 2 * (float)border_width;
406   float max_base_pairs = 0;
407   size_t track_count = track_container.size();
408
409   if (track_count > 1) {
410     // we have several sequences
411     float track_spacing = available_height / (track_count-1);
412     float y = available_height + (float)border_width;
413     for(glseq_itor_type seq_i = track_container.begin();
414         seq_i != track_container.end();
415         ++seq_i, y-=track_spacing)
416     {
417       seq_i->setX(0);
418       seq_i->setY(y);
419       if (seq_i->length() > max_base_pairs)
420         max_base_pairs = seq_i->length();
421     }
422   } else if (track_count == 1) {
423     // center the single track
424     glseq_itor_type seq_i = track_container.begin();
425     seq_i->setX(0);
426     seq_i->setY(viewport_size.x /2);
427     max_base_pairs = seq_i->length();
428   } else {
429     // nothing to do as we're empty
430     return;
431   }
432   cur_ortho.right = max_base_pairs + border_width;
433   cur_ortho.left = -border_width;
434   cur_ortho.top = viewport_size.x;
435   cur_ortho.bottom = 0;
436   viewport_center = (cur_ortho.width()/2) + cur_ortho.left;
437   zoomOut();
438 }
439
440 void GlSeqBrowser::draw() const
441 {
442   glMatrixMode(GL_MODELVIEW);
443   glInitNames();
444   glPushName(MussaSegment);
445   draw_segments();
446   glLoadName(MussaTrack);
447   draw_tracks();
448   glPopName();
449   // a selection shouldn't have a glName associated with it
450   draw_selection();
451 }
452
453 void GlSeqBrowser::draw_selection() const
454 {
455   // draw selection box
456   glEnable(GL_BLEND);
457   glDepthMask(GL_FALSE);
458   if (selectedMode) {
459     glColor4f(0.6, 0.6, 0.6, 0.9);
460     glRectf(selectedRegion.left, selectedRegion.top, 
461             selectedRegion.right, selectedRegion.bottom);
462   }
463   glDepthMask(GL_TRUE);
464   glDisable(GL_BLEND);
465 }
466
467 void GlSeqBrowser::draw_tracks() const
468 {
469   for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
470   {
471     glPushName(track_i);
472     track_container[track_i].draw(cur_ortho.left, cur_ortho.right);
473     glPopName();
474   }
475 }
476
477 void GlSeqBrowser::draw_segments() const
478 {
479   glLineWidth(1);
480   // each vector contains path_segment_maps of all the connections
481   // between this track and the next
482   path_segment_map_vector::const_iterator psmv_i;
483   for(psmv_i = path_segments.begin();
484       psmv_i != path_segments.end();
485       ++psmv_i)
486   {
487     path_segment_map_vector::difference_type path_index;
488     path_index = psmv_i - path_segments.begin();
489     // these maps contain the pair index (used so we dont keep drawing the
490     // same segment) and the actual segment structure.
491     pair_segment_map::const_iterator psm_i;
492     for(psm_i = psmv_i->begin();
493         psm_i != psmv_i->end();
494         ++psm_i)
495     {
496       // grab the index into our segment map
497       const segment_key& key = psm_i->first;
498       // the second element of our map pair is a segment
499       const Segment &s = psm_i->second;
500       // need to do something so we can detect our selection
501       vector<int> selected;
502       set_intersection(selected_paths.begin(), selected_paths.end(),
503                        s.path_ids.begin(), s.path_ids.end(),
504                        back_inserter(selected));
505
506       if (not s.reversed) {
507         if (selected_paths.size() == 0 or selected.size() > 0) {
508           glColor3f(1.0, 0.0, 0.0);
509         } else {
510           glColor3f(1.0, 0.8, 0.8);
511         }
512       } else { 
513         // hack for demo, hide the reverse compliment color bug
514         if (selected_paths.size() == 0 or selected.size() > 0) {
515           glColor3f(1.0, 0.0, 0.0);
516         } else {
517           glColor3f(1.0, 0.8, 0.8);
518         }
519         /*
520         if (selected_paths.size() == 0 or selected.size() > 0) {
521           glColor3f(0.0, 0.0, 1.0);
522         } else {
523           glColor3f(0.8, 0.8, 1.0);
524         }
525         */
526       }
527       // save the multipart name for our segment
528       glPushName(path_index); glPushName(key.first); glPushName(key.second);
529       glBegin(GL_LINES);
530       float seq_start_x = track_container[path_index].x();
531       float seq_end_x = track_container[path_index+1].x();
532       glVertex3f(s.start.x + seq_start_x, s.start.y, -1);
533       glVertex3f(s.end.x   + seq_end_x  , s.end.y, -1);
534       glEnd();
535       // clear the names
536       glPopName(); glPopName(); glPopName();
537     }
538   }
539 }