Class: BEL::Translator::Plugins::Xbel::NanopubHandler

Inherits:
Object
  • Object
show all
Includes:
Nanopub, REXML::StreamListener
Defined in:
lib/bel/translator/plugins/xbel/nanopub_handler.rb

Constant Summary collapse

ANNOTATION =
'annotation'.freeze
ANNOTATION_DEFINITION_GROUP =
'annotationDefinitionGroup'.freeze
ANNOTATION_GROUP =
'annotationGroup'.freeze
AUTHOR =
'author'.freeze
AUTHOR_GROUP =
'authorGroup'.freeze
BEL_VERSION =
'belVersion'.freeze
CITATION =
'citation'.freeze
COMMENT =
'comment'.freeze
CONTACT_INFO =
'contactInfo'.freeze
'copyright'.freeze
DATE =
'date'.freeze
DEFAULT_NAMESPACE =
'defaultNamespace'.freeze
DESCRIPTION =
'description'.freeze
DOCUMENT =
'document'.freeze
EVIDENCE =
'evidence'.freeze
EXTERNAL_ANNOTATION_DEFINITION =
'externalAnnotationDefinition'.freeze
FUNCTION =
'function'.freeze
HEADER =
'header'.freeze
ID =
'id'.freeze
INTERNAL_ANNOTATION_DEFINITION =
'internalAnnotationDefinition'.freeze
LICENSE =
'license'.freeze
LICENSE_GROUP =
'licenseGroup'.freeze
LIST_ANNOTATION =
'listAnnotation'.freeze
LIST_VALUE =
'listValue'.freeze
NAME =
'name'.freeze
NAMESPACE =
'namespace'.freeze
NAMESPACE_GROUP =
'namespaceGroup'.freeze
NANOPUB =
'nanopub'.freeze
NS =
'ns'.freeze
OBJECT =
'object'.freeze
PARAMETER =
'parameter'.freeze
PATTERN_ANNOTATION =
'patternAnnotation'.freeze
PREFIX =
'prefix'.freeze
RESOURCE =
'resource'.freeze
REFERENCE =
'reference'.freeze
REF_ID =
'refID'.freeze
RELATIONSHIP =
'relationship'.freeze
RESOURCE_LOCATION =
'resourceLocation'.freeze
STATEMENT =
'statement'.freeze
STATEMENT_GROUP =
'statementGroup'.freeze
SUBJECT =
'subject'.freeze
SUPPORT =
'support'.freeze
TERM =
'term'.freeze
TYPE =
'type'.freeze
URI =
'uri'.freeze
URL =
'url'.freeze
USAGE =
'usage'.freeze
VERSION =
'version'.freeze
Model =
BELParser::Expression::Model

Instance Method Summary collapse

Methods included from Nanopub

union_annotation_references, union_by_keyword, union_namespace_references

Constructor Details

#initialize(callable) ⇒ NanopubHandler

Returns a new instance of NanopubHandler.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 59

def initialize(callable)
  @callable      = callable
  @element_stack = []
  @text          = nil
  @nanopub       = Nanopub.new
  @annotations   = {}
  @namespaces    = {}
  @spec          = BELParser::Language.latest_supported_specification

  @last_prefix   = nil
  @last_keyword  = nil
  # default BEL version to 1 for backwards compatibility
  @bel_version   = 1
  @nanopub..bel_version = "1.0"
end

Instance Method Details

#end_annotationObject



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 513

def end_annotation
  if @element_stack[-3] == :statement
    ref_id = @annotation_id

    annotation  = @nanopub.experiment_context.find { |annotation|
      annotation[:name] == ref_id
    }
    if annotation
      # create array for multiple values by refID
      annotation[:value] = [annotation[:value], @text].flatten
    else
      @nanopub.experiment_context << {
        :name  => ref_id,
        :value => @text
      }
    end
  end

  @element_stack.pop
end

#end_annotation_definition_groupObject



360
361
362
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 360

def end_annotation_definition_group
  @element_stack.pop
end

#end_annotation_groupObject



499
500
501
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 499

def end_annotation_group
  @element_stack.pop
end

#end_authorObject



568
569
570
571
572
573
574
575
576
577
578
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 568

def end_author
  if stack_top == :header
    @nanopub..document_header[:Authors] ||= []
    @nanopub..document_header[:Authors] <<  @text
  end

  if stack_top == :citation
    @nanopub.citation.authors ||= []
    @nanopub.citation.authors  << @text
  end
end

#end_bel_versionObject



346
347
348
349
350
351
352
353
354
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 346

def end_bel_version
  if stack_top == :header
    @nanopub..document_header[:BELVersion] ||= []
    version = @text
    @nanopub..document_header[:BELVersion]  << version
    @bel_version = version
    @nanopub..bel_version = @bel_version
  end
