Class: Unipept::Formatter

Inherits:
Object show all
Defined in:
lib/formatters.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.availableArray<String>

Returns a list of the available formatters

Returns:

  • (Array<String>)

    The list of available formatters



34
35
36
# File 'lib/formatters.rb', line 34

def self.available
  formatters.reject { |_key, value| value.hidden? }.keys
end

.defaultString

Returns The type of the default formatter: csv.

Returns:

  • (String)

    The type of the default formatter: csv



39
40
41
# File 'lib/formatters.rb', line 39

def self.default
  'csv'
end

.formattersHash

The Hash of available formatters

Returns:

  • (Hash)

    A hash of the available formatters



8
9
10
# File 'lib/formatters.rb', line 8

def self.formatters
  @@formatters ||= {}
end

.hidden?Boolean

Returns:

  • (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

Parameters:

  • format (String)

    The type of the formatter we want

Returns:



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

Parameters:

  • format (Symbol)

    The type of the format we want to register



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.

Parameters:

  • data (Array)

    The data we wish to convert

  • Is (Boolean)

    this the first output batch?

Returns:

  • (String)

    The converted input data

Raises:

  • (NotImplementedError)


101
102
103
# File 'lib/formatters.rb', line 101

def convert(_data, _first)
  raise NotImplementedError, 'This must be implemented in a subclass.'
end

Returns the footer row. This row is output only once at the end of the output

Returns:

  • (String)

    The footer row

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/formatters.rb', line 72

def footer
  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

Parameters:

  • data (Array)

    The data we wish to convert

  • fasta_mapper (Array<Array<String>>)

    Optional mapping between input

  • Is (Boolean)

    this the first output batch?

Returns:

  • (String)

    The converted 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]}

Parameters:

  • data (Array<Hash>)

    The data we wish to group

Returns:

  • (Hash)

    The input data grouped by the first key



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

Parameters:

  • _sample_data (Object)

    The data that we will output after this

  • _fasta_mapper (Array<Array<String>>) (defaults to: nil)

    Optional mapping between input

Returns:

  • (String)

    The header row

Raises:

  • (NotImplementedError)


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

#typeString

Returns The type of the current formatter.

Returns:

  • (String)

    The type of the current formatter

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/formatters.rb', line 44

def type
  raise NotImplementedError, 'This must be implemented in a subclass.'
end