Class: Labimotion::MapperUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/utils/mapper_utils.rb

Class Method Summary collapse

Class Method Details

.extract_array_parameters(file_content, array_parameters) ⇒ Object

Extracts scalar values from Bruker array parameters such as ##$D= (0..63) 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0002 ... where the values live on the line(s) following the header. The config maps each output key to an array name + index, e.g. { "D1" => { "name" => "D", "index" => 1 } } => D1 = the 2nd value (here 1).



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/labimotion/utils/mapper_utils.rb', line 79

def extract_array_parameters(file_content, array_parameters)
  return {} if file_content.blank? || array_parameters.blank?

  lines = file_content.lines
  extracted = {}
  lines.each_with_index do |line, idx|
    header = line.match(/^\s*##\$(?<name>[A-Za-z0-9_]+)\s*=\s*\(\s*\d+\s*\.\.\s*\d+\s*\)/)
    next unless header

    targets = array_parameters.select { |_key, cfg| cfg['name'] == header[:name] }
    next if targets.empty?

    values = collect_array_values(lines, idx + 1)
    targets.each do |out_key, cfg|
      value = values[cfg['index'].to_i]
      extracted[out_key] = clean_value(value) if value.present?
    end
  end
  extracted.compact_blank!
  extracted
rescue StandardError => e
  Rails.logger.error "Error extracting array parameters: #{e.message}"
  {}
end

.extract_data_from_zip(zip_file_url, source_map) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/labimotion/utils/mapper_utils.rb', line 37

def extract_data_from_zip(zip_file_url, source_map)
  return nil if zip_file_url.nil?

  process_zip_file(zip_file_url, source_map)
rescue Zip::Error => e
  Rails.logger.error "Zip file error: #{e.message}"
  nil
rescue StandardError => e
  Rails.logger.error "Unexpected error extracting metadata: #{e.message}"
  nil
end

.extract_parameters(file_content, parameter_names) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/labimotion/utils/mapper_utils.rb', line 49

def extract_parameters(file_content, parameter_names)
  return nil if file_content.blank? || parameter_names.blank?

  patterns = {
    standard: build_parameter_pattern(parameter_names, :standard),
    parm: build_parameter_pattern(parameter_names, :parm)
  }

  extracted_parameters = {}
  begin
    file_content.each_line do |line|
      if (match = match_parameter(line, patterns))
        value = clean_value(match[:value])
        extracted_parameters[match[:param_name]] = value
      end
    end
  rescue StandardError => e
    Rails.logger.error "Error reading file content: #{e.message}"
    return nil
  end
  extracted_parameters.compact_blank!
  extracted_parameters
end

.format_timestamp(timestamp_str, give_format = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/labimotion/utils/mapper_utils.rb', line 104

def format_timestamp(timestamp_str, give_format = nil)
  return nil if timestamp_str.blank?

  begin
    timestamp = Integer(timestamp_str)
    time_object = Time.at(timestamp).in_time_zone(Constants::DateTime::TIME_ZONE)
    case give_format
    when 'date'
      time_object.strftime(Constants::DateTime::DATE_FORMAT)
    when 'time'
      time_object.strftime(Constants::DateTime::TIME_FORMAT)
    else
      time_object.strftime(Constants::DateTime::DATETIME_FORMAT)
    end
  rescue ArgumentError, TypeError => e
    Rails.logger.error "Error parsing timestamp '#{timestamp_str}': #{e.message}"
    nil
  end
end

.load_brucker_configObject



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

def load_brucker_config
  config = load_config(File.read(Constants::Mapper::NMR_CONFIG))
  return if config.nil? || config['sourceMap'].nil?

  source_selector = config['sourceMap']['sourceSelector']
  return if source_selector.blank?

  parameters = config['sourceMap']['parameters']
  return if parameters.blank?

  config
end

.load_config(config_json) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/labimotion/utils/mapper_utils.rb', line 11

def load_config(config_json)
  JSON.parse(config_json)
rescue JSON::ParserError => e
  Rails.logger.error "Error parsing JSON: #{e.message}"
  nil
rescue Errno::ENOENT => e
  Rails.logger.error "Config file not found at #{Constants::Mapper::NMR_CONFIG}: #{e.message}"
  nil
rescue StandardError => e
  Rails.logger.error "Unexpected error loading config: #{e.message}"
  nil
end