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 int GlSequence::get_viewport_width_in_pixels()
270 glGetIntegerv(GL_VIEWPORT, viewport);
271 return viewport[3]; // grab the viewport width
274 GLfloat GlSequence::pixelWidth(GLfloat left, GLfloat right)
276 return pixelWidth(left, right, get_viewport_width_in_pixels());
280 GlSequence::pixelWidth(GLfloat left, GLfloat right, int vp_width)
282 return round((right-left)/vp_width);
285 bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right) const
287 return is_sequence_renderable(left, right, get_viewport_width_in_pixels());
290 bool GlSequence::is_sequence_renderable(GLfloat left,
292 int viewport_width) const
294 GLfloat world_width = right - left;
295 GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
297 // if the number of pixels taken up by rendering the characters
298 // that'd show up in the current ortho width is less than the window
299 // width we can actually draw something
300 return pixels_needed < viewport_width;
304 void GlSequence::draw(GLfloat left, GLfloat right) const
306 if ( not is_sequence_renderable(left, right) ) {
307 draw_track(left, right);
309 draw_sequence(left, right);
311 draw_annotations(left, right);
314 void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
315 GLfloat left, GLfloat right,
316 GLfloat height, GLfloat y, GLfloat z,
319 GLfloat pixel_width = pixelWidth(world_left, world_right);
320 GLfloat offset = height/2.0;
321 GLfloat top = y + offset;
322 GLfloat bottom = y - offset;
324 // make our box be at least 1 pixel
325 if ((right-left) < pixel_width) {
326 right = left + pixel_width;
329 glVertex3f(left, top, z);
330 glVertex3f(left, bottom, z);
331 glVertex3f(right, bottom, z);
332 glVertex3f(right, top, z);
336 void GlSequence::draw_track(GLfloat left, GLfloat right) const
338 glColor3fv(color()->get());
339 // draw main sequence track
340 draw_box(left, right, x(), x()+Sequence::size(), height(), y(), 0.0, GL_LINE_LOOP);
343 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
346 GLfloat annotation_z = z() + 10.0;
347 const SeqSpanRefList& annots = Sequence::annotations();
348 const MotifList& motifs = Sequence::motifs();
349 for (SeqSpanRefList::const_iterator annot_itor = annots.begin();
350 annot_itor != annots.end();
353 DrawableRef drawable((*annot_itor)->drawable());
354 if (drawable and drawable->drawFunction()) {
355 assert((*annot_itor)->parent() == seq);
356 drawable->drawFunction()((*annot_itor), left, right);
358 glColor3f(0.0, 0.8, 0.0);
359 draw_box(left, right, x()+(*annot_itor)->start(), x()+(*annot_itor)->stop(),
360 height(), y(), annotation_z);
364 for (MotifList::const_iterator motifs_itor = motifs.begin();
365 motifs_itor != motifs.end();
368 glColor3fv(color_mapper->lookup("motif", motifs_itor->sequence).get());
369 draw_box(left, right, x()+motifs_itor->begin, x()+motifs_itor->end,
370 height(), y(), annotation_z+1.0);
374 // this way of drawing characters, came from the red open gl book
376 const int STROKE = 2;
379 typedef struct charpoint {
385 {0, -5, PT}, {2.5, 5, PT}, {5, -5, STROKE},
386 {0.75, -2, PT}, {4.25, -2, END}
390 {2.5, -5, PT}, {2.5,5, STROKE}, {0, 5, PT}, {5, 5, END}
394 {5, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
395 {2, -5, PT}, {3, -5, PT}, {5, -3, STROKE},
396 {2.5, -1, PT}, {5, -1,PT}, {5, -5, END}
400 {4.9, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
401 {2, -5, PT}, {3, -5, PT}, {5, -3, END}
404 CP Xdata[] = {{ 0, 5, PT}, {5, -5,STROKE},{0,-5,PT},{5, 5, END}};
405 CP Ndata[] = {{ 0, -5, PT}, {0, 5, PT}, {5, -5, PT}, {5, 5, END}};
407 //! the maximum width used for a charcter glyph
408 const int max_glyph_width = 5; // unit ( glyph_coord )
410 static void drawLetter(CP *l, GLfloat z)
412 glBegin(GL_LINE_STRIP);
416 glVertex3f(l->x, l->y, z);
419 glVertex3f(l->x, l->y, z);
421 glBegin(GL_LINE_STRIP);
424 glVertex3f(l->x, l->y, z);
429 throw runtime_error("data structure failure");
435 void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
437 // FIXME: basically this needs to be greater than the number of annotations
438 const GLfloat z = 30;
440 glColor3fv(color()->get());
442 Sequence::const_iterator seq_itor = region_begin(left, right);
443 Sequence::const_iterator seq_end = region_end(left, right);
444 Sequence::size_type basepair = 0;
445 const float bp_per_world = 1.0; //( world coord )
446 const float glyph_x_scale = 0.125; // unit = ( world coord / glyph coord )
447 // compute how much space there should be to either size of a letter
448 const float glyph_margin = (bp_per_world - glyph_x_scale * max_glyph_width)
451 assert(seq_end - seq_itor >= 0);
452 while(seq_itor != seq_end)
454 assert ( basepair < Sequence::size() );
456 glTranslatef( x()+leftbase(left) + basepair + glyph_margin, y(), 1.0 );
457 glScalef(glyph_x_scale, 1.0, 1.0);
460 drawLetter(Adata, z);
463 drawLetter(Tdata, z);
466 drawLetter(Gdata, z);
469 drawLetter(Cdata, z);
472 drawLetter(Ndata, z);
475 drawLetter(Xdata, z);
484 bool operator==(const GlSequence &left, const GlSequence &right)
486 return ( (left.x() == right.x()) and
487 (left.y() == right.y()) and
488 (left.z() == right.z()) and
489 (left.height() == right.height()) and
490 (left.color() == right.color()));
493 void draw_narrow_track(SeqSpanRef s, float left, float right)
495 SeqSpanRef parent(s->parent());
496 DrawableRef parent_draw(parent->drawable());
497 float x( (parent_draw) ? parent_draw->x() : 0);
498 float y( (parent_draw) ? parent_draw->y() : 0);
499 float z( (parent_draw) ? parent_draw->z() : 10 );
500 float height( (parent_draw) ? parent_draw->height() : default_height );
501 Color c( (s->drawable()) ? s->drawable()->color() : Color(0.0, 0.0, 0.0) );
504 float hsmall = height * 0.25;
505 GlSequence::draw_box(left, right, x+s->start(), x+s->stop(),
509 void draw_track(SeqSpanRef s, float left, float right)
511 SeqSpanRef parent(s->parent());
512 DrawableRef parent_draw(parent->drawable());
513 float x( (parent_draw) ? parent_draw->x() : 0);
514 float y( (parent_draw) ? parent_draw->y() : 0);
515 float z( (parent_draw) ? parent_draw->z() : 10 );
516 float height( (parent_draw) ? parent_draw->height() : default_height );
517 Color c( (s->drawable()) ? s->drawable()->color() : Color(0.0, 0.0, 0.0) );
519 glColor3fv( c.get() );
520 GlSequence::draw_box(left, right, x+s->start(), x+s->stop(),