Class: BEL::Extension::Format::FormatXBEL::EvidenceHandler

Inherits:
Object
  • Object
show all
Includes:
Model, REXML::StreamListener
Defined in:
lib/bel/extensions/xbel.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(callable) ⇒ EvidenceHandler

Returns a new instance of EvidenceHandler.



507
508
509
510
511
512
# File 'lib/bel/extensions/xbel.rb', line 507

def initialize(callable)
  @callable          = callable
  @element_stack     = []
  @text              = nil
  @evidence          = Evidence.new
end

Instance Method Details

#end_annotationObject



838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'lib/bel/extensions/xbel.rb', line 838

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

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

  @element_stack.pop
end

#end_annotation_definition_groupObject



718
719
720
# File 'lib/bel/extensions/xbel.rb', line 718

def end_annotation_definition_group
  @element_stack.pop
end

#end_annotation_groupObject



834
835
836
# File 'lib/bel/extensions/xbel.rb', line 834

def end_annotation_group
  @element_stack.pop
end

#end_authorObject



893
894
895
896
897
898
899
900
901
902
# File 'lib/bel/extensions/xbel.rb', line 893

def end_author
  if stack_top == :header
    @evidence..document_header['authors'] ||= []
    @evidence..document_header['authors'] <<  @text
  end

  if stack_top == :citation
    (@evidence.citation.authors ||= []) << @text
  end
end

#end_citationObject



867
868
869
# File 'lib/bel/extensions/xbel.rb', line 867

def end_citation
  @element_stack.pop
end

#end_commentObject



904
905
906
907
908
# File 'lib/bel/extensions/xbel.rb', line 904

def end_comment
  if stack_top == :citation
    @evidence.citation.comment = @text
  end
end

#end_contact_infoObject



701
702
703
704
705
# File 'lib/bel/extensions/xbel.rb', line 701

def end_contact_info
  if stack_top == :header
    @evidence..document_header['contactInfo'] = @text
  end
end


695
696
697
698
699
# File 'lib/bel/extensions/xbel.rb', line 695

def end_copyright
  if stack_top == :header
    @evidence..document_header['copyright'] = @text
  end
end

#end_dateObject



887
888
889
890
891
# File 'lib/bel/extensions/xbel.rb', line 887

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

#end_descriptionObject



748
749
750
751
752
753
754
755
756
# File 'lib/bel/extensions/xbel.rb', line 748

def end_description
  if stack_top == :header
    @evidence..document_header['description'] = @text
  end

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

#end_evidenceObject



859
860
861
862
863
864
865
# File 'lib/bel/extensions/xbel.rb', line 859

def end_evidence
  if @element_stack[-3] == :statement
    @evidence.summary_text.value = @text
  end

  @element_stack.pop
end

#end_external_annotation_definitionObject



726
727
728
# File 'lib/bel/extensions/xbel.rb', line 726

def end_external_annotation_definition
  @element_stack.pop
end

#end_headerObject

End element methods, dynamically invoked.



685
686
687
# File 'lib/bel/extensions/xbel.rb', line 685

def end_header
  @element_stack.pop
end

#end_internal_annotation_definitionObject



730
731
732
# File 'lib/bel/extensions/xbel.rb', line 730

def end_internal_annotation_definition
  @element_stack.pop
end

#end_licenseObject



707
708
709
710
711
712
# File 'lib/bel/extensions/xbel.rb', line 707

def end_license
  if stack_top == :header
    @evidence..document_header['licenses'] ||= []
    @evidence..document_header['licenses'] <<  @text
  end
end

#end_list_annotationObject



734
735
736
# File 'lib/bel/extensions/xbel.rb', line 734

def end_list_annotation
  @element_stack.pop
end

#end_list_valueObject



764
765
766
767
768
# File 'lib/bel/extensions/xbel.rb', line 764

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

#end_nameObject



877
878
879
880
881
882
883
884
885
# File 'lib/bel/extensions/xbel.rb', line 877

def end_name
  if stack_top == :header
    @evidence..document_header['name'] = @text
  end

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

#end_namespaceObject



722
723
724
# File 'lib/bel/extensions/xbel.rb', line 722

def end_namespace
  @element_stack.pop
end

#end_namespace_groupObject



714
715
716
# File 'lib/bel/extensions/xbel.rb', line 714

def end_namespace_group
  @element_stack.pop
end

#end_objectObject



811
812
813
814
815
816
817
# File 'lib/bel/extensions/xbel.rb', line 811

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



828
829
830
831
832
# File 'lib/bel/extensions/xbel.rb', line 828

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

#end_pattern_annotationObject



738
739
740
741
742
743
744
745
746
# File 'lib/bel/extensions/xbel.rb', line 738

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



871
872
873
874
875
# File 'lib/bel/extensions/xbel.rb', line 871

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

#end_statementObject



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/bel/extensions/xbel.rb', line 774

def end_statement
  @element_stack.pop

  stmt = @statement_stack.pop
  if @statement_stack.empty?
    # create new evidence from parsed data
    evidence_copy = Evidence.create({
      :bel_statement      => stmt,
      :citation           => @evidence.citation.to_h,
      :summary_text       => @evidence.summary_text.value,
      :experiment_context => @evidence.experiment_context.values.dup,
      :references         => @evidence.references.values.dup,
      :metadata           => @evidence..values.dup
    })

    # yield evidence
    @callable.call(evidence_copy)

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

