Module: Mspire::Paramable

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cv_paramsObject

Returns the value of attribute cv_params.



10
11
12
# File 'lib/mspire/paramable.rb', line 10

def cv_params
  @cv_params
end

#ref_param_groupsObject

Returns the value of attribute ref_param_groups.



12
13
14
# File 'lib/mspire/paramable.rb', line 12

def ref_param_groups
  @ref_param_groups
end

#user_paramsObject

Returns the value of attribute user_params.



11
12
13
# File 'lib/mspire/paramable.rb', line 11

def user_params
  @user_params
end

Instance Method Details

#accessionable_paramsObject



39
40
41
# File 'lib/mspire/paramable.rb', line 39

def accessionable_params
  cv_params + ref_param_groups.flat_map(&:params)
end

#describe!(*args) ⇒ Object

Expects arguments describing a single CV::Param, Mspire::UserParam, or Mspire::Mzml::ReferenceableParamGroup

obj.describe! 'MS:1000130'  # a positive scan
obj.describe! CV::Param['MS:1000130']  # same behavior

# base peak intensity, units=number of counts
obj.describe! "MS:1000505", 1524.5865478515625, 'MS:1000131'

returns self



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mspire/paramable.rb', line 173

def describe!(*args)
  return self if args.first.nil?
  case (arg=args.first)
  when String
    @cv_params << Mspire::CV::Param[ *args ]
  when Mspire::Mzml::ReferenceableParamGroup
    @ref_param_groups << arg
  else
    if arg.is_a?(Mspire::UserParam)
      @user_params << arg
    else
      @cv_params << arg
    end
  end
  self
end

#describe_from_xml!(xml_node, ref_hash = nil) ⇒ Object

takes a node with children that are cvParam, userParam or referenceableParamGroupRef and a hash containing referenceableParamGroup objects indexed by id. The only time ref_hash should be left nil is for the referenceableParamGroup itself.

All param elements are required to appear before other elements, so the code walks through each child node to see if it is a paramable element. The first child node that is not paramable is returned (or nil if none)

returns the next child node after the paramable elements or nil if none



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mspire/paramable.rb', line 141

def describe_from_xml!(xml_node, ref_hash=nil)
  return nil unless (child_n = xml_node.child) 
  loop do
    array = 
      case child_n.name
      when 'referenceableParamGroupRef'
        @ref_param_groups << ref_hash[child_n[:ref]]
      when 'cvParam'
        @cv_params << Mspire::CV::Param[ child_n[:accession], child_n[:value] ]
      when 'userParam'
        @user_params << Mspire::UserParam.new(child_n[:name], child_n[:value], child_n[:type])
      else # assumes that the above precede any following children as per the spec
        break 
      end
    if (unit_acc = child_n[:unitAccession])
      array.last.unit = ::CV::Param.new(child_n[:unitCvRef], unit_acc, child_n[:unitName])
    end
    break unless child_n = child_n.next
  end
  child_n
end

#describe_many!(array) ⇒ Object

takes an array of values, each of which is fed into describe! returns self.



112
113
114
115
116
117
118
119
120
121
# File 'lib/mspire/paramable.rb', line 112

def describe_many!(array)
  array.each do |arg|
    if arg.is_a?(Array)
      describe!(*arg)
    else
      describe!(arg)
    end
  end
  self
end

#describe_self_from_xml!(xml_node, ref_hash = nil) ⇒ Object

reads the paramable nodes and returns self. Use this if your element does not have anything besides paramable elements.



125
126
127
128
# File 'lib/mspire/paramable.rb', line 125

def describe_self_from_xml!(xml_node, ref_hash=nil)
  describe_from_xml!(xml_node, ref_hash)
  self
end

#each_accessionable_param(&block) ⇒ Object



32
33
34
35
36
37
# File 'lib/mspire/paramable.rb', line 32

def each_accessionable_param(&block)
  return enum_for __method__ unless block
  cv_params.each(&block)
  ref_param_groups.flat_map(&:params).each(&block)
  nil
end

#each_param(&block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/mspire/paramable.rb', line 18

def each_param(&block)
  return enum_for __method__ unless block
  cv_params.each(&block)
  ref_param_groups.flat_map(&:params).each(&block)
  user_params.each(&block)
  nil
end

#fetch(name) ⇒ Object

returns the value if the param exists by that name. Returns true if the param exists but has no value. returns false if no param



72
73
74
75
76
77
78
79
# File 'lib/mspire/paramable.rb', line 72

def fetch(name)
  param = each_param.find {|param| param.name == name}
  if param
    param.value || true
  else
    false
  end
end

#fetch_by_accession(acc) ⇒ Object Also known as: fetch_by_acc

returns the value if the param exists with that accession. Returns true if the param exists but has no value. returns false if no param with that accession.



84
85
86
87
88
89
90
91
# File 'lib/mspire/paramable.rb', line 84

def fetch_by_accession(acc)
  param = accessionable_params.find {|v| v.accession == acc }
  if param
    param.value || true
  else
    false
  end
end

#initializeObject Also known as: params_init



98
99
100
101
102
# File 'lib/mspire/paramable.rb', line 98

def initialize
  @cv_params = []
  @user_params = []
  @ref_param_groups = []
end

#param?(name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/mspire/paramable.rb', line 94

def param?(name)
  params.any? {|param| param.name == name }
end

#param_by_accession(acc) ⇒ Object Also known as: param_by_acc



105
106
107
# File 'lib/mspire/paramable.rb', line 105

def param_by_accession(acc)
  each_accessionable_param.find {|v| v.accession == acc }
end

#paramsObject



14
15
16
# File 'lib/mspire/paramable.rb', line 14

def params
  cv_params + ref_param_groups.flat_map(&:params) + user_params 
end

#params?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/mspire/paramable.rb', line 26

def params?
  cv_params.size > 0 || 
    ref_param_groups.any? {|group| group.params.size > 0 } || 
    user_params.size > 0
end

#reject!(&block) ⇒ Object

returns self



51
52
53
54
55
56
# File 'lib/mspire/paramable.rb', line 51

def reject!(&block)
  cv_params.reject!(&block)
  ref_param_groups.each {|group| group.reject!(&block) }
  user_params.reject!(&block)
  self
end

#replace!(*describe_args, &block) ⇒ Object

yields each current param. If the return value is not false or nil, it is deleted (i.e., any true value and it is deleted). Then adds the given parameter or makes a new one by accession number.



46
47
48
# File 'lib/mspire/paramable.rb', line 46

def replace!(*describe_args, &block)
  reject!(&block).describe!(*describe_args)
end

#replace_many!(describe_many_arg, &block) ⇒ Object



58
59
60
# File 'lib/mspire/paramable.rb', line 58

def replace_many!(describe_many_arg, &block)
  reject!(&block).describe_many!(describe_many_arg)
end

#to_xml(xml) ⇒ Object

iterates over @params and calls .to_xml on each object.



191
192
193
194
195
196
197
198
# File 'lib/mspire/paramable.rb', line 191

def to_xml(xml)
  [:ref_param_groups, :cv_params, :user_params].each do |kind|
    self.send(kind).each do |obj|
      obj.to_xml(xml)
    end
  end
  xml
end