store default colors
[mussa.git] / alg / glsequence.cpp
1 #include "alg/glsequence.hpp"
2
3 #include <iostream>
4 #include <cassert>
5 #include <math.h>
6 #include <stdexcept>
7 using namespace std;
8
9 static const float default_height = 12.0;
10
11 GlSequence::GlSequence(const Sequence &s, 
12                        boost::shared_ptr<AnnotationColors> cm) 
13   : Sequence(s),
14     color_mapper(cm),
15     char_pix_per_world_unit(2.5)
16 {
17   seq->setDrawable(default_drawable());
18 }
19
20 GlSequence::GlSequence(const GlSequence &s)
21   : Sequence(s),
22     color_mapper(s.color_mapper),
23     char_pix_per_world_unit(s.char_pix_per_world_unit)
24 {
25   seq->setDrawable(copy_drawable(s.seq->drawable()));
26 }
27
28 GlSequence::GlSequence(const GlSequence *s)
29   : Sequence(s),
30     color_mapper(s->color_mapper),
31     char_pix_per_world_unit(s->char_pix_per_world_unit)
32 {
33   seq->setDrawable(copy_drawable(s->seq->drawable()));
34 }
35
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)
40 {
41   seq->setDrawable(copy_drawable(s->seq->drawable()));
42 }
43
44 GlSequence &GlSequence::operator=(const GlSequence & s)
45 {
46   if (this != &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);
51   }
52   return *this;
53 }
54
55 DrawableRef GlSequence::default_drawable()
56 {
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));
59   return d;
60 }
61
62 DrawableRef GlSequence::copy_drawable(DrawableRef old_d)
63 {
64   ColorRef c(old_d->color());
65   DrawableRef d(new Drawable(old_d));
66   // use the same color
67   d->setColor(c);
68   return d;
69 }
70
71 SeqSpanRef GlSequence::make_drawable_annotation(
72   Drawable::draw_func_ptr draw,
73   std::string name, 
74   size_type start,
75   size_type count,
76   ColorRef color)
77 {
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);
88   return empty_seq;
89 }
90
91 void GlSequence::add_annotations_for_defined_sequence(Drawable::draw_func_ptr draw)
92 {
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();
97   
98   Sequence::const_iterator start_block_i = end();
99   for(; seq_i != end_i; ++seq_i)
100   {
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;
105       }
106     } else {
107       if (start_block_i != end_i) {
108         // we got one.
109         size_type start = start_block_i - start_i;
110         size_type count = seq_i - start_block_i;
111         // add the annotation
112         add_annotation(
113           make_drawable_annotation(draw, "sequence", start, count, 
114                                    sequence_color)
115         );        
116         // reset our counter...
117         start_block_i = end_i;
118       }
119     }
120   }
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, 
126                                             sequence_color)
127                   );        
128   }
129 }
130
131 void GlSequence::update_annotation_draw_function(
132        std::string type, 
133        Drawable::draw_func_ptr draw_func,
134        ColorRef color
135 )
136 {
137   for(SeqSpanRefList::iterator annot_i = annotation_list->begin();
138       annot_i != annotation_list->end();
139       ++annot_i)
140   {
141     AnnotationsRef metadata( (*annot_i)->annotations() );
142      
143     if (metadata->has_key("type") and metadata->get("type") == type) {
144       // we should update
145       DrawableRef d((*annot_i)->drawable());
146       if (!d) {
147         d = default_drawable();
148         (*annot_i)->setDrawable(d);
149       }
150       d->setDrawFunction(draw_func);
151       d->setColor(color);
152     }
153   }
154 }
155
156 void GlSequence::setX(float value)
157 {
158   seq->drawable()->setX(value);
159 }
160
161 float GlSequence::x() const
162 {
163   return seq->drawable()->x();
164 }
165
166 void GlSequence::setY(GLfloat value)
167 {
168   seq->drawable()->setY(value);
169 }
170
171 float GlSequence::y() const
172 {
173   return seq->drawable()->y();
174 }
175
176 float GlSequence::z() const
177 {
178   return seq->drawable()->z();
179 }
180
181 float GlSequence::height() const
182 {
183   return seq->drawable()->height();
184 }
185
186 GLfloat GlSequence::right() const
187 {
188   return size()+x();
189 }
190
191 GLfloat GlSequence::size() const
192 {
193   return Sequence::size();
194 }
195
196 Sequence::size_type GlSequence::leftbase(GLfloat left) const
197 {
198   left = ceil(left - x());
199   if (left < 0)
200     return 0;
201   else if (left > Sequence::size() )
202     return Sequence::size();
203   else
204     return (Sequence::size_type)left;
205 }
206
207 Sequence::size_type GlSequence::rightbase(GLfloat right) const
208 {
209   right = floor(right) - x();
210   if (right > Sequence::size())
211     return Sequence::size();
212   else if ( right < 0) 
213     return 0;
214   else 
215     return (Sequence::size_type)right;
216 }
217
218 Sequence::const_iterator 
219 GlSequence::region_begin(GLfloat left, GLfloat right) const
220 {
221   if ( leftbase(left) > Sequence::size() or left > right )
222     return Sequence::end();
223   else
224     return Sequence::begin() + leftbase(left);
225 }
226
227 Sequence::const_iterator 
228 GlSequence::region_end(GLfloat left, GLfloat right) const
229 {
230   if ( rightbase(right) > Sequence::size() or left > right )
231     return Sequence::end();
232   else
233     return Sequence::begin() + rightbase(right); 
234 }
235
236 GlSequence GlSequence::subseq(size_type start, size_type count) const
237 {
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
244   ColorRef c(color());
245   d->setColor(c);
246   new_seq.seq->setDrawable(d);
247   copy_children(new_seq, start, count);
248   
249   return new_seq;
250 }
251
252 void GlSequence::setColor(ColorRef &c)
253 {
254   seq->drawable()->setColor(c);
255 }
256
257 ColorRef GlSequence::color()
258 {
259   return seq->drawable()->color();
260 }
261
262 const ColorRef GlSequence::color() const
263 {
264   return seq->drawable()->color();
265 }
266
267 ColorRef GlSequence::default_gene_color()
268 {
269   static ColorRef default_color;
270   if (not default_color) {
271     default_color.reset(new Color(0.0, 0.8, 0.0));
272   }
273   return default_color;
274 }
275
276 ColorRef GlSequence::default_track_color()
277 {
278   static ColorRef default_color;
279   if (not default_color) {
280     default_color.reset(new Color(0.0, 0.0, 0.0));
281   }
282   return default_color;
283 }
284
285 int GlSequence::get_viewport_width_in_pixels()
286 {
287   GLint viewport[4];
288   glGetIntegerv(GL_VIEWPORT, viewport);
289   return viewport[3]; // grab the viewport width
290 }
291
292 GLfloat GlSequence::pixelWidth(GLfloat left, GLfloat right)
293 {
294   return pixelWidth(left, right, get_viewport_width_in_pixels());
295 }
296
297 GLfloat
298 GlSequence::pixelWidth(GLfloat left, GLfloat right, int vp_width)
299 {
300   return round((right-left)/vp_width);
301 }
302
303 bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right) const
304 {
305   return is_sequence_renderable(left, right, get_viewport_width_in_pixels());
306 }
307
308 bool GlSequence::is_sequence_renderable(GLfloat left, 
309                                         GLfloat right, 
310                                         int viewport_width) const
311 {
312   GLfloat world_width = right - left;
313   GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
314
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;
319 }
320
321
322 void GlSequence::draw(GLfloat left, GLfloat right) const
323 {
324   if ( not is_sequence_renderable(left, right) ) {
325     draw_track(left, right);
326   } else {
327     draw_sequence(left, right);
328   }
329   draw_annotations(left, right);
330 }
331
332 void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
333                           GLfloat left, GLfloat right, 
334                           GLfloat height, GLfloat y, GLfloat z,
335                           GLint primitive)
336 {
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;
341   
342   // make our box be at least 1 pixel
343   if ((right-left) < pixel_width) {
344     right = left + pixel_width;
345   }
346   glBegin(primitive);
347     glVertex3f(left,  top,    z);
348     glVertex3f(left,  bottom, z);
349     glVertex3f(right, bottom, z);
350     glVertex3f(right, top,    z);
351   glEnd();
352 }
353
354 void GlSequence::draw_track(GLfloat left, GLfloat right) const
355 {
356   glColor3fv(color()->get());
357   // draw main sequence track
358   draw_box(left, right, x(), x()+Sequence::size(), height(), y(), 0.0, GL_LINE_LOOP);
359 }
360
361 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
362 {
363   // draw annotations
364   GLfloat annotation_z = z() + 10.0;
365   const SeqSpanRefList& annots = Sequence::annotations();
366   const MotifList& motifs = Sequence::motifs();
367   for (SeqSpanRefList::const_iterator annot_itor = annots.begin();
368        annot_itor != annots.end();
369        ++annot_itor)
370   {
371     DrawableRef drawable((*annot_itor)->drawable());
372     if (drawable and drawable->drawFunction()) {
373       assert((*annot_itor)->parent() == seq);
374       drawable->drawFunction()((*annot_itor), left, right); 
375     } else {
376       glColor3fv(default_gene_color()->get());
377       draw_box(left, right, x()+(*annot_itor)->start(), x()+(*annot_itor)->stop(), 
378                height(), y(), annotation_z);
379     }
380   }
381   // if motifs?
382   for (MotifList::const_iterator motifs_itor = motifs.begin();
383        motifs_itor != motifs.end();
384        ++motifs_itor)
385   {
386     glColor3fv(color_mapper->lookup("motif", motifs_itor->sequence).get());
387     draw_box(left, right, x()+motifs_itor->begin, x()+motifs_itor->end, 
388              height(), y(), annotation_z+1.0);
389   }
390 }
391
392 // this way of drawing characters, came from the red open gl book
393 const int PT = 1;
394 const int STROKE = 2;
395 const int END =3;
396
397 typedef struct charpoint {
398   GLfloat x, y;
399   int type;
400 } CP;
401
402 CP Adata[] = {
403   {0, -5, PT}, {2.5, 5, PT}, {5, -5, STROKE}, 
404   {0.75, -2, PT}, {4.25, -2, END}
405 };
406
407 CP Tdata[] = {
408   {2.5, -5, PT}, {2.5,5, STROKE}, {0, 5, PT}, {5, 5, END}
409 };
410
411 CP Gdata[] = {
412   {5, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
413   {2, -5, PT}, {3, -5, PT}, {5, -3, STROKE}, 
414   {2.5, -1, PT}, {5, -1,PT}, {5, -5, END}
415 };
416
417 CP Cdata[] = {
418   {4.9, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
419   {2, -5, PT}, {3, -5, PT}, {5, -3, END}
420 };
421
422 CP Xdata[] = {{ 0, 5, PT}, {5, -5,STROKE},{0,-5,PT},{5, 5, END}};
423 CP Ndata[] = {{ 0, -5, PT}, {0, 5, PT}, {5, -5, PT}, {5, 5, END}};
424
425 //! the maximum width used for a charcter glyph
426 const int max_glyph_width = 5; // unit ( glyph_coord )
427
428 static void drawLetter(CP *l, GLfloat z)
429 {
430   glBegin(GL_LINE_STRIP);
431   while(1) {
432     switch (l->type) {
433       case PT:
434         glVertex3f(l->x, l->y, z);
435         break;
436       case STROKE:
437         glVertex3f(l->x, l->y, z);
438         glEnd();
439         glBegin(GL_LINE_STRIP);
440         break;
441       case END:
442         glVertex3f(l->x, l->y, z);
443         glEnd();
444         return;
445         break;
446       default:
447          throw runtime_error("data structure failure");
448     }
449     l++;
450   }
451 }
452
453 void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
454 {
455   // FIXME: basically this needs to be greater than the number of annotations
456   const GLfloat z = 30;
457   glLineWidth(1);
458   glColor3fv(color()->get());
459
460   Sequence::const_iterator seq_itor = region_begin(left, right);
461   Sequence::const_iterator seq_end = region_end(left, right);
462   Sequence::size_type basepair = 0;
463   const float bp_per_world = 1.0; //( world coord )
464   const float glyph_x_scale = 0.125; // unit = ( world coord / glyph coord )
465   // compute how much space there should be to either size of a letter
466   const float glyph_margin = (bp_per_world - glyph_x_scale * max_glyph_width) 
467                            / 2.0;  
468
469   assert(seq_end - seq_itor >= 0);
470   while(seq_itor != seq_end)
471   {
472     assert ( basepair < Sequence::size() );
473     glPushMatrix();
474     glTranslatef( x()+leftbase(left) + basepair + glyph_margin, y(), 1.0 );
475     glScalef(glyph_x_scale, 1.0, 1.0);
476     switch (*seq_itor) {
477       case 'A': case 'a':
478         drawLetter(Adata, z);
479         break;
480       case 'T': case 't':
481         drawLetter(Tdata, z);
482         break;
483       case 'G': case 'g':
484         drawLetter(Gdata, z);
485         break;
486       case 'C': case 'c':
487         drawLetter(Cdata, z);
488         break;
489       case 'N': case 'n':
490         drawLetter(Ndata, z);
491         break;
492       default:
493         drawLetter(Xdata, z);
494         break;
495     }
496     glPopMatrix();
497     ++seq_itor;
498     ++basepair;
499   }
500 }
501
502 bool operator==(const GlSequence &left, const GlSequence &right)
503 {
504   return ( (left.x() == right.x()) and
505            (left.y() == right.y()) and
506            (left.z() == right.z()) and
507            (left.height() == right.height()) and
508            (left.color() == right.color()));
509 }
510
511 void draw_narrow_track(SeqSpanRef s, float left, float right)
512 {
513   SeqSpanRef parent(s->parent());
514   DrawableRef parent_draw(parent->drawable());
515   float x( (parent_draw) ? parent_draw->x() : 0);
516   float y( (parent_draw) ? parent_draw->y() : 0);
517   float z( (parent_draw) ? parent_draw->z() : 10 );
518   float height( (parent_draw) ? parent_draw->height() : default_height ); 
519   Color c( (s->drawable()) ? s->drawable()->color() : *GlSequence::default_track_color() );
520   glColor3fv(c.get());
521   
522   float hsmall = height * 0.25;
523   GlSequence::draw_box(left, right, x+s->start(), x+s->stop(), 
524                        hsmall, y, z+10);
525 }
526
527 void draw_track(SeqSpanRef s, float left, float right)
528 {
529   SeqSpanRef parent(s->parent());
530   DrawableRef parent_draw(parent->drawable());
531   float x( (parent_draw) ? parent_draw->x() : 0);
532   float y( (parent_draw) ? parent_draw->y() : 0);
533   float z( (parent_draw) ? parent_draw->z() : 10 );
534   float height( (parent_draw) ? parent_draw->height() : default_height ); 
535   Color c( (s->drawable()) ? s->drawable()->color() : *GlSequence::default_track_color() );
536
537   glColor3fv( c.get() );
538   GlSequence::draw_box(left, right, x+s->start(), x+s->stop(), 
539                        height, y, z+10);
540 }