end

#end_citationObject



542
543
544
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 542

def end_citation
  @element_stack.pop
end

#end_commentObject



580
581
582
583
584
585
586
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 580

def end_comment
  if stack_top == :citation
    @nanopub.citation.comment = @text
  elsif stack_top == :statement
    @statement_stack.last[:comment] = @text
  end
end

#end_contact_infoObject



333
334
335
336
337
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 333

def end_contact_info
  if stack_top == :header
    @nanopub..document_header[:ContactInfo] = @text
  end
end


327
328
329
330
331
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 327

def end_copyright
  if stack_top == :header
    @nanopub..document_header[:Copyright] = @text
  end
end

#end_dateObject



562
563
564
565
566
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 562

def end_date
  if stack_top == :citation
    @nanopub.citation.date = @text
  end
end

#end_default_namespaceObject



268
269
270
271
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 268

def end_default_namespace
  @element_stack.pop
  @last_prefix = nil
end

#end_descriptionObject



396
397
398
399
400
401
402
403
404
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 396

def end_description
  if stack_top == :header
    @nanopub..document_header[:Description] = @text
  end

  if stack_top == :internal_annotation_definition
    @current_anno_def[:description] = @text
  end
end

#end_evidenceObject



503
504
505
506
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 503

def end_evidence
  @nanopub.support.value = @text
  @element_stack.pop
end

#end_external_annotation_definitionObject



369
370
371
372
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 369

def end_external_annotation_definition
  @element_stack.pop
  @last_keyword = nil
end

#end_headerObject

End element methods, dynamically invoked.



317
318
319
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 317

def end_header
  @element_stack.pop
end

#end_internal_annotation_definitionObject



374
375
376
377
378
379
380
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 374

def end_internal_annotation_definition
  @element_stack.pop
  keyword, type, domain =
    @current_anno_def.values_at(:keyword, :type, :domain)
  @annotations[keyword] =
    {keyword: keyword, type: type, domain: domain}
end

#end_licenseObject



339
340
341
342
343
344
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 339

def end_license
  if stack_top == :header
    @nanopub..document_header[:Licenses] ||= []
    @nanopub..document_header[:Licenses]  << @text
  end
end

#end_list_annotationObject



382
383
384
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 382

def end_list_annotation
  @element_stack.pop
end

#end_list_valueObject



412
413
414
415
416
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 412

def end_list_value
  if stack_top == :list_annotation
    @current_anno_def[:domain] << @text
  end
end

#end_nameObject



552
553
554
555
556
557
558
559
560
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 552

def end_name
  if stack_top == :header
    @nanopub..document_header[:Name] = @text
  end

  if stack_top == :citation
    @nanopub.citation.name = @text
  end
end

#end_namespaceObject



364
365
366
367
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 364

def end_namespace
  @element_stack.pop
  @last_prefix = nil
end

#end_namespace_groupObject



356
357
358
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 356

def end_namespace_group
  @element_stack.pop
end

#end_nanopubObject



534
535
536
537
538
539
540
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 534

def end_nanopub
  if @element_stack[-3] == :statement
    @nanopub.support.value = @text
  end

  @element_stack.pop
end

#end_objectObject



476
477
478
479
480
481
482
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 476

def end_object
  if @statement_stack.last[:object] == nil
    # sets object if it wasn't already set by OBJECT STATEMENT
    @statement_stack.last[:object] = @term_stack.last
  end
  @element_stack.pop
end

#end_parameterObject



493
494
495
496
497
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 493

def end_parameter
  @current_parameter.value    = @text
  @term_stack.last.arguments << @current_parameter
  @element_stack.pop
end

#end_pattern_annotationObject



386
387
388
389
390
391
392
393
394
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 386

def end_pattern_annotation
  begin
    @current_anno_def[:domain] = Regexp.new(@text)
  rescue RegexpError
    @text = Regexp.escape(@text)
    retry
  end
  @element_stack.pop
end

#end_referenceObject



546
547
548
549
550
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 546

def end_reference
  if stack_top == :citation
    @nanopub.citation.id   = @text
  end
end

#end_resourceObject



277
278
279
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 277

def end_resource
  @element_stack.pop
end

#end_statementObject



422
423
424
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
466
467
468
469
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 422

