Class: RunCsv::PacbioSampleSheetLegacy

Inherits:
Object
  • Object
show all
Includes:
DataStructureBuilder
Defined in:
app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb

Overview

RunCsv::PacbioSampleSheet Used to generate sample sheets specific to the PacBio pipeline for v12 and above

Instance Method Summary collapse

Methods included from DataStructureBuilder

#data_structure, #instance_value

Instance Method Details

#csv_headersObject

return a list of column names ie headers eg [‘System Name’, ‘Run Name’]



11
12
13
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 11

def csv_headers
  configuration.column_order
end

#eachable?(object) ⇒ Boolean

Returns true if the object can be iterated using each, false otherwise

Returns:

  • (Boolean)


17
18
19
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 17

def eachable?(object)
  object.respond_to?(:each)
end

#generate_csv_rows(csv) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 47

def generate_csv_rows(csv)
  data_rows = recursive_array_extractor([data_structure])
  # Initialize a flag to track if this is the first row
  first_row = true

  data_rows.each do |row|
    row_values = process_row(row, first_row)
    first_row = false if first_row && !row_values.all?(nil)
    csv << row_values unless row_values.all?(nil)
  end
end

#handle_csv_version(row_values, first_row) ⇒ Object



65
66
67
68
69
70
71
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 65

def handle_csv_version(row_values, first_row)
  # CSV VERSION should be set only on first row
  return if first_row

  index = csv_headers.index('CSV Version')
  row_values[index] = nil if index
end

#payloadObject

Parse the JSON data structure to derive the CSV. If a value contains an array, the key name will be ignored and the individual array elements parsed.



40
41
42
43
44
45
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 40

def payload
  CSV.generate do |csv|
    csv << csv_headers
    generate_csv_rows(csv)
  end
end

#process_row(row, first_row) ⇒ Object



59
60
61
62
63
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 59

def process_row(row, first_row)
  row_values = row.values_at(*csv_headers)
  handle_csv_version(row_values, first_row)
  row_values
end

#recursive_array_extractor(array_of_hashes) ⇒ Object

Flattens a nested hash of arrays into a 1D array. Includes non-iterable key-value pairs in the result.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/exchanges/run_csv/pacbio_sample_sheet_legacy.rb', line 23

def recursive_array_extractor(array_of_hashes)
  rows = []
  array_of_hashes.each do |hash_element|
    rows << hash_element
    hash_element.values.each do |values|
      if eachable? values
        child_rows = recursive_array_extractor(values)
        rows.push(*child_rows)
      end
    end
  end
  rows
end