ncbo_annotator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ncbo_annotator.rb +68 -0
- data/lib/ncbo_annotator/parser.rb +105 -0
- metadata +83 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'net/http'
|
3
|
+
require 'xml'
|
4
|
+
require 'uri'
|
5
|
+
require 'ncbo_annotator/parser'
|
6
|
+
|
7
|
+
module NCBO
|
8
|
+
class Annotator
|
9
|
+
|
10
|
+
def initialize(args = {})
|
11
|
+
@options = {}
|
12
|
+
|
13
|
+
@options[:annotator_location] = "http://rest.bioontology.org/obs/annotator"
|
14
|
+
@options[:filterNumber] = true
|
15
|
+
@options[:isStopWordsCaseSensitive] = false
|
16
|
+
@options[:isVirtualOntologyId] = true
|
17
|
+
@options[:levelMax] = 0
|
18
|
+
@options[:longestOnly] = false
|
19
|
+
@options[:ontologiesToExpand] = []
|
20
|
+
@options[:ontologiesToKeepInResult] = []
|
21
|
+
@options[:mappingTypes] = []
|
22
|
+
@options[:minTermSize] = 3
|
23
|
+
@options[:scored] = true
|
24
|
+
@options[:semanticTypes] = []
|
25
|
+
@options[:stopWords] = []
|
26
|
+
@options[:wholeWordOnly] = true
|
27
|
+
@options[:withDefaultStopWords] = false
|
28
|
+
@options[:withSynonyms] = true
|
29
|
+
|
30
|
+
@options.merge!(args)
|
31
|
+
|
32
|
+
raise ArgumentError, ":apikey is required, you can obtain one at http://bioportal.bioontology.org/accounts/new" if @options[:apikey].nil?
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.annotate(text, options = {})
|
36
|
+
options[:textToAnnotate] = text
|
37
|
+
new(options).annotate
|
38
|
+
end
|
39
|
+
|
40
|
+
def annotate(text = nil, options = {})
|
41
|
+
@options[:textToAnnotate] = text unless text.nil?
|
42
|
+
@options.merge!(options) unless options.empty?
|
43
|
+
|
44
|
+
raise ArgumentError, ":textToAnnotate must be included" if @options[:textToAnnotate].nil?
|
45
|
+
|
46
|
+
result_xml = post
|
47
|
+
Parser::Annotator.parse_results(result_xml)
|
48
|
+
end
|
49
|
+
|
50
|
+
def options
|
51
|
+
@options
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def post
|
57
|
+
url = @options[:annotator_location]
|
58
|
+
options = @options.clone
|
59
|
+
options.each do |k,v|
|
60
|
+
if v.kind_of?(Array)
|
61
|
+
options[k] = v.join(",")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
res = Net::HTTP.post_form(URI.parse(url), options)
|
65
|
+
return res.body
|
66
|
+
end
|
67
|
+
end # end Annotator class
|
68
|
+
end # end NCBO module
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module NCBO
|
2
|
+
module Parser
|
3
|
+
|
4
|
+
class BaseParser
|
5
|
+
def parse_xml(xml)
|
6
|
+
if xml.kind_of?(String)
|
7
|
+
parser = XML::Parser.string(xml, :options => LibXML::XML::Parser::Options::NOBLANKS)
|
8
|
+
else
|
9
|
+
parser = XML::Parser.io(xml, :options => LibXML::XML::Parser::Options::NOBLANKS)
|
10
|
+
end
|
11
|
+
parser.parse
|
12
|
+
end
|
13
|
+
|
14
|
+
def safe_to_i(str)
|
15
|
+
Integer(str) rescue str
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Annotator < BaseParser
|
20
|
+
def initialize(results)
|
21
|
+
@root = "/success/data/annotatorResultBean"
|
22
|
+
@results = parse_xml(results)
|
23
|
+
@result = AnnotatorResult.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.parse_results(results)
|
27
|
+
new(results).parse_results
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_results
|
31
|
+
@result.id = @results.find_first(@root + "/resultID").content
|
32
|
+
@result.statistics = parse_statistics
|
33
|
+
@result.annotations = parse_annotations
|
34
|
+
@result.ontologies = parse_ontologies
|
35
|
+
@result
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_statistics
|
39
|
+
statistics = {}
|
40
|
+
@results.find(@root + "/statistics/statisticsBean").each do |statistic|
|
41
|
+
statistics[statistic.children[0].content.downcase.to_sym] = statistic.children[1].content.to_i
|
42
|
+
end
|
43
|
+
statistics
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_annotations
|
47
|
+
annotations = []
|
48
|
+
@results.find(@root + "/annotations/annotationBean").each do |annotation|
|
49
|
+
a = {}
|
50
|
+
a[:score] = annotation.find_first("score").content.to_i
|
51
|
+
a[:concept] = parse_concept(annotation)
|
52
|
+
a[:context] = parse_context(annotation)
|
53
|
+
annotations << a
|
54
|
+
end
|
55
|
+
annotations
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_ontologies
|
59
|
+
ontologies = []
|
60
|
+
@results.find(@root + "/ontologies/ontologyUsedBean").each do |ontology|
|
61
|
+
ont = {}
|
62
|
+
ontology.children.each {|child| ont[child.name.to_sym] = safe_to_i(child.content)}
|
63
|
+
ontologies << ont
|
64
|
+
end
|
65
|
+
ontologies
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def parse_concept(annotation, concept_location = "concept")
|
71
|
+
a = {}
|
72
|
+
annotation.find("#{concept_location}/*").each {|child| a[child.name.to_sym] = safe_to_i(child.content) if !child.first.nil? && !child.first.children?}
|
73
|
+
|
74
|
+
a[:synonyms] = annotation.find("#{concept_location}/synonyms/string").map {|syn| safe_to_i(syn.content)}
|
75
|
+
|
76
|
+
semantic_types = {}
|
77
|
+
annotation.find("#{concept_location}/semanticTypes/semanticTypeBean/*").each {|child| semantic_types[child.name.to_sym] = safe_to_i(child.content)}
|
78
|
+
a[:semantic_types] = semantic_types
|
79
|
+
|
80
|
+
a
|
81
|
+
end
|
82
|
+
|
83
|
+
def parse_context(annotation)
|
84
|
+
a = {}
|
85
|
+
annotation.find("context/*").each {|child| a[child.name.to_sym] = safe_to_i(child.content) if !child.first.nil? && !child.first.children?}
|
86
|
+
|
87
|
+
if a[:contextName].downcase.include?("mapping")
|
88
|
+
a[:mappedConcept] = parse_concept(annotation.find_first("context"), "mappedConcept")
|
89
|
+
elsif a[:contextName].downcase.include?("mgrep")
|
90
|
+
term = {}
|
91
|
+
annotation.find("context/term/*").each {|trm| term[trm.name.to_sym] = safe_to_i(trm.content)}
|
92
|
+
a[:term] = term
|
93
|
+
elsif a[:contextName].downcase.include?("closure")
|
94
|
+
a[:concept] = parse_concept(annotation.find_first("context"))
|
95
|
+
end
|
96
|
+
|
97
|
+
a
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class AnnotatorResult
|
102
|
+
attr_accessor :id, :statistics, :annotations, :ontologies
|
103
|
+
end
|
104
|
+
end # end Parser module
|
105
|
+
end # end of NCBO module
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ncbo_annotator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Paul R Alexander
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-11 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: libxml-ruby
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 19
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
version: 1.1.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: The NCBO Annotator Gem is a Ruby client for NCBO's Annotator Web service. The NCBO Annotator (formerly referred to as the Open Biomedical Annotator (OBA)) is an ontology-based Web service that annotates public datasets with biomedical ontology concepts based on their textual metadata. The biomedical community can use the annotator service to tag their data automatically with ontology concepts. These concepts come from the Unified Medical Language System (UMLS) Metathesaurus and the National Center for Biomedical Ontology (NCBO) BioPortal ontologies. Such annotations facilitate translational discoveries by integrating annotated data.
|
38
|
+
email: support@bioontology.org
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/ncbo_annotator/parser.rb
|
47
|
+
- lib/ncbo_annotator.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://rubygems.org/gems/ncbo_annotator
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.5.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: The NCBO Annotator Gem is a Ruby client for NCBO's Annotator Web service
|
82
|
+
test_files: []
|
83
|
+
|