dont draw sequence box when we can see sequence text
[mussa.git] / alg / glsequence.cpp
index 6c8b12b80410418e2e8bc2c0ec537a574724283a..9f861bbac7bf016b404f6e16865590f1e6af2c1b 100644 (file)
@@ -7,36 +7,33 @@
 using namespace std;
 
 static const float default_height = 12.0;
+static const float char_pix_per_world_unit = 2.5;
 
 GlSequence::GlSequence(const Sequence &s, 
                        boost::shared_ptr<AnnotationColors> cm) 
   : Sequence(s),
-    color_mapper(cm),
-    char_pix_per_world_unit(2.5)
+    color_mapper(cm)
 {
   seq->setDrawable(default_drawable());
 }
 
 GlSequence::GlSequence(const GlSequence &s)
   : Sequence(s),
-    color_mapper(s.color_mapper),
-    char_pix_per_world_unit(s.char_pix_per_world_unit)
+    color_mapper(s.color_mapper)
 {
   seq->setDrawable(copy_drawable(s.seq->drawable()));
 }
 
 GlSequence::GlSequence(const GlSequence *s)
   : Sequence(s),
-    color_mapper(s->color_mapper),
-    char_pix_per_world_unit(s->char_pix_per_world_unit)
+    color_mapper(s->color_mapper)
 {
   seq->setDrawable(copy_drawable(s->seq->drawable()));
 }
 
 GlSequence::GlSequence(const GlSequenceRef s)
   : Sequence( (SequenceRef)s ),
-    color_mapper(s->color_mapper),
-    char_pix_per_world_unit(s->char_pix_per_world_unit)
+    color_mapper(s->color_mapper)
 {
   seq->setDrawable(copy_drawable(s->seq->drawable()));
 }
@@ -47,7 +44,6 @@ GlSequence &GlSequence::operator=(const GlSequence & s)
     Sequence::operator=(s);
     seq->setDrawable(copy_drawable(s.seq->drawable()));
     color_mapper = s.color_mapper;
-    assert(char_pix_per_world_unit == s.char_pix_per_world_unit);
   }
   return *this;
 }
@@ -68,25 +64,29 @@ DrawableRef GlSequence::copy_drawable(DrawableRef old_d)
   return d;
 }
 
-SeqSpanRef GlSequence::make_undefined_sequence_annotation(
+SeqSpanRef GlSequence::make_drawable_annotation(
   Drawable::draw_func_ptr draw,
+  std::string name, 
   size_type start,
-  size_type count)
+  size_type count,
+  ColorRef color)
 {
   // create all the components of our annotation
   // (should seq_i-start_i 
   SeqSpanRef empty_seq(seq->subseq(start, count)); 
-  AnnotationsRef empty_seq_annot(new Annotations("null"));
+  AnnotationsRef empty_seq_annot(new Annotations(name));
   DrawableRef drawable(default_drawable());
   // glue everything to gether
   drawable->setDrawFunction(draw);
+  drawable->setColor(color);
   empty_seq->setAnnotations(empty_seq_annot);
   empty_seq->setDrawable(drawable);
   return empty_seq;
 }
 
