ols 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,11 +2,9 @@
2
2
  .bundle
3
3
  .yardoc/*
4
4
  .rbx/*
5
- Gemfile.lock
6
5
  doc/*
7
6
  pkg/*
8
7
  coverage/*
9
- test/vcr_cassettes*
10
8
  *.dot
11
9
  *.png
12
- *.jpg
10
+ *.jpg
@@ -0,0 +1 @@
1
+ 1.9.3-p0
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ # - jruby
7
+ # - rbx-18mode
8
+ # - rbx-19mode
9
+ # - ree
10
+ script: bundle exec rake test
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ols (0.3.3)
5
+ savon
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.6)
11
+ akami (1.0.0)
12
+ gyoku (>= 0.4.0)
13
+ awesome_print (1.0.1)
14
+ builder (3.0.0)
15
+ coderay (0.9.8)
16
+ crack (0.3.1)
17
+ gyoku (0.4.4)
18
+ builder (>= 2.1.2)
19
+ httpi (0.9.5)
20
+ rack
21
+ metaclass (0.0.1)
22
+ method_source (0.6.7)
23
+ ruby_parser (>= 2.3.1)
24
+ mocha (0.10.0)
25
+ metaclass (~> 0.0.1)
26
+ multi_json (1.0.3)
27
+ nokogiri (1.5.0)
28
+ nori (1.0.2)
29
+ pry (0.9.7.4)
30
+ coderay (~> 0.9.8)
31
+ method_source (~> 0.6.7)
32
+ ruby_parser (>= 2.3.1)
33
+ slop (~> 2.1.0)
34
+ rack (1.3.5)
35
+ rake (0.9.2.2)
36
+ ruby_parser (2.3.1)
37
+ sexp_processor (~> 3.0)
38
+ savon (0.9.7)
39
+ akami (~> 1.0)
40
+ builder (>= 2.1.2)
41
+ gyoku (>= 0.4.0)
42
+ httpi (~> 0.9)
43
+ nokogiri (>= 1.4.0)
44
+ nori (~> 1.0)
45
+ wasabi (~> 2.0)
46
+ sexp_processor (3.0.10)
47
+ shoulda (2.11.3)
48
+ simplecov (0.5.4)
49
+ multi_json (~> 1.0.3)
50
+ simplecov-html (~> 0.5.3)
51
+ simplecov-html (0.5.3)
52
+ simplecov-rcov (0.2.3)
53
+ simplecov (>= 0.4.1)
54
+ slop (2.1.0)
55
+ vcr (1.11.3)
56
+ wasabi (2.0.0)
57
+ nokogiri (>= 1.4.0)
58
+ webmock (1.7.8)
59
+ addressable (~> 2.2, > 2.2.5)
60
+ crack (>= 0.1.7)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ awesome_print
67
+ jruby-openssl
68
+ mocha
69
+ ols!
70
+ pry
71
+ rake
72
+ shoulda
73
+ simplecov
74
+ simplecov-rcov
75
+ vcr
76
+ webmock
@@ -1,3 +1,18 @@
1
+ === 0.3.4 2012-01-18
2
+
3
+ * Minor update/clarification - the OLS::Term method .level() is now .depth(),
4
+ an alias is still in place for .level() though, and when this is called on a
5
+ term with multiple paths through the graph it will give a depth level for the
6
+ shortest route through the graph.
7
+ * Bugfix - the cache functionallity (and test suite) will now work with Ruby 1.8.7.
8
+ * Bugfix - the OLS::Term methods .all_children() and .all_parents() now return
9
+ arrays of uniq OLS::Term objects.
10
+ * Bugfix - when using a cached copy of an ontology, we will now pull the root
11
+ term out of the cache rather than hitting the soap service.
12
+ * Bugfix - when caching an ontology, we now grab the metadata for each term
13
+ and put that into the cache also.
14
+ * Some code cleaning/refactoring.
15
+
1
16
  === 0.3.3 2011-11-11
2
17
 
3
18
  * Minor cache modification - hold the marshaled strings in ram rather than repeadedly
@@ -1,5 +1,6 @@
1
- = OLS
1
+ = OLS {<img src="https://secure.travis-ci.org/dazoakley/ols.png?branch=master" />}[http://travis-ci.org/dazoakley/ols] {<img src="https://gemnasium.com/dazoakley/ols.png?travis" />}[https://gemnasium.com/dazoakley/ols]
2
2
 
3
+ http://rubygems.org/gems/ols
3
4
  http://github.com/dazoakley/ols
4
5
 
5
6
  OLS provides a simple interface to the EBI's Ontology Lookup Service (http://www.ebi.ac.uk/ontology-lookup/).
@@ -117,6 +118,7 @@ Different cache back-ends and other features will be built into future releases.
117
118
 
118
119
  Written by Darren Oakley (daz dot oakley at gmail dot com)
119
120
 
121
+ http://rubygems.org/gems/ols
120
122
  http://github.com/dazoakley/ols
121
123
 
122
124
  == License
data/lib/ols.rb CHANGED
@@ -62,15 +62,19 @@ module OLS
62
62
  # @return [Array] An array of OLS::Term objects for all root terms the requested ontology
63
63
  def root_terms(ontology)
64
64
  root_terms = []
65
- response = request(:get_root_terms) { soap.body = { :ontologyName => ontology } }
65
+ root_terms = @cache.root_terms(ontology) if using_cache?
66
66
 
67
- if response[:item].is_a? Array
68
- response[:item].each do |term|
67
+ if root_terms.empty?
68
+ response = request(:get_root_terms) { soap.body = { :ontologyName => ontology } }
69
+
70
+ if response[:item].is_a? Array
71
+ response[:item].each do |term|
72
+ root_terms.push( OLS::Term.new(term[:key],term[:value]) )
73
+ end
74
+ else
75
+ term = response[:item]
69
76
  root_terms.push( OLS::Term.new(term[:key],term[:value]) )
70
77
  end
71
- else
72
- term = response[:item]
73
- root_terms.push( OLS::Term.new(term[:key],term[:value]) )
74
78
  end
75
79
 
76
80
  root_terms
@@ -199,9 +203,7 @@ module OLS
199
203
  end
200
204
  end
201
205
 
202
- directory = File.expand_path(File.dirname(__FILE__))
203
-
204
- require File.join(directory, 'ols', 'version')
205
- require File.join(directory, 'ols', 'graph')
206
- require File.join(directory, 'ols', 'term')
207
- require File.join(directory, 'ols', 'cache')
206
+ require 'ols/version'
207
+ require 'ols/graph'
208
+ require 'ols/term'
209
+ require 'ols/cache'
@@ -25,7 +25,7 @@ module OLS
25
25
  found_term = nil
26
26
  filename = @term_id_to_files[term_id]
27
27
 
28
- unless filename.nil? || filename.empty?
28
+ unless filename.nil? || filename.to_s.empty?
29
29
  root_term = Marshal.load( @the_cache[filename] )
30
30
  found_term = root_term.send(:find_in_graph,term_id)
31
31
  end
@@ -33,6 +33,21 @@ module OLS
33
33
  found_term
34
34
  end
35
35
 
36
+ # Pull root_terms (as OLS::Term objects) out of the cache. Returns an empty array
37
+ # if the specified ontology is not in the cache.
38
+ #
39
+ # @param [String] ontology The ontology name
40
+ # @return [Array] An array of root terms
41
+ def root_terms(ontology)
42
+ root_terms = []
43
+
44
+ if @cached_ontologies.keys.include? ontology
45
+ root_terms = @cached_ontologies[ontology][:root_terms].map { |term_id| self.find_by_id(term_id) }
46
+ end
47
+
48
+ root_terms
49
+ end
50
+
36
51
  # Returns a list of the cached ontologies.
37
52
  #
38
53
  # @return [Array] A list of the cached ontologies
@@ -47,17 +62,20 @@ module OLS
47
62
  def add_ontology_to_cache(ontology)
48
63
  raise ArgumentError, "'#{ontology}' is not a valid OLS ontology" unless OLS.ontologies.include?(ontology)
49
64
 
50
- new_filenames = []
65
+ remove_ontology_from_cache(ontology) if @cached_ontologies.has_key?(ontology)
51
66
 
52
67
  OLS.root_terms(ontology).each do |term|
53
- term_filename = "#{term.term_id.gsub(':','')}.marshal"
54
68
  term.focus_graph!
69
+ term.send(:get_term_metadata)
70
+ term.all_children.each { |child| child.send(:get_term_metadata) }
71
+
72
+ term_filename = "#{term.term_id.gsub(':','')}.marshal"
55
73
  File.open("#{@cache_directory}/#{term_filename}",'w') { |f| f << Marshal.dump(term) }
56
- @cached_ontologies[ontology] ||= { :filenames => [], :date => Date.today }
74
+
75
+ @cached_ontologies[ontology] ||= { :root_terms => [], :filenames => [], :date => Date.today }
76
+ @cached_ontologies[ontology][:root_terms].push(term.term_id) unless @cached_ontologies[ontology][:root_terms].include? term.term_id
57
77
  @cached_ontologies[ontology][:filenames].push(term_filename) unless @cached_ontologies[ontology][:filenames].include? term_filename
58
- new_filenames.push(term_filename)
59
78
  end
60
- @cached_ontologies[ontology][:filenames].delete_if { |file| !new_filenames.include?(file) }
61
79
 
62
80
  write_cached_ontologies_to_disk
63
81
  prepare_cache
@@ -95,7 +113,7 @@ module OLS
95
113
  @term_id_to_files = {}
96
114
  @the_cache = {}
97
115
 
98
- Dir.mkdir(@cache_directory) unless Dir.exists?(@cache_directory)
116
+ Dir.mkdir(@cache_directory) unless File.directory?(@cache_directory)
99
117
 
100
118
  @cached_ontologies = YAML.load( File.open("#{@cache_directory}/cached_ontologies.yaml") ) if File.exists?("#{@cache_directory}/cached_ontologies.yaml")
101
119
 
@@ -93,12 +93,18 @@ module OLS
93
93
  #
94
94
  # Depth:: Length of the terms path to its root. Depth of a root term is zero.
95
95
  #
96
+ # If this term has multiple parents (and more than one route through the graph to
97
+ # the root node) we will return the value for the shortest distance through the graph.
98
+ #
96
99
  # @return [Number] Depth of this node.
97
- def level
100
+ # @since 0.3.4
101
+ def depth
98
102
  return 0 if self.is_root?
99
- 1 + parents.first.level
103
+ 1 + parents.map(&:depth).sort.first
100
104
  end
101
105
 
106
+ alias :level :depth
107
+
102
108
  # Returns the root term for this ontology.
103
109
  #
104
110
  # @return [OLS::Term] The root term for this ontology
@@ -166,17 +172,17 @@ module OLS
166
172
  prev_parents = prev_parents.map(&:parents).flatten
167
173
  end
168
174
 
169
- parentage_array.reverse.flatten
175
+ parentage_array.reverse.flatten.uniq
170
176
  end
171
177
 
172
178
  # Returns an array of all parent term_ids for this ontology term
173
179
  # (all the way to the top of the ontology). The array is ordered
174
- # with the root term first and the most direct parent(s) last.
180
+ # with the root term first and the most direct parent(s) last.
175
181
  # Duplicates are also filtered out.
176
182
  #
177
183
  # @return [Array] An array of ontology term_ids
178
184
  def all_parent_ids
179
- all_parents.map(&:term_id).uniq
185
+ all_parents.map(&:term_id)
180
186
  end
181
187
 
182
188
  # Returns an array of all parent term_names for this ontology term
@@ -186,7 +192,7 @@ module OLS
186
192
  #
187
193
  # @return [Array] An array of ontology term_names
188
194
  def all_parent_names
189
- all_parents.map(&:term_name).uniq
195
+ all_parents.map(&:term_name)
190
196
  end
191
197
 
192
198
  alias :all_parent_term_ids :all_parent_ids
@@ -269,7 +275,7 @@ module OLS
269
275
  prev_children = prev_children.map(&:children).flatten
270
276
  end
271
277
 
272
- children_array.flatten
278
+ children_array.flatten.uniq
273
279
  end
274
280
 
275
281
  # Returns an array of all child term_ids for this ontology term
@@ -311,13 +317,13 @@ module OLS
311
317
 
312
318
  # Flesh out and/or focus the ontology graph around this term.
313
319
  #
314
- # This will fetch all children and parents for this term, and will also trick each
315
- # parent/child object into thinking the ontology graph is fully formed so no further
316
- # requests to OLS will be made (to further flesh out the graph). It will also cut down
320
+ # This will fetch all children and parents for this term, and will also trick each
321
+ # parent/child object into thinking the ontology graph is fully formed so no further
322
+ # requests to OLS will be made (to further flesh out the graph). It will also cut down
317
323
  # a much larger ontology graph to just focus on the parents/descendants of this term.
318
324
  #
319
- # *NOTE:* This method will totally clobber the existing ontology graph that this term
320
- # is part of by removing any term and relationship that is not supposed to be part of this
325
+ # *NOTE:* This method will totally clobber the existing ontology graph that this term
326
+ # is part of by removing any term and relationship that is not supposed to be part of this
321
327
  # focused graph. Use #focus_graph to pull out a copy and be non-destructive.
322
328
  #
323
329
  # i.e. This allows us to do the following
@@ -356,13 +362,13 @@ module OLS
356
362
 
357
363
  # Flesh out and/or focus the ontology graph around this term.
358
364
  #
359
- # This will fetch all children and parents for this term, and will also trick each
360
- # parent/child object into thinking the ontology graph is fully formed so no further
361
- # requests to OLS will be made (to further flesh out the graph). It will also cut down
365
+ # This will fetch all children and parents for this term, and will also trick each
366
+ # parent/child object into thinking the ontology graph is fully formed so no further
367
+ # requests to OLS will be made (to further flesh out the graph). It will also cut down
362
368
  # a much larger ontology graph to just focus on the parents/descendants of this term.
363
369
  #
364
- # *NOTE:* This method does not affect self. It returns a completley new OLS::Term
365
- # object containing a completley new internal OLS::Graph. Use #focus_graph! if you
370
+ # *NOTE:* This method does not affect self. It returns a completley new OLS::Term
371
+ # object containing a completley new internal OLS::Graph. Use #focus_graph! if you
366
372
  # wish to cut down the existing graph.
367
373
  #
368
374
  # i.e. This allows us to do the following
@@ -513,7 +519,7 @@ module OLS
513
519
  write_dot_and_image_file(dotfile,imgfile,fmt,nodes,node_ranks,edges)
514
520
  end
515
521
 
516
- # Image drawing utility function. This is responsible for writing the DOT
522
+ # Image drawing utility function. This is responsible for writing the DOT
517
523
  # source file and converting it into the desired image format.
518
524
  #
519
525
  # @param [String] dotfile The DOT filename
@@ -2,5 +2,5 @@
2
2
 
3
3
  module OLS
4
4
  # OLS::VERSION - The OLS gem version
5
- VERSION = "0.3.3"
5
+ VERSION = "0.3.4"
6
6
  end
@@ -1,5 +1,7 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
1
+ # encoding: utf-8
2
+
3
+ $LOAD_PATH << File.expand_path("../lib", __FILE__)
4
+
3
5
  require "ols/version"
4
6
 
5
7
  Gem::Specification.new do |s|
@@ -21,9 +23,9 @@ Gem::Specification.new do |s|
21
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
24
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
25
  s.require_paths = ["lib"]
24
-
26
+
25
27
  s.add_dependency "savon"
26
-
28
+
27
29
  s.add_development_dependency "rake"
28
30
  s.add_development_dependency "shoulda"
29
31
  s.add_development_dependency "vcr"
@@ -31,4 +33,6 @@ Gem::Specification.new do |s|
31
33
  s.add_development_dependency "simplecov"
32
34
  s.add_development_dependency "simplecov-rcov"
33
35
  s.add_development_dependency "mocha"
36
+ s.add_development_dependency "awesome_print"
37
+ s.add_development_dependency "pry"
34
38
  end
@@ -1,9 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
- # File: script/console
3
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
2
 
5
- libs = " -r irb/completion"
6
- libs << " -r #{File.dirname(__FILE__) + '/../lib/ols.rb'}"
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__) + '/../lib')
4
+
5
+ require 'bundler/setup'
6
+ require 'pry'
7
+ require 'ols'
8
+
9
+ puts "Welcome to the OLS console (note - you're using pry, not irb)"
10
+
11
+ Pry.config.prompt = proc { "ols> " }
12
+ Pry.start
7
13
 
8
- puts "Loading ols gem"
9
- exec "#{irb} #{libs} --simple-prompt"
Binary file
@@ -1,9 +1,13 @@
1
1
  ---
2
2
  EMAP:
3
+ :root_terms:
4
+ - EMAP:0
3
5
  :filenames:
4
6
  - EMAP0.marshal
5
- :date: 2011-11-07
7
+ :date: 2012-01-10
6
8
  MP:
9
+ :root_terms:
10
+ - MP:0000001
7
11
  :filenames:
8
12
  - MP0000001.marshal
9
- :date: 2011-11-07
13
+ :date: 2012-01-10
@@ -1,7 +1,7 @@
1
1
  # Add the lib directory to the search path
2
- $:.unshift( "#{File.expand_path(File.dirname(__FILE__))}/../lib" )
2
+ $LOAD_PATH.unshift( "#{File.expand_path(File.dirname(__FILE__))}/../lib" )
3
3
 
4
- require 'rubygems'
4
+ require 'bundler/setup'
5
5
 
6
6
  # Set-up SimpleCov (code coverage tool for Ruby 1.9)
7
7
  if /^1.9/ === RUBY_VERSION
@@ -25,11 +25,51 @@ if /^1.9/ === RUBY_VERSION
25
25
  end
26
26
  end
27
27
 
28
+ require 'test/unit'
28
29
  require 'shoulda'
29
30
  require 'vcr'
31
+ require 'webmock'
30
32
  require 'mocha'
33
+ require 'awesome_print'
31
34
  require 'ols'
32
35
 
36
+ # Backport some useful assertions from minitest
37
+ if /^1\.8/ === RUBY_VERSION
38
+ module Test::Unit::Assertions
39
+ def assert_output stdout = nil, stderr = nil
40
+ out, err = capture_io do
41
+ yield
42
+ end
43
+
44
+ x = assert_equal stdout, out, "In stdout" if stdout
45
+ y = assert_equal stderr, err, "In stderr" if stderr
46
+
47
+ (!stdout || x) && (!stderr || y)
48
+ end
49
+
50
+ def assert_silent
51
+ assert_output "", "" do
52
+ yield
53
+ end
54
+ end
55
+
56
+ def capture_io
57
+ require 'stringio'
58
+
59
+ orig_stdout, orig_stderr = $stdout, $stderr
60
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
61
+ $stdout, $stderr = captured_stdout, captured_stderr
62
+
63
+ yield
64
+
65
+ return captured_stdout.string, captured_stderr.string
66
+ ensure
67
+ $stdout = orig_stdout
68
+ $stderr = orig_stderr
69
+ end
70
+ end
71
+ end
72
+
33
73
  # Set-up VCR for mocking up web requests.
34
74
  VCR.config do |c|
35
75
  if /^1\.8/ === RUBY_VERSION
@@ -43,8 +83,10 @@ VCR.config do |c|
43
83
  c.stub_with :webmock
44
84
  c.ignore_localhost = true
45
85
  c.default_cassette_options = {
46
- :record => :new_episodes,
47
- :re_record_interval => 604800, # 7 days in seconds
86
+ # :record => :new_episodes,
87
+ # :re_record_interval => 5184000, # 60 days in seconds
88
+ :record => :none,
48
89
  :match_requests_on => [:uri, :method, :body]
49
90
  }
50
91
  end
92
+