Class: Bio::Graphics::SubFeature

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

Overview

TODO: Documentation for SubFeature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feature, feature_object, opts = {}) ⇒ SubFeature

!!Not to be used directly.


Arguments:

  • feature (required)

    Bio::Graphics::Feature

    object that this subfeature belongs to

  • feature object (required)

    A Bio::Feature object (see bioruby)

  • :glyph

    Glyph to use. Default = glyph of the track

  • :colour

    Colour. Default = colour of the track

Returns

Bio::Graphics::SubFeature object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bio/graphics/subfeature.rb', line 21

def initialize(feature, feature_object, opts = {})
  @feature = feature
  @feature_object = feature_object
  opts = {
    :glyph => @feature.glyph,
    :colour => @feature.colour
  }.merge(opts)
  
  @glyph = opts[:glyph]
  @colour = opts[:colour]

  @locations = @feature_object.locations

  @start = @locations.collect{|l| l.from}.min.to_i
  @stop = @locations.collect{|l| l.to}.max.to_i
  @strand = @locations[0].strand.to_i
  @pixel_range_collection = Array.new
  @chopped_at_start = false
  @chopped_at_stop = false
  @hidden_subfeatures_at_start = false
  @hidden_subfeatures_at_stop = false

  # Get all pixel ranges for the subfeatures
  @locations.each do |l|
    #   xxxxxx  [          ]
    if l.to < @feature.track.panel.display_start
      @hidden_subfeatures_at_start = true
      next
    #           [          ]   xxxxx
    elsif l.from > @feature.track.panel.display_stop
      @hidden_subfeatures_at_stop = true
      next
    #      xxxx[xxx       ]
    elsif l.from < @feature.track.panel.display_start and l.to > @feature.track.panel.display_start
      start_pixel = 1
      stop_pixel = ( l.to - @feature.track.panel.display_start ).to_f / @feature.track.panel.rescale_factor
      @chopped_at_start = true
    #          [      xxxx]xxxx
    elsif l.from < @feature.track.panel.display_stop and l.to > @feature.track.panel.display_stop
      start_pixel = ( l.from - @feature.track.panel.display_start ).to_f / @feature.track.panel.rescale_factor
      stop_pixel = @feature.track.panel.width
      @chopped_at_stop = true
    #      xxxx[xxxxxxxxxx]xxxx
    elsif l.from < @feature.track.panel.display_start and l.to > @feature.track.panel.display_stop
      start_pixel = 1
      stop_pixel = @feature.track.panel.width
      @chopped_at_start = true
      @chopped_at_stop = true
    #          [   xxxxx  ]
    else
      start_pixel = ( l.from - @feature.track.panel.display_start ).to_f / @feature.track.panel.rescale_factor
      stop_pixel = ( l.to - @feature.track.panel.display_start ).to_f / @feature.track.panel.rescale_factor
    end

    @pixel_range_collection.push(Range.new(start_pixel, stop_pixel))

  end
end

Instance Attribute Details

#chopped_at_startObject

Is the first subfeature incomplete?


115
116
117
# File 'lib/bio/graphics/subfeature.rb', line 115

def chopped_at_start
  @chopped_at_start
end

#chopped_at_stopObject

Is the last subfeature incomplete?


118
119
120
# File 'lib/bio/graphics/subfeature.rb', line 118

def chopped_at_stop
  @chopped_at_stop
end

#colourObject

The colour to use to draw this (sub)feature


107
108
109
# File 'lib/bio/graphics/subfeature.rb', line 107

def colour
  @colour
end

#featureObject

The feature that this subfeature belongs to


84
85
86
# File 'lib/bio/graphics/subfeature.rb', line 84

def feature
  @feature
end

#feature_objectObject

The bioruby Bio::Feature object


81
82
83
# File 'lib/bio/graphics/subfeature.rb', line 81

def feature_object
  @feature_object
end

#glyphObject

The glyph to use to draw this (sub)feature


104
105
106
# File 'lib/bio/graphics/subfeature.rb', line 104

