Class: BEL::Extension::Format::FormatXBEL::XBELYielder

Inherits:
Object
  • Object
show all
Defined in:
lib/bel/extensions/xbel.rb

Constant Summary collapse

FUNCTIONS =
::BEL::Language::FUNCTIONS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ XBELYielder

Returns a new instance of XBELYielder.



60
61
62
# File 'lib/bel/extensions/xbel.rb', line 60

def initialize(data, options = {})
  @data         = data
end

Class Method Details

.annotation_definitions(hash) ⇒ Object



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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/bel/extensions/xbel.rb', line 381

def self.annotation_definitions(hash)
  el_ad = REXML::Element.new('bel:annotationDefinitionGroup')
  internal = []
  external = []
  hash.each do |k, v|
    type, domain = v.values_at(:type, :domain)
    case type.to_sym
    when :url
      el = REXML::Element.new('bel:externalAnnotationDefinition')
      el.add_attribute 'bel:url', domain.to_s
      el.add_attribute 'bel:id',  k.to_s
      external << el
    when :pattern
      el = REXML::Element.new('bel:internalAnnotationDefinition')
      el.add_attribute 'bel:id', k.to_s
      el.add_element(REXML::Element.new('bel:description'))
      el.add_element(REXML::Element.new('bel:usage'))

      el_pattern      = REXML::Element.new('bel:patternAnnotation')
      el_pattern.text =
        domain.respond_to?(:source) ? domain.source : domain.to_s

      el.add_element(el_pattern)
      internal << el
    when :list
      el = REXML::Element.new('bel:internalAnnotationDefinition')
      el.add_attribute 'bel:id', k.to_s
      el.add_element(REXML::Element.new('bel:description'))
      el.add_element(REXML::Element.new('bel:usage'))

      el_list = REXML::Element.new('bel:listAnnotation')
      if domain.respond_to?(:each)
        domain.each do |value|
          el_list_value      = REXML::Element.new('bel:listValue')
          el_list_value.text = value.to_s
          el_list.add_element(el_list_value)
        end
      else
        el_list_value      = REXML::Element.new('bel:listValue')
        el_list_value.text = domain.to_s
        el_list.add_element(el_list_value)
      end

      el.add_element(el_list)
      internal << el
    else
      el = nil
    end
  end

  # first add internal annotation definitions
  internal.each do |internal_element|
    el_ad.add_element(internal_element)
  end

  # second add external annotation definitions
  external.each do |external_element|
    el_ad.add_element(external_element)
  end

  el_ad
end

.annotation_group(evidence) ⇒ Object



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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/bel/extensions/xbel.rb', line 191

def self.annotation_group(evidence)
  el_ag = REXML::Element.new('bel:annotationGroup')

  # XBEL citation
  if evidence.citation && evidence.citation.valid?
    el_ag.add_element(self.citation(evidence.citation))
  end

  # XBEL evidence (::BEL::Model::SummaryText)
  if evidence.summary_text && evidence.summary_text.value
    xbel_evidence      = REXML::Element.new('bel:evidence')
    xbel_evidence.text = evidence.summary_text.value
    el_ag.add_element(xbel_evidence)
  end

  evidence.experiment_context.each do |annotation|
    name, value = annotation.values_at(:name, :value)

    if value.respond_to?(:each)
      value.each do |v|
        el_anno      = REXML::Element.new('bel:annotation')
        el_anno.text = v
        el_anno.add_attribute 'bel:refID', name.to_s

        el_ag.add_element(el_anno)
      end
    else
        el_anno      = REXML::Element.new('bel:annotation')
        el_anno.text = value
        el_anno.add_attribute 'bel:refID', name.to_s

        el_ag.add_element(el_anno)
    end
  end

   = evidence..keys - [:document_header]
  .each do |k|
    v = evidence.[k]
    if v.respond_to?(:each)
      v.each do |value|
        el_anno      = REXML::Element.new('bel:annotation')
        el_anno.text = value
        el_anno.add_attribute 'bel:refID', k.to_s

        el_ag.add_element(el_anno)
      end
    else
      el_anno      = REXML::Element.new('bel:annotation')
      el_anno.text = v
      el_anno.add_attribute 'bel:refID', k.to_s

      el_ag.add_element(el_anno)
    end
  end

  el_ag
