1 #include "alg/glseqbrowser.hpp"
2 #include "mussa_exceptions.hpp"
10 GlSeqBrowser::GlSeqBrowser()
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),
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)
32 void GlSeqBrowser::initializeGL()
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);
40 void GlSeqBrowser::resizeGL(int width, int height)
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);
48 void GlSeqBrowser::paintGL() const
50 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
53 glMatrixMode(GL_PROJECTION);
55 glOrtho(cur_ortho.left, cur_ortho.right,
56 cur_ortho.bottom, cur_ortho.top,
65 void GlSeqBrowser::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
69 GLuint consumed_names = 0;
74 GLuint path_index = 0;
75 GLuint pair_key_0 = 0;
76 GLuint pair_key_1 = 0;
78 selected_paths.clear();
79 selected_tracks.clear();
81 std::cout << "hits = " << hits << std::endl;
82 ptr = (GLuint *) buffer;
85 for (GLuint i=0; i < hits; ++i)
87 if ((i + 5) > bufsize) {
88 std::clog << "*** selection overflow***" << std::endl;
92 z1 = ((float)*ptr++)/0x7fffffff;
93 z2 = ((float)*ptr++)/0x7fffffff;
94 objtype = *ptr++; ++consumed_names;
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());
108 // else something else is wrong
110 // something wasn't right
111 clog << "invalid path_index " << path_index
112 << " should have been [0,"<<path_segments.size()
117 objid = *ptr++; ++consumed_names;
118 selected_tracks.insert(objid);
121 cout << "unknown type " << objtype << " ";
122 for(; consumed_names < names; ++consumed_names) {
123 cout << consumed_names << "," << *ptr++ << " ";
132 void GlSeqBrowser::selectRegion(int top, int left, int bottom, int right)
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);
140 // woah, someone gave us a rectangle with the origin in the lower left
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);
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);
159 (void)glRenderMode(GL_SELECT);
161 glMatrixMode(GL_PROJECTION);
163 glOrtho(x_left, x_right, y_top, y_bottom, -50.0, 50.0);
164 glMatrixMode(GL_MODELVIEW);
171 hits = glRenderMode(GL_RENDER);
172 processSelection(hits, selectBuf, select_buf_size);
175 float GlSeqBrowser::border() const
180 float GlSeqBrowser::left() const
183 if (track_container.size() == 0)
185 return cur_ortho.left;
187 vector<GlSequence>::const_iterator track_i = track_container.begin();
189 for( ; track_i != track_container.end(); ++track_i)
191 if (track_i->x() < left) {
195 return left-border_width;
199 float GlSeqBrowser::right() const
202 if (track_container.size() == 0) {
203 return cur_ortho.right;
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();
211 return right+border_width;
215 void GlSeqBrowser::setViewportCenter(float x)
217 update_viewport(x, zoom_level);
221 float GlSeqBrowser::viewportLeft() const
223 return cur_ortho.left;
226 float GlSeqBrowser::viewportCenter() const
228 return viewport_center;
231 float GlSeqBrowser::viewportRight() const
233 return cur_ortho.right;
236 float GlSeqBrowser::viewportHeight() const
238 return cur_ortho.top - cur_ortho.bottom;
241 float GlSeqBrowser::viewportWidth() const
243 return cur_ortho.right - cur_ortho.left;
246 int GlSeqBrowser::zoomOut()
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);
255 // made up number representing 50 bp / pixel
259 int GlSeqBrowser::zoomToSequence()
261 // (experimentally determined zoom level)
262 const int friendly_zoom = 7;
263 setZoom(friendly_zoom);
264 return friendly_zoom;
266 void GlSeqBrowser::setZoom(int new_zoom)
268 update_viewport(viewport_center, new_zoom);
269 zoom_level = new_zoom;
272 int GlSeqBrowser::zoom() const
277 void GlSeqBrowser::setColorMapper(AnnotationColors& cm)
282 AnnotationColors& GlSeqBrowser::colorMapper()
287 void GlSeqBrowser::clear()
290 path_segments.clear();
291 track_container.clear();
294 void GlSeqBrowser::push_sequence(const Sequence &s)
296 GlSequence gs(s, color_mapper);
300 void GlSeqBrowser::push_sequence(GlSequence &gs)
304 track_container.push_back(gs);
306 if (track_container.size() > 1)
307 path_segments.push_back(pair_segment_map());
310 const std::vector<GlSequence>& GlSeqBrowser::sequences() const
312 return track_container;
315 void GlSeqBrowser::clear_links()
317 path_segments.clear();
318 for (int i = track_container.size()-1; i > 0; --i)
320 path_segments.push_back(pair_segment_map());
325 GlSeqBrowser::link(const vector<int>& path, const vector<bool>& rc, int )
327 if (path.size() < 2) {
328 // should i throw an error instead?
331 if (path.size() != track_container.size()-1 ) {
333 msg << "Path size [" << path.size() << "] and track size ["
334 << track_container.size() << "] don't match" << endl;
335 throw mussa_error(msg.str());
337 if (path.size() != rc.size()) {
338 throw runtime_error("path and reverse compliment must be the same length");
340 vector<int>::const_iterator path_i = path.begin();
341 vector<bool>::const_iterator rc_i = rc.begin();
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())
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()) {
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;
356 Segment s(prev_x, y1, *path_i, y2, prev_rc);
357 s.path_ids.insert(pathid);
358 path_segments[track_i][p] = s;
361 found_segment->second.path_ids.insert(pathid);
369 // pathid is reset by push_sequence
373 const set<int>& GlSeqBrowser::selectedPaths() const
375 return selected_paths;
378 void GlSeqBrowser::centerOnPath(const vector<int>& paths)
380 if (paths.size() != track_container.size()) {
381 throw mussa_error("Path length didn't match the number of sequences");
384 for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
386 // -15 = shift more to the left
387 track_container[track_i].setX((viewport_center-15) - paths[track_i]);
391 void GlSeqBrowser::update_viewport(float center, int new_zoom)
393 // limit how close we can get
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;
402 void GlSeqBrowser::update_layout()
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();
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)
419 if (seq_i->length() > max_base_pairs)
420 max_base_pairs = seq_i->length();
422 } else if (track_count == 1) {
423 // center the single track
424 glseq_itor_type seq_i = track_container.begin();
426 seq_i->setY(viewport_size.x /2);
427 max_base_pairs = seq_i->length();
429 // nothing to do as we're empty
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;
440 void GlSeqBrowser::draw() const
442 glMatrixMode(GL_MODELVIEW);
444 glPushName(MussaSegment);
446 glLoadName(MussaTrack);
449 // a selection shouldn't have a glName associated with it
453 void GlSeqBrowser::draw_selection() const
455 // draw selection box
457 glDepthMask(GL_FALSE);
459 glColor4f(0.6, 0.6, 0.6, 0.9);
460 glRectf(selectedRegion.left, selectedRegion.top,
461 selectedRegion.right, selectedRegion.bottom);
463 glDepthMask(GL_TRUE);
467 void GlSeqBrowser::draw_tracks() const
469 for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
472 track_container[track_i].draw(cur_ortho.left, cur_ortho.right);
477 void GlSeqBrowser::draw_segments() const
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();
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();
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));
506 if (not s.reversed) {
507 if (selected_paths.size() == 0 or selected.size() > 0) {
508 glColor3f(1.0, 0.0, 0.0);
510 glColor3f(1.0, 0.8, 0.8);
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);
517 glColor3f(1.0, 0.8, 0.8);
520 if (selected_paths.size() == 0 or selected.size() > 0) {
521 glColor3f(0.0, 0.0, 1.0);
523 glColor3f(0.8, 0.8, 1.0);
527 // save the multipart name for our segment
528 glPushName(path_index); glPushName(key.first); glPushName(key.second);
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);
536 glPopName(); glPopName(); glPopName();