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