1 #include "alg/glseqbrowser.hpp"
2 #include "mussa_exceptions.hpp"
9 GlSeqBrowser::GlSeqBrowser()
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),
20 GlSeqBrowser::GlSeqBrowser(const GlSeqBrowser& gt)
21 : border_width(gt.border_width),
22 max_ortho(gt.max_ortho),
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);
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 = 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)
194 return left-border_width;
198 float GlSeqBrowser::right() const
201 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)
209 if (track_i->right() > right)
210 right = track_i->right();
212 return right+border_width;
216 void GlSeqBrowser::setViewportCenter(float x)
218 update_viewport(x, zoom_level);
222 float GlSeqBrowser::viewportLeft() const
224 return cur_ortho.left;
227 float GlSeqBrowser::viewportCenter() const
229 return viewport_center;
232 float GlSeqBrowser::viewportRight() const
234 return cur_ortho.right;
237 float GlSeqBrowser::viewportHeight() const
239 return cur_ortho.top - cur_ortho.bottom;
242 float GlSeqBrowser::viewportWidth() const
244 return cur_ortho.right - cur_ortho.left;
247 void GlSeqBrowser::setZoom(int new_zoom)
249 update_viewport(viewport_center, new_zoom);
250 zoom_level = new_zoom;
253 int GlSeqBrowser::zoom() const
258 void GlSeqBrowser::setColorMapper(AnnotationColors& cm)
263 AnnotationColors& GlSeqBrowser::colorMapper()
268 void GlSeqBrowser::clear()
271 path_segments.clear();
272 track_container.clear();
275 void GlSeqBrowser::push_sequence(const Sequence &s)
277 GlSequence gs(s, color_mapper);
281 void GlSeqBrowser::push_sequence(GlSequence &gs)
285 track_container.push_back(gs);
287 if (track_container.size() > 1)
288 path_segments.push_back(pair_segment_map());
291 const std::vector<GlSequence>& GlSeqBrowser::sequences() const
293 return track_container;
296 void GlSeqBrowser::clear_links()
298 path_segment_map_vector::iterator psmv_i;
299 for(psmv_i = path_segments.begin();
300 psmv_i != path_segments.end();
308 GlSeqBrowser::link(const vector<int>& path, const vector<bool>& rc, int length)
310 if (path.size() < 2) {
311 // should i throw an error instead?
314 if (path.size() != rc.size()) {
315 throw runtime_error("path and reverse compliment must be the same length");
317 vector<int>::const_iterator path_i = path.begin();
318 vector<bool>::const_iterator rc_i = rc.begin();
320 int prev_x = *path_i; ++path_i;
321 bool prev_rc = *rc_i; ++rc_i;
322 while (path_i != path.end() and rc_i != rc.end())
324 segment_key p(prev_x, *path_i);
325 pair_segment_map::iterator found_segment = path_segments[track_i].find(p);
326 if (found_segment == path_segments[track_i].end()) {
328 float y1 = track_container[track_i].y();
329 y1 -= track_container[track_i].height()/2;
330 float y2 = track_container[track_i+1].y();
331 y2 += track_container[track_i+1].height()/2;
333 Segment s(prev_x, y1, *path_i, y2, prev_rc);
334 s.path_ids.insert(pathid);
335 path_segments[track_i][p] = s;
338 found_segment->second.path_ids.insert(pathid);
346 // pathid is reset by push_sequence
350 const set<int>& GlSeqBrowser::selectedPaths() const
352 return selected_paths;
355 void GlSeqBrowser::centerOnPath(const vector<int>& paths)
357 if (paths.size() != track_container.size()) {
358 throw mussa_error("Path length didn't match the number of sequences");
361 for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
363 track_container[track_i].setX(viewport_center - paths[track_i]);
367 void GlSeqBrowser::update_viewport(float center, int new_zoom)
369 float max_width = max_ortho.width();
370 // division by zero is a major bummer
374 float new_max_width = max_width / new_zoom;
375 cur_ortho.left = center-new_max_width;
376 cur_ortho.right = center+new_max_width;
379 void GlSeqBrowser::update_layout()
381 typedef std::vector<GlSequence>::iterator glseq_itor_type;
382 float available_height = (float)cur_ortho.top - 2 * (float)border_width;
383 float max_base_pairs = 0;
384 size_t track_count = track_container.size();
386 if (track_count > 1) {
387 // we have several sequences
388 float track_spacing = available_height / (track_count-1);
389 float y = available_height + (float)border_width;
390 for(glseq_itor_type seq_i = track_container.begin();
391 seq_i != track_container.end();
392 ++seq_i, y-=track_spacing)
396 if (seq_i->length() > max_base_pairs)
397 max_base_pairs = seq_i->length();
399 } else if (track_count == 1) {
400 // center the single track
401 glseq_itor_type seq_i = track_container.begin();
403 seq_i->setY(viewport_size.x /2);
404 max_base_pairs = seq_i->length();
406 // nothing to do as we're empty
409 max_ortho.right = max_base_pairs + border_width;
410 max_ortho.left = -border_width;
411 max_ortho.top = viewport_size.x;
412 max_ortho.bottom = 0;
413 cur_ortho = max_ortho;
414 viewport_center = (cur_ortho.width()/2) + cur_ortho.left;
417 void GlSeqBrowser::draw() const
419 glMatrixMode(GL_MODELVIEW);
421 glPushName(MussaSegment);
423 glLoadName(MussaTrack);
426 // a selection shouldn't have a glName associated with it
430 void GlSeqBrowser::draw_selection() const
432 // draw selection box
434 glDepthMask(GL_FALSE);
436 glColor4f(0.6, 0.6, 0.6, 0.9);
437 glRectf(selectedRegion.left, selectedRegion.top,
438 selectedRegion.right, selectedRegion.bottom);
440 glDepthMask(GL_TRUE);
444 void GlSeqBrowser::draw_tracks() const
446 for(size_t track_i = 0; track_i != track_container.size(); ++track_i)
449 track_container[track_i].draw(cur_ortho.left, cur_ortho.right);
454 void GlSeqBrowser::draw_segments() const
457 // each vector contains path_segment_maps of all the connections
458 // between this track and the next
459 path_segment_map_vector::const_iterator psmv_i;
460 for(psmv_i = path_segments.begin();
461 psmv_i != path_segments.end();
464 path_segment_map_vector::difference_type path_index;
465 path_index = psmv_i - path_segments.begin();
466 // these maps contain the pair index (used so we dont keep drawing the
467 // same segment) and the actual segment structure.
468 pair_segment_map::const_iterator psm_i;
469 for(psm_i = psmv_i->begin();
470 psm_i != psmv_i->end();
473 // grab the index into our segment map
474 const segment_key& key = psm_i->first;
475 // the second element of our map pair is a segment
476 const Segment &s = psm_i->second;
477 // need to do something so we can detect our selection
478 vector<int> selected;
479 set_intersection(selected_paths.begin(), selected_paths.end(),
480 s.path_ids.begin(), s.path_ids.end(),
481 back_inserter(selected));
483 if (not s.reversed) {
484 if (selected_paths.size() == 0 or selected.size() > 0) {
485 glColor3f(1.0, 0.0, 0.0);
487 glColor3f(1.0, 0.8, 0.8);
490 if (selected_paths.size() == 0 or selected.size() > 0) {
491 glColor3f(0.0, 0.0, 1.0);
493 glColor3f(0.8, 0.8, 1.0);
496 // save the multipart name for our segment
497 glPushName(path_index); glPushName(key.first); glPushName(key.second);
499 glVertex3f(s.start.x, s.start.y, -1);
500 glVertex3f(s.end.x, s.end.y, -1);
503 glPopName(); glPopName(); glPopName();