-void GlSequence::add_annotations_for_undefined_sequence(Drawable::draw_func_ptr draw)
+void GlSequence::add_annotations_for_defined_sequence(Drawable::draw_func_ptr draw)
 {
+  ColorRef sequence_color(new Color(0.0, 0.0, 0.0));
   Sequence::const_iterator start_i = begin();
   Sequence::const_iterator seq_i = begin();
   Sequence::const_iterator end_i = end();
@@ -95,7 +95,7 @@ void GlSequence::add_annotations_for_undefined_sequence(Drawable::draw_func_ptr
   for(; seq_i != end_i; ++seq_i)
   {
     // need a better set of characters to serch for
-    if (*seq_i == 'N' or *seq_i == 'n') {
+    if (not (*seq_i == 'N' or *seq_i == 'n')) {
       if (start_block_i == end_i) {
         start_block_i = seq_i;
       }
@@ -105,7 +105,10 @@ void GlSequence::add_annotations_for_undefined_sequence(Drawable::draw_func_ptr
         size_type start = start_block_i - start_i;
         size_type count = seq_i - start_block_i;
         // add the annotation
-        add_annotation(make_undefined_sequence_annotation(draw, start, count));        
+        add_annotation(
+          make_drawable_annotation(draw, "sequence", start, count, 
+                                   sequence_color)
+        );        
         // reset our counter...
         start_block_i = end_i;
       }
@@ -115,10 +118,36 @@ void GlSequence::add_annotations_for_undefined_sequence(Drawable::draw_func_ptr
   if( start_block_i != end_i ) {
     size_type start = start_block_i - start_i;
     size_type count = seq_i - start_block_i;
-    add_annotation(make_undefined_sequence_annotation(draw, start, count));        
+    add_annotation(make_drawable_annotation(draw, "sequence", start, count, 
+                                            sequence_color)
+                  );        
   }
 }
 
+void GlSequence::update_annotation_draw_function(
+       std::string type, 
+       Drawable::draw_func_ptr draw_func,
+       ColorRef color
+)
+{
+  for(SeqSpanRefList::iterator annot_i = annotation_list->begin();
+      annot_i != annotation_list->end();
+      ++annot_i)
+  {
+    AnnotationsRef metadata( (*annot_i)->annotations() );
+     
+    if (metadata->has_key("type") and metadata->get("type") == type) {
+      // we should update
+      DrawableRef d((*annot_i)->drawable());
+      if (!d) {
+        d = default_drawable();
+        (*annot_i)->setDrawable(d);
+      }
+      d->setDrawFunction(draw_func);
+      d->setColor(color);
+    }
+  }
+}
 
 void GlSequence::setX(float value)
 {
@@ -231,6 +260,24 @@ const ColorRef GlSequence::color() const
   return seq->drawable()->color();
 }
 
+ColorRef GlSequence::default_gene_color()
+{
+  static ColorRef default_color;
+  if (not default_color) {
+    default_color.reset(new Color(0.0, 0.8, 0.0));
+  }
+  return default_color;
+}
+
+ColorRef GlSequence::default_track_color()
+{
+  static ColorRef default_color;
+  if (not default_color) {
+    default_color.reset(new Color(0.0, 0.0, 0.0));
+  }
+  return default_color;
+}
+
 int GlSequence::get_viewport_width_in_pixels()
 {
   GLint viewport[4];
@@ -249,14 +296,14 @@ GlSequence::pixelWidth(GLfloat left, GLfloat right, int vp_width)
   return round((right-left)/vp_width);
 }
 
-bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right) const
+bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right)
 {
   return is_sequence_renderable(left, right, get_viewport_width_in_pixels());
 }
 
 bool GlSequence::is_sequence_renderable(GLfloat left, 
                                         GLfloat right, 
-                                        int viewport_width) const
+                                        int viewport_width)
 {
   GLfloat world_width = right - left;
   GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
@@ -280,7 +327,8 @@ void GlSequence::draw(GLfloat left, GLfloat right) const
 
 void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
                           GLfloat left, GLfloat right, 
-                          GLfloat height, GLfloat y, GLfloat z)
+                          GLfloat height, GLfloat y, GLfloat z,
+                          GLint primitive)
 {
   GLfloat pixel_width = pixelWidth(world_left, world_right);
   GLfloat offset = height/2.0;
@@ -291,7 +339,7 @@ void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
   if ((right-left) < pixel_width) {
     right = left + pixel_width;
   }
-  glBegin(GL_QUADS);
+  glBegin(primitive);
     glVertex3f(left,  top,    z);
     glVertex3f(left,  bottom, z);
     glVertex3f(right, bottom, z);
@@ -301,8 +349,11 @@ void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
 
 void GlSequence::draw_track(GLfloat left, GLfloat right) const
 {
-  glColor3fv(color()->get());
   // draw main sequence track
+  glColor3fv(color()->get());
+  draw_box(left, right, x(), x()+Sequence::size(), height(), y(), 0.0, GL_LINE_LOOP);
+
+  glColor3f(0.8, 0.8, 0.8);
   draw_box(left, right, x(), x()+Sequence::size(), height(), y(), 0.0);
 }
 
@@ -321,7 +372,7 @@ void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
       assert((*annot_itor)->parent() == seq);
       drawable->drawFunction()((*annot_itor), left, right); 
     } else {
-      glColor3f(0.0, 0.8, 0.0);
+      glColor3fv(default_gene_color()->get());
       draw_box(left, right, x()+(*annot_itor)->start(), x()+(*annot_itor)->stop(), 
                height(), y(), annotation_z);
     }
@@ -464,9 +515,28 @@ void draw_narrow_track(SeqSpanRef s, float left, float right)
   float y( (parent_draw) ? parent_draw->y() : 0);
   float z( (parent_draw) ? parent_draw->z() : 10 );
   float height( (parent_draw) ? parent_draw->height() : default_height ); 
-
-  glColor3f(1.0, 1.0, 1.0);
-  float hsmall = height * 3.0/4.0;
+  Color c( (s->drawable()) ? s->drawable()->color() : *GlSequence::default_track_color() );
+  glColor3fv(c.get());
+  
+  float hsmall = height * 0.25;
   GlSequence::draw_box(left, right, x+s->start(), x+s->stop(), 
                        hsmall, y, z+10);
-}
\ No newline at end of file
+}
+
+void draw_summarized_track(SeqSpanRef s, float left, float right)
+{
+  // if we can see the sequence text (AGCTN), don't draw the black boxes
+  if (not GlSequence::is_sequence_renderable(left, right)) { 
+    SeqSpanRef parent(s->parent());
+    DrawableRef parent_draw(parent->drawable());
+    float x( (parent_draw) ? parent_draw->x() : 0);
+    float y( (parent_draw) ? parent_draw->y() : 0);
+    float z( (parent_draw) ? parent_draw->z() : 10 );
+    float height( (parent_draw) ? parent_draw->height() : default_height ); 
+    Color c( (s->drawable()) ? s->drawable()->color() : *GlSequence::default_track_color() );
+
+    glColor3fv( c.get() );
+    GlSequence::draw_box(left, right, x+s->start(), x+s->stop(), 
+                         height, y, z+10);
+  }
+}