solvebio 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. data/.bumpversion.cfg +6 -0
  2. data/.gitignore +5 -4
  3. data/.travis.yml +1 -1
  4. data/Gemfile +3 -0
  5. data/README.md +34 -34
  6. data/Rakefile +1 -18
  7. data/bin/solvebio.rb +14 -16
  8. data/installer +64 -0
  9. data/lib/solvebio.rb +50 -11
  10. data/lib/solvebio/acccount.rb +4 -0
  11. data/lib/solvebio/annotation.rb +11 -0
  12. data/lib/solvebio/api_operations.rb +147 -0
  13. data/lib/solvebio/api_resource.rb +32 -0
  14. data/lib/solvebio/cli.rb +75 -0
  15. data/lib/solvebio/cli/auth.rb +106 -0
  16. data/lib/solvebio/cli/credentials.rb +54 -0
  17. data/lib/{cli → solvebio/cli}/irb.rb +0 -23
  18. data/lib/solvebio/cli/irbrc.rb +48 -0
  19. data/lib/solvebio/cli/tutorial.rb +12 -0
  20. data/lib/solvebio/client.rb +149 -0
  21. data/lib/solvebio/dataset.rb +60 -0
  22. data/lib/solvebio/dataset_field.rb +12 -0
  23. data/lib/solvebio/depository.rb +38 -0
  24. data/lib/solvebio/depository_version.rb +40 -0
  25. data/lib/solvebio/errors.rb +64 -0
  26. data/lib/solvebio/filter.rb +315 -0
  27. data/lib/solvebio/list_object.rb +73 -0
  28. data/lib/solvebio/locale.rb +43 -0
  29. data/lib/solvebio/query.rb +341 -0
  30. data/lib/solvebio/sample.rb +54 -0
  31. data/lib/solvebio/singleton_api_resource.rb +25 -0
  32. data/lib/solvebio/solve_object.rb +164 -0
  33. data/lib/solvebio/tabulate.rb +589 -0
  34. data/lib/solvebio/user.rb +4 -0
  35. data/lib/solvebio/util.rb +59 -0
  36. data/lib/solvebio/version.rb +3 -0
  37. data/solvebio.gemspec +10 -18
  38. data/test/helper.rb +6 -2
  39. data/test/solvebio/data/.gitignore +1 -0
  40. data/test/solvebio/data/.netrc +6 -0
  41. data/test/{data → solvebio/data}/netrc-save +0 -0
  42. data/test/solvebio/data/sample.vcf.gz +0 -0
  43. data/test/solvebio/data/test_creds +3 -0
  44. data/test/solvebio/test_annotation.rb +45 -0
  45. data/test/solvebio/test_client.rb +29 -0
  46. data/test/solvebio/test_conversion.rb +14 -0
  47. data/test/solvebio/test_credentials.rb +67 -0
  48. data/test/solvebio/test_dataset.rb +52 -0
  49. data/test/solvebio/test_depository.rb +24 -0
  50. data/test/solvebio/test_depositoryversion.rb +22 -0
  51. data/test/solvebio/test_error.rb +31 -0
  52. data/test/solvebio/test_filter.rb +86 -0
  53. data/test/solvebio/test_query.rb +282 -0
  54. data/test/solvebio/test_query_batch.rb +38 -0
  55. data/test/solvebio/test_query_init.rb +30 -0
  56. data/test/solvebio/test_query_tabulate.rb +73 -0
  57. data/test/solvebio/test_ratelimit.rb +31 -0
  58. data/test/solvebio/test_resource.rb +29 -0
  59. data/test/solvebio/test_sample_access.rb +60 -0
  60. data/test/solvebio/test_sample_download.rb +20 -0
  61. data/test/solvebio/test_tabulate.rb +129 -0
  62. data/test/solvebio/test_util.rb +39 -0
  63. metadata +100 -85
  64. data/Makefile +0 -17
  65. data/demo/README.md +0 -14
  66. data/demo/cheatsheet.rb +0 -31
  67. data/demo/dataset/facets.rb +0 -13
  68. data/demo/dataset/field.rb +0 -13
  69. data/demo/depository/README.md +0 -24
  70. data/demo/depository/all.rb +0 -13
  71. data/demo/depository/retrieve.rb +0 -13
  72. data/demo/depository/versions-all.rb +0 -13
  73. data/demo/query/query-filter.rb +0 -30
  74. data/demo/query/query.rb +0 -13
  75. data/demo/query/range-filter.rb +0 -18
  76. data/demo/test-api.rb +0 -98
  77. data/lib/cli/auth.rb +0 -122
  78. data/lib/cli/help.rb +0 -13
  79. data/lib/cli/irbrc.rb +0 -54
  80. data/lib/cli/options.rb +0 -75
  81. data/lib/client.rb +0 -154
  82. data/lib/credentials.rb +0 -67
  83. data/lib/errors.rb +0 -81
  84. data/lib/filter.rb +0 -312
  85. data/lib/locale.rb +0 -47
  86. data/lib/main.rb +0 -46
  87. data/lib/query.rb +0 -414
  88. data/lib/resource/annotation.rb +0 -23
  89. data/lib/resource/apiresource.rb +0 -241
  90. data/lib/resource/dataset.rb +0 -91
  91. data/lib/resource/datasetfield.rb +0 -37
  92. data/lib/resource/depository.rb +0 -50
  93. data/lib/resource/depositoryversion.rb +0 -69
  94. data/lib/resource/main.rb +0 -123
  95. data/lib/resource/sample.rb +0 -75
  96. data/lib/resource/solveobject.rb +0 -122
  97. data/lib/resource/user.rb +0 -5
  98. data/lib/tabulate.rb +0 -706
  99. data/lib/util.rb +0 -29
  100. data/test/Makefile +0 -9
  101. data/test/data/sample.vcf.gz +0 -0
  102. data/test/test-annotation.rb +0 -46
  103. data/test/test-auth.rb +0 -58
  104. data/test/test-client.rb +0 -27
  105. data/test/test-conversion.rb +0 -13
  106. data/test/test-dataset.rb +0 -42
  107. data/test/test-depository.rb +0 -35
  108. data/test/test-error.rb +0 -36
  109. data/test/test-filter.rb +0 -70
  110. data/test/test-netrc.rb +0 -52
  111. data/test/test-query-batch.rb +0 -40
  112. data/test/test-query-init.rb +0 -29
  113. data/test/test-query-paging.rb +0 -102
  114. data/test/test-query.rb +0 -71
  115. data/test/test-resource.rb +0 -40
  116. data/test/test-sample-access.rb +0 -59
  117. data/test/test-sample-download.rb +0 -20
  118. data/test/test-tabulate.rb +0 -131
  119. data/test/test-util.rb +0 -42
