Class: Bio::Graphics::Ruler

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/graphics/ruler.rb

Overview

The Bio::Graphics::Ruler class describes the ruler to be drawn on the graph. This is created automatically when creating the picture by using Bio::Graphics::Panel.to_svg. See BioExt::Graphics documentation for explanation of interplay between different classes. – TODO: the ruler might be implemented as a special case of a track, so it would inherit from it (class Ruler < Bio::Graphics::Track). But I haven’t really thought this through yet. ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(panel, colour = [0,0,0]) ⇒ Ruler

Creates a new Bio::Graphics::Ruler object.


Arguments:

  • panel (required)

    Bio::Graphics::Panel object that this ruler

    belongs to

  • colour

    colour of the ruler. Default = ‘black’

Returns

Bio::Graphics::Ruler object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bio/graphics/ruler.rb', line 27

def initialize(panel, colour = [0,0,0])
  @panel = panel
  @name = 'ruler'
  @colour = colour
  
  # Number of pixels between each tick, used to calculate tick spacing
  @min_pixels_per_tick = 5
  # The base height of minor ticks in pixels
  @tick_height = 5
  # The height of the text in pixels
  @tick_text_height = 10

  @minor_tick_distance = @min_pixels_per_tick ** self.scaling_factor
  @major_tick_distance = @minor_tick_distance * 10       
end

Instance Attribute Details

#colourObject

Returns the value of attribute colour.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def colour
  @colour
end

#heightObject

Returns the value of attribute height.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def height
  @height
end

#major_tick_distanceObject

Returns the value of attribute major_tick_distance.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def major_tick_distance
  @major_tick_distance
end

#min_pixels_per_tickObject

Returns the value of attribute min_pixels_per_tick.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def min_pixels_per_tick
  @min_pixels_per_tick
end

#minor_tick_distanceObject

Returns the value of attribute minor_tick_distance.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def minor_tick_distance
  @minor_tick_distance
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def name
  @name
end

#panelObject

Returns the value of attribute panel.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def panel
  @panel
end

#tick_heightObject

Returns the value of attribute tick_height.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def tick_height
  @tick_height
end

#tick_text_heightObject

Returns the value of attribute tick_text_height.



42
43
44
# File 'lib/bio/graphics/ruler.rb', line 42

def tick_text_height
  @tick_text_height
end

Instance Method Details

#draw(panel_drawing) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bio/graphics/ruler.rb', line 64

def draw(panel_drawing)
  ruler_drawing = Cairo::Context.new(panel_drawing)

  # Draw line
  ruler_drawing.move_to(0,10)
  ruler_drawing.line_to(panel.width, 10)
  ruler_drawing.stroke

  # Draw ticks
  #  * And start drawing the rest.
  first_tick_position.step(@panel.display_stop, @minor_tick_distance) do |tick|
    tick_pixel_position = (tick - panel.display_start) / @panel.rescale_factor
    ruler_drawing.move_to(tick_pixel_position.floor, @min_pixels_per_tick)
    if tick.modulo(@major_tick_distance) == 0
      ruler_drawing.rel_line_to(0, 3*@tick_height)
      
      # Draw tick number
      ruler_drawing.select_font_face(*Bio::Graphics::FONT)
      ruler_drawing.set_font_size(@tick_text_height)
      ruler_drawing.move_to(tick_pixel_position.floor, 4*@tick_height + @tick_text_height)
      ruler_drawing.show_text(tick.to_i.to_s)
    else
      ruler_drawing.rel_line_to(0, @tick_height)
      
    end
    ruler_drawing.stroke
  end

  @height = 5*@tick_height + @tick_text_height          
end

#first_tick_position(start = @panel.display_start, minor_tick = @minor_tick_distance) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bio/graphics/ruler.rb', line 52

def first_tick_position(start = @panel.display_start,
                        minor_tick = @minor_tick_distance)
  #  * Find position of first tick.
  #    Most of the time, we don't want the first tick on the very first
  #    basepair of the view. Suppose that would be position 333 in the
  #    sequence. Then the numbers under the major tickmarks would be:
  #    343, 353, 363, 373 and so on. Instead, we want 350, 360, 370, 380.
  #    So we want to find the position of the first tick.
  modulo_from_tick = (start % minor_tick)
  start + (modulo_from_tick > 0 ? (minor_tick - modulo_from_tick + 1) : 0)
end

#scaling_factor(min_pixels_per_tick = @min_pixels_per_tick, rescale_factor = @panel.rescale_factor) ⇒ Object



46
47
48
49
50
# File 'lib/bio/graphics/ruler.rb', line 46

def scaling_factor(min_pixels_per_tick = @min_pixels_per_tick,
                        rescale_factor = @panel.rescale_factor)
  (Math.log(min_pixels_per_tick * rescale_factor) /
   Math.log(min_pixels_per_tick)).ceil 
end