1 #include "alg/glsequence.hpp"
9 static const float default_height = 12.0;
11 GlSequence::GlSequence(const Sequence &s,
12 boost::shared_ptr<AnnotationColors> cm)
15 char_pix_per_world_unit(2.5)
17 seq->setDrawable(default_drawable());
20 GlSequence::GlSequence(const GlSequence &s)
22 color_mapper(s.color_mapper),
23 char_pix_per_world_unit(s.char_pix_per_world_unit)
25 seq->setDrawable(copy_drawable(s.seq->drawable()));
28 GlSequence::GlSequence(const GlSequence *s)
30 color_mapper(s->color_mapper),
31 char_pix_per_world_unit(s->char_pix_per_world_unit)
33 seq->setDrawable(copy_drawable(s->seq->drawable()));
36 GlSequence::GlSequence(const GlSequenceRef s)
37 : Sequence( (SequenceRef)s ),
38 color_mapper(s->color_mapper),
39 char_pix_per_world_unit(s->char_pix_per_world_unit)
41 seq->setDrawable(copy_drawable(s->seq->drawable()));
44 GlSequence &GlSequence::operator=(const GlSequence & s)
47 Sequence::operator=(s);
48 seq->setDrawable(copy_drawable(s.seq->drawable()));
49 color_mapper = s.color_mapper;
50 assert(char_pix_per_world_unit == s.char_pix_per_world_unit);
55 DrawableRef GlSequence::default_drawable()
57 ColorRef c(new Color(0.0, 0.0, 0.0));
58 DrawableRef d(new Drawable(0.0, 0.0, 1.0, default_height, c));
62 DrawableRef GlSequence::copy_drawable(DrawableRef old_d)
64 ColorRef c(old_d->color());
65 DrawableRef d(new Drawable(old_d));
71 SeqSpanRef GlSequence::make_drawable_annotation(
72 Drawable::draw_func_ptr draw,
78 // create all the components of our annotation
79 // (should seq_i-start_i
80 SeqSpanRef empty_seq(seq->subseq(start, count));
81 AnnotationsRef empty_seq_annot(new Annotations(name));
82 DrawableRef drawable(default_drawable());
83 // glue everything to gether
84 drawable->setDrawFunction(draw);
85 drawable->setColor(color);
86 empty_seq->setAnnotations(empty_seq_annot);
87 empty_seq->setDrawable(drawable);
91 void GlSequence::add_annotations_for_defined_sequence(Drawable::draw_func_ptr draw)
93 ColorRef sequence_color(new Color(0.0, 0.0, 0.0));
94 Sequence::const_iterator start_i = begin();
95 Sequence::const_iterator seq_i = begin();
96 Sequence::const_iterator end_i = end();
98 Sequence::const_iterator start_block_i = end();
99 for(; seq_i != end_i; ++seq_i)
101 // need a better set of characters to serch for
102 if (not (*seq_i == 'N' or *seq_i == 'n')) {
103 if (start_block_i == end_i) {
104 start_block_i = seq_i;
107 if (start_block_i != end_i) {
109 size_type start = start_block_i - start_i;
110 size_type count = seq_i - start_block_i;
111 // add the annotation
113 make_drawable_annotation(draw, "sequence", start, count,
116 // reset our counter...
117 start_block_i = end_i;
121 // catch stuff at the end
122 if( start_block_i != end_i ) {
123 size_type start = start_block_i - start_i;
124 size_type count = seq_i - start_block_i;
125 add_annotation(make_drawable_annotation(draw, "sequence", start, count,
131 void GlSequence::update_annotation_draw_function(
133 Drawable::draw_func_ptr draw_func,
137 for(SeqSpanRefList::iterator annot_i = annotation_list->begin();
138 annot_i != annotation_list->end();
141 AnnotationsRef metadata( (*annot_i)->annotations() );
143 if (metadata->has_key("type") and metadata->get("type") == type) {
145 DrawableRef d((*annot_i)->drawable());
147 d = default_drawable();
148 (*annot_i)->setDrawable(d);
150 d->setDrawFunction(draw_func);
156 void GlSequence::setX(float value)
158 seq->drawable()->setX(value);
161 float GlSequence::x() const
163 return seq->drawable()->x();
166 void GlSequence::setY(GLfloat value)
168 seq->drawable()->setY(value);
171 float GlSequence::y() const
173 return seq->drawable()->y();
176 float GlSequence::z() const
178 return seq->drawable()->z();
181 float GlSequence::height() const
183 return seq->drawable()->height();
186 GLfloat GlSequence::right() const
191 GLfloat GlSequence::size() const
193 return Sequence::size();
196 Sequence::size_type GlSequence::leftbase(GLfloat left) const
198 left = ceil(left - x());
201 else if (left > Sequence::size() )
202 return Sequence::size();
204 return (Sequence::size_type)left;
207 Sequence::size_type GlSequence::rightbase(GLfloat right) const
209 right = floor(right) - x();
210 if (right > Sequence::size())
211 return Sequence::size();
215 return (Sequence::size_type)right;
218 Sequence::const_iterator
219 GlSequence::region_begin(GLfloat left, GLfloat right) const
221 if ( leftbase(left) > Sequence::size() or left > right )
222 return Sequence::end();
224 return Sequence::begin() + leftbase(left);
227 Sequence::const_iterator
228 GlSequence::region_end(GLfloat left, GLfloat right) const
230 if ( rightbase(right) > Sequence::size() or left > right )
231 return Sequence::end();
233 return Sequence::begin() + rightbase(right);
236 GlSequence GlSequence::subseq(size_type start, size_type count) const
238 GlSequence new_seq(*this);
239 new_seq.seq = seq->subseq(start, count);
240 // make sure our subseq has a drawable attached to it
241 // perhaps we should figure out correct x,y,z,h coords
242 DrawableRef d(default_drawable());
243 // and default to our current color
246 new_seq.seq->setDrawable(d);
247 copy_children(new_seq, start, count);
252 void GlSequence::setColor(ColorRef &c)
254 seq->drawable()->setColor(c);
257 ColorRef GlSequence::color()
259 return seq->drawable()->color();
262 const ColorRef GlSequence::color() const
264 return seq->drawable()->color();
267 ColorRef GlSequence::default_gene_color()
269 static ColorRef default_color;
270 if (not default_color) {
271 default_color.reset(new Color(0.0, 0.8, 0.0));
273 return default_color;
276 ColorRef GlSequence::default_track_color()
278 static ColorRef default_color;
279 if (not default_color) {
280 default_color.reset(new Color(0.0, 0.0, 0.0));
282 return default_color;
285 int GlSequence::get_viewport_width_in_pixels()
288 glGetIntegerv(GL_VIEWPORT, viewport);
289 return viewport[3]; // grab the viewport width
292 GLfloat GlSequence::pixelWidth(GLfloat left, GLfloat right)
294 return pixelWidth(left, right, get_viewport_width_in_pixels());
298 GlSequence::pixelWidth(GLfloat left, GLfloat right, int vp_width)
300 return round((right-left)/vp_width);
303 bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right) const
305 return is_sequence_renderable(left, right, get_viewport_width_in_pixels());
308 bool GlSequence::is_sequence_renderable(GLfloat left,
310 int viewport_width) const
312 GLfloat world_width = right - left;
313 GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
315 // if the number of pixels taken up by rendering the characters
316 // that'd show up in the current ortho width is less than the window
317 // width we can actually draw something
318 return pixels_needed < viewport_width;
322 void GlSequence::draw(GLfloat left, GLfloat right) const
324 if ( not is_sequence_renderable(left, right) ) {
325 draw_track(left, right);
327 draw_sequence(left, right);
329 draw_annotations(left, right);
332 void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
333 GLfloat left, GLfloat right,
334 GLfloat height, GLfloat y, GLfloat z,
337 GLfloat pixel_width = pixelWidth(world_left, world_right);
338 GLfloat offset = height/2.0;
339 GLfloat top = y + offset;
340 GLfloat bottom = y - offset;
342 // make our box be at least 1 pixel
343 if ((right-left) < pixel_width) {
344 right = left + pixel_width;
347 glVertex3f(left, top, z);
348 glVertex3f(left, bottom, z);
349 glVertex3f(right, bottom, z);
350 glVertex3f(right, top, z);
354 void GlSequence::draw_track(GLfloat left, GLfloat right) const
356 // draw main sequence track
357 glColor3fv(color()->get());
358 draw_box(left, right, x(), x()+Sequence::size(), height(), y(), 0.0, GL_LINE_LOOP);
360 glColor3f(0.8, 0.8, 0.8);
361 draw_box(left, right, x(), x()+Sequence::size(), height(), y(), 0.0);
364 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
367 GLfloat annotation_z = z() + 10.0;
368 const SeqSpanRefList& annots = Sequence::annotations();
369 const MotifList& motifs = Sequence::motifs();
370 for (SeqSpanRefList::const_iterator annot_itor = annots.begin();
371 annot_itor != annots.end();
374 DrawableRef drawable((*annot_itor)->drawable());
375 if (drawable and drawable->drawFunction()) {
376 assert((*annot_itor)->parent() == seq);
377 drawable->drawFunction()((*annot_itor), left, right);
379 glColor3fv(default_gene_color()->get());
380 draw_box(left, right, x()+(*annot_itor)->start(), x()+(*annot_itor)->stop(),
381 height(), y(), annotation_z);
385 for (MotifList::const_iterator motifs_itor = motifs.begin();
386 motifs_itor != motifs.end();
389 glColor3fv(color_mapper->lookup("motif", motifs_itor->sequence).get());
390 draw_box(left, right, x()+motifs_itor->begin, x()+motifs_itor->end,
391 height(), y(), annotation_z+1.0);
395 // this way of drawing characters, came from the red open gl book
397 const int STROKE = 2;
400 typedef struct charpoint {
406 {0, -5, PT}, {2.5, 5, PT}, {5, -5, STROKE},
407 {0.75, -2, PT}, {4.25, -2, END}
411 {2.5, -5, PT}, {2.5,5, STROKE}, {0, 5, PT}, {5, 5, END}
415 {5, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
416 {2, -5, PT}, {3, -5, PT}, {5, -3, STROKE},
417 {2.5, -1, PT}, {5, -1,PT}, {5, -5, END}
421 {4.9, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
422 {2, -5, PT}, {3, -5, PT}, {5, -3, END}
425 CP Xdata[] = {{ 0, 5, PT}, {5, -5,STROKE},{0,-5,PT},{5, 5, END}};
426 CP Ndata[] = {{ 0, -5, PT}, {0, 5, PT}, {5, -5, PT}, {5, 5, END}};
428 //! the maximum width used for a charcter glyph
429 const int max_glyph_width = 5; // unit ( glyph_coord )
431 static void drawLetter(CP *l, GLfloat z)
433 glBegin(GL_LINE_STRIP);
437 glVertex3f(l->x, l->y, z);
440 glVertex3f(l->x, l->y, z);
442 glBegin(GL_LINE_STRIP);
445 glVertex3f(l->x, l->y, z);
450 throw runtime_error("data structure failure");
456 void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
458 // FIXME: basically this needs to be greater than the number of annotations
459 const GLfloat z = 30;
461 glColor3fv(color()->get());
463 Sequence::const_iterator seq_itor = region_begin(left, right);
464 Sequence::const_iterator seq_end = region_end(left, right);
465 Sequence::size_type basepair = 0;
466 const float bp_per_world = 1.0; //( world coord )
467 const float glyph_x_scale = 0.125; // unit = ( world coord / glyph coord )
468 // compute how much space there should be to either size of a letter
469 const float glyph_margin = (bp_per_world - glyph_x_scale * max_glyph_width)
472 assert(seq_end - seq_itor >= 0);
473 while(seq_itor != seq_end)
475 assert ( basepair < Sequence::size() );
477 glTranslatef( x()+leftbase(left) + basepair + glyph_margin, y(), 1.0 );
478 glScalef(glyph_x_scale, 1.0, 1.0);
481 drawLetter(Adata, z);
484 drawLetter(Tdata, z);
487 drawLetter(Gdata, z);
490 drawLetter(Cdata, z);
493 drawLetter(Ndata, z);
496 drawLetter(Xdata, z);
505 bool operator==(const GlSequence &left, const GlSequence &right)
507 return ( (left.x() == right.x()) and
508 (left.y() == right.y()) and
509 (left.z() == right.z()) and
510 (left.height() == right.height()) and
511 (left.color() == right.color()));
514 void draw_narrow_track(SeqSpanRef s, float left, float right)
516 SeqSpanRef parent(s->parent());
517 DrawableRef parent_draw(parent->drawable());
518 float x( (parent_draw) ? parent_draw->x() : 0);
519 float y( (parent_draw) ? parent_draw->y() : 0);
520 float z( (parent_draw) ? parent_draw->z() : 10 );
521 float height( (parent_draw) ? parent_draw->height() : default_height );
522 Color c( (s->drawable()) ? s->drawable()->color() : *GlSequence::default_track_color() );
525 float hsmall = height * 0.25;
526 GlSequence::draw_box(left, right, x+s->start(), x+s->stop(),
530 void draw_track(SeqSpanRef s, float left, float right)
532 SeqSpanRef parent(s->parent());
533 DrawableRef parent_draw(parent->drawable());
534 float x( (parent_draw) ? parent_draw->x() : 0);
535 float y( (parent_draw) ? parent_draw->y() : 0);
536 float z( (parent_draw) ? parent_draw->z() : 10 );
537 float height( (parent_draw) ? parent_draw->height() : default_height );
538 Color c( (s->drawable()) ? s->drawable()->color() : *GlSequence::default_track_color() );
540 glColor3fv( c.get() );
541 GlSequence::draw_box(left, right, x+s->start(), x+s->stop(),