Module: BioCReader

Defined in:
lib/simple_bioc/bioc_reader.rb

Class Method Summary collapse

Class Method Details

.read(path) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple_bioc/bioc_reader.rb', line 7

def read(path) 
  collection = nil
  File.open(path) do |file|
    xml_doc  = Nokogiri::XML(file) do |config|
      config.noent.strict.noblanks
    end
    xml = xml_doc.at_xpath("//collection")
    if xml.nil?
      fail 'Wrong format'
    end
    collection = SimpleBioC::Collection.new
    read_collection(xml, collection)
  end

  collection
end

.read_annotation(xml, annotation) ⇒ Object


80
81
82
83
84
85
# File 'lib/simple_bioc/bioc_reader.rb', line 80

def read_annotation(xml, annotation) 
  annotation.id = xml["id"]
  annotation.text = read_text(xml, "text")
  read_infon(xml, annotation)
  read_recursive(xml, annotation, "location")
end

.read_collection(xml, collection) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/simple_bioc/bioc_reader.rb', line 47

def read_collection(xml, collection)
  collection.source = read_text(xml, "source")
  collection.date = read_text(xml, "date")
  collection.key = read_text(xml, "key")
  read_infon(xml, collection)
  read_recursive(xml, collection, "document")
end

.read_document(xml, document) ⇒ Object


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

def read_document(xml, document)
  document.id = read_text(xml, "id")
  read_infon(xml, document)
  read_recursive(xml, document, "passage")
  read_recursive(xml, document, "relation")
  document.adjust_ref
end

.read_infon(xml, obj) ⇒ Object


34
35
36
# File 'lib/simple_bioc/bioc_reader.rb', line 34

def read_infon(xml, obj)
  xml.xpath("infon").each{ |i| obj.infons[i["key"]] = i.content}
end

.read_int(xml, name) ⇒ Object


29
30
31
32
# File 'lib/simple_bioc/bioc_reader.rb', line 29

def read_int(xml, name)
  val = read_text(xml, name) 
  val && val.to_i
end

.read_location(xml, location) ⇒ Object


93
94
95
96
# File 'lib/simple_bioc/bioc_reader.rb', line 93

def read_location(xml, location) 
  location.offset = xml["offset"]
  location.length = xml["length"]
end

.read_node(xml, node) ⇒ Object


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

def read_node(xml, node)
  node.refid = xml["refid"]
  node.role = xml["role"]
end

.read_passage(xml, passage) ⇒ Object


63
64
65
66
67
68
69
70
# File 'lib/simple_bioc/bioc_reader.rb', line 63

def read_passage(xml, passage)
  passage.text = read_text(xml, "text")
  passage.offset = read_int(xml, "offset")
  read_infon(xml, passage)
  read_recursive(xml, passage, "sentence")
  read_recursive(xml, passage, "annotation")
  read_recursive(xml, passage, "relation")
end

.read_recursive(xml, obj, name) ⇒ Object


38
39
40
41
42
43
44
45
# File 'lib/simple_bioc/bioc_reader.rb', line 38

def read_recursive(xml, obj, name)
  target_class = SimpleBioC.const_get(name.capitalize)
  xml.xpath(name).each do |node|
    instance = target_class.new(obj)
    send(:"read_#{name}", node, instance)
    obj.instance_variable_get(:"@#{name}s")  << instance
  end
end

.read_relation(xml, relation) ⇒ Object


87
88
89
90
91
# File 'lib/simple_bioc/bioc_reader.rb', line 87

def read_relation(xml, relation) 
  relation.id = xml["id"]
  read_infon(xml, relation)
  read_recursive(xml, relation, "node")
end

.read_sentence(xml, sentence) ⇒ Object


72
73
74
75
76
77
78
# File 'lib/simple_bioc/bioc_reader.rb', line 72

def read_sentence(xml, sentence)
  sentence.text = read_text(xml, "text")
  sentence.offset = read_int(xml, "offset")
  read_infon(xml, sentence)
  read_recursive(xml, sentence, "annotation")
  read_recursive(xml, sentence, "relation")
end

.read_text(xml, name) ⇒ Object


24
25
26
27
# File 'lib/simple_bioc/bioc_reader.rb', line 24

def read_text(xml, name)
  node = xml.at_xpath(name)
  node && node.content
end