end

.citation(citation) ⇒ Object



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
# File 'lib/bel/extensions/xbel.rb', line 249

def self.citation(citation)
  el_citation  = REXML::Element.new('bel:citation')

  if citation.type && !citation.type.to_s.empty?
    el_citation.add_attribute 'bel:type', citation.type
  end

  if citation.id && !citation.id.to_s.empty?
    el_reference      = REXML::Element.new('bel:reference')
    el_reference.text = citation.id
    el_citation.add_element(el_reference)
  end

  if citation.name && !citation.name.to_s.empty?
    el_name         = REXML::Element.new('bel:name')
    el_name.text    = citation.name
    el_citation.add_element(el_name)
  end

  if citation.date && !citation.date.to_s.empty?
    el_date         = REXML::Element.new('bel:date')
    el_date.text    = citation.date
    el_citation.add_element(el_date)
  end

  if citation.comment && !citation.comment.to_s.empty?
    el_comment      = REXML::Element.new('bel:comment')
    el_comment.text = citation.comment
    el_citation.add_element(el_comment)
  end

  if citation.authors && !citation.authors.to_s.empty?
    el_author_group = REXML::Element.new('bel:authorGroup')
    citation.authors.split('|').each do |author|
      el_author      = REXML::Element.new('bel:author')
      el_author.text = author
      el_author_group.add_element(el_author)
    end
  end

  el_citation
end

.documentObject



292
293
294
295
296
297
298
299
# File 'lib/bel/extensions/xbel.rb', line 292

def self.document
  el = REXML::Element.new('bel:document')
  el.add_namespace('bel', 'http://belframework.org/schema/1.0/xbel')
  el.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
  el.add_attribute('xsi:schemaLocation', 'http://belframework.org/schema/1.0/xbel http://resource.belframework.org/belframework/1.0/schema/xbel.xsd')

  el
end

.evidence(evidence) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/bel/extensions/xbel.rb', line 116

def self.evidence(evidence)
  statement    = evidence.bel_statement
  el_statement = REXML::Element.new('bel:statement')

  el_statement.add_element(self.annotation_group(evidence))
  self.statement(statement, el_statement)

  el_statement
end

.header(hash) ⇒ Object



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
# File 'lib/bel/extensions/xbel.rb', line 313

def self.header(hash)
  el_header = REXML::Element.new('bel:header')

  # normalize hash keys
  hash = Hash[
    hash.map { |k, v|
      k = k.to_s.dup
      k.downcase!
      k.gsub!(/[-_ .]/, '')
      [k, v]
    }
  ]

  (%w(name description version copyright) & hash.keys).each do |field|
    el      = REXML::Element.new("bel:#{field}")
    el.text = hash[field]
    el_header.add_element(el)
  end

  # add contactinfo with different element name (contactInfo)
  if hash.has_key?('contactinfo')
    el      = REXML::Element.new('bel:contactInfo')
    el.text = hash['contactinfo']
    el_header.add_element(el)
  end

  # handle grouping for authors and licenses
  if hash.has_key?('authors')
    authors  = hash['authors']
    el_group = REXML::Element.new('bel:authorGroup')

    [authors].flatten.each do |author|
      el      = REXML::Element.new('bel:author')
      el.text = author.to_s
      el_group.add_element(el)
    end
    el_header.add_element(el_group)
  end
  if hash.has_key?('licenses')
    licenses = hash['licenses']
    el_group = REXML::Element.new('bel:licenseGroup')

    [licenses].flatten.each do |license|
      el      = REXML::Element.new('bel:license')
      el.text = license.to_s
      el_group.add_element(el)
    end
    el_header.add_element(el_group)
  end

  el_header
end

.namespace_definitions(hash) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/bel/extensions/xbel.rb', line 366

def self.namespace_definitions(hash)
  el_nd = REXML::Element.new('bel:namespaceGroup')
  hash.each do |k, v|
    el               = REXML::Element.new('bel:namespace')
    el.add_attribute 'bel:prefix', k.to_s

    resourceLocation = v.respond_to?(:url) ? v.url : v
    el.add_attribute 'bel:resourceLocation', "#{resourceLocation}"

    el_nd.add_element(el)
  end

  el_nd
