Module: BioCWriter

Defined in:
lib/simple_bioc/bioc_writer.rb

Class Method Summary collapse

Class Method Details

.write(collection) ⇒ Object


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

def write(collection) 
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    write_collection(xml, collection)
  end
  builder.to_xml
end

.write_annotation(xml, annotation) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/simple_bioc/bioc_writer.rb', line 61

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

.write_collection(xml, collection) ⇒ Object


21
22
23
24
25
26
27
28
29
# File 'lib/simple_bioc/bioc_writer.rb', line 21

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)}
  }
end

.write_document(xml, document) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/simple_bioc/bioc_writer.rb', line 31

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

.write_infon(xml, obj) ⇒ Object


13
14
15
16
17
18
19
# File 'lib/simple_bioc/bioc_writer.rb', line 13

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

.write_location(xml, location) ⇒ Object


86
87
88
# File 'lib/simple_bioc/bioc_writer.rb', line 86

def write_location(xml, location) 
  xml.location(:offset => location.offset, :length => location.length)
end

.write_node(xml, node) ⇒ Object


90
91
92
# File 'lib/simple_bioc/bioc_writer.rb', line 90

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

.write_passage(xml, passage) ⇒ Object


40
41
42
43
44
45
46
47
48
49
# File 'lib/simple_bioc/bioc_writer.rb', line 40

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

.write_relation(xml, relation) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/simple_bioc/bioc_writer.rb', line 74

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)}
  }
end

.write_sentence(xml, sentence) ⇒ Object


51
52
53
54
55
56
57
58
59
# File 'lib/simple_bioc/bioc_writer.rb', line 51

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)}
    sentence.relations.each{|r| write_relation(xml, r)}
  }
end