implement zoom in terms of base pairs per pixel
[mussa.git] / alg / glseqbrowser.cpp
1 #include "alg/glseqbrowser.hpp"
2 #include "mussa_exceptions.hpp"
3
4 #include <iostream>
5 #include <stdexcept>
6
7 using namespace std;
8
9 GlSeqBrowser::GlSeqBrowser()
10   : border_width(25),
11     cur_ortho(400.0, 0.0, 600.0, 0.0),
12     viewport_size(600, 400),
13     viewport_center((cur_ortho.right-cur_ortho.left)/2+cur_ortho.left),
14     zoom_level(2),
15     color_mapper(),
16     track_container()
17 {
18 }
19
20 GlSeqBrowser::GlSeqBrowser(const GlSeqBrowser& gt)
21   : border_width(gt.border_width),
22     cur_ortho(gt.cur_ortho),
23     viewport_size(gt.viewport_size),
24     viewport_center(gt.viewport_center),
25     zoom_level(gt.zoom_level),
26     color_mapper(gt.color_mapper),
27     track_container(gt.track_container)
28 {
29 }
30
31 void GlSeqBrowser::initializeGL()
32 {
33   glEnable(GL_DEPTH_TEST);
34   glClearColor(1.0, 1.0, 1.0, 0.0);
35   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
36   glShadeModel(GL_FLAT);
37 }
38
39 void GlSeqBrowser::resizeGL(int width, int height)
40 {
41   viewport_size.x = width;
42   viewport_size.y = height;
43   glViewport(0, 0, (GLsizei)width, (GLsizei)height);
44   //update_layout();
45 }
46
47 void GlSeqBrowser::paintGL() const
48 {
49   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
50
51   glPushMatrix();
52   glMatrixMode(GL_PROJECTION);
53   glLoadIdentity();
54   glOrtho(cur_ortho.left, cur_ortho.right,
55           cur_ortho.bottom, cur_ortho.top,
56           -50.0, 50);
57
58   draw();
59
60   glPopMatrix();
61   glFlush();
62 }
63
64 void GlSeqBrowser::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
65 {
66   GLuint *ptr;
67   GLuint names;
68   GLuint consumed_names = 0;
69   float z1;
70   float z2;
71   GLuint objtype;
72   GLuint objid;
73   GLuint path_index = 0;
74   GLuint pair_key_0 = 0;
75   GLuint pair_key_1 = 0;
76
77   selected_paths.clear();
78   selected_tracks.clear();
79
80   std::cout << "hits = " << hits << std::endl;
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 = 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     return left-border_width;
194   }
195 }
196
197 float GlSeqBrowser::right() const
198
199   float right;
200   if (track_container.size() == 0)
201   {
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     {
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
260 void GlSeqBrowser::setZoom(int new_zoom)
261 {
262   update_viewport(viewport_center, new_zoom);
263   zoom_level = new_zoom;
264 }
265
266 int GlSeqBrowser::zoom() const
267 {
268   return zoom_level;
269 }
270
271 void GlSeqBrowser::setColorMapper(AnnotationColors& cm)
272 {
273   color_mapper = cm;
274 }
275
276 AnnotationColors& GlSeqBrowser::colorMapper()
277 {
278   return color_mapper;
279 }
280
281 void GlSeqBrowser::clear()
282 {
283   clear_links();
284   path_segments.clear();
285   track_container.clear();
286 }
287
288 void GlSeqBrowser::push_sequence(const Sequence &s)
289 {
290   GlSequence gs(s, color_mapper);
291   push_sequence(gs);
292 }
293
294 void GlSeqBrowser::push_sequence(GlSequence &gs)
295 {
296   clear_links();
297   pathid = 0;
298   track_container.push_back(gs);
299   update_layout();
300   if (track_container.size() > 1)
301     path_segments.push_back(pair_segment_map());
302 }
303
304 const std::vector<GlSequence>& GlSeqBrowser::sequences() const
305 {
306   return track_container;
307 }
308
309 void GlSeqBrowser::clear_links()
310 {
311   path_segment_map_vector::iterator psmv_i;
312   for(psmv_i = path_segments.begin();
313       psmv_i != path_segments.end();
314       ++psmv_i)
315   {
316     psmv_i->clear();
317   }
318 }
319
320 void 
321 GlSeqBrowser::link(const vector<int>& path, const vector<bool>& rc, int )
322 {
323   if (path.size() < 2) {
324     // should i throw an error instead?
325     return;
326   }
327   if (path.size() != rc.size()) {
328     throw runtime_error("path and reverse compliment must be the same length");
329   }
330   vector<int>::const_iterator path_i = path.begin();
331   vector<bool>::const_iterator rc_i = rc.begin();
332   int track_i = 0;
333   int prev_x = *path_i; ++path_i;
334   bool prev_rc = *rc_i; ++rc_i;
335   while (path_i != path.end() and rc_i != rc.end())
336   {
337     segment_key p(prev_x, *path_i);
338     pair_segment_map::iterator found_segment = path_segments[track_i].find(p);
339     if (found_segment == path_segments[track_i].end()) {
340       // not already found
341       float y1 = track_container[track_i].y();
342             y1 -= track_container[track_i].height()/2;
343       float y2 = track_container[track_i+1].y();
344             y2 += track_container[track_i+1].height()/2;
345             
346       Segment s(prev_x, y1, *path_i, y2, prev_rc);
347       s.path_ids.insert(pathid);
348       path_segments[track_i][p] = s;
349     } else {
350       //found
351       found_segment->second.path_ids.insert(pathid);
352     }
353     prev_x = *path_i;
354     prev_rc = *rc_i;
355     ++track_i;
356     ++path_i;
357     ++rc_i;
358   }
359   // pathid is reset by push_sequence
360   ++pathid;
361 }
362
363 const set<int>& GlSeqBrowser::selectedPaths() const
364 {
365   return selected_paths;
366 }
367
368 void GlSeqBrowser::centerOnPath(const vector<int>& paths)
369 {
370   if (paths.size() != track_container.size()) {
371     throw mussa_error("Path length didn't match the number of sequences");
372   }
373
374   for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
375   {
376     track_container[track_i].setX(viewport_center - paths[track_i]);
377   }
378 }
379
380 void GlSeqBrowser::update_viewport(float center, int new_zoom)
381 {
382   // limit how close we can get
383   if (new_zoom < 1) {
384     new_zoom = 1;
385   }
386   float new_width = (((float)new_zoom / 100.0) * (float)viewport_size.x);
387   cur_ortho.left = center-new_width/2.0;
388   cur_ortho.right = center+new_width/2.0;
389 }
390
391 void GlSeqBrowser::update_layout()
392 {
393   typedef std::vector<GlSequence>::iterator glseq_itor_type;
394   float available_height = (float)cur_ortho.top - 2 * (float)border_width;
395   float max_base_pairs = 0;
396   size_t track_count = track_container.size();
397
398   if (track_count > 1) {
399     // we have several sequences
400     float track_spacing = available_height / (track_count-1);
401     float y = available_height + (float)border_width;
402     for(glseq_itor_type seq_i = track_container.begin();
403         seq_i != track_container.end();
404         ++seq_i, y-=track_spacing)
405     {
406       seq_i->setX(0);
407       seq_i->setY(y);
408       if (seq_i->length() > max_base_pairs)
409         max_base_pairs = seq_i->length();
410     }
411   } else if (track_count == 1) {
412     // center the single track
413     glseq_itor_type seq_i = track_container.begin();
414     seq_i->setX(0);
415     seq_i->setY(viewport_size.x /2);
416     max_base_pairs = seq_i->length();
417   } else {
418     // nothing to do as we're empty
419     return;
420   }
421   cur_ortho.right = max_base_pairs + border_width;
422   cur_ortho.left = -border_width;
423   cur_ortho.top = viewport_size.x;
424   cur_ortho.bottom = 0;
425   viewport_center = (cur_ortho.width()/2) + cur_ortho.left;
426   zoomOut();
427 }
428
429 void GlSeqBrowser::draw() const
430 {
431   glMatrixMode(GL_MODELVIEW);
432   glInitNames();
433   glPushName(MussaSegment);
434   draw_segments();
435   glLoadName(MussaTrack);
436   draw_tracks();
437   glPopName();
438   // a selection shouldn't have a glName associated with it
439   draw_selection();
440 }
441
442 void GlSeqBrowser::draw_selection() const
443 {
444   // draw selection box
445   glEnable(GL_BLEND);
446   glDepthMask(GL_FALSE);
447   if (selectedMode) {
448     glColor4f(0.6, 0.6, 0.6, 0.9);
449     glRectf(selectedRegion.left, selectedRegion.top, 
450             selectedRegion.right, selectedRegion.bottom);
451   }
452   glDepthMask(GL_TRUE);
453   glDisable(GL_BLEND);
454 }
455
456 void GlSeqBrowser::draw_tracks() const
457 {
458   for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
459   {
460     glPushName(track_i);
461     track_container[track_i].draw(cur_ortho.left, cur_ortho.right);
462     glPopName();
463   }
464 }
465
466 void GlSeqBrowser::draw_segments() const
467 {
468   glLineWidth(0.1);
469   // each vector contains path_segment_maps of all the connections
470   // between this track and the next
471   path_segment_map_vector::const_iterator psmv_i;
472   for(psmv_i = path_segments.begin();
473       psmv_i != path_segments.end();
474       ++psmv_i)
475   {
476     path_segment_map_vector::difference_type path_index;
477     path_index = psmv_i - path_segments.begin();
478     // these maps contain the pair index (used so we dont keep drawing the
479     // same segment) and the actual segment structure.
480     pair_segment_map::const_iterator psm_i;
481     for(psm_i = psmv_i->begin();
482         psm_i != psmv_i->end();
483         ++psm_i)
484     {
485       // grab the index into our segment map
486       const segment_key& key = psm_i->first;
487       // the second element of our map pair is a segment
488       const Segment &s = psm_i->second;
489       // need to do something so we can detect our selection
490       vector<int> selected;
491       set_intersection(selected_paths.begin(), selected_paths.end(),
492                        s.path_ids.begin(), s.path_ids.end(),
493                        back_inserter(selected));
494
495       if (not s.reversed) {
496         if (selected_paths.size() == 0 or selected.size() > 0) {
497           glColor3f(1.0, 0.0, 0.0);
498         } else {
499           glColor3f(1.0, 0.8, 0.8);
500         }
501       } else { 
502         if (selected_paths.size() == 0 or selected.size() > 0) {
503           glColor3f(0.0, 0.0, 1.0);
504         } else {
505           glColor3f(0.8, 0.8, 1.0);
506         }
507       }
508       // save the multipart name for our segment
509       glPushName(path_index); glPushName(key.first); glPushName(key.second);
510       glBegin(GL_LINES);
511       glVertex3f(s.start.x, s.start.y, -1);
512       glVertex3f(s.end.x, s.end.y, -1);
513       glEnd();
514       // clear the names
515       glPopName(); glPopName(); glPopName();
516     }
517   }
518 }