Skip to content

Commit

Permalink
Fixed Rubocop warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pverscha committed Aug 14, 2019
1 parent 4fe4a60 commit 1083a56
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Metrics/MethodLength:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Naming/HeredocDelimiterNaming:
Enabled: false
Naming/UncommunicativeMethodParamName:
Expand Down
22 changes: 10 additions & 12 deletions lib/commands/unipept/api_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ def handle_failed_response(response)
def filter_result(json_response)
result = JSON[json_response] rescue []
result = [result] unless result.is_a? Array
key_order = result.first.keys
result = flatten_functional_fields(result) if formatter.instance_of? CSVFormatter
key_order = result.first.keys if result.first
result = flatten_functional_fields(result) if formatter.instance_of?(Unipept::CSVFormatter)
result.map! { |r| r.select! { |k, _v| selected_fields.any? { |f| f.match k } } } unless selected_fields.empty?
result = inflate_functional_fields(result, key_order) if formatter.instance_of? CSVFormatter
result = inflate_functional_fields(result, key_order) if formatter.instance_of?(Unipept::CSVFormatter) && result.first
result
end

Expand All @@ -208,10 +208,8 @@ def flatten_functional_fields(data)
if %w[ec go].include? k
v.each do |item|
item.each do |field_name, field_value|
new_field_name = (%w[ec_number go_term].include? field_name) ? field_name : k + '_' + field_name
if !(output_row.key? new_field_name)
output_row[new_field_name] = []
end
new_field_name = %w[ec_number go_term].include?(field_name) ? field_name : k + '_' + field_name
output_row[new_field_name] = [] unless output_row.key? new_field_name
output_row[new_field_name] << field_value
end
end
Expand All @@ -221,7 +219,7 @@ def flatten_functional_fields(data)
end
output << output_row
end
return output
output
end

# Transforms a flattened input created by flatten_functional_fields to the original
Expand All @@ -237,14 +235,14 @@ def inflate_functional_fields(data, original_key_order)
# First, we take all distinct keys that start with "ec" or "go"
annotation_keys = row.keys.select { |key| key.start_with? original_key }
processed_keys += annotation_keys
if annotation_keys.length > 0
unless annotation_keys.empty?
# Each of the values of the annotation_keys is an array. All respective values of each of
# these arrays need to be put together into one hash. (E.g. {a => [1, 2], b=> [x, y]} --> [{a: 1, b: x}, {a: 2, b: y}])
reconstructed_objects = []
for i in 0..annotation_keys[0].length
(0..annotation_keys[0].length).each do |i|
reconstructed_object = {}
annotation_keys.each do |annotation_key|
reconstructed_object[(%w[ec_number go_term].include? annotation_key) ? annotation_key : annotation_key[3, annotation_key.length]] = row[annotation_key][i]
reconstructed_object[%w[ec_number go_term].include?(annotation_key) ? annotation_key : annotation_key[3, annotation_key.length]] = row[annotation_key][i]
end
reconstructed_objects << reconstructed_object
end
Expand All @@ -257,7 +255,7 @@ def inflate_functional_fields(data, original_key_order)

output << output_row
end
return output
output
end

def glob_to_regex(string)
Expand Down
36 changes: 17 additions & 19 deletions lib/formatters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,36 +185,34 @@ def type
def header(data, fasta_mapper = nil)
# This global variable is necessary because we need to know how many items should be
# nil in the convert function.
$keys_length = 0
$keys_length = 0 # rubocop:disable Style/GlobalVars
# This array keeps track of items that are certainly filled in for each type of annotation
non_empty_items = {"ec" => nil, "go" => nil}
non_empty_items = { 'ec' => nil, 'go' => nil }

# First we look for items for both ec numbers and go terms that are fully filled in.
data.each do |row|
non_empty_items.keys.each do |annotation_type|
if row[annotation_type] && row[annotation_type].length > 0
non_empty_items[annotation_type] = row
end
non_empty_items[annotation_type] = row if row[annotation_type] && !row[annotation_type].empty?
end
end

CSV.generate do |csv|
keys = fasta_mapper ? ['fasta_header'] : []
processed_keys = []

non_empty_items.each do |annotation_type, non_empty_item|
if non_empty_item
keys += (non_empty_item.keys - processed_keys)
processed_keys += non_empty_item.keys

idx = keys.index(annotation_type)
keys.delete_at(idx)
keys.insert(idx, *non_empty_item[annotation_type].first.keys.map { |el| (%w[ec_number go_term].include? el) ? el: annotation_type + '_' + el })
$keys_length = *non_empty_item[annotation_type].first.keys.length
end
next unless non_empty_item

keys += (non_empty_item.keys - processed_keys)
processed_keys += non_empty_item.keys

idx = keys.index(annotation_type)
keys.delete_at(idx)
keys.insert(idx, *non_empty_item[annotation_type].first.keys.map { |el| %w[ec_number go_term].include?(el) ? el : annotation_type + '_' + el })
$keys_length = *non_empty_item[annotation_type].first.keys.length # rubocop:disable Style/GlobalVars
end

csv << keys.map(&:to_s) if non_empty_items.values.any? {|item| item != nil}
csv << keys.map(&:to_s) if non_empty_items.values.any? { |item| !item.nil? }
end
end

Expand All @@ -235,18 +233,18 @@ def convert(data, _first)
row = []
o.each do |k, v|
if %w[ec go].include? k
if v && v.length > 0
if v && !v.empty?
v.first.keys.each do |key|
row << (v.map { |el| el[key] }).join(' ')
end
else
row = row.concat(Array.new($keys_length[0], nil))
row = row.concat(Array.new($keys_length[0], nil)) # rubocop:disable Style/GlobalVars
end
else
row << (v == '' ? nil : v)
end
end
csv << row if $keys_length[0] > 0
csv << row if $keys_length[0].positive? # rubocop:disable Style/GlobalVars
end
end
end
Expand Down

0 comments on commit 1083a56

Please sign in to comment.