def end_statement
  @element_stack.pop
  stmt = @statement_stack.pop

  if stack_top == :object
    @statement_stack.last[:object] =
      BELParser::Expression::Model::Statement.new(
        stmt[:subject],
        stmt[:relationship],
        stmt[:object],
        stmt[:comment]
      )
  elsif @statement_stack.empty?
    # create new nanopub from parsed data
    nanopub_copy = Nanopub.create({
      :bel_statement      =>
        BELParser::Expression::Model::Statement.new(
          stmt[:subject],
          stmt[:relationship],
          stmt[:object],
          stmt[:comment]
        ),
      :citation           => @nanopub.citation.to_h,
      :support            => @nanopub.support.value,
      :experiment_context => @nanopub.experiment_context.values.dup,
      :references         => {
        :annotations => @annotations.values,
        :namespaces => @namespaces.values
      },
      :metadata           => @nanopub..values.dup
    })

    # yield nanopub
    @callable.call(nanopub_copy)

    # clear nanopub parser state
    # note:
    #   - preserve @nanopub.references
    #   - preserve @nanopub.metadata.document_header
    @nanopub.bel_statement      = nil
    @nanopub.citation           = nil
    @nanopub.support            = nil
    @nanopub.experiment_context = nil
    @nanopub..delete_if { |key|
      key != :document_header
    }
  end
end

#end_statement_groupObject



418
419
420
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 418

def end_statement_group
  @element_stack.pop
end

#end_subjectObject



471
472
473
474
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 471

def end_subject
  @statement_stack.last[:subject] = @term_stack.last
  @element_stack.pop
end

#end_supportObject



508
509
510
511
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 508

def end_support
  @nanopub.support.value = @text
  @element_stack.pop
end

#end_termObject



484
485
486
487
488
489
490
491
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 484

def end_term
  @element_stack.pop

  if @term_stack.size > 1
    @term_stack.pop
    @current_term = @term_stack.last
  end
end

#end_uriObject



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 285

def end_uri
  # context is namespace or annotation
  context = peek(-2)
  if context == :namespace || context == :default_namespace
    @namespaces[@last_prefix][:uri] = @text
  elsif context == :external_annotation_definition
    annotation = @annotations[@last_keyword]
    annotation[:type] = :uri
    annotation[:domain] = @text
  end
  @element_stack.pop
end

#end_urlObject



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 302

def end_url
  # context is namespace or annotation
  context = peek(-2)
  if context == :namespace || context == :default_namespace
    @namespaces[@last_prefix][:url] = @text
  elsif context == :external_annotation_definition
    annotation = @annotations[@last_keyword]
    annotation[:type] = :url
    annotation[:domain] = @text
  end
  @element_stack.pop
end

#end_usageObject



406
407
408
409
410
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 406

def end_usage
  if stack_top == :internal_annotation_definition
    @current_anno_def[:usage] = @text
  end
end

#end_versionObject



321
322
323
324
325
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 321

def end_version
  if stack_top == :header
    @nanopub..document_header[:Version] = @text
  end
end

#start_annotation(attributes) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 233

def start_annotation(attributes)
  if @element_stack[-2] == :statement
    ref_id = attr_value(attributes, REF_ID)
    @annotation_id = ref_id
    @element_stack << :annotation
  end
end

#start_annotation_definition_group(attributes) ⇒ Object



118
119
120
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 118

def start_annotation_definition_group(attributes)
  @element_stack << :annotation_definition_group
end

#start_annotation_group(attributes) ⇒ Object



228
229
230
231
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 228

def start_annotation_group(attributes)
  @annotation_id = nil
  @element_stack << :annotation_group
end

#start_citation(attributes) ⇒ Object



249
250
251
252
253
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 249

def start_citation(attributes)
  type = attr_value(attributes, TYPE)
  @nanopub.citation.type = type
  @element_stack << :citation
end

#start_default_namespace(attributes) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 259

def start_default_namespace(attributes)
  if stack_top == :namespace_group
    prefix              = attr_value(attributes, PREFIX)
    @namespaces[prefix] = {keyword: prefix}
    @last_prefix        = prefix
  end
  @element_stack << :default_namespace
end

#start_evidence(attributes) ⇒ Object



241
242
243
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 241

def start_evidence(attributes)
  @element_stack << :evidence
end

#start_external_annotation_definition(attributes) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 134

def start_external_annotation_definition(attributes)
  if stack_top == :annotation_definition_group
    keyword               = attr_value(attributes, ID)
    @last_keyword         = keyword
    if @bel_version == 1
      uri                   = attr_value(attributes, URL)
      @annotations[keyword] =
        {keyword: keyword, type: :uri, domain: uri}
    end
  end
  @element_stack << :external_annotation_definition
end

#start_header(attributes) ⇒ Object

Start element methods, dynamically invoked.



110
111
112
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 110

def start_header(attributes)
  @element_stack << :header
end

#start_internal_annotation_definition(attributes) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 147

