unipept 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +26 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -10
- data/Gemfile.lock +35 -21
- data/README.md +6 -4
- data/Rakefile +11 -10
- data/VERSION +1 -1
- data/bin/peptfilter +2 -44
- data/bin/prot2pept +4 -49
- data/bin/unipept +2 -197
- data/bin/uniprot +4 -53
- data/lib/batch_iterator.rb +73 -0
- data/lib/batch_order.rb +20 -0
- data/lib/commands/peptfilter.rb +118 -0
- data/lib/commands/prot2pept.rb +61 -0
- data/lib/commands/unipept/api_runner.rb +199 -0
- data/lib/commands/unipept/config.rb +29 -0
- data/lib/commands/unipept/pept2lca.rb +12 -0
- data/lib/commands/unipept/pept2prot.rb +13 -0
- data/lib/{unipept/commands → commands/unipept}/pept2taxa.rb +7 -0
- data/lib/commands/unipept/taxa2lca.rb +18 -0
- data/lib/{unipept/commands → commands/unipept}/taxonomy.rb +3 -0
- data/lib/commands/unipept.rb +226 -0
- data/lib/commands/uniprot.rb +69 -0
- data/lib/commands.rb +10 -0
- data/lib/configuration.rb +45 -0
- data/lib/formatters.rb +252 -0
- data/lib/version.rb +3 -0
- data/test/commands/test_peptfilter.rb +170 -0
- data/test/commands/test_prot2pept.rb +82 -0
- data/test/commands/test_unipept.rb +37 -0
- data/test/commands/test_uniprot.rb +136 -0
- data/test/commands/unipept/test_api_runner.rb +486 -0
- data/test/commands/unipept/test_config.rb +64 -0
- data/test/commands/unipept/test_pept2lca.rb +40 -0
- data/test/commands/unipept/test_pept2prot.rb +39 -0
- data/test/commands/unipept/test_pept2taxa.rb +39 -0
- data/test/commands/unipept/test_taxa2lca.rb +39 -0
- data/test/commands/unipept/test_taxonomy.rb +37 -0
- data/test/helper.rb +69 -23
- data/test/test_bach_order.rb +57 -0
- data/test/test_base.rb +6 -0
- data/test/test_batch_iterator.rb +87 -0
- data/test/test_configuration.rb +43 -0
- data/test/test_formatters.rb +140 -0
- data/unipept.gemspec +55 -33
- metadata +62 -40
- data/lib/unipept/batch_order.rb +0 -28
- data/lib/unipept/commands/api_runner.rb +0 -239
- data/lib/unipept/commands/pept2lca.rb +0 -6
- data/lib/unipept/commands/pept2prot.rb +0 -20
- data/lib/unipept/commands/taxa2lca.rb +0 -12
- data/lib/unipept/commands.rb +0 -7
- data/lib/unipept/configuration.rb +0 -29
- data/lib/unipept/formatters.rb +0 -135
- data/lib/unipept/version.rb +0 -3
- data/lib/unipept.rb +0 -8
- data/test/test_unipept.rb +0 -7
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unipept
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toon Willems
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-
|
13
|
+
date: 2015-06-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: cri
|
@@ -41,75 +41,75 @@ dependencies:
|
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0.6'
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
|
-
name:
|
44
|
+
name: rake
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - "
|
47
|
+
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '
|
49
|
+
version: '0'
|
50
50
|
type: :development
|
51
51
|
prerelease: false
|
52
52
|
version_requirements: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
|
-
- - "
|
54
|
+
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: '
|
56
|
+
version: '0'
|
57
57
|
- !ruby/object:Gem::Dependency
|
58
|
-
name:
|
58
|
+
name: minitest
|
59
59
|
requirement: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
|
-
- - "
|
61
|
+
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: '
|
63
|
+
version: '0'
|
64
64
|
type: :development
|
65
65
|
prerelease: false
|
66
66
|
version_requirements: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
|
-
- - "
|
68
|
+
- - ">="
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
version: '
|
70
|
+
version: '0'
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
|
-
name:
|
72
|
+
name: rubocop
|
73
73
|
requirement: !ruby/object:Gem::Requirement
|
74
74
|
requirements:
|
75
|
-
- - "
|
75
|
+
- - ">="
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version: '
|
77
|
+
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
80
|
version_requirements: !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|
82
|
-
- - "
|
82
|
+
- - ">="
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
version: '
|
84
|
+
version: '0'
|
85
85
|
- !ruby/object:Gem::Dependency
|
86
86
|
name: jeweler
|
87
87
|
requirement: !ruby/object:Gem::Requirement
|
88
88
|
requirements:
|
89
|
-
- - "
|
89
|
+
- - ">="
|
90
90
|
- !ruby/object:Gem::Version
|
91
|
-
version: '
|
91
|
+
version: '0'
|
92
92
|
type: :development
|
93
93
|
prerelease: false
|
94
94
|
version_requirements: !ruby/object:Gem::Requirement
|
95
95
|
requirements:
|
96
|
-
- - "
|
96
|
+
- - ">="
|
97
97
|
- !ruby/object:Gem::Version
|
98
|
-
version: '
|
98
|
+
version: '0'
|
99
99
|
- !ruby/object:Gem::Dependency
|
100
|
-
name:
|
100
|
+
name: coveralls
|
101
101
|
requirement: !ruby/object:Gem::Requirement
|
102
102
|
requirements:
|
103
|
-
- - "
|
103
|
+
- - ">="
|
104
104
|
- !ruby/object:Gem::Version
|
105
|
-
version: '0
|
105
|
+
version: '0'
|
106
106
|
type: :development
|
107
107
|
prerelease: false
|
108
108
|
version_requirements: !ruby/object:Gem::Requirement
|
109
109
|
requirements:
|
110
|
-
- - "
|
110
|
+
- - ">="
|
111
111
|
- !ruby/object:Gem::Version
|
112
|
-
version: '0
|
112
|
+
version: '0'
|
113
113
|
description: |2
|
114
114
|
Command line interface to the Unipept (http://unipept.ugent.be) web services
|
115
115
|
(pept2lca, taxa2lca, pept2taxa and taxonomy) and some utility commands for
|
@@ -126,6 +126,8 @@ extra_rdoc_files:
|
|
126
126
|
- README.md
|
127
127
|
files:
|
128
128
|
- ".document"
|
129
|
+
- ".rubocop.yml"
|
130
|
+
- ".travis.yml"
|
129
131
|
- Gemfile
|
130
132
|
- Gemfile.lock
|
131
133
|
- LICENSE.txt
|
@@ -136,20 +138,40 @@ files:
|
|
136
138
|
- bin/prot2pept
|
137
139
|
- bin/unipept
|
138
140
|
- bin/uniprot
|
139
|
-
- lib/
|
140
|
-
- lib/
|
141
|
-
- lib/
|
142
|
-
- lib/
|
143
|
-
- lib/
|
144
|
-
- lib/
|
145
|
-
- lib/unipept/
|
146
|
-
- lib/unipept/
|
147
|
-
- lib/unipept/
|
148
|
-
- lib/unipept/
|
149
|
-
- lib/unipept/
|
150
|
-
- lib/unipept/
|
141
|
+
- lib/batch_iterator.rb
|
142
|
+
- lib/batch_order.rb
|
143
|
+
- lib/commands.rb
|
144
|
+
- lib/commands/peptfilter.rb
|
145
|
+
- lib/commands/prot2pept.rb
|
146
|
+
- lib/commands/unipept.rb
|
147
|
+
- lib/commands/unipept/api_runner.rb
|
148
|
+
- lib/commands/unipept/config.rb
|
149
|
+
- lib/commands/unipept/pept2lca.rb
|
150
|
+
- lib/commands/unipept/pept2prot.rb
|
151
|
+
- lib/commands/unipept/pept2taxa.rb
|
152
|
+
- lib/commands/unipept/taxa2lca.rb
|
153
|
+
- lib/commands/unipept/taxonomy.rb
|
154
|
+
- lib/commands/uniprot.rb
|
155
|
+
- lib/configuration.rb
|
156
|
+
- lib/formatters.rb
|
157
|
+
- lib/version.rb
|
158
|
+
- test/commands/test_peptfilter.rb
|
159
|
+
- test/commands/test_prot2pept.rb
|
160
|
+
- test/commands/test_unipept.rb
|
161
|
+
- test/commands/test_uniprot.rb
|
162
|
+
- test/commands/unipept/test_api_runner.rb
|
163
|
+
- test/commands/unipept/test_config.rb
|
164
|
+
- test/commands/unipept/test_pept2lca.rb
|
165
|
+
- test/commands/unipept/test_pept2prot.rb
|
166
|
+
- test/commands/unipept/test_pept2taxa.rb
|
167
|
+
- test/commands/unipept/test_taxa2lca.rb
|
168
|
+
- test/commands/unipept/test_taxonomy.rb
|
151
169
|
- test/helper.rb
|
152
|
-
- test/
|
170
|
+
- test/test_bach_order.rb
|
171
|
+
- test/test_base.rb
|
172
|
+
- test/test_batch_iterator.rb
|
173
|
+
- test/test_configuration.rb
|
174
|
+
- test/test_formatters.rb
|
153
175
|
- unipept.gemspec
|
154
176
|
homepage: https://github.com/unipept/unipept-cli/
|
155
177
|
licenses:
|
@@ -171,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
193
|
version: '0'
|
172
194
|
requirements: []
|
173
195
|
rubyforge_project:
|
174
|
-
rubygems_version: 2.
|
196
|
+
rubygems_version: 2.1.11
|
175
197
|
signing_key:
|
176
198
|
specification_version: 4
|
177
199
|
summary: Command line interface to Unipept web services.
|
data/lib/unipept/batch_order.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
module Unipept
|
2
|
-
|
3
|
-
class BatchOrder
|
4
|
-
|
5
|
-
attr_reader :order
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@order = {}
|
9
|
-
@current = 0
|
10
|
-
end
|
11
|
-
|
12
|
-
def wait(i, &block)
|
13
|
-
if i == @current
|
14
|
-
# start writing + those who have been waiting as well
|
15
|
-
block.call
|
16
|
-
@current += 1
|
17
|
-
while order[@current]
|
18
|
-
order.delete(@current).call
|
19
|
-
@current += 1
|
20
|
-
end
|
21
|
-
else
|
22
|
-
@order[i] = block
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
@@ -1,239 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
module Unipept::Commands
|
4
|
-
class ApiRunner < Cri::CommandRunner
|
5
|
-
def initialize(args, opts, cmd)
|
6
|
-
super
|
7
|
-
@configuration = Unipept::Configuration.new
|
8
|
-
set_configuration
|
9
|
-
|
10
|
-
@user_agent = 'Unipept CLI - unipept ' + Unipept::VERSION
|
11
|
-
|
12
|
-
@url = "#{@host}/api/v1/#{cmd.name}.json"
|
13
|
-
@message_url = "#{@host}/api/v1/messages.json"
|
14
|
-
end
|
15
|
-
|
16
|
-
def set_configuration
|
17
|
-
# find host in opts first
|
18
|
-
if options[:host]
|
19
|
-
host = options[:host]
|
20
|
-
else
|
21
|
-
host = @configuration['host']
|
22
|
-
end
|
23
|
-
|
24
|
-
# No host has been set?
|
25
|
-
if host.nil? || host.empty?
|
26
|
-
puts 'WARNING: no host has been set, you can set the host with `unipept config host http://localhost:3000/`'
|
27
|
-
exit 1
|
28
|
-
end
|
29
|
-
unless host.start_with? 'http://'
|
30
|
-
host = "http://#{host}"
|
31
|
-
end
|
32
|
-
|
33
|
-
@host = host
|
34
|
-
end
|
35
|
-
|
36
|
-
def input_iterator
|
37
|
-
# Argument over file input over stdin
|
38
|
-
if !arguments.empty?
|
39
|
-
arguments.each
|
40
|
-
else
|
41
|
-
if options[:input]
|
42
|
-
IO.foreach(options[:input])
|
43
|
-
else
|
44
|
-
STDIN.each_line
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def batch_size
|
50
|
-
100
|
51
|
-
end
|
52
|
-
|
53
|
-
def url_options(sub_part)
|
54
|
-
filter = options[:select] ? options[:select] : []
|
55
|
-
if filter.empty?
|
56
|
-
names = true
|
57
|
-
else
|
58
|
-
names = filter.any? { |f| /.*name.*/.match f }
|
59
|
-
end
|
60
|
-
{ input: sub_part,
|
61
|
-
equate_il: options[:equate],
|
62
|
-
extra: options[:all],
|
63
|
-
names: names
|
64
|
-
}
|
65
|
-
end
|
66
|
-
|
67
|
-
def get_server_message
|
68
|
-
return if options[:quiet]
|
69
|
-
return unless STDOUT.tty?
|
70
|
-
last_fetched = @configuration['last_fetch_date']
|
71
|
-
if last_fetched.nil? || (last_fetched + 60 * 60 * 24) < Time.now
|
72
|
-
version = Unipept::VERSION
|
73
|
-
resp = Typhoeus.get(@message_url, params: { version: version })
|
74
|
-
puts resp.body unless resp.body.chomp.empty?
|
75
|
-
@configuration['last_fetch_date'] = Time.now
|
76
|
-
@configuration.save
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def run
|
81
|
-
get_server_message
|
82
|
-
|
83
|
-
formatter = Unipept::Formatter.new_for_format(options[:format])
|
84
|
-
peptides = input_iterator
|
85
|
-
|
86
|
-
filter_list = options[:select] ? options[:select] : []
|
87
|
-
# Parse filter list: convert to regex and split on commas
|
88
|
-
filter_list = filter_list.map { |f| f.include?(',') ? f.split(',') : f }.flatten.map { |f| glob_to_regex(f) }
|
89
|
-
|
90
|
-
batch_order = Unipept::BatchOrder.new
|
91
|
-
|
92
|
-
printed_header = false
|
93
|
-
result = []
|
94
|
-
|
95
|
-
hydra = Typhoeus::Hydra.new(max_concurrency: 10)
|
96
|
-
num_req = 0
|
97
|
-
|
98
|
-
peptide_iterator(peptides) do |sub_division, i, fasta_input|
|
99
|
-
request = Typhoeus::Request.new(
|
100
|
-
@url,
|
101
|
-
method: :post,
|
102
|
-
body: url_options(sub_division),
|
103
|
-
accept_encoding: 'gzip',
|
104
|
-
headers: { 'User-Agent' => @user_agent }
|
105
|
-
)
|
106
|
-
request.on_complete do |resp|
|
107
|
-
if resp.success?
|
108
|
-
# if JSON parsing goes wrong
|
109
|
-
sub_result = JSON[resp.response_body] rescue []
|
110
|
-
sub_result = [sub_result] unless sub_result.is_a? Array
|
111
|
-
|
112
|
-
sub_result.map! { |r| r.select! { |k, _v| filter_list.any? { |f| f.match k } } } unless filter_list.empty?
|
113
|
-
|
114
|
-
if options[:xml]
|
115
|
-
result << sub_result
|
116
|
-
end
|
117
|
-
|
118
|
-
# wait till it's our turn to write
|
119
|
-
batch_order.wait(i) do
|
120
|
-
unless sub_result.empty?
|
121
|
-
unless printed_header
|
122
|
-
write_to_output formatter.header(sub_result, fasta_input)
|
123
|
-
printed_header = true
|
124
|
-
end
|
125
|
-
write_to_output formatter.format(sub_result, fasta_input)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
elsif resp.timed_out?
|
130
|
-
|
131
|
-
batch_order.wait(i) do
|
132
|
-
$stderr.puts 'request timed out, continuing anyway, but results might be incomplete'
|
133
|
-
save_error('request timed out, continuing anyway, but results might be incomplete')
|
134
|
-
end
|
135
|
-
|
136
|
-
elsif resp.code == 0
|
137
|
-
|
138
|
-
batch_order.wait(i) do
|
139
|
-
$stderr.puts 'could not get an http response, continuing anyway, but results might be incomplete'
|
140
|
-
save_error(resp.return_message)
|
141
|
-
end
|
142
|
-
|
143
|
-
else
|
144
|
-
|
145
|
-
batch_order.wait(i) do
|
146
|
-
$stderr.puts "received a non-successful http response #{resp.code}, continuing anyway, but results might be incomplete"
|
147
|
-
save_error("Got #{resp.code}: #{resp.response_body}\nRequest headers: #{resp.request.options}\nRequest body:\n#{resp.request.encoded_body}\n\n")
|
148
|
-
end
|
149
|
-
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
hydra.queue request
|
154
|
-
|
155
|
-
num_req += 1
|
156
|
-
if num_req % 200 == 0
|
157
|
-
hydra.run
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
hydra.run
|
162
|
-
|
163
|
-
begin
|
164
|
-
download_xml(result)
|
165
|
-
rescue
|
166
|
-
STDERR.puts 'Something went wrong while downloading xml information! please check the output'
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
def save_error(message)
|
171
|
-
path = File.expand_path(File.join(Dir.home, '.unipept', "unipept-#{Time.now.strftime('%F-%T')}.log"))
|
172
|
-
FileUtils.mkdir_p File.dirname(path)
|
173
|
-
File.open(path, 'w') do |f|
|
174
|
-
f.write message
|
175
|
-
end
|
176
|
-
$stderr.puts "API request failed! log can be found in #{path}"
|
177
|
-
end
|
178
|
-
|
179
|
-
def write_to_output(string)
|
180
|
-
if options[:output]
|
181
|
-
File.open(options[:output], 'a') do |f|
|
182
|
-
f.write string
|
183
|
-
end
|
184
|
-
else
|
185
|
-
puts string
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def download_xml(result)
|
190
|
-
if options[:xml]
|
191
|
-
File.open(options[:xml] + '.xml', 'wb') do |f|
|
192
|
-
f.write Typhoeus.get("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=#{result.first.map { |h| h['taxon_id'] }.join(',')}&retmode=xml").response_body
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
def peptide_iterator(peptides, &block)
|
198
|
-
first = peptides.next rescue return
|
199
|
-
if first.start_with? '>'
|
200
|
-
# FASTA MODE ENGAGED
|
201
|
-
fasta_header = first.chomp
|
202
|
-
peptides.each_slice(batch_size).with_index do |sub, i|
|
203
|
-
fasta_input = []
|
204
|
-
# Use a set so we don't ask data twice
|
205
|
-
newsub = Set.new
|
206
|
-
|
207
|
-
# Iterate to find fasta headers
|
208
|
-
sub.each do |s|
|
209
|
-
s.chomp!
|
210
|
-
if s.start_with? '>'
|
211
|
-
# Save the FASTA header when found
|
212
|
-
fasta_header = s
|
213
|
-
else
|
214
|
-
# Add the input pair to our input list
|
215
|
-
fasta_input << [fasta_header, s]
|
216
|
-
newsub << s
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
block.call(newsub.to_a, i, fasta_input)
|
221
|
-
end
|
222
|
-
else
|
223
|
-
# shame we have to be this explicit, but it appears to be the only way
|
224
|
-
Enumerator.new do |y|
|
225
|
-
y << first
|
226
|
-
loop do
|
227
|
-
y << peptides.next
|
228
|
-
end
|
229
|
-
end.each_slice(batch_size).with_index(&block)
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
private
|
234
|
-
|
235
|
-
def glob_to_regex(string)
|
236
|
-
/^#{string.gsub('*', '.*')}$/
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require_relative 'api_runner'
|
2
|
-
|
3
|
-
module Unipept::Commands
|
4
|
-
class Pept2prot < ApiRunner
|
5
|
-
def download_xml(result)
|
6
|
-
if options[:xml]
|
7
|
-
FileUtils.mkdir_p(options[:xml])
|
8
|
-
result.first.each do |prot|
|
9
|
-
File.open(options[:xml] + "/#{prot['uniprot_id']}.xml", "wb") do |f|
|
10
|
-
f.write Typhoeus.get("http://www.uniprot.org/uniprot/#{prot['uniprot_id']}.xml").response_body
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def batch_size
|
17
|
-
10
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/lib/unipept/commands.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
module Unipept
|
2
|
-
class Configuration
|
3
|
-
|
4
|
-
attr_reader :config
|
5
|
-
attr_reader :file_name
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@file_name = File.join(Dir.home, ".unipeptrc")
|
9
|
-
if !File.exists? file_name
|
10
|
-
@config = {}
|
11
|
-
else
|
12
|
-
@config = YAML.load_file file_name
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def save
|
17
|
-
File.open(file_name, 'w') { |f| f.write config.to_yaml }
|
18
|
-
end
|
19
|
-
|
20
|
-
def [](*args)
|
21
|
-
config.[](*args)
|
22
|
-
end
|
23
|
-
|
24
|
-
def []=(*args)
|
25
|
-
config.[]=(*args)
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
data/lib/unipept/formatters.rb
DELETED
@@ -1,135 +0,0 @@
|
|
1
|
-
module Unipept
|
2
|
-
class Formatter
|
3
|
-
|
4
|
-
def self.formatters
|
5
|
-
@@formatters ||= {}
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.new_for_format(format)
|
9
|
-
begin
|
10
|
-
formatters[format].new
|
11
|
-
rescue
|
12
|
-
formatters[self.default].new
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.register(format)
|
17
|
-
self.formatters[format.to_s] = self
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.available
|
21
|
-
self.formatters.keys
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.default
|
25
|
-
'csv'
|
26
|
-
end
|
27
|
-
|
28
|
-
def header(sample_data, fasta_mapper = nil)
|
29
|
-
""
|
30
|
-
end
|
31
|
-
|
32
|
-
# JSON formatted data goes in, something other comes out
|
33
|
-
def format(data, fasta_mapper = nil)
|
34
|
-
data
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class JSONFormatter < Formatter
|
39
|
-
require 'json'
|
40
|
-
register :json
|
41
|
-
|
42
|
-
def format(data, fasta_mapper = nil)
|
43
|
-
# TODO: add fasta header based on fasta_mapper information
|
44
|
-
data.to_json
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
class CSVFormatter < Formatter
|
49
|
-
require 'csv'
|
50
|
-
|
51
|
-
register :csv
|
52
|
-
|
53
|
-
def header(data, fasta_input = nil)
|
54
|
-
CSV.generate do |csv|
|
55
|
-
first = data.first
|
56
|
-
if first.kind_of? Array
|
57
|
-
first = first.first
|
58
|
-
end
|
59
|
-
if fasta_input
|
60
|
-
csv << (['fasta_header'] + first.keys).map(&:to_s) if first
|
61
|
-
else
|
62
|
-
csv << first.keys.map(&:to_s) if first
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def format(data, fasta_input = nil)
|
68
|
-
CSV.generate do |csv|
|
69
|
-
|
70
|
-
if fasta_input
|
71
|
-
# Process the output from {key1: value1, key2: value2, ...}
|
72
|
-
# to {value => {key1: value1, key2: value2, ...}}
|
73
|
-
data_dict = {}
|
74
|
-
data.each do |d|
|
75
|
-
data_dict[d.values.first.to_s] ||= []
|
76
|
-
data_dict[d.values.first.to_s] << d
|
77
|
-
end
|
78
|
-
|
79
|
-
# Iterate over the input
|
80
|
-
fasta_input.each do |input_pair|
|
81
|
-
fasta_header, id = input_pair
|
82
|
-
|
83
|
-
# Retrieve the corresponding API result (if any)
|
84
|
-
unless data_dict[id].nil?
|
85
|
-
data_dict[id].each do |r|
|
86
|
-
csv << ([fasta_header] + r.values).map { |v| v == "" ? nil : v }
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
else
|
92
|
-
|
93
|
-
data.each do |o|
|
94
|
-
csv << o.values.map { |v| v == "" ? nil : v }
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
class XMLFormatter < Formatter
|
103
|
-
|
104
|
-
# Monkey patch (do as to_xml, but saner)
|
105
|
-
|
106
|
-
class ::Object
|
107
|
-
def to_xml(name = nil)
|
108
|
-
name ? %{<#{name}>#{self.to_s}</#{name}>} : self.to_s
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
class ::Array
|
113
|
-
def to_xml(array_name = :array, item_name = :item)
|
114
|
-
%|<#{array_name} size="#{self.size}">| + self.map{|n|n.to_xml( :item )}.join+"</#{array_name}>"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
class ::Hash
|
119
|
-
def to_xml(name = nil)
|
120
|
-
data = to_a.map{|k,v|v.to_xml(k)}.join
|
121
|
-
name ? "<#{name}>#{data}</#{name}>" : data
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
register :xml
|
126
|
-
|
127
|
-
def format(data, fasta_mapper = nil)
|
128
|
-
# TODO: add fasta header based on fasta_mapper information
|
129
|
-
data.to_xml
|
130
|
-
end
|
131
|
-
|
132
|
-
end
|
133
|
-
|
134
|
-
|
135
|
-
end
|
data/lib/unipept/version.rb
DELETED
data/lib/unipept.rb
DELETED