#!/usr/bin/env ruby #!/usr/bin/env ruby require "flex_station_data/services/load_plates" require "flex_station_data/presenters/plates_csv" require "flex_station_data/presenters/plate_csv" require "flex_station_data/presenters/sample_linear_regression_csv" module FlexStationData class LinearRegressionApp include Concerns::Service OPTION_RE = /\A--(\w+(?:-\w+)*)(?:=(.*))?\z/.freeze attr_reader :args, :options def initialize(*args) @options, @args = args.partition { |arg| arg =~ OPTION_RE } @options.map! do |option| option.scan(OPTION_RE).first end end def threshold name, value = options.reverse.detect { |name, value| name == "threshold" } return nil if name.blank? Float(value) end def value_quality_control @value_quality_control ||= begin value_threshold = threshold ->(value) { ValueQuality.call(value, threshold: value_threshold) } end end def sample_quality_control ->(sample) { SampleQuality.call(sample, value_quality_control: value_quality_control) } end def files @files ||= args.map { |arg| Pathname(arg) } end def plates @plates ||= files.map do |file| [ file, LoadPlates.call(file) ] end end def csv plates.flat_map do |file, file_plates| Presenters::PlatesCsv.present(file, file_plates) do |plate| Presenters::PlateCsv.present(plate) do |times, sample| Presenters::SampleLinearRegressionCsv.present(times, sample, quality_control: sample_quality_control) end end end end def call CSV do |out| csv.each do |row| out << row end end end end end FlexStationData::LinearRegressionApp.call(*ARGV)