egor 0.0.5 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ module Egor
2
+ class Environment
3
+
4
+ attr_accessor :amino_acids,
5
+ :number,
6
+ :label,
7
+ :freq_array,
8
+ :prob_array,
9
+ :logo_array,
10
+ :smooth_prob_array
11
+
12
+ def initialize(number, label, amino_acids = "ACDEFGHIKLMNPQRSTVWYJ".split(''))
13
+ @amino_acids = amino_acids
14
+ @number = number
15
+ @label = label
16
+ @freq_array = $noweight ? NArray.int(@amino_acids.size) : NArray.float(@amino_acids.size)
17
+ @prob_array = NArray.float(@amino_acids.size)
18
+ @logo_array = NArray.float(@amino_acids.size)
19
+ @smooth_prob_array = NArray.float(@amino_acids.size)
20
+ end
21
+
22
+ def increase_residue_count(a, inc = 1.0)
23
+ @freq_array[@amino_acids.index(a.upcase)] += inc
24
+ end
25
+
26
+ def label_set
27
+ label.split("").map_with_index { |l, i| "#{i}#{l}" }.to_set
28
+ end
29
+
30
+ def to_s
31
+ "#{number}-#{label}"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module Egor
2
+ class EnvironmentClassHash < Hash
3
+
4
+ def group_by_non_residue_labels
5
+ self.values.group_by { |env| env.label[1..-1] }
6
+ end
7
+
8
+ def groups_sorted_by_residue_labels
9
+ group_by_non_residue_labels.to_a.sort_by { |env_group|
10
+ env_group[0].split('').map_with_index { |l, i|
11
+ $env_features[i + 1].labels.index(l)
12
+ }
13
+ }
14
+ end
15
+
16
+ def group_size
17
+ group_by_non_residue_labels.size
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ module Egor
2
+ class EnvironmentFeature
3
+
4
+ attr_accessor :name, :symbols, :labels, :constrained, :silent
5
+
6
+ def initialize(name, symbols, labels, constrained, silent)
7
+ @name = name
8
+ @symbols = symbols
9
+ @labels = labels
10
+ @constrained = constrained
11
+ @silent = silent
12
+ end
13
+
14
+ def to_s
15
+ [name, symbols.join, labels.join, constrained, silent].join(";")
16
+ end
17
+
18
+ def constrained?
19
+ constrained == "T"
20
+ end
21
+
22
+ def silent?
23
+ silent == "T"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ module Egor
2
+ class EnvironmentFeatureArray < Array
3
+
4
+ def label_combinations
5
+ self.inject([]) { |sum, ec|
6
+ sum << ec.labels
7
+ }.inject { |pro, lb|
8
+ pro.product(lb)
9
+ }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,111 @@
1
+ require 'rubygems'
2
+ require 'facets'
3
+
4
+ begin
5
+ require 'RMagick'
6
+ include Magick
7
+ rescue
8
+ $logger.warn "A RubyGems package, 'rmagick' is not found, so heat maps cannot be generated."
9
+ $no_rmagick = true
10
+ end
11
+
12
+
13
+
14
+ module Egor
15
+ class HeatmapArray < Array
16
+
17
+ def heatmap(options = {})
18
+ if $no_rmagick
19
+ return nil
20
+ end
21
+
22
+ opts = {:columns => 4,
23
+ :rvg_width => nil,
24
+ :dpi => 100,
25
+ :title => '',
26
+ :max_val => nil,
27
+ :mid_val => nil,
28
+ :min_val => nil,
29
+ :print_gradient => true,
30
+ :gradient_beg_color => '#FFFFFF',
31
+ :gradient_mid_color => nil,
32
+ :gradient_end_color => '#FF0000',
33
+ :gradient_border_width => 1,
34
+ :gradient_border_color => '#000000'}.merge(options)
35
+
36
+ row_images = ImageList.new
37
+
38
+ self.each_by(opts[:columns]) { |maps|
39
+ images = ImageList.new
40
+ maps.each { |m| images << m }
41
+ row_images << images.append(false)
42
+ }
43
+
44
+ tbl_img = row_images.append(true)
45
+
46
+ unless opts[:print_gradient]
47
+ return tbl_img
48
+ else
49
+ RVG::dpi = opts[:dpi]
50
+ rvg_width = opts[:rvg_width] * opts[:columns]
51
+ rvg_height = rvg_width / 10.0
52
+
53
+ rvg = RVG.new(rvg_width, rvg_height) do |canvas|
54
+ canvas.viewbox(0, 0, rvg_width, rvg_height)
55
+ canvas.background_fill = 'white'
56
+ canvas.desc = 'gradient key'
57
+
58
+ gradient_width = rvg_width / 2.0
59
+ gradient_height = gradient_width / 15.0
60
+
61
+ if opts[:gradient_mid_color]
62
+ img1 = Image.new(gradient_width / 2,
63
+ gradient_height,
64
+ GradientFill.new(0, 0, 0, gradient_width / 2,
65
+ opts[:gradient_beg_color], opts[:gradient_mid_color]))
66
+
67
+ img2 = Image.new(gradient_width / 2,
68
+ gradient_height,
69
+ GradientFill.new(0, 0, 0, gradient_width / 2,
70
+ opts[:gradient_mid_color], opts[:gradient_end_color]))
71
+
72
+ img3 = ImageList.new
73
+ img3 << img1 << img2
74
+ img = img3.append(false)
75
+ else
76
+ img = Image.new(gradient_width,
77
+ gradient_height,
78
+ GradientFill.new(0, 0, 0, gradient_width,
79
+ opts[:gradient_beg_color], opts[:gradient_end_color]))
80
+ end
81
+
82
+ img.border!(opts[:gradient_border_width],
83
+ opts[:gradient_border_width],
84
+ opts[:gradient_border_color])
85
+
86
+ gradient_x = (rvg_width - gradient_width) / 2.0
87
+ gradient_y = (rvg_height - gradient_height) / 2.0
88
+ gradient_font_size = rvg_width / 45.0
89
+
90
+ canvas.image(img,
91
+ gradient_width,
92
+ gradient_height,
93
+ gradient_x,
94
+ gradient_y)
95
+
96
+ canvas.text(gradient_x,
97
+ gradient_y + gradient_height + gradient_font_size * 1.1,
98
+ "#{'%.1f' % opts[:min_val]}").styles(:font_size => gradient_font_size)
99
+
100
+ canvas.text(gradient_x + gradient_width,
101
+ gradient_y + gradient_height + gradient_font_size * 1.1,
102
+ "#{'%.1f' % opts[:max_val]}").styles(:font_size => gradient_font_size)
103
+ end
104
+
105
+ fin_img = ImageList.new
106
+ fin_img << tbl_img << rvg.draw
107
+ fin_img.append(true)
108
+ end
109
+ end
110
+ end
111
+ end
@@ -1,5 +1,6 @@
1
- require "rubygems"
2
- require "facets"
1
+ #require 'rubygems'
2
+ require 'narray'
3
+ require 'facets'
3
4
 
