Class: Labimotion::ExportDataset

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/export_dataset.rb

Overview

ExportDataset

Constant Summary collapse

DEFAULT_ROW_WIDTH =
100
DEFAULT_ROW_HEIGHT =
20

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ ExportDataset

Returns a new instance of ExportDataset.



11
12
13
14
15
# File 'lib/labimotion/libs/export_dataset.rb', line 11

def initialize(**args)
  @xfile = Axlsx::Package.new
  @file_extension = 'xlsx'
  @xfile.workbook.styles.fonts.first.name = 'Calibri'
end

Instance Method Details

#description(ds, id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/labimotion/libs/export_dataset.rb', line 37

def description(ds, id)
  wb = @xfile.workbook
  sheet = @xfile.workbook.add_worksheet(name: 'Description')
  header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
  sheet.add_row(['File name', res_name(id)])
  sheet.add_row(['Time', Time.now.strftime("%Y-%m-%d %H:%M:%S %Z")] )
  sheet.add_row([''])
  sheet.add_row(['Fields description of sheet:' + ds.dataset_klass.label])
  sheet.add_row(['Fields', 'Field description'], style: header_style)
  sheet.add_row(['Layer Label', 'The label of the layer'])
  sheet.add_row(['Field Label', 'The label of the field'])
  sheet.add_row(['Value', 'The current value of the field'])
  sheet.add_row(['Unit', 'The unit of the field'])
  sheet.add_row(['Name', 'The key of the field, can be used to identify the field'])
  sheet.add_row(['Type', 'The type of the field'])
  sheet.add_row(['From device?', '[Yes] if the field is from the device, [No] if the field is manually entered by the user, [SYS] if the field is automatically generated by the system'])
  sheet.add_row(['Device source', 'The source tag of the device file, available only if the ontology term is 1H NMR or 13C NMR'])
  sheet.add_row(['Device data', 'The original device data, can not be changed after data uploaded'])
  sheet.add_row([''])
  sheet.add_row([''])
  sheet.add_row([''])
  sheet.add_row(['', '(This file is automatically generated by the system.)'])
end

#export(id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/labimotion/libs/export_dataset.rb', line 61

def export(id)
  ds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
  return if ds.nil?

  description(ds, id)

  wb = @xfile.workbook
  name = ols_name(id)
  return if name.nil?

  sheet = @xfile.workbook.add_worksheet(name: name)
  sheet.add_row([ds.dataset_klass.label])
  header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
  layer_style = sheet.styles.add_style(b: true, bg_color: 'CEECF5')
  sheet.add_row(header, style: header_style)

  layers = ds.properties['layers'] || {}
  layer_keys = layers.keys.sort_by { |key| layers[key]['position'] }
  layer_keys.each do |key|
    layer = layers[key]
    sheet.add_row([layer['label'], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], style: layer_style)
    sorted_fields = layer['fields'].sort_by { |obj| obj['position'] }
    sorted_fields.each do |field|
      next if field['type'] == 'dummy'

      type = field['type']
      from_device = field['device'].present? ? 'Yes' : 'No'
      from_device = field['system'].present? ? 'SYS' : from_device
      type = "#{field['type']}-#{field['option_layers']}" if field['type'] == 'select' || field['type'] == 'system-defined'

      show_value = field['value'] =~ /\A\d+,\d+\z/ ? field['value']&.gsub(',', '.') : field['value']
      sheet.add_row([' ', field['label'], show_value, field['value_system'], field['field'], type, from_device, field['dkey'], field['system'] || field['device']].freeze)
    end
    # sheet.column_widths nil, nil, nil, nil, 0, 0, 0, 0, 0
  end
end

#headerObject



127
128
129
# File 'lib/labimotion/libs/export_dataset.rb', line 127

def header
  ['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'from device?', 'Device source', 'Device data'].freeze
end

#ols_name(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/labimotion/libs/export_dataset.rb', line 23

def ols_name(id)
  ds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
  return nil if ds.nil?

  name = ds.dataset_klass.label

  match = name.match(/\((.*?)\)/)
  name = match && match.length > 1 ? match[1] : name

  name = '1H NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000593'
  name = '13C NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000595'
  name.slice(0, 26)
end

#readObject



131
132
133
# File 'lib/labimotion/libs/export_dataset.rb', line 131

def read
  @xfile.to_stream.read
end

#res_name(id) ⇒ Object



17
18
19
20
21
# File 'lib/labimotion/libs/export_dataset.rb', line 17

def res_name(id)
  element_name = Container.find(id)&.root_element&.short_label
  ols = ols_name(id)
  "#{element_name}_#{ols.gsub(' ', '_')}.xlsx"
end

#spectra(id) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/labimotion/libs/export_dataset.rb', line 98

def spectra(id)
  wb = @xfile.workbook
  gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
  cds = Container.find(id)
  cds_csv = cds.attachments.where(aasm_state: 'csv')
  csv_length = cds_csv.length
  return if csv_length.zero?
  cds_csv.each_with_index do |att, idx|
    name = File.basename(att.filename, '.csv')
    name = name.slice(0, (25 - csv_length.to_s.length - 1))
    sheet_name = "#{name}_#{idx}"
    sheet = @xfile.workbook.add_worksheet(name: sheet_name)

    if Labimotion::IS_RAILS5 == true
      File.open(att.store.path) do |fi|
        fi.each_line do |line|
          sheet.add_row(line.split(','))
        end
      end
    else
      File.open(att.attachment_url) do |fi|
        fi.each_line do |line|
          sheet.add_row(line.split(','))
        end
      end
    end
  end
end