Class: Bio::Graphics::Panel::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/graphics/track.rb,
lib/bio/graphics/feature.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.

Defined Under Namespace

Classes: Feature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(panel, name, label = true, colour = [0,0,1], glyph = :generic) ⇒ 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

  • colour

    Colour to be used to draw the features within the track.

    Default = ‘blue’

  • glyph

    Glyph to use for drawing the features. Options are:

    :generic, :directed_generic, :spliced, :directed_spliced, :line and :triangle. Triangles can be used for features whose start and stop positions are the same (e.g. SNPs). If you try to draw a feature that is longer with triangles, an error will be shown.

Returns

Bio::Graphics::Track object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bio/graphics/track.rb', line 39

def initialize(panel, name, label = true, colour = [0,0,1], glyph = :generic)
  @panel = panel
  @name = name
  @show_label = label
  @colour = colour
  @glyph = glyph
  @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.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def colour
  @colour
end

#featuresObject

Returns the value of attribute features.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def features
  @features
end

#glyphObject

Returns the value of attribute glyph.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def glyph
  @glyph
end

#gridObject

Returns the value of attribute grid.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def grid
  @grid
end

#heightObject

Returns the value of attribute height.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def height
  @height
end

#nameObject

Returns the value of attribute name.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def name
  @name
end

#number_of_feature_rowsObject

Returns the value of attribute number_of_feature_rows.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def number_of_feature_rows
  @number_of_feature_rows
end

#panelObject

Returns the value of attribute panel.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def panel
  @panel
end

#show_labelObject

Returns the value of attribute show_label.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def show_label
  @show_label
end

#vertical_offsetObject

Returns the value of attribute vertical_offset.



50
51
52
# File 'lib/bio/graphics/track.rb', line 50

def vertical_offset
  @vertical_offset
end

Instance Method Details

#add_feature(name, location_string = '1..' + @panel.length.to_s, link = nil) ⇒ Object

Adds a Bio::Graphics::Panel::Track::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('EST1','50..60')
est_track.add_feature('EST2','52..73')
est_track.add_feature('EST3','41..69')
gene_track.add_feature('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:

  • name (required)

    Name of the feature

  • location

    String. Default: whole of panel, forward strand.

  • link

    URL to link to for this glyph

Returns

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bio/graphics/track.rb', line 80

def add_feature(name, location_string = '1..' + @panel.length.to_s, link = nil)
  if link == ''
    link = nil
  end
  
  # 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).
  location_object = Bio::Locations.new(location_string)
  start = location_object.collect{|l| l.from}.min.to_i
  stop = location_object.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::Panel::Track::Feature.new(self, name, location_object, link))
    return @features[-1]
  end
  
  return self
end

#draw(panel_drawing) ⇒ 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:

  • paneldrawing (required)

    the panel cairo object

Returns

FIXME: I don’t know



115
116
117
118
119
120
121
122
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
# File 'lib/bio/graphics/track.rb', line 115

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

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

  # Draw track title
  track_drawing.set_source_rgb(0,0,0)
#          track_drawing.select_font_face('Georgia',1,1)
  track_drawing.select_font_face(*(FONT))
  track_drawing.set_font_size(TRACK_HEADER_HEIGHT)
  track_drawing.move_to(0,TRACK_HEADER_HEIGHT + self.vertical_offset + 10)
  track_drawing.show_text(self.name)

  # Draw the features
  track_drawing.save do
    track_drawing.translate(0, self.vertical_offset + TRACK_HEADER_HEIGHT)
    track_drawing.set_source_rgb(@colour)

    @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(track_drawing)
      end
    end

  end

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

  return panel_drawing
end