1 #include "alg/glsequence.hpp"
9 GlSequence::GlSequence(const Sequence &s,
10 boost::shared_ptr<AnnotationColors> cm)
13 char_pix_per_world_unit(2.5)
15 seq->setDrawable(default_drawable());
18 GlSequence::GlSequence(const GlSequence &s)
20 color_mapper(s.color_mapper),
21 char_pix_per_world_unit(s.char_pix_per_world_unit)
23 seq->setDrawable(copy_drawable(s.seq->drawable()));
26 GlSequence::GlSequence(const GlSequence *s)
28 color_mapper(s->color_mapper),
29 char_pix_per_world_unit(s->char_pix_per_world_unit)
31 seq->setDrawable(copy_drawable(s->seq->drawable()));
34 GlSequence::GlSequence(const GlSequenceRef s)
35 : Sequence( (SequenceRef)s ),
36 color_mapper(s->color_mapper),
37 char_pix_per_world_unit(s->char_pix_per_world_unit)
39 seq->setDrawable(copy_drawable(s->seq->drawable()));
42 GlSequence &GlSequence::operator=(const GlSequence & s)
45 Sequence::operator=(s);
46 seq->setDrawable(copy_drawable(s.seq->drawable()));
47 color_mapper = s.color_mapper;
48 assert(char_pix_per_world_unit == s.char_pix_per_world_unit);
53 DrawableRef GlSequence::default_drawable()
55 ColorRef c(new Color(0.0, 0.0, 0.0));
56 DrawableRef d(new Drawable(0.0, 0.0, 1.0, 12.0, c));
60 DrawableRef GlSequence::copy_drawable(DrawableRef old_d)
62 ColorRef c(old_d->color());
63 DrawableRef d(new Drawable(old_d));
69 void GlSequence::setX(float value)
71 seq->drawable()->setX(value);
74 float GlSequence::x() const
76 return seq->drawable()->x();
79 void GlSequence::setY(GLfloat value)
81 seq->drawable()->setY(value);
84 float GlSequence::y() const
86 return seq->drawable()->y();
89 float GlSequence::z() const
91 return seq->drawable()->z();
94 float GlSequence::height() const
96 return seq->drawable()->height();
99 GLfloat GlSequence::right() const
104 GLfloat GlSequence::size() const
106 return Sequence::size();
109 Sequence::size_type GlSequence::leftbase(GLfloat left) const
111 left = ceil(left - x());
114 else if (left > Sequence::size() )
115 return Sequence::size();
117 return (Sequence::size_type)left;
120 Sequence::size_type GlSequence::rightbase(GLfloat right) const
122 right = floor(right) - x();
123 if (right > Sequence::size())
124 return Sequence::size();
128 return (Sequence::size_type)right;
131 Sequence::const_iterator
132 GlSequence::region_begin(GLfloat left, GLfloat right) const
134 if ( leftbase(left) > Sequence::size() or left > right )
135 return Sequence::end();
137 return Sequence::begin() + leftbase(left);
140 Sequence::const_iterator
141 GlSequence::region_end(GLfloat left, GLfloat right) const
143 if ( rightbase(right) > Sequence::size() or left > right )
144 return Sequence::end();
146 return Sequence::begin() + rightbase(right);
149 GlSequence GlSequence::subseq(size_type start, size_type count) const
151 GlSequence new_seq(*this);
152 new_seq.seq = seq->subseq(start, count);
153 // make sure our subseq has a drawable attached to it
154 // perhaps we should figure out correct x,y,z,h coords
155 DrawableRef d(default_drawable());
156 // and default to our current color
159 new_seq.seq->setDrawable(d);
160 copy_children(new_seq, start, count);
165 void GlSequence::setColor(ColorRef &c)
167 seq->drawable()->setColor(c);
170 ColorRef GlSequence::color()
172 return seq->drawable()->color();
175 const ColorRef GlSequence::color() const
177 return seq->drawable()->color();
180 int GlSequence::get_viewport_width_in_pixels()
183 glGetIntegerv(GL_VIEWPORT, viewport);
184 return viewport[3]; // grab the viewport width
187 GLfloat GlSequence::pixelWidth(GLfloat left, GLfloat right) const
189 return pixelWidth(left, right, get_viewport_width_in_pixels());
193 GlSequence::pixelWidth(GLfloat left, GLfloat right, int vp_width) const
195 return round((right-left)/vp_width);
198 bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right) const
200 return is_sequence_renderable(left, right, get_viewport_width_in_pixels());
203 bool GlSequence::is_sequence_renderable(GLfloat left,
205 int viewport_width) const
207 GLfloat world_width = right - left;
208 GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
210 // if the number of pixels taken up by rendering the characters
211 // that'd show up in the current ortho width is less than the window
212 // width we can actually draw something
213 return pixels_needed < viewport_width;
217 void GlSequence::draw(GLfloat left, GLfloat right) const
219 if ( not is_sequence_renderable(left, right) ) {
220 draw_track(left, right);
222 draw_sequence(left, right);
224 draw_annotations(left, right);
227 void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
228 GLfloat left, GLfloat right,
229 GLfloat height, GLfloat z) const
231 GLfloat pixel_width = pixelWidth(world_left, world_right);
232 GLfloat offset = height/2.0;
233 GLfloat top = y() + offset;
234 GLfloat bottom = y() - offset;
236 // make our box be at least 1 pixel
237 if ((right-left) < pixel_width) {
238 right = left + pixel_width;
241 glVertex3f(left, top, z);
242 glVertex3f(left, bottom, z);
243 glVertex3f(right, bottom, z);
244 glVertex3f(right, top, z);
248 void GlSequence::draw_track(GLfloat left, GLfloat right) const
250 glColor3fv(color()->get());
251 // draw main sequence track
252 draw_box(left, right, x(), x()+Sequence::size(), height(), 0.0);
255 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
258 GLfloat annotation_z = z() + 10.0;
259 const std::list<annot>& annots = Sequence::annotations();
260 const MotifList& motifs = Sequence::motifs();
261 for (std::list<annot>::const_iterator annot_itor = annots.begin();
262 annot_itor != annots.end();
265 glColor3f(0.0, 0.8, 0.0);
266 draw_box(left, right, x()+annot_itor->begin, x()+annot_itor->end,
267 height(), annotation_z);
270 for (MotifList::const_iterator motifs_itor = motifs.begin();
271 motifs_itor != motifs.end();
274 glColor3fv(color_mapper->lookup("motif", motifs_itor->sequence).get());
275 draw_box(left, right, x()+motifs_itor->begin, x()+motifs_itor->end,
276 height(), annotation_z+1.0);
280 // this way of drawing characters, came from the red open gl book
282 const int STROKE = 2;
285 typedef struct charpoint {
291 {0, -5, PT}, {2.5, 5, PT}, {5, -5, STROKE},
292 {0.75, -2, PT}, {4.25, -2, END}
296 {2.5, -5, PT}, {2.5,5, STROKE}, {0, 5, PT}, {5, 5, END}
300 {5, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
301 {2, -5, PT}, {3, -5, PT}, {5, -3, STROKE},
302 {2.5, -1, PT}, {5, -1,PT}, {5, -5, END}
306 {4.9, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
307 {2, -5, PT}, {3, -5, PT}, {5, -3, END}
310 CP Xdata[] = {{ 0, 5, PT}, {5, -5,STROKE},{0,-5,PT},{5, 5, END}};
311 CP Ndata[] = {{ 0, -5, PT}, {0, 5, PT}, {5, -5, PT}, {5, 5, END}};
313 //! the maximum width used for a charcter glyph
314 const int max_glyph_width = 5; // unit ( glyph_coord )
316 static void drawLetter(CP *l, GLfloat z)
318 glBegin(GL_LINE_STRIP);
322 glVertex3f(l->x, l->y, z);
325 glVertex3f(l->x, l->y, z);
327 glBegin(GL_LINE_STRIP);
330 glVertex3f(l->x, l->y, z);
335 throw runtime_error("data structure failure");
341 void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
343 // FIXME: basically this needs to be greater than the number of annotations
344 const GLfloat z = 30;
346 glColor3fv(color()->get());
348 Sequence::const_iterator seq_itor = region_begin(left, right);
349 Sequence::const_iterator seq_end = region_end(left, right);
350 Sequence::size_type basepair = 0;
351 const float bp_per_world = 1.0; //( world coord )
352 const float glyph_x_scale = 0.125; // unit = ( world coord / glyph coord )
353 // compute how much space there should be to either size of a letter
354 const float glyph_margin = (bp_per_world - glyph_x_scale * max_glyph_width)
357 assert(seq_end - seq_itor >= 0);
358 while(seq_itor != seq_end)
360 assert ( basepair < Sequence::size() );
362 glTranslatef( x()+leftbase(left) + basepair + glyph_margin, y(), 1.0 );
363 glScalef(glyph_x_scale, 1.0, 1.0);
366 drawLetter(Adata, z);
369 drawLetter(Tdata, z);
372 drawLetter(Gdata, z);
375 drawLetter(Cdata, z);
378 drawLetter(Ndata, z);
381 drawLetter(Xdata, z);
390 bool operator==(const GlSequence &left, const GlSequence &right)
392 return ( (left.x() == right.x()) and
393 (left.y() == right.y()) and
394 (left.z() == right.z()) and
395 (left.height() == right.height()) and
396 (left.color() == right.color()));