Class: Bio::Graphics::Track

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

Overview

The Bio::Graphics::Track class describes the container for features of the same type. See Bio::Graphics documentation for explanation of interplay between different classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(panel, name, opts = {}) ⇒ Track

!!Not to be used directly. Use Bio::Graphics::Panel.add_track instead!! A track can not exist except within the confines of a Bio::Graphics::Panel object.

– This is necessary because the track needs to know the rescale_factor and width of the picture, both of which are defined within the panel. ++


Arguments:

  • panel (required)

    Bio::Graphics::Panel object that this track

    belongs to

  • name (required)

    Name of the track to be displayed (e.g. 'genes')

  • :label

    Boolean: should the label for each feature be drawn or not

    Default = true

  • :glyph

    Glyph to use for drawing the features. Options are:

    :generic, :directed_generic, :box, directed_box, :spliced, :directed_spliced, :dot, :triangle, :line and :line_with_handles. Default = :generic

  • colour

    Colour to be used to draw the features within the track (in

    RGB) Default = [0,0,1] (i.e. blue)

Returns

Bio::Graphics::Track object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bio/graphics/track.rb', line 37

def initialize(panel, name, opts = {})
  @panel = panel
  @name = name
  opts = {
    :label => true,
    :glyph => :generic,
    :colour => [0,0,1]
  }.merge(opts)
  
  @show_label = opts[:label]
  @glyph = opts[:glyph]
  @colour = opts[:colour]

  # As far as I know, I can't do this in the glyph file for transcript, so we
  # have to do it here instead.
  if @glyph == :transcript
    @glyph = { 'utr5' => :box, 'utr3' => :directed_box, 'cds' => :spliced }
  end
  
  @features = Array.new
  @number_of_feature_rows = 0
  @vertical_offset = 0
  @grid = Hash.new
end

Instance Attribute Details

#colourObject

Returns the value of attribute colour


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def colour
  @colour
end

#featuresObject

Returns the value of attribute features


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def features
  @features
end

#glyphObject

Returns the value of attribute glyph


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def glyph
  @glyph
end

#gridObject

Returns the value of attribute grid


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def grid
  @grid
end

#heightObject

Returns the value of attribute height


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def height
  @height
end

#nameObject

Returns the value of attribute name


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def name
  @name
end

#number_of_feature_rowsObject

Returns the value of attribute number_of_feature_rows


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def number_of_feature_rows
  @number_of_feature_rows
end

#panelObject

Returns the value of attribute panel


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def panel
  @panel
end

#show_labelObject

Returns the value of attribute show_label


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def show_label
  @show_label
end

#vertical_offsetObject

Returns the value of attribute vertical_offset


61
62
63
# File 'lib/bio/graphics/track.rb', line 61

def vertical_offset
  @vertical_offset
end

Instance Method Details

#add_feature(feature_object, opts = {}) ⇒ Object

Takes a Bio::Feature and adds it as a Bio::Graphics::Feature to this track. A track contains features of the same type, e.g. (for sequence annotation:) genes, polymorphisms, ESTs, etc.

est_track.add_feature(Bio::Feature.new('EST1','50..60'))
est_track.add_feature(Bio::Feature.new('EST2','52..73'))
est_track.add_feature(Bio::Feature.new('EST3','41..69'))
gene_track.add_feature(Bio::Feature.new('gene2','39..73'))

For spliced features:

est_track.add_feature('EST4','join(34..53,153..191)')

Or on the complement strand:

est_track.add_feature('EST5','complement(join(34..53,153..191))')

See the documentation in Bio::Locations for a full description of how locations can be defined.

Features are only added if they are at least partly in the displayed region. If a feature is completely outside of the region, it's not added. If it should be only partly visible, it is added completely.


Arguments:

  • feature object (required)

    A Bio::Feature object

  • label

    Label for the feature. Default = 'anonymous'

  • link

    URL to link to for this glyph. Default = nil

  • glyph

    Glyph for the feature. Default = glyph of the track

  • colour

    Colour for the feature. Default = colour of the track

Returns

Bio::Graphics::Feature object that was created or nil


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bio/graphics/track.rb', line 93

def add_feature(feature_object, opts = {})
  # Calculate the ultimate start and stop of the feature: the start
  # of the first subfeature (e.g. exon) and the stop of the last one.
  # The only reason we want to know these positions, is because we want
  # to determine if the feature falls within the view of the image or
  # not (see below).
  start = feature_object.locations.collect{|l| l.from}.min.to_i
  stop = feature_object.locations.collect{|l| l.to}.max.to_i

  # If the feature wouldn't show because it's not in the region we're
  # looking at, don't bother storing the stuff. I think this makes huge
  # speed and memory differences if you've got a chromosome with
  # thousands of features.
  if stop <= self.panel.display_start or start >= self.panel.display_stop
    return nil
  else #elsif start >= panel.display_start and stop <= panel.display_stop
    @features.push(Bio::Graphics::Feature.new(self, feature_object, opts))
    return @features[-1]
  end

  return self
end

#draw(panel_destination) ⇒ Object

Adds the track to a cairo drawing. This method should not be used directly by the user, but is called by Bio::Graphics::Panel.draw


Arguments:

  • panel__drawing (required)

    the panel cairo object

Returns

FIXME: I don't know


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/bio/graphics/track.rb', line 123

def draw(panel_destination)
  track_context = Cairo::Context.new(panel_destination)

  # Draw thin line above title
  track_context.set_source_rgb(0.75,0.75,0.75)
  track_context.move_to(0, self.vertical_offset)
  track_context.line_to(self.panel.width, self.vertical_offset)
  track_context.stroke

  # Draw track title
  track_context.set_source_rgb(0,0,0)
  track_context.select_font_face(*(Bio::Graphics::FONT))
  track_context.set_font_size(Bio::Graphics::TRACK_HEADER_HEIGHT)
  track_context.move_to(0,Bio::Graphics::TRACK_HEADER_HEIGHT + self.vertical_offset + 10)
  track_context.show_text(self.name)

  # Draw the features
  @features.sort_by{|f| f.start}.each do |feature|
    # Don't even bother if the feature is not in the view
    if feature.stop <= self.panel.display_start or feature.start >= self.panel.display_stop
      next
    else
      feature.draw(panel_destination)
    end
  end

  @number_of_feature_rows = ( @grid.keys.length == 0 ) ? 1 : @grid.keys.max + 1

  return panel_destination
end