4
5
  module NArrayExtensions
5
6
 
@@ -1,11 +1,20 @@
1
- require "rubygems"
2
- require "facets"
3
- require "narray"
1
+ require 'rubygems'
2
+ require 'narray'
3
+ require 'facets'
4
+
5
+ begin
6
+ require 'rvg/rvg'
7
+ include Magick
8
+ rescue
9
+ $logger.warn "A RubyGems package, 'rmagick' is not found, so heat maps cannot be generated."
10
+ $no_rmagick = true
11
+ end
4
12
 
5
13
  module NMatrixExtensions
6
- def pretty_string(opts={})
7
- { :col_header => nil,
8
- :row_header => nil }.merge!(opts)
14
+
15
+ def pretty_string(options={})
16
+ opts = {:col_header => nil,
17
+ :row_header => nil }.merge(options)
9
18
 
10
19
  ("%-3s" % "#") + opts[:col_header].inject("") { |s, a|
11
20
  s + ("%7s" % a)
@@ -19,6 +28,218 @@ module NMatrixExtensions
19
28
  }
20
29
  }.join("\n")
21
30
  end
31
+
32
+ def heatmap(options={})
33
+ if $no_rmagick
34
+ return nil
35
+ end
36
+
37
+ opts = {:col_header => 'ACDEFGHIKLMNPQRSTVWYJ'.split(''),
38
+ :row_header => 'ACDEFGHIKLMNPQRSTVWYJ'.split(''),
39
+ :max_val => self.max,
40
+ :mid_val => (self.max - self.min) / 2.0,
41
+ :min_val => self.min,
42
+ :dpi => 100,
43
+ :margin_width => 30,
44
+ :rvg_width => nil,
45
+ :rvg_height => nil,
46
+ :canvas_width => nil,
47
+ :canvas_height => nil,
48
+ :cell_width => 20,
49
+ :cell_height => 20,
50
+ :cell_border_color => '#888888',
51
+ :cell_border_width => 1,
52
+ :table_border_color => '#000000',
53
+ :table_border_width => 2,
54
+ :header_height => 100,
55
+ :footer_height => 50,
56
+ :print_gradient => true,
57
+ :gradient_width => 300,
58
+ :gradient_height => 30,
59
+ :gradient_beg_color => '#FFFFFF',
60
+ :gradient_mid_color => nil,
61
+ :gradient_end_color => '#FF0000',
62
+ :gradient_border_width => 1,
63
+ :gradient_border_color => '#000000',
64
+ :font_scale => 0.9,
65
+ :font_family => 'san serif',
66
+ :small_gap_width => 2,
67
+ :title? => true,
68
+ :title => '',
69
+ :title_font_size => 35,
70
+ :print_value => false,
71
+ :key_font_size => 15,
72
+ :value_font_size => 8,
73
+ :background => '#FFFFFF'}.merge(options)
74
+
75
+ RVG::dpi = opts[:dpi]
76
+
77
+ rvg = RVG.new(opts[:rvg_width], opts[:rvg_height]) do |canvas|
78
+ title_x = (opts[:canvas_width] - opts[:title].length * opts[:title_font_size] * 0.6) / 2.0
79
+ title_y = opts[:header_height] - opts[:title_font_size]
80
+
81
+ canvas.viewbox(0, 0, opts[:canvas_width], opts[:canvas_height])
82
+ canvas.background_fill = opts[:background]
83
+ canvas.desc = opts[:title]
84
+
85
+ if opts[:title?]
86
+ canvas.text(title_x, title_y, opts[:title]).styles(:font_size => opts[:title_font_size])
87
+ end
88
+
89
+ # border for whole matrix
90
+ table_x = (opts[:canvas_width] - opts[:cell_width] * self.shape[0]) / 2.0
91
+ table_y = opts[:header_height] + opts[:cell_height]
92
+
93
+ canvas.rect(self.shape[0] * opts[:cell_width],
94
+ self.shape[1] * opts[:cell_height],
95
+ table_x,
96
+ table_y).styles(:stroke => opts[:table_border_color],
97
+ :stroke_width => opts[:table_border_width])
98
+
99
+ # drawing column and row labels
100
+ 0.upto(self.shape[0] - 1) do |col|
101
+ canvas.text(table_x + col * opts[:cell_width] + opts[:small_gap_width],
102
+ opts[:cell_height] + opts[:header_height] - opts[:small_gap_width],
103
+ opts[:col_header][col]).styles( :font_family => opts[:font_family],
104
+ :font_size => opts[:cell_width] * opts[:font_scale])
105
+ end
106
+
107
+ 0.upto(self.shape[1] - 1) do |row|
108
+ canvas.text(table_x - opts[:cell_width],
109
+ table_y + (row + 1) * opts[:cell_height],
110
+ opts[:row_header][row]).styles( :font_family => opts[:font_family],
111
+ :font_size => opts[:cell_height] * opts[:font_scale])
112
+ end
113
+
114
+ # drawing cells
115
+
116
+ # calculating a unit of RGB color in a decimal number
117
+ r_beg = (opts[:gradient_beg_color].rgb_to_integer & 0xFF0000) >> 16
118
+ g_beg = (opts[:gradient_beg_color].rgb_to_integer & 0x00FF00) >> 8
119
+ b_beg = (opts[:gradient_beg_color].rgb_to_integer & 0x0000FF) >> 0
120
+ r_end = (opts[:gradient_end_color].rgb_to_integer & 0xFF0000) >> 16
121
+ g_end = (opts[:gradient_end_color].rgb_to_integer & 0x00FF00) >> 8
122
+ b_end = (opts[:gradient_end_color].rgb_to_integer & 0x0000FF) >> 0
123
+ gap = opts[:max_val] - opts[:min_val]
124
+
125
+ if opts[:gradient_mid_color]
126
+ r_mid = (opts[:gradient_mid_color].rgb_to_integer & 0xFF0000) >> 16
127
+ g_mid = (opts[:gradient_mid_color].rgb_to_integer & 0x00FF00) >> 8
128
+ b_mid = (opts[:gradient_mid_color].rgb_to_integer & 0x0000FF) >> 0
129
+ gap1 = opts[:mid_val] - opts[:min_val]
130
+ gap2 = opts[:max_val] - opts[:mid_val]
131
+ end
132
+
133
+ 0.upto(self.shape[0] - 1) do |col|
134
+ 0.upto(self.shape[1] - 1) do |row|
135
+ if opts[:gradient_mid_color]
136
+ if self[col, row] <= opts[:mid_val]
137
+ r = interpolate(r_beg, r_mid, self[col, row] - opts[:min_val], gap1)
138
+ g = interpolate(g_beg, g_mid, self[col, row] - opts[:min_val], gap1)
139
+ b = interpolate(b_beg, b_mid, self[col, row] - opts[:min_val], gap1)
140
+ else
141
+ r = interpolate(r_mid, r_end, self[col, row] - opts[:mid_val], gap2)
142
+ g = interpolate(g_mid, g_end, self[col, row] - opts[:mid_val], gap2)
143
+ b = interpolate(b_mid, b_end, self[col, row] - opts[:mid_val], gap2)
144
+ end
145
+ else
146
+ r = interpolate(r_beg, r_end, self[col, row] - opts[:min_val], gap)
147
+ g = interpolate(g_beg, g_end, self[col, row] - opts[:min_val], gap)
148
+ b = interpolate(b_beg, b_end, self[col, row] - opts[:min_val], gap)
149
+ end
150
+
151
+ color = ("#%6X" % ((((r << 8) | g) << 8) | b)).gsub(" ", "0")
152
+
153
+ canvas.rect(opts[:cell_width],
154
+ opts[:cell_height],
155
+ table_x + col * opts[:cell_width],
156
+ table_y + row * opts[:cell_height]).styles( :fill => color,
157
+ :stroke => opts[:cell_border_color],
158
+ :stroke_width => opts[:cell_border_width])
159
+
160
+ if opts[:print_value]
161
+ canvas.text(table_x + col * opts[:cell_width] + opts[:cell_border_width],
162
+ table_y + (row + 1) * opts[:cell_height],
163
+ "#{'%.1f' % self[col, row]}").styles(:font_size => opts[:value_font_size])
164
+ end
165
+ end
166
+ end
167
+
168
+ # gradient key
169
+ if opts[:print_gradient]
170
+ if opts[:gradient_mid_color]
171
+ img1 = Image.new(opts[:gradient_height],
172
+ opts[:gradient_width] / 2,
173
+ GradientFill.new(0,
174
+ opts[:gradient_width] / 2,
175
+ opts[:gradient_height],
176
+ opts[:gradient_width] / 2,
177
+ opts[:gradient_beg_color],
178
+ opts[:gradient_mid_color])).rotate(90)
179
+
180
+ img2 = Image.new(opts[:gradient_height],
181
+ opts[:gradient_width] / 2,
182
+ GradientFill.new(0,
183
+ opts[:gradient_width] / 2,
184
+ opts[:gradient_height],
185
+ opts[:gradient_width] / 2,
186
+ opts[:gradient_mid_color],
187
+ opts[:gradient_end_color])).rotate(90)
188
+ img3 = ImageList.new
189
+ img3 << img1 << img2
190
+ img = img3.append(false)
191
+ else
192
+ img = Image.new(opts[:gradient_height],
193
+ opts[:gradient_width],
194
+ GradientFill.new(0,
195
+ opts[:gradient_width],
196
+ opts[:gradient_height],
197
+ opts[:gradient_width],
198
+ opts[:gradient_beg_color],
199
+ opts[:gradient_end_color])).rotate(90)
200
+ end
201
+
202
+ img.border!(opts[:gradient_border_width],
203
+ opts[:gradient_border_width],
204
+ opts[:gradient_border_color])
205
+
206
+ gradient_x = (opts[:canvas_width] - opts[:gradient_width]) / 2
207
+ gradient_y = opts[:header_height] + opts[:cell_height] * opts[:row_header].count + opts[:margin_width]
208
+
209
+ canvas.image(img,
210
+ opts[:gradient_width],
211
+ opts[:gradient_height] + opts[:margin_width],
212
+ gradient_x,
213
+ gradient_y)
214
+
215
+ canvas.text(gradient_x,
216
+ gradient_y + opts[:gradient_height] + opts[:key_font_size] * 2,
217
+ "#{'%.1f' % opts[:min_val]}").styles(:font_size => opts[:key_font_size])
218
+
219
+ canvas.text(gradient_x + opts[:gradient_width],
220
+ gradient_y + opts[:gradient_height] + opts[:key_font_size] * 2,
221
+ "#{'%.1f' % opts[:max_val]}").styles(:font_size => opts[:key_font_size])
222
+ end
223
+ end
224
+
225
+ rvg.draw
226
+ end
227
+
228
+
229
+ private
230
+
231
+ def interpolate(start_val, end_val, step, no_steps)
232
+ begin
233
+ if (start_val < end_val)
234
+ ((end_val - start_val) / no_steps.to_f) * step + start_val
235
+ else
236
+ start_val - ((start_val - end_val) / no_steps.to_f) * step
237
+ end.round
238
+ rescue FloatDomainError
239
+ start_val
240
+ end
241
+ end
242
+
22
243
  end
23
244
 
24
245
  NMatrix.send(:include, NMatrixExtensions)