Class: BEL::Model::Term

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bel/evidence_model/bel_term.rb,
lib/bel/extensions/rdf/rdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fx, *arguments) ⇒ Term

Returns a new instance of Term.



7
8
9
10
# File 'lib/bel/evidence_model/bel_term.rb', line 7

def initialize(fx, *arguments)
  @fx = fx
  @arguments = (arguments ||= []).flatten
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



5
6
7
# File 'lib/bel/evidence_model/bel_term.rb', line 5

def arguments
  @arguments
end

#fxObject

Returns the value of attribute fx.



5
6
7
# File 'lib/bel/evidence_model/bel_term.rb', line 5

def fx
  @fx
end

#signatureObject

Returns the value of attribute signature.



5
6
7
# File 'lib/bel/evidence_model/bel_term.rb', line 5

def signature
  @signature
end

Instance Method Details

#<<(item) ⇒ Object



38
39
40
# File 'lib/bel/evidence_model/bel_term.rb', line 38

def <<(item)
  @arguments << item
end

#==(other) ⇒ Object Also known as: eql?



66
67
68
69
# File 'lib/bel/evidence_model/bel_term.rb', line 66

def ==(other)
  return false if other == nil
  @fx == other.fx && @arguments == other.arguments
end

#hashObject



62
63
64
# File 'lib/bel/evidence_model/bel_term.rb', line 62

def hash
  [@fx, @arguments].hash
end

#invalid_signaturesObject



58
59
60
# File 'lib/bel/evidence_model/bel_term.rb', line 58

def invalid_signatures
  @fx.signatures.find_all { |sig| (@signature <=> sig) < 0 }
end

#rdf_typeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bel/extensions/rdf/rdf.rb', line 74

def rdf_type
  if respond_to? 'fx'
    fx = @fx.respond_to?(:short_form) ? @fx.short_form : @fx.to_s.to_sym
    if [:p, :proteinAbundance].include?(fx) &&
       @arguments.find{ |x|
         if x.is_a? BEL::Model::Term
           arg_fx = x.fx
           arg_fx = arg_fx.respond_to?(:short_form) ? arg_fx.short_form : arg_fx.to_s.to_sym
           [:pmod, :proteinModification].include?(arg_fx)
         else
           false
         end
       }

      return BEL::RDF::BELV.ModifiedProteinAbundance
    end

    if [:p, :proteinAbundance].include?(fx) &&
       @arguments.find{ |x|
         if x.is_a? BEL::Model::Term
           arg_fx = x.fx
           arg_fx = arg_fx.respond_to?(:short_form) ? arg_fx.short_form : arg_fx.to_s.to_sym
           BEL::RDF::PROTEIN_VARIANT.include?(arg_fx)
         else
           false
         end
       }

      return BEL::RDF::BELV.ProteinVariantAbundance
    end

    BEL::RDF::FUNCTION_TYPE[fx] || BEL::RDF::BELV.Abundance
  end
end

#to_belObject Also known as: to_s



72
73
74
75
# File 'lib/bel/evidence_model/bel_term.rb', line 72

def to_bel
  arguments = [@arguments].flatten.map(&:to_bel).join(',') 
  "#{@fx.short_form}(#{arguments})"
end

#to_rdfObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/bel/extensions/rdf/rdf.rb', line 109

def to_rdf
  uri = to_uri
  statements = []

  # rdf:type
  type = rdf_type
  statements << [uri, BEL::RDF::RDF.type, BEL::RDF::BELV.Term]
  statements << [uri, BEL::RDF::RDF.type, type]
  fx = @fx.respond_to?(:short_form) ? @fx.short_form : @fx.to_s.to_sym
  if BEL::RDF::ACTIVITY_TYPE.include? fx
    statements << [uri, BEL::RDF::BELV.hasActivityType, BEL::RDF::ACTIVITY_TYPE[fx]]
  end

  # rdfs:label
  statements << [uri, BEL::RDF::RDFS.label, to_s.force_encoding('UTF-8')]

  # special proteins (does not recurse into pmod)
  if [:p, :proteinAbundance].include?(fx)
    pmod =
      @arguments.find{ |x|
        if x.is_a? BEL::Model::Term
          arg_fx = x.fx
          arg_fx = arg_fx.respond_to?(:short_form) ? arg_fx.short_form : arg_fx.to_s.to_sym
          [:pmod, :proteinModification].include?(arg_fx)
        else
          false
        end
      }
    if pmod
      mod_string = pmod.arguments.map(&:to_s).join(',')
      mod_type = BEL::RDF::MODIFICATION_TYPE.find {|k,v| mod_string.start_with? k}
      mod_type = (mod_type ? mod_type[1] : BEL::RDF::BELV.Modification)
      statements << [uri, BEL::RDF::BELV.hasModificationType, mod_type]
      last = pmod.arguments.last.to_s
      if last.match(/^\d+$/)
        statements << [uri, BEL::RDF::BELV.hasModificationPosition, last.to_i]
      end
      # link root protein abundance as hasChild
      root_param = @arguments.find{|x| x.is_a? BEL::Model::Parameter}
      (root_id, root_statements) = BEL::Model::Term.new(:p, [root_param]).to_rdf
      statements << [uri, BEL::RDF::BELV.hasChild, root_id]
      statements += root_statements
      return [uri, statements]
    elsif @arguments.find{|x| x.is_a? BEL::Model::Term and BEL::RDF::PROTEIN_VARIANT.include? x.fx}
      # link root protein abundance as hasChild
      root_param = @arguments.find{|x| x.is_a? BEL::Model::Parameter}
      (root_id, root_statements) = BEL::Model::Term.new(:p, [root_param]).to_rdf
      statements << [uri, BEL::RDF::BELV.hasChild, root_id]
      statements += root_statements
      return [uri, statements]
    end
  end

  # BEL::RDF::BELV.hasConcept]
  @arguments.find_all{ |x|
    x.is_a? BEL::Model::Parameter and x.ns != nil
  }.each do |param|
    concept_uri = param.ns.to_rdf_vocabulary[param.value.to_s]
    statements << [uri, BEL::RDF::BELV.hasConcept, BEL::RDF::RDF::URI(Addressable::URI.encode(concept_uri))]
  end

  # BEL::RDF::BELV.hasChild]
  @arguments.find_all{|x| x.is_a? BEL::Model::Term}.each do |child|
    (child_id, child_statements) = child.to_rdf
    statements << [uri, BEL::RDF::BELV.hasChild, child_id]
    statements += child_statements
  end

  return [uri, statements]
end

#to_uriObject



69
70
71
72
# File 'lib/bel/extensions/rdf/rdf.rb', line 69

def to_uri
  tid = to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
  BEL::RDF::BELR[URI::encode(tid)]
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bel/evidence_model/bel_term.rb', line 42

def valid?
  invalid_signatures = @arguments.find_all { |arg|
    arg.is_a? Term
  }.find_all { |term|
    not term.valid?
  }
  return false if not invalid_signatures.empty?

  sigs = @fx.signatures
  sigs.any? do |sig| (@signature <=> sig) >= 0 end
end

#valid_signaturesObject



54
55
56
# File 'lib/bel/evidence_model/bel_term.rb', line 54

def valid_signatures
  @fx.signatures.find_all { |sig| (@signature <=> sig) >= 0 }
end