Module: BioCWriter

Defined in:
lib/simple_bioc/bioc_writer.rb

Class Method Summary collapse

Class Method Details

.write(collection, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/simple_bioc/bioc_writer.rb', line 7

def write(collection, options = {})
  options[:save_with] = 1 if options[:save_with].nil?
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.doc.create_internal_subset( 'collection', nil, 'BioC.dtd' )
    write_collection(xml, collection)
  end
  builder.to_xml(options)
end

.write_annotation(xml, annotation) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simple_bioc/bioc_writer.rb', line 65

def write_annotation(xml, annotation) 
  if annotation.id.nil?
    attribute = nil
  else
    attribute = {id: annotation.id}
  end
  xml.annotation(attribute) {
    write_infon(xml, annotation)
    annotation.locations.each{|l| write_location(xml, l)} unless annotation.locations.nil?
    xml.text_ annotation.text
  }
end

.write_collection(xml, collection) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/simple_bioc/bioc_writer.rb', line 25

def write_collection(xml, collection)
  xml.collection {
    xml.source collection.source
    xml.date collection.date
    xml.key collection.key
    write_infon(xml, collection)
    collection.documents.each{|d| write_document(xml, d)} unless collection.documents.nil?
  }
end

.write_document(xml, document) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/simple_bioc/bioc_writer.rb', line 35

def write_document(xml, document)
  xml.document {
    xml.id_ document.id
    write_infon(xml, document)
    document.passages.each{|p| write_passage(xml, p)} unless document.passages.nil?
    document.relations.each{|r| write_relation(xml, r)} unless document.relations.nil?
  }
end

.write_infon(xml, obj) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/simple_bioc/bioc_writer.rb', line 16

def write_infon(xml, obj)
  return if obj.infons.nil?
  obj.infons.each do |k, v| 
    xml.infon(:key => k) {
      xml.text v
    }
  end
end

.write_location(xml, location) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/simple_bioc/bioc_writer.rb', line 90

def write_location(xml, location) 
  return if location.nil? || location == false
  # if location.original_offset.nil? || location.original_offset == location.offset
    xml.location(:offset => location.offset, :length => location.length)
  # else
    # xml.location(:offset => location.offset, :length => location.length, :original_offset => location.original_offset)
  # end
end

.write_node(xml, node) ⇒ Object



99
100
101
# File 'lib/simple_bioc/bioc_writer.rb', line 99

def write_node(xml, node)
  xml.node_(:refid => node.refid, :role => node.role)
end

.write_passage(xml, passage) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/simple_bioc/bioc_writer.rb', line 44

def write_passage(xml, passage)
  xml.passage {
    write_infon(xml, passage)
    xml.offset passage.offset
    xml.text_ passage.text unless passage.text.nil?
    passage.annotations.each{|a| write_annotation(xml, a)} unless passage.annotations.nil?
    passage.sentences.each{|s| write_sentence(xml, s)} unless passage.sentences.nil?
    passage.relations.each{|r| write_relation(xml, r)} unless passage.relations.nil?
  }
end

.write_relation(xml, relation) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/simple_bioc/bioc_writer.rb', line 78

def write_relation(xml, relation) 
  if relation.id.nil?
    attribute = nil
  else
    attribute = {id: relation.id}
  end
  xml.relation(attribute) {
    write_infon(xml, relation)
    relation.nodes.each{|n| write_node(xml, n)} unless relation.nodes.nil?
  }
end

.write_sentence(xml, sentence) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/simple_bioc/bioc_writer.rb', line 55

def write_sentence(xml, sentence)
  xml.sentence {
    write_infon(xml, sentence)
    xml.offset sentence.offset
    xml.text_ sentence.text unless sentence.text.nil?
    sentence.annotations.each{|a| write_annotation(xml, a)} unless sentence.annotations.nil?
    sentence.relations.each{|r| write_relation(xml, r)} unless sentence.relations.nil?
  }
end