Class: Bioinform::Collection

Inherits:
Object
  • Object
show all
Includes:
Parameters, Enumerable
Defined in:
lib/bioinform/data_models/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#collect_hash, #same_by?

Methods included from Parameters

#get_parameters, included, #parameter_defined?, #parameters, #set_parameters

Constructor Details

#initialize(parameters = {}) {|@parameters| ... } ⇒ Collection

collection name is a tag name for each motif in a collection. But motif can be included in several collections so have several tags

Yields:



12
13
14
15
16
# File 'lib/bioinform/data_models/collection.rb', line 12

def initialize(parameters = {})
  @container = []
  @parameters = OpenStruct.new(parameters)
  yield @parameters  if block_given?
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



6
7
8
# File 'lib/bioinform/data_models/collection.rb', line 6

def container
  @container
end

Instance Method Details

#+(other) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/bioinform/data_models/collection.rb', line 30

def +(other)
  result = self.class.new
  container.each do |motif|
    result.container << motif
  end
  other.container.each do |motif|
    result.container << motif
  end
  result
end

#<<(pm) ⇒ Object



48
49
50
# File 'lib/bioinform/data_models/collection.rb', line 48

def <<(pm)
  add_pm(pm, OpenStruct.new)
end

#==(other) ⇒ Object



68
69
70
71
72
# File 'lib/bioinform/data_models/collection.rb', line 68

def ==(other)
  (parameters == other.parameters) && (container == other.container)
rescue
  false
end

#add_pm(pm, info) ⇒ Object



41
42
43
44
45
46
# File 'lib/bioinform/data_models/collection.rb', line 41

def add_pm(pm, info)
#      pm.mark(self)
  container << Motif.new(info.marshal_dump.merge(pm: pm))
  #### What if pm already is a Motif
  self
end

#each(*args) ⇒ Object

collection.each{|motif| … } collection.each(:pwm, :threshold){|pwm,threshold| }



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bioinform/data_models/collection.rb', line 54

def each(*args)
  if block_given?
    if args.empty?
      container.each{|motif| yield motif}
    else
      container.each{|motif| yield( *args.map{|arg| motif.parameters.send(arg)} ) }
    end
  else
    self.to_enum(:each, *args)
  end
end

#sizeObject



18
19
20
# File 'lib/bioinform/data_models/collection.rb', line 18

def size
  container.size
end

#to_s(with_name = true) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/bioinform/data_models/collection.rb', line 22

def to_s(with_name = true)
  result = (with_name) ? "Collection: #{name.to_s}\n" : ''
  each do |pm, infos|
    result << pm.to_s << "\n\n"
  end
  result
end