Class: Labimotion::ExportDataset
- Inherits:
- 
      Object
      
        - Object
- Labimotion::ExportDataset
 
- 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
- #description(ds, id) ⇒ Object
- #export(id) ⇒ Object
- #header ⇒ Object
- 
  
    
      #initialize(**args)  ⇒ ExportDataset 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of ExportDataset. 
- #ols_name(id) ⇒ Object
- #opt_value(field, options) ⇒ Object
- #read ⇒ Object
- #res_name(id) ⇒ Object
- #spectra(id) ⇒ Object
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
| 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # File 'lib/labimotion/libs/export_dataset.rb', line 42 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(['(This file is automatically generated by the system.)']) sheet.add_row(['']) 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(['Source?', '[Device] from device, [Chemotion] from Chemotion']) sheet.add_row(['Source identifier', 'The source identifier']) sheet.add_row(['Source data', 'The data from Device or Chemotion, cannot be modified once a generic dataset is created']) sheet.add_row(['']) sheet.add_row(['']) rescue StandardError => e Labimotion.log_exception(e) end | 
#export(id) ⇒ Object
| 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 97 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 126 | # File 'lib/labimotion/libs/export_dataset.rb', line 68 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[Labimotion::Prop::LAYERS] || {} = ds.properties[Labimotion::Prop::SEL_OPTIONS] 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[Labimotion::Prop::FIELDS].sort_by { |obj| obj['position'] } sorted_fields.each do |field| next if field['type'] == 'dummy' type = field['type'] from_device = field['device'].present? ? 'Device' : '' from_device = field['system'].present? ? 'Chemotion' : from_device type = "#{field['type']}-#{field['option_layers']}" if field['type'] == Labimotion::FieldType::SELECT || field['type'] == Labimotion::FieldType::SYSTEM_DEFINED show_value = field['value'] =~ /\A\d+,\d+\z/ ? field['value']&.gsub(',', '.') : field['value'] ols_short_form = (field['ontology'] && field['ontology']['short_form']) || '' ols_label = (field['ontology'] && field['ontology']['label']) || '' ols_iri = (field['ontology'] && field['ontology']['iri']) || '' sheet.add_row([' ', field['label'], nil, field['value_system'], field['field'], type, from_device, field['dkey'], nil, ols_short_form, ols_label, ols_iri].freeze) case field['type'] when Labimotion::FieldType::SELECT sheet.rows.last.cells[2].type = :string sheet.rows.last.cells[8].type = :string show_value = opt_value(field, ) if show_value.present? when Labimotion::FieldType::SYSTEM_DEFINED, Labimotion::FieldType::INTEGER sheet.rows.last.cells[2].type = :float sheet.rows.last.cells[8].type = :float else sheet.rows.last.cells[2].type = :string sheet.rows.last.cells[8].type = :string end sheet.rows.last.cells[2].value = show_value sheet.rows.last.cells[8].value = field['system'] || field['device'] end # sheet.column_widths nil, nil, nil, nil, 0, 0, 0, 0, 0 end rescue StandardError => e Labimotion.log_exception(e) end | 
#header ⇒ Object
| 171 172 173 | # File 'lib/labimotion/libs/export_dataset.rb', line 171 def header ['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'Source?', 'Source identifier', 'Source data', 'Ontology', 'Ontology Label', 'iri'].freeze end | 
#ols_name(id) ⇒ Object
| 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # File 'lib/labimotion/libs/export_dataset.rb', line 25 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) rescue StandardError => e Labimotion.log_exception(e) 'ols_name' end | 
#opt_value(field, options) ⇒ Object
| 128 129 130 131 132 133 134 135 136 137 | # File 'lib/labimotion/libs/export_dataset.rb', line 128 def opt_value(field, ) return nil if field.nil? || .nil? || field['value']&.empty? || field['option_layers']&.empty? return nil unless opts = .fetch(field['option_layers'], nil)&.fetch('options', nil) selected = opts&.find { |ss| ss['key'] == field['value'] } selected&.fetch('label', nil) || field['value'] rescue StandardError => e Labimotion.log_exception(e) field['value'] end | 
#read ⇒ Object
| 175 176 177 | # File 'lib/labimotion/libs/export_dataset.rb', line 175 def read @xfile.to_stream.read end | 
#res_name(id) ⇒ Object
| 17 18 19 20 21 22 23 | # 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" rescue StandardError => e Labimotion.log_exception(e) end | 
#spectra(id) ⇒ Object
| 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | # File 'lib/labimotion/libs/export_dataset.rb', line 139 def spectra(id) name_mapping = [] wb = @xfile.workbook gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container') cds = Container.find(id) cds_csv = cds..where(aasm_state: 'csv').order(:filename) csv_length = cds_csv.length return if csv_length.zero? cds_csv.each_with_index do |att, idx| sheet_name = "Sheet#{idx+1}" sheet = @xfile.workbook.add_worksheet(name: sheet_name) name_mapping.push([sheet_name, att.filename]) File.open(att.) do |fi| fi.each_line do |line| sheet.add_row(line.split(',')) end end end if name_mapping.length > 1 first_sheet = @xfile.workbook.worksheets&.first header_style = first_sheet&.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] }) first_sheet&.add_row(['Sheet name', 'File name'], style: header_style) name_mapping&.each do |mapping| next if mapping.length < 2 @xfile.workbook.worksheets&.first&.add_row([mapping[0].to_s, mapping[1].to_s]) end end end |