end

.object(object) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bel/extensions/xbel.rb', line 149

def self.object(object)
  el_object = REXML::Element.new('bel:object')

  case object
  when BEL::Model::Term
    el_object.add_element(self.term(object))
  when BEL::Model::Statement
    el_statement = REXML::Element.new('bel:statement')
    el_object.add_element(el_statement)
    self.statement(object, el_statement)
  end
  el_object
end

.parameter(parameter) ⇒ Object



184
185
186
187
188
189
# File 'lib/bel/extensions/xbel.rb', line 184

def self.parameter(parameter)
  el_parameter      = REXML::Element.new('bel:parameter')
  el_parameter.text = parameter.value
  el_parameter.add_attribute 'bel:ns', parameter.ns
  el_parameter
end

.statement(statement, el_statement) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bel/extensions/xbel.rb', line 126

def self.statement(statement, el_statement)
  if statement.relationship
    el_statement.add_attribute 'bel:relationship', statement.relationship
  end

  el_subject = self.subject(statement.subject)
  el_statement.add_element(el_subject)

  if statement.object
    el_object = self.object(statement.object)
    el_statement.add_element(el_object)
  end

  el_statement
end

.statement_group(declare_namespaces = false) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
# File 'lib/bel/extensions/xbel.rb', line 301

def self.statement_group(declare_namespaces = false)
  el = REXML::Element.new('bel:statementGroup')

  if declare_namespaces
    el.add_namespace('bel', 'http://belframework.org/schema/1.0/xbel')
    el.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
    el.add_attribute('xsi:schemaLocation', 'http://belframework.org/schema/1.0/xbel http://resource.belframework.org/belframework/1.0/schema/xbel.xsd')
  end

  el
end

.subject(subject) ⇒ Object



142
143
144
145
146
147
# File 'lib/bel/extensions/xbel.rb', line 142

def self.subject(subject)
  el_subject = REXML::Element.new('bel:subject')
  el_subject.add_element(self.term(subject))

  el_subject
end

.term(term) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/bel/extensions/xbel.rb', line 163

def self.term(term)
  el_term    = REXML::Element.new('bel:term')
  term_fx    = term.fx.to_s
  term_fx    = (
                 FUNCTIONS.fetch(term_fx.to_sym, {}).
                           fetch(:long_form, nil) || term_fx
               ).to_s

  el_term.add_attribute 'bel:function', term_fx

  term.arguments.each do |arg|
    case arg
    when BEL::Model::Term
      el_term.add_element(self.term(arg))
    when BEL::Model::Parameter
      el_term.add_element(self.parameter(arg))
    end
  end
  el_term
end

Instance Method Details

#eachObject



64
65
66
67
68
69
70
71
72
73
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
108
109
110
111
112
113
114
# File 'lib/bel/extensions/xbel.rb', line 64

def each
  if block_given?
    header_flag        = true

    # yield <document>
    el_document = XBELYielder.document
    yield start_element_string(el_document)

    el_statement_group = nil
    evidence_count = 0
    @data.each { |evidence|
      if header_flag
        # document header
        el_statement_group = XBELYielder.statement_group

        yield element_string(
          XBELYielder.header(evidence..document_header)
        )
        yield element_string(
          XBELYielder.namespace_definitions(evidence.references.namespace_definitions)
        )
        yield element_string(
          XBELYielder.annotation_definitions(evidence.references.annotation_definitions)
        )

        yield start_element_string(el_statement_group)

        header_flag = false
      end

      yield element_string(XBELYielder.evidence(evidence))
      evidence_count += 1
    }

    if evidence_count > 0
      yield end_element_string(el_statement_group)
    else
      # empty head sections; required for XBEL schema
      empty_header = Hash[%w(name description version).map { |element|
        [element, ""]
      }]
      yield element_string(XBELYielder.header(empty_header))
      yield element_string(XBELYielder.namespace_definitions({}))
      yield element_string(XBELYielder.annotation_definitions({}))
      yield element_string(XBELYielder.statement_group)
    end
    yield end_element_string(el_document)
  else
    to_enum(:each)
  end
end