Class: BaseFunction
- Inherits:
-
Object
- Object
- BaseFunction
- Defined in:
- lib/scbi_cominer/classes/base_function.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fft ⇒ Object
Returns the value of attribute fft.
-
#freq_table ⇒ Object
Returns the value of attribute freq_table.
-
#regions ⇒ Object
Returns the value of attribute regions.
-
#single_points ⇒ Object
Returns the value of attribute single_points.
-
#snps ⇒ Object
Returns the value of attribute snps.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #add_region(regions, r) ⇒ Object
- #calculate ⇒ Object
- #evaluate_pos(i) ⇒ Object
- #filter_regions(data, comp, only_single_points = false, mandatory_data = nil) ⇒ Object
- #graph(file_name = nil) ⇒ Object
- #graph2(file_name = nil) ⇒ Object
- #group_regions(data) ⇒ Object
-
#initialize(freq_table) ⇒ BaseFunction
constructor
A new instance of BaseFunction.
- #purge_regions(regions1, regions2) ⇒ Object
- #purge_snps(regions) ⇒ Object
- #regions_to_graph_data(regions, total_length) ⇒ Object
- #valid_region(region, comp, only_single_points, mandatory_data) ⇒ Object
Constructor Details
#initialize(freq_table) ⇒ BaseFunction
Returns a new instance of BaseFunction.
15 16 17 18 19 20 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 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 15 def initialize(freq_table) @freq_table = freq_table @values = [] @fft = nil @lim1 = 0 @lim2 = 0 calculate #puts @freq_table.inspect_pos(86,100) #puts @fft.original_data[86].to_json over_lim2 = lambda {|v| v > @lim2} below_lim1 = lambda {|v| v < @lim1} #puts @lim1,@lim2 single_points = filter_regions(@fft.original_data, over_lim2, true) #puts single_points.to_json # @regions=filter_regions(@fft.filtered_data, over_lim2, false , @values) # @regions=filter_regions(@values, over_lim2, false , @values) @regions=group_regions(single_points) #@regions_below=filter_regions(@fft.filtered_data, below_lim1, false, @values) @single_points = purge_regions(single_points,@regions) # repeat snps that are already in a region @snps = purge_snps(single_points) # do not repeat snps that are in a region # @snps = purge_snps(@single_points) #puts @snps.to_json #puts @regions.to_json #puts @single_points.to_json # puts @single_points.join(',') #graph # puts @values.to_json end |
Instance Attribute Details
#fft ⇒ Object
Returns the value of attribute fft.
13 14 15 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 13 def fft @fft end |
#freq_table ⇒ Object
Returns the value of attribute freq_table.
13 14 15 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 13 def freq_table @freq_table end |
#regions ⇒ Object
Returns the value of attribute regions.
13 14 15 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 13 def regions @regions end |
#single_points ⇒ Object
Returns the value of attribute single_points.
13 14 15 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 13 def single_points @single_points end |
#snps ⇒ Object
Returns the value of attribute snps.
13 14 15 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 13 def snps @snps end |
#values ⇒ Object
Returns the value of attribute values.
13 14 15 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 13 def values @values end |
Instance Method Details
#add_region(regions, r) ⇒ Object
296 297 298 299 300 301 302 303 304 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 296 def add_region(regions,r) w=(r['end']-r['start'])+1 if w>0 then r['score'] = r['score'].to_f/w.to_f regions.push r end end |
#calculate ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 56 def calculate values = [] length = @freq_table.max_length # evaluate freq table length.times do |i| val = evaluate_pos(i) values.push val end @values = values @fft = LowPassFilter.new(@values) @lim1,@lim2 = @fft.limits end |
#evaluate_pos(i) ⇒ Object
77 78 79 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 77 def evaluate_pos(i) raise "You must create a child class to override this method" end |
#filter_regions(data, comp, only_single_points = false, mandatory_data = nil) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 224 def filter_regions(data, comp, only_single_points = false, mandatory_data = nil) # =========== pos = 0 regions = [] region = {} region['start'] = 0 region['end'] = 0 region['score'] = 0 anotate = false # filter regions data.each do |v| if not anotate if comp.call(v) # is out anotate = true region['start'] = pos region['end'] = 0 region['score'] = v #else # is inside limits end else # we are anotating a region, outside limits if comp.call(v) # is ok region['score'] += v else # finish region anotate = false # actually it finished at previos pos region['end'] = pos - 1 if (valid_region(region, comp, only_single_points, mandatory_data)) add_region(regions,region) end region = {} end end pos = pos + 1 end # anotate last region if any if anotate # finish region anotate = false # actually it finished at previos pos region['end'] = pos if valid_region(region,comp,only_single_points,mandatory_data) add_region(regions,region) end region = {} end return regions end |
#graph(file_name = nil) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 306 def graph(file_name=nil) Gnuplot.open do |gp| Gnuplot::Plot.new( gp ) do |plot| if !file_name.nil? plot.terminal "png size #{@fft.filtered_data.length},600" plot.output "#{file_name}" end plot.set "multiplot layout 2,1 upwards" plot.xrange("[0:#{@fft.original_data.length-1}]") #plot.yrange("[#{@fft.original_data.min}:#{@fft.original_data.max}]") # plot.ytics("#{@fft.original_data.min},10,#{@fft.original_data.max}]") #plot.ylabel "f" #plot.xlabel "x" #plot.set "bmargin #{dx+1}" plot.set "tmargin 0.0" #plot.set "lmargin #{dy}" # graph fft data plot.title "" plot.ylabel "Region" plot.xlabel "Nucleotide" # ===================== if !@regions.empty? x, y = regions_to_graph_data(@regions, @fft.original_data.length-1) plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"red\" ti \"Regions #{x.length}\"" #ds.notitle end end # ===================== if !@single_points.empty? x, y = regions_to_graph_data(@single_points, @fft.original_data.length-1) plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"blue\" ti \"Points #{x.length}\"" #ds.notitle end end # ===================== end Gnuplot::Plot.new( gp ) do |plot| plot.title "Filter Base: #{fft.filter_base} , skip: #{fft.skip}" plot.set "bmargin 0.0" plot.set "tmargin 2" #plot.set "xtics" plot.xrange("[0:#{@fft.original_data.length-1}]") #plot.set "origin #{DX},#{DY+SY};" plot.ylabel "f" plot.xlabel '' plot.noxtics x = (0..@fft.original_data.length-1).collect.to_a y = @fft.original_data.to_a plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"green\" ti \"Original data\"" #ds.notitle end x = (0..@fft.filtered_data.length-1).collect.to_a y = @fft.filtered_data.to_a plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"blue\" ti \"Filtered data\"" #ds.notitle end x=[0] y=[@lim1] x.push(@fft.filtered_data.length-1) y.push(@lim1) plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"red\" ti \"Lim1 [#{@lim1}]\"" #ds.notitle end x=[0] y=[@lim2] x.push(@fft.filtered_data.length-1) y.push(@lim2) #puts @lim1, @lim2 plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"red\" ti \"Lim2 [ #{@lim2}]\"" #ds.notitle end end end end |
#graph2(file_name = nil) ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 467 def graph2(file_name = nil) Gnuplot.open do |gp| Gnuplot::Plot.new( gp ) do |plot| if !file_name.nil? plot.terminal "png size #{@fft.filtered_data.length},600" plot.output "#{file_name}" end plot.title "Filter Base: #{@fft.filter_base} , skip: #{@fft.skip}" plot.ylabel "f" plot.xlabel "x" x = (0..@fft.original_data.length-1).collect.to_a y = @fft.original_data.to_a plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"green\"" ds.notitle end x = (0..@fft.filtered_data.length-1).collect.to_a y = @fft.filtered_data.to_a plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"blue\"" ds.notitle end x = (0..@fft.filtered_data.length-1).collect.to_a y = [@lim1] @fft.filtered_data.length.times { y.push(@lim1) } plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"red\"" ds.notitle end x = (0..@fft.filtered_data.length-1).collect.to_a y = [@lim2] @fft.filtered_data.length.times { y.push(@lim2) } plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| ds.with = "lines lt rgb \"red\"" ds.notitle end end end end |
#group_regions(data) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 168 def group_regions(data) max_separation = 15 last_end = 0 group_start = 0 group_end = 0 group_score = 0 group_size = 0 regions = [] if !data.empty? region = {} region['start'] = data[0]['start'] region['end'] = data[0]['end'] region['score'] = data[0]['score'] # filter regions data.each do |r| if r['start'] < last_end+max_separation # group group_score += r['score'] group_end = r['end'] group_size += 1 else #close previous group, start new one region = {} region['start'] = group_start region['end'] = group_end region['score'] = group_score.to_f/group_size.to_f #save region if region['start']<region['end'] regions.push region end # init new one group_start = r['start'] group_end = r['end'] group_score = r['score'] group_size = 1 end last_end = r['end'] end end return regions end |
#purge_regions(regions1, regions2) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 81 def purge_regions(regions1, regions2) res = [] #puts "to purge: #{regions1.length}" regions1.each do |r1| if !((regions2.find{ |r2| ((r1['start']<=r2['end']) and (r2['start']<=r1['end'])) })) res.push(r1) end end #puts "purged: #{res.length}" return res end |
#purge_snps(regions) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 102 def purge_snps(regions) res = [] #puts "to purge: #{regions1.length}" regions.each do |r1| # is a one point region if r1['start']==r1['end'] pos =r1['start'] if @freq_table.valid_snp(pos) res.push(r1) end end end #puts "purged SNPS: #{res.length} from #{regions.length}\n #{res.to_yaml}" return res end |
#regions_to_graph_data(regions, total_length) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 425 def regions_to_graph_data(regions,total_length) x = [] y = [] # x = [0] # y = [0] regions.each do |r| x.push r['start']-1 y.push 0 x.push r['start'] y.push r['score'] x.push r['end'] y.push r['score'] x.push r['end']+1 y.push 0 end # x.push total_length # y.push 0 if x.empty? x.push 0 end if y.empty? y.push 0 end return [x,y] end |
#valid_region(region, comp, only_single_points, mandatory_data) ⇒ Object
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/scbi_cominer/classes/base_function.rb', line 126 def valid_region(region, comp, only_single_points, mandatory_data) region_start = region['start'] region_end = region['end'] res = false if only_single_points # only get SNPs #print "check: #{region_start} - #{region_end}" res = ((region_end - region_start) >= 0) else if mandatory_data.nil? # if no mandatory data, add all regions res = ((region_end - region_start) >=0) else # there is mandatory data # region must have al least one base res = ((region_end - region_start) >0) # negar la siguiente linea para no tener en cuenta regiones anchas sin snps dentro if res # check for inner regions in this range of the mandatory_data data = mandatory_data[region_start,region_end-region_start+1] regions = filter_regions(data,comp,nil) # if there is more than one region, then is valid if regions.empty? or regions.count<=1 res = false end end end end return res end |