def glyph
  @glyph
end

#hidden_subfeatures_at_startObject

Are there subfeatures out of view at the left side of the picture?


121
122
123
# File 'lib/bio/graphics/subfeature.rb', line 121

def hidden_subfeatures_at_start
  @hidden_subfeatures_at_start
end

#hidden_subfeatures_at_stopObject

Are there subfeatures out of view at the right side of the picture?


124
125
126
# File 'lib/bio/graphics/subfeature.rb', line 124

def hidden_subfeatures_at_stop
  @hidden_subfeatures_at_stop
end

#labelObject Also known as: name

The label of the feature


87
88
89
# File 'lib/bio/graphics/subfeature.rb', line 87

def label
  @label
end

#locationsObject Also known as: location

The locations of the feature (which is a Bio::Locations object)


91
92
93
# File 'lib/bio/graphics/subfeature.rb', line 91

def locations
  @locations
end

#pixel_range_collectionObject

The array keeping the pixel ranges for the sub-features. Unspliced features will just have one element, while spliced features will have more than one.


112
113
114
# File 'lib/bio/graphics/subfeature.rb', line 112

def pixel_range_collection
  @pixel_range_collection
end

#startObject

The start position of the feature (in bp)


95
96
97
# File 'lib/bio/graphics/subfeature.rb', line 95

def start
  @start
end

#stopObject

The stop position of the feature (in bp)


98
99
100
# File 'lib/bio/graphics/subfeature.rb', line 98

def stop
  @stop
end

#strandObject

The strand of the feature


101
102
103
# File 'lib/bio/graphics/subfeature.rb', line 101

def strand
  @strand
end

Instance Method Details

#draw(feature_context) ⇒ Object

Adds the subfeature to the track cairo context. This method should not be used directly by the user, but is called by Bio::Graphics::Feature::SubFeature.draw


Arguments:

  • track_drawing (required)

    the track cairo object

Returns

FIXME: I don't know


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/bio/graphics/subfeature.rb', line 133

def draw(feature_context)
  # Set the glyph to be used. The glyph can be set as a symbol (e.g. :generic)
  # or as a hash (e.g. {'utr' => :line, 'cds' => :directed_spliced}).
  if @feature.glyph.class == Hash
    @glyph = @feature.glyph[@feature_object.feature]
  else
    @glyph = @feature.glyph
  end

  # We have to check if we want to change the glyph type from directed to
  #    undirected
  # There are 2 cases where we don't want to draw arrows on
  # features:
  # (a) when the picture is really zoomed out, features are
  #     so small that the arrow itself is too big
  # (b) if a directed feature on the fw strand extends beyond
  #     the end of the picture, the arrow is out of view. This
  #     is the same as considering the feature as undirected.
  #     The same obviously goes for features on the reverse
  #     strand that extend beyond the left side of the image.
  #
  # (a) Zoomed out
  replace_directed_with_undirected = false
  if (@stop - @start).to_f/@feature.track.panel.rescale_factor.to_f < 2
    replace_directed_with_undirected = true
  end
  # (b) Extending beyond borders picture
  if ( @chopped_at_stop and @strand = 1 ) or ( @chopped_at_start and @strand = -1 )
    replace_directed_with_undirected = true
  end

  local_feature_glyph = nil
  if @glyph == :directed_generic and replace_directed_with_undirected
    local_feature_glyph = :generic
  elsif @glyph == :directed_spliced and replace_directed_with_undirected
    local_feature_glyph = :spliced
  elsif @glyph == :directed_box and replace_directed_with_undirected
    local_feature_glyph = :box
  else
    local_feature_glyph = @glyph
  end

  # And draw the thing.

  feature_context.set_source_rgb(@colour)

  glyph = ("Bio::Graphics::Glyph::" + local_feature_glyph.to_s.camel_case).to_class.new(self, feature_context)
  glyph.draw

  @feature.left_pixel_of_subfeatures.push(glyph.left_pixel)
  @feature.right_pixel_of_subfeatures.push(glyph.right_pixel)

    
end