Class: Unipept::Formatter
Direct Known Subclasses
BlastFormatter, CSVFormatter, HtmlFormatter, JSONFormatter, UrlFormatter, XMLFormatter
Class Method Summary collapse
-
.available ⇒ Array<String>
Returns a list of the available formatters.
-
.default ⇒ String
The type of the default formatter: csv.
-
.formatters ⇒ Hash
The Hash of available formatters.
- .hidden? ⇒ Boolean
-
.new_for_format(format) ⇒ Formatter
Returns a new formatter of the given format.
-
.register(format) ⇒ Object
Adds a new formatter to the list of available formats.
Instance Method Summary collapse
-
#convert(_data, _first) ⇒ String
Converts the given input data to another format.
-
#footer ⇒ String
Returns the footer row.
-
#format(data, fasta_mapper, first) ⇒ String
Converts the given input data and corresponding fasta headers to another format.
-
#group_by_first_key(data) ⇒ Hash
Groups the data by the first key of each element, for example [v1, key2: v2,v1, key2: v3,v4, key2: v2] to => [{key1: v1, key2: v2,v1, key2: v3], v4 => [v4, key2: v2]}.
-
#header(_sample_data, _fasta_mapper = nil) ⇒ String
Returns the header row for the given sample_data and fasta_mapper.
-
#integrate_fasta_headers(data, fasta_mapper) ⇒ Object
Integrates the fasta headers into the data object.
-
#type ⇒ String
The type of the current formatter.
Class Method Details
.available ⇒ Array<String>
Returns a list of the available formatters
34 35 36 |
# File 'lib/formatters.rb', line 34 def self.available formatters.reject { |_key, value| value.hidden? }.keys end |
.default ⇒ String
Returns The type of the default formatter: csv.
39 40 41 |
# File 'lib/formatters.rb', line 39 def self.default 'csv' end |
.formatters ⇒ Hash
The Hash of available formatters
8 9 10 |
# File 'lib/formatters.rb', line 8 def self.formatters @@formatters ||= {} end |
.hidden? ⇒ Boolean
48 49 50 |
# File 'lib/formatters.rb', line 48 def self.hidden? false end |
.new_for_format(format) ⇒ Formatter
Returns a new formatter of the given format. If the given format is not available, the default formatter is returned
18 19 20 21 22 |
# File 'lib/formatters.rb', line 18 def self.new_for_format(format) formatters[format].new rescue StandardError formatters[default].new end |
.register(format) ⇒ Object
Adds a new formatter to the list of available formats
27 28 29 |
# File 'lib/formatters.rb', line 27 def self.register(format) formatters[format.to_s] = self end |
Instance Method Details
#convert(_data, _first) ⇒ String
Converts the given input data to another format.
101 102 103 |
# File 'lib/formatters.rb', line 101 def convert(_data, _first) raise NotImplementedError, 'This must be implemented in a subclass.' end |
#footer ⇒ String
Returns the footer row. This row is output only once at the end of the output
72 73 74 |
# File 'lib/formatters.rb', line 72 def raise NotImplementedError, 'This must be implemented in a subclass.' end |
#format(data, fasta_mapper, first) ⇒ String
Converts the given input data and corresponding fasta headers to another format.
data and corresponding fasta header. The data is represented as a list containing tuples where the first element is the fasta header and second element is the input data
89 90 91 92 |
# File 'lib/formatters.rb', line 89 def format(data, fasta_mapper, first) data = integrate_fasta_headers(data, fasta_mapper) if fasta_mapper convert(data, first) end |
#group_by_first_key(data) ⇒ Hash
Groups the data by the first key of each element, for example
- v1, key2: v2,v1, key2: v3,v4, key2: v2
-
to => [{key1: v1, key2: v2,v1, key2: v3], v4 => [v4, key2: v2]}
128 129 130 |
# File 'lib/formatters.rb', line 128 def group_by_first_key(data) data.group_by { |el| el.values.first.to_s } end |
#header(_sample_data, _fasta_mapper = nil) ⇒ String
Returns the header row for the given sample_data and fasta_mapper. This row is output only once at the beginning of the output
header. Can be used to extract the keys.
data and corresponding fasta header. The data is represented as a list containing tuples where the first element is the fasta header and second element is the input data
64 65 66 |
# File 'lib/formatters.rb', line 64 def header(_sample_data, _fasta_mapper = nil) raise NotImplementedError, 'This must be implemented in a subclass.' end |
#integrate_fasta_headers(data, fasta_mapper) ⇒ Object
Integrates the fasta headers into the data object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/formatters.rb', line 106 def integrate_fasta_headers(data, fasta_mapper) data_dict = group_by_first_key(data) data = fasta_mapper.map do |header, key| result = data_dict[key] unless result.nil? result = result.map do |row| copy = { fasta_header: header } copy.merge(row) end end result end data.compact.flatten(1) end |
#type ⇒ String
Returns The type of the current formatter.
44 45 46 |
# File 'lib/formatters.rb', line 44 def type raise NotImplementedError, 'This must be implemented in a subclass.' end |