Skip to content

Commit

Permalink
* added some tests for ruler
Browse files Browse the repository at this point in the history
 * fixed ruler to display tick marks correctly on all cases in test_draw


git-svn-id: svn+ssh://rubyforge.org/var/svn/bio-graphics@37 a2f46d20-7dc0-45cf-9d05-bfa4e4ff58eb
  • Loading branch information
dgtized committed Nov 27, 2007
1 parent aecde32 commit 4dabb2b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
43 changes: 24 additions & 19 deletions trunk/lib/bio/graphics/ruler.rb
Expand Up @@ -34,37 +34,42 @@ def initialize(panel, colour = [0,0,0])
# The base height of minor ticks in pixels
@tick_height = 5
# The height of the text in pixels
@tick_text_height = 10
@tick_text_height = 10

@minor_tick_distance = @min_pixels_per_tick ** self.scaling_factor
@major_tick_distance = @minor_tick_distance * 10
end
attr_accessor(:panel, :name, :colour, :height,
:minor_tick_distance, :major_tick_distance, :min_pixels_per_tick,
:tick_height, :tick_text_height)
:minor_tick_distance, :major_tick_distance,
:min_pixels_per_tick, :tick_height, :tick_text_height)

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

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

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

# calculate tick distance in basepairs
tick_scaling_factor = (Math.log(@min_pixels_per_tick * @panel.rescale_factor) /
Math.log(@min_pixels_per_tick)).ceil
@minor_tick_distance = @min_pixels_per_tick**tick_scaling_factor
@major_tick_distance = @minor_tick_distance * 10

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

# Draw ticks
# * 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 = (panel.display_start % minor_tick_distance)
first_tick_position = panel.display_start +
modulo_from_tick > 0 ? (minor_tick_distance - modulo_from_tick + 1) : 0

# * 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
Expand Down
27 changes: 27 additions & 0 deletions trunk/test/unit/test_creation.rb
Expand Up @@ -39,6 +39,33 @@ def test_track_creation
end
end

class TestRuler < Test::Unit::TestCase
def test_scaling_factor
panel = Bio::Graphics::Panel.new(1000, :width => 600, :display_start => 0, :display_stop => 1000)
ruler = Bio::Graphics::Ruler.new(panel)
assert_equal(1,ruler.scaling_factor(5,1000/600))
assert_equal(2,ruler.scaling_factor(5,1000/500))
assert_equal(1,ruler.scaling_factor(5,500/500))
end

def test_drawing
panel = Bio::Graphics::Panel.new(375, :display_start => 100, :display_stop => 370, :width => 600)
ruler = Bio::Graphics::Ruler.new(panel)

assert_equal(270.0/600.0,panel.rescale_factor)
assert_equal(1,ruler.scaling_factor)
assert_equal(5,ruler.minor_tick_distance)
assert_equal(50,ruler.major_tick_distance)
assert_equal(100,ruler.first_tick_position)

i = 0
ruler.first_tick_position.step(panel.display_stop, ruler.minor_tick_distance) do |tick|
assert(i*ruler.min_pixels_per_tick,(tick - panel.display_start) / panel.rescale_factor)
i += 1
end
end
end

class TestFeature < Test::Unit::TestCase
def setup
@panel = Bio::Graphics::Panel.new(1000, :width => 500)
Expand Down

0 comments on commit 4dabb2b

Please sign in to comment.