#end_statement_groupObject



770
771
772
# File 'lib/bel/extensions/xbel.rb', line 770

def end_statement_group
  @element_stack.pop
end

#end_subjectObject



806
807
808
809
# File 'lib/bel/extensions/xbel.rb', line 806

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

#end_termObject



819
820
821
822
823
824
825
826
# File 'lib/bel/extensions/xbel.rb', line 819

def end_term
  @element_stack.pop

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

#end_usageObject



758
759
760
761
762
# File 'lib/bel/extensions/xbel.rb', line 758

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

#end_versionObject



689
690
691
692
693
# File 'lib/bel/extensions/xbel.rb', line 689

def end_version
  if stack_top == :header
    @evidence..document_header['version'] = @text
  end
end

#start_annotation(attributes) ⇒ Object



665
666
667
668
669
670
671
# File 'lib/bel/extensions/xbel.rb', line 665

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



557
558
559
# File 'lib/bel/extensions/xbel.rb', line 557

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

#start_annotation_group(attributes) ⇒ Object



660
661
662
663
# File 'lib/bel/extensions/xbel.rb', line 660

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

#start_citation(attributes) ⇒ Object



673
674
675
676
677
# File 'lib/bel/extensions/xbel.rb', line 673

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

#start_evidence(attributes) ⇒ Object



679
680
681
# File 'lib/bel/extensions/xbel.rb', line 679

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

#start_external_annotation_definition(attributes) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
# File 'lib/bel/extensions/xbel.rb', line 570

def start_external_annotation_definition(attributes)
  if stack_top == :annotation_definition_group
    id                 = attr_value(attributes, ID)
    url                = attr_value(attributes, URL)
    @evidence.references.annotation_definitions[id] = {
      :type   => :url,
      :domain => url
    }
  end
  @element_stack << :external_annotation_definition
end

#start_header(attributes) ⇒ Object

Start element methods, dynamically invoked.



549
550
551
# File 'lib/bel/extensions/xbel.rb', line 549

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

#start_internal_annotation_definition(attributes) ⇒ Object



582
583
584
585
586
587
588
589
# File 'lib/bel/extensions/xbel.rb', line 582

def start_internal_annotation_definition(attributes)
  if stack_top == :annotation_definition_group
    id                 = attr_value(attributes, ID)
    @current_anno_def  = {}
    @evidence.references.annotation_definitions[id] = @current_anno_def
  end
  @element_stack << :internal_annotation_definition
end

#start_list_annotation(attributes) ⇒ Object



591
592
593
594
595
596
597
# File 'lib/bel/extensions/xbel.rb', line 591

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



561
562
563
564
565
566
567
568
# File 'lib/bel/extensions/xbel.rb', line 561

def start_namespace(attributes)
  if stack_top == :namespace_group
    prefix             = attr_value(attributes, PREFIX)
    resource_location  = attr_value(attributes, RESOURCE_LOCATION)
    @evidence.references.namespace_definitions[prefix] = resource_location
  end
  @element_stack << :namespace
end

#start_namespace_group(attributes) ⇒ Object



553
554
555
# File 'lib/bel/extensions/xbel.rb', line 553

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

#start_object(attributes) ⇒ Object



628
629
630
# File 'lib/bel/extensions/xbel.rb', line 628

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

#start_parameter(attributes) ⇒ Object



646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/bel/extensions/xbel.rb', line 646

def start_parameter(attributes)
  if stack_top == :term
    ns_id              = attr_value(attributes, NS)
    # XXX Hitting a SystemStackError on line 174 (inside call).
    # Example: large_corpus.xbel
    ns                 = {
      :prefix => ns_id,
      :url    => @evidence.references.namespace_definitions[ns_id]
    }
    @current_parameter = Parameter.new(ns, nil)
  end
  @element_stack << :parameter
end

#start_pattern_annotation(attributes) ⇒ Object



599
600
601
602
603
604
# File 'lib/bel/extensions/xbel.rb', line 599

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

#start_statement(attributes) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/bel/extensions/xbel.rb', line 610

def start_statement(attributes)
  stmt = Statement.new
  stmt.relationship = attr_value(attributes, 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



606
607
608
# File 'lib/bel/extensions/xbel.rb', line 606

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

#start_subject(attributes) ⇒ Object



624
625
626
# File 'lib/bel/extensions/xbel.rb', line 624

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

#start_term(attributes) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/bel/extensions/xbel.rb', line 632

def start_term(attributes)
  term = Term.new(attr_value(attributes, 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

#tag_end(name) ⇒ Object

Called on element end by REXML.



527
528
529
530
531
532
533
534
535
536
# File 'lib/bel/extensions/xbel.rb', line 527

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.



515
516
517
518
519
520
521
522
523
524
# File 'lib/bel/extensions/xbel.rb', line 515

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.



539
540
541
542
543
544
545
# File 'lib/bel/extensions/xbel.rb', line 539

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