def start_internal_annotation_definition(attributes)
  if stack_top == :annotation_definition_group
    @current_anno_def = {
      :keyword => attr_value(attributes, ID)
    }
  end
  @element_stack << :internal_annotation_definition
end

#start_list_annotation(attributes) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 156

def start_list_annotation(attributes)
  if stack_top == :internal_annotation_definition
    @current_anno_def[:type]   = :list
    @current_anno_def[:domain] = []
  end
  @element_stack << :list_annotation
end

#start_namespace(attributes) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 122

def start_namespace(attributes)
  if stack_top == :namespace_group
    prefix = attr_value(attributes, PREFIX)
    @last_prefix = prefix
    if @bel_version == 1
      resource_location   = attr_value(attributes, RESOURCE_LOCATION)
      @namespaces[prefix] = {keyword: prefix, uri: resource_location}
    end
  end
  @element_stack << :namespace
end

#start_namespace_group(attributes) ⇒ Object



114
115
116
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 114

def start_namespace_group(attributes)
  @element_stack << :namespace_group
end

#start_nanopub(attributes) ⇒ Object



255
256
257
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 255

def start_nanopub(attributes)
  @element_stack << :nanopub
end

#start_object(attributes) ⇒ Object



195
196
197
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 195

def start_object(attributes)
  @element_stack << :object
end

#start_parameter(attributes) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 214

def start_parameter(attributes)
  if stack_top == :term
    ns =
      if has_attr?(attributes, NS)
        ns_id = attr_value(attributes, NS)
        namespace_reference(ns_id, @nanopub)
      else
        nil
      end
    @current_parameter = Model::Parameter.new(ns, ns_id)
  end
  @element_stack << :parameter
end

#start_pattern_annotation(attributes) ⇒ Object



164
165
166
167
168
169
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 164

def start_pattern_annotation(attributes)
  if stack_top == :internal_annotation_definition
    @current_anno_def[:type] = :pattern
  end
  @element_stack << :pattern_annotation
end

#start_resource(attributes) ⇒ Object



273
274
275
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 273

def start_resource(attributes)
  @element_stack << :resource
end

#start_statement(attributes) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 175

def start_statement(attributes)
  relationship = attr_value(attributes, RELATIONSHIP)
  stmt = {
    relationship: convert_relationship(relationship)
  }
  if stack_top == :statement_group
    @statement_stack = []
    @statement_stack << stmt
  elsif stack_top == :object
    #@statement_stack.last[:object] = stmt
    @statement_stack << stmt
  end

  @element_stack << :statement
end

#start_statement_group(attributes) ⇒ Object



171
172
173
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 171

def start_statement_group(attributes)
  @element_stack << :statement_group
end

#start_subject(attributes) ⇒ Object



191
192
193
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 191

def start_subject(attributes)
  @element_stack << :subject
end

#start_support(attributes) ⇒ Object



245
246
247
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 245

def start_support(attributes)
  @element_stack << :support
end

#start_term(attributes) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 199

def start_term(attributes)
  function = @spec.function(attr_value(attributes, FUNCTION).to_sym)
  term = BELParser::Expression::Model::Term.new(function, [])
  if stack_top == :subject || stack_top == :object
    # outer term
    @term_stack = []
  elsif stack_top == :term
    # nested term
    @term_stack.last.arguments << term
  end

  @term_stack    << term
  @element_stack << :term
end

#start_uri(attributes) ⇒ Object



281
282
283
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 281

def start_uri(attributes)
  @element_stack << :uri
end

#start_url(attributes) ⇒ Object



298
299
300
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 298

def start_url(attributes)
  @element_stack << :url
end

#tag_end(name) ⇒ Object

Called on element end by REXML.



88
89
90
91
92
93
94
95
96
97
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 88

def tag_end(name)
  name.
    sub!(/^bel:/, '').
    gsub!(/([A-Z])/) { |match| "_#{match.downcase}" }

  end_method = "end_#{name}"
  if self.respond_to? end_method
    self.send end_method
  end
end

#tag_start(name, attributes) ⇒ Object

Called on element start by REXML.



76
77
78
79
80
81
82
83
84
85
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 76

def tag_start(name, attributes)
  name.
    sub!(/^bel:/, '').
    gsub!(/([A-Z])/) { |match| "_#{match.downcase}" }

  start_method = "start_#{name}"
  if self.respond_to? start_method
    self.send(start_method, attributes)
  end
end

#text(*args) ⇒ Object

Called on text node by REXML.



100
101
102
103
104
105
106
# File 'lib/bel/translator/plugins/xbel/nanopub_handler.rb', line 100

def text(*args)
  if args.size.zero?
    @text = ''
  else
    @text = args.first
  end
end