@@ -0,0 +1,6 @@
1
+ [bumpversion]
2
+ current_version = 1.7.0
3
+ files = lib/solvebio/version.rb
4
+ commit = True
5
+ tag = False
6
+
data/.gitignore CHANGED
@@ -1,7 +1,8 @@
1
1
  *~
2
2
  .rvmrc
3
- /Gemfile.lock
4
- /pkg
5
- /solvebio-*.gem
6
- /test/data/.netrc
7
3
  Gemfile.lock
4
+ pkg
5
+ solvebio-*.gem
6
+ test/data/.netrc
7
+ Gemfile.lock
8
+ *.swp
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
  before_script:
3
3
  - gem install netrc --version=0.7.7
4
- - gem install rest_client addressable
4
+ - gem install rest_client addressable webmock
5
5
  - touch ~/.netrc
6
6
  - chmod 0600 ~/.netrc
7
7
  env:
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ source "https://rubygems.org"
4
4
  gem 'addressable'
5
5
  gem 'rest_client'
6
6
  gem 'netrc', '~>0.7.7' # rest_client needs netrc 0.7.7
7
+ group :test do
8
+ gem 'webmock'
9
+ end
data/README.md CHANGED
@@ -1,35 +1,49 @@
1
1
  [![Build Status](https://travis-ci.org/solvebio/solvebio-ruby.svg)](https://travis-ci.org/solvebio/solvebio-ruby)
2
2
 
3
- # SolveBio Ruby Client
3
+ SolveBio Ruby Client
4
+ ====================
4
5
 
6
+ This is the SolveBio Ruby module and command-line interface (CLI).
5
7
 
6
- This packages provides a command-line interface (CLI) and Ruby API interface to SolveBio.
8
+ For more information about SolveBio, see [solvebio.com](https://www.solvebio.com).
7
9
 
8
- For more information about the SolveBio API, https://www.solvebio.com/docs/api/
9
- For more information about SolveBio, see https://www.solvebio.com
10
10
 
11
- # Installation
11
+ Guided Installation
12
+ -------------------
12
13
 
13
- gem install solvebio # may need sudo
14
+ To use the guided installer, open up your terminal and paste this:
14
15
 
15
- Installing from git:
16
+ curl -kL install.solvebio.com/ruby | bash
16
17
 
17
- git clone https://github.com/solvebio/solvebio-ruby.git
18
- cd solvebio-ruby
19
- gem install netrc # install gem dependencies, may need sudo
20
- rake test # or "make test"
21
- sudo rake install # or "make install"
22
18
 
23
- This also builds a *solvebio* gem which you can use elsewhere.
19
+ Manual Installation
20
+ -------------------
21
+
22
+ sudo gem install solvebio
23
+
24
24
 
25
- But note, you can also run right inside the git repository without installing anything. For example, running:
25
+ Installing from Git
26
+ -------------------
27
+
28
+ First, install dependencies:
29
+
30
+ gem install rest_client
31
+ gem install addressable
32
+ gem install netrc
33
+
34
+ Install from source:
26
35
 
27
36
  git clone https://github.com/solvebio/solvebio-ruby.git
28
- solvebio-ruby/bin/solvebio.rb
37
+ cd solvebio-ruby
38
+ rake test
39
+ sudo rake install
40
+
29
41
 
30
- will get you into a solvebio irb shell. Just about any Ruby file in the project can be run standalone, and when done so, it demos that Ruby code.
42
+ This will install the `solvebio` gem and the `solvebio.rb` CLI.
31
43
 
32
- # Optional Gem dependencies
44
+
45
+ Optional Dependencies
46
+ ---------------------
33
47
 
34
48
  The following optional gems can make your shell experience better
35
49
 
@@ -45,22 +59,8 @@ The following optional gems can make your shell experience better
45
59
 
46
60
  Localization for number formatting
47
61
 
48
- # Demo code and Documentation
49
-
50
- See the [demo folder](https://github.com/solvebio/solvebio-ruby/tree/master/demo) for ready-to-run examples.
51
-
52
- The online [SolveBio Python API documentation](https://www.solvebio.com/docs/api/?python) has additional examples. To use these with the Ruby client, change `import solvebio` to `require 'solvebio'` and change `solvebio.` to `SolveBio::` everywhere it occurs.
53
-
54
- e.g., a Python example:
55
-
56
- solvebio.Depository.retrieve("ClinVar").versions()
57
-
58
- the same Python example ported to Ruby:
59
-
60
- SolveBio::Depository.retrieve("ClinVar").versions()
61
-
62
- As with any other Ruby method call, you can drop the final parenthesis if you like.
63
62
 
64
- # To Do
63
+ Documentation
64
+ -------------
65
65
 
66
- This hasn't been extensively field tested yet.
66
+ See the [SolveBio API Reference](https://www.solvebio.com/docs/api/?ruby) for more information about using the API.
data/Rakefile CHANGED
@@ -10,12 +10,10 @@ end
10
10
 
11
11
  require 'rubygems/package_task'
12
12
 
13
- desc "Validate the gemspec"
14
13
  task :gemspec do
15
14
  gemspec.validate
16
15
  end
17
16
 
18
- desc "Build the gem"
19
17
  task :package=>:gem
20
18
  task :gem=>:gemspec do
21
19
  Dir.chdir(ROOT_DIR) do
@@ -25,7 +23,6 @@ task :gem=>:gemspec do
25
23
  end
26
24
  end
27
25
 
28
- desc "Install the gem locally"
29
26
  task :install => :gem do
30
27
  Dir.chdir(ROOT_DIR) do
31
28
  sh %{gem install --both pkg/#{gemspec.file_name}}
@@ -33,27 +30,13 @@ task :install => :gem do
33
30
  end
34
31
 
35
32
  require 'rake/testtask'
36
- desc "Test everything."
37
33
  Rake::TestTask.new(:test) do |t|
38
34
  t.libs << './lib'
39
- t.test_files = FileList['test/test-*.rb']
35
+ t.pattern = './test/**/test*.rb'
40
36
  t.verbose = true
41
37
  end
42
38
 
43
-
44
- require 'rbconfig'
45
- RUBY_PATH = File.join(RbConfig::CONFIG['bindir'],
46
- RbConfig::CONFIG['RUBY_INSTALL_NAME'])
47
- desc "Run all of the demo files."
48
- task :'run-demo' do
49
- FileList['demo/*.rb']+FileList['demo/*/*.rb'].each do |ruby_file|
50
- puts(('-' * 20) + ' ' + ruby_file + ' ' + ('-' * 20))
51
- system(RUBY_PATH, ruby_file)
52
- end
53
- end
54
-
55
39
  task :test => :lib
56
40
 
57
- desc "same as test"
58
41
  task :check => :test
59
42
  task :default => :test
@@ -1,36 +1,34 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
- # SolveBio Ruby command-line program
5
-
6
- require_relative '../lib/solvebio'
7
- require_relative '../lib/cli/options'
8
- require_relative '../lib/cli/irb'
4
+ require 'solvebio'
5
+ require 'solvebio/cli'
9
6
 
10
7
  DIR = File.dirname(__FILE__)
11
- TEST_PROGRAM = File.join(DIR, %w(.. demo test-api.rb))
12
-
13
- include SolveBio::CLIOptions
14
- options, rest, parser = process_options(ARGV)
15
8
 
16
- rest = ['shell'] if rest.empty?
9
+ include SolveBio
10
+ include SolveBio::CLI
11
+ include SolveBio::CLI::Auth
12
+ include SolveBio::CLI::Credentials
13
+ include SolveBio::CLI::Tutorial
17
14
 
18
- include SolveBio::Auth
15
+ options, argv = process_options(ARGV)
16
+ argv = ['shell'] if argv.empty?
19
17
 
20
- cmd = rest.shift
18
+ cmd = argv.shift
21
19
  case cmd
22
20
  when 'shell'
23
21
  IRB::shell
24
22
  when 'login'
25
- login *rest
23
+ login
26
24
  when 'logout'
27
25
  logout
28
26
  when 'whoami'
29
27
  whoami
30
- when 'test'
31
- system(TEST_PROGRAM)
28
+ when 'tutorial'
29
+ tutorial
32
30
  else
33
- $stderr.puts "Unknown solvbio.rb command: #{cmd}"
31
+ $stderr.puts "Unknown subcommand command: #{cmd}"
34
32
  $stderr.puts parser
35
33
  exit 1
36
34
  end
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env bash
2
+ #
3
+ # This is the guided installer for the SolveBio Ruby Client:
4
+ #
5
+ # curl -kL install.solve.bio/ruby | bash
6
+ #
7
+
8
+ echo
9
+ echo " ____ _ ____ _"
10
+ echo " / ___| ___ | |_ _____| __ )(_) ___"
11
+ echo " \___ \ / _ \| \ \ / / _ \ _ \| |/ _ \\"
12
+ echo " ___) | (_) | |\ V / __/ |_) | | (_) |"
13
+ echo " |____/ \___/|_| \_/ \___|____/|_|\___/"
14
+ echo
15
+ echo " Copyright © 2014 Solve, Inc. <https://www.solvebio.com>. All rights reserved."
16
+ echo
17
+
18
+ shopt -s extglob
19
+
20
+ function fail_exit() {
21
+ echo
22
+ echo " #####################################"
23
+ echo
24
+ echo " Failed to install SolveBio."
25
+ echo " Contact us at support@solvebio.com"
26
+ echo
27
+ echo " #####################################"
28
+ echo
29
+ exit
30
+ }
31
+
32
+ trap ctrl_c INT
33
+
34
+ function ctrl_c() {
35
+ echo
36
+ echo
37
+ echo " Installation was aborted..."
38
+ fail_exit
39
+ }
40
+
41
+ echo " Installing SolveBio for Ruby..."
42
+ echo
43
+ echo " IMPORTANT: Your computer's password may be required. It will NOT be sent to SolveBio."
44
+
45
+ # Are we installing or upgrading?
46
+ if [ $(gem list | grep solvebio -c) -ne 0 ]; then
47
+ sudo gem update -q solvebio 2>&1 > /dev/null
48
+ else
49
+ sudo gem install -q solvebio 2>&1 > /dev/null
50
+ fi
51
+
52
+ if [ $? -eq 0 ]; then
53
+ echo
54
+ echo " ##############################################"
55
+ echo
56
+ echo " Success! SolveBio for Ruby is now installed."
57
+ echo
58
+ echo " Please run 'solvebio.rb login' to finish the setup."
59
+ echo
60
+ echo " ##############################################"
61
+ echo
62
+ else
63
+ fail_exit
64
+ fi
@@ -1,14 +1,53 @@
1
- # -*- coding: utf-8 -*-
2
- # Something to pull in the entire SolveBio API.
1
+ # SolveBio Ruby bindings
2
+ # Learn more at https://www.solvebio.com/docs/api
3
+ require 'logger'
4
+ require 'fileutils'
5
+ require 'json'
6
+ require 'logger'
7
+ require 'netrc'
8
+ require 'openssl'
9
+ require 'rest_client'
10
+ require 'uri'
11
+ require 'addressable/uri'
12
+ require 'set'
3
13
 
4
- require_relative 'resource/main'
5
- require_relative 'query'
14
+ require 'solvebio/version'
15
+ require 'solvebio/util'
16
+ require 'solvebio/tabulate'
17
+ require 'solvebio/solve_object'
18
+ require 'solvebio/api_resource'
19
+ require 'solvebio/singleton_api_resource'
20
+ require 'solvebio/list_object'
21
+ require 'solvebio/client'
22
+ require 'solvebio/api_operations'
23
+ require 'solvebio/locale'
24
+ require 'solvebio/query'
25
+ require 'solvebio/filter'
26
+ require 'solvebio/acccount'
27
+ require 'solvebio/annotation'
28
+ require 'solvebio/dataset'
29
+ require 'solvebio/dataset_field'
30
+ require 'solvebio/depository'
31
+ require 'solvebio/depository_version'
32
+ require 'solvebio/sample'
33
+ require 'solvebio/user'
34
+ require 'solvebio/errors'
6
35
 
7
- # cli/auth is a little nicer than credentials
8
- # FIXME: consider moving cli/auth moving out of cli?
9
- require_relative 'cli/auth'
36
+ module SolveBio
37
+ class << self
38
+ attr_accessor :api_key, :api_host, :logger
39
+ end
10
40
 
11
- # Set authentication if possible
12
- include SolveBio::Credentials
13
- creds = get_credentials()
14
- SolveBio.api_key = SolveBio::Client.client.api_key = creds[1] if creds
41
+ @api_key = ENV['SOLVEBIO_API_KEY']
42
+ @api_host = ENV['SOLVEBIO_API_HOST'] || 'https://api.solvebio.com'
43
+
44
+ logfile =
45
+ if ENV['SOLVEBIO_LOGFILE']
46
+ ENV['SOLVEBIO_LOGFILE']
47
+ else
48
+ dir = File::expand_path '~/.solvebio'
49
+ FileUtils.mkdir_p(dir) unless File.exist? dir
50
+ File::expand_path File.join(dir, 'solvebio.log')
51
+ end
52
+ @logger = Logger.new(logfile)
53
+ end
@@ -0,0 +1,4 @@
1
+ module SolveBio
2
+ class Account < SingletonAPIResource
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ # Annotations are genomic samples that have been annotated.
2
+ # See https://www.solvebio.com/docs/api/?ruby#annotations
3
+ module SolveBio
4
+ class Annotation < APIResource
5
+ include SolveBio::APIOperations::Create
6
+ include SolveBio::APIOperations::Update
7
+ include SolveBio::APIOperations::Download
8
+ include SolveBio::APIOperations::List
9
+ include SolveBio::APIOperations::Delete
10
+ end
11
+ end
@@ -0,0 +1,147 @@
1
+ module SolveBio
2
+ module APIOperations
3
+ module Create
4
+ module ClassMethods
5
+ def create(params={})
6
+ response = Client.request('post', url, {:payload => params} )
7
+ Util.to_solve_object(response)
8
+ end
9
+ end
10
+
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+ end
15
+
16
+ module Delete
17
+ def delete(params={})
18
+ response = Client.request('delete', url, params)
19
+ refresh_from(response)
20
+ end
21
+ end
22
+
23
+ module Update
24
+ def save
25
+ refresh_from(request('patch', url,
26
+ {:payload => serialize(self)}))
27
+ return self
28
+ end
29
+
30
+ def serialize(obj)
31
+ params = {}
32
+ if obj.unsaved_values
33
+ obj.unsaved_values.each do |k|
34
+ next if k == 'id'
35
+ params[k] = getattr(obj, k) or ''
36
+ end
37
+ end
38
+ params
39
+ end
40
+ end
41
+
42
+ module Download
43
+ def download(path=nil)
44
+ download_url = url + '/download'
45
+ response = Client.get(download_url, :raw => true)
46
+
47
+ if response.code != 302
48
+ # Some kind of error. We expect a redirect
49
+ raise SolveError('Could not download file: response code' %
50
+ response.status_code)
51
+ end
52
+
53
+ download_url = response.headers[:location]
54
+ filename = download_url.split('%3B%20filename%3D')[1]
55
+
56
+ path = Dir.tmpdir unless path
57
+ filename = File.join(path, filename)
58
+ response = nil
59
+
60
+ response = Client.get(download_url, :raw => true,
61
+ :default_headers => false)
62
+
63
+ File.open(filename, 'wb') do |fh|
64
+ fh.write(response.body)
65
+ end
66
+
67
+ self['filename'] = filename
68
+ self['code'] = response.code
69
+ self
70
+ end
71
+ end
72
+
73
+ module Help
74
+ attr_reader :have_launchy
75
+
76
+ @@have_launchy = false
77
+ begin
78
+ @@have_launchy = require 'launchy'
79
+ rescue LoadError
80
+ end
81
+
82
+ def self.included base
83
+ base.send :include, InstanceMethods
84
+ end
85
+
86
+ module InstanceMethods
87
+ def help
88
+ open_help(self['full_name'])
89
+ end
90
+ end
91
+
92
+ def open_help(path)
93
+ url = URI::join('https://www.solvebio.com/', path)
94
+ if @@have_launchy
95
+ Launchy.open(url)
96
+ else
97
+ puts('The SolveBio Ruby client needs the "launchy" gem to ' +
98
+ "open help url: #{url.to_s}")
99
+ end
100
+ end
101
+ end
102
+
103
+ module List
104
+ module ClassMethods
105
+ def all(params={})
106
+ response = Client.get(url, {:params => params})
107
+ Util.to_solve_object(response)
108
+ end
109
+ end
110
+
111
+ def self.included base
112
+ base.extend ClassMethods
113
+ end
114
+
115
+ def to_s
116
+ if self.class.constants.member?(:TAB_FIELDS)
117
+ items = self.class.const_get(:TAB_FIELDS).map{
118
+ |name| [name, self[name]]
119
+ }
120
+ else
121
+ items = self
122
+ end
123
+ return Tabulate.tabulate(items, ['Fields', 'Data'],
124
+ ['right', 'left'], true)
125
+ end
126
+
127
+ def size
128
+ self[:total]
129
+ end
130
+ alias :total :size
131
+ end
132
+
133
+ module Search
134
+ module ClassMethods
135
+ def search(query='', params={})
136
+ params['q'] = query
137
+ response = Client.get(url, {:params => params})
138
+ Util.to_solve_object(response)
139
+ end
140
+ end
141
+
142
+ def self.included base
143
+ base.extend ClassMethods
144
+ end
145
+ end
146
+ end
147
+ end