Class: Parsers::BioanalysisCsvParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/parsers/bioanalysis_csv_parser.rb

Defined Under Namespace

Classes: InvalidFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ BioanalysisCsvParser


13
14
15
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 13

def initialize(content)
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content


11
12
13
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 11

def content
  @content
end

Class Method Details

.parses?(content) ⇒ Boolean


155
156
157
158
159
160
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 155

def self.parses?(content)
  # We don't go through the whole file
  content[0..10].detect do |line|
    /Version Created/ === line[0] && /^B.*/ === line[1]
  end.present?
end

Instance Method Details

#build_range(range) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 48

def build_range(range)
  if range == nil
    range = [0, content.length - 1]
  else
    range = range.dup
  end
  range.push(content.length - 1) if (range.length == 1)
  range
end

#concentration(plate_position) ⇒ Object


24
25
26
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 24

def concentration(plate_position)
  get_parsed_attribute(plate_position, field_name_for(:concentration))
end

#each_well_and_parametersObject


146
147
148
149
150
151
152
153
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 146

def each_well_and_parameters
  parsed_content.each do |well, values|
    yield(well, {
      'concentration' => Unit.new(values[:peak_table][field_name_for(:concentration)], 'ng/ul'),
      'molarity' => Unit.new(values[:peak_table][field_name_for(:molarity)], 'nmol/l')
    })
  end
end

#field_name_for(sym_name) ⇒ Object


17
18
19
20
21
22
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 17

def field_name_for(sym_name)
  {
    concentration: 'Conc. [ng/┬Ál]',
    molarity: 'Molarity [nmol/l]'
  }[sym_name]
end

#get_group_content(group) ⇒ Object


88
89
90
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 88

def get_group_content(group)
  content.slice(group[0], group[1] - group[0] + 1)
end

#get_groups(regexp, range = nil) ⇒ Object

Finds groups of lines by range in which the beginning of the range contains the matching regexp as text in the first column and the end of the range is an empty line

  • regexp -> Regular expression to be matched in the first column as beginning of range

  • range -> In case it is specified, restricts the searching process to this range of lines

instead of using all the content of the CSV file


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 63

def get_groups(regexp, range = nil)
  groups = []
  group = []
  range = build_range(range)

  group_contents = get_group_content(range)

  group_contents.each_with_index do |line, pos|
    if line[0].present? && line[0].match(regexp) && group.empty?
      group.push(pos)
    elsif (line.empty? && group.one?)
      group.push(pos - 1)
    end

    if group.length == 2
      groups.push [group[0] + range[0], group[1] + range[0]]
      group = []
    end
    if ((group.length == 1) && (pos == (group_contents.length - 1)))
      groups.push [group[0] + range[0], pos + range[0]]
    end
  end
  groups
end

#get_parsed_attribute(plate_position, field) ⇒ Object


140
141
142
143
144
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 140

def get_parsed_attribute(plate_position, field)
  return nil if parsed_content.nil? || parsed_content[plate_position].nil?

  parsed_content[plate_position][:peak_table][field]
end

#molarity(plate_position) ⇒ Object


28
29
30
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 28

def molarity(plate_position)
  get_parsed_attribute(plate_position, field_name_for(:molarity))
end

#parse_cell(group) ⇒ Object


105
106
107
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 105

def parse_cell(group)
  get_group_content(group)[0][1]
end

#parse_overall(group) ⇒ Object


100
101
102
103
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 100

def parse_overall(group)
  # Number of peaks
  get_group_content(get_groups(/Overall.*/m, group)[0])[1][1]
end

#parse_peak_table(group) ⇒ Object


92
93
94
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 92

def parse_peak_table(group)
  table_content_hash(get_groups(/Peak Table/m, group)[0])
end

#parse_region_table(group) ⇒ Object


96
97
98
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 96

def parse_region_table(group)
  table_content_hash(get_groups(/Region Table/m, group)[0])
end

#parse_sample(group) ⇒ Object


109
110
111
112
113
114
115
116
117
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 109

def parse_sample(group)
  {
    parse_cell(group) => {
      peak_table: parse_peak_table(group),
      region_table: parse_region_table(group),
      overall: parse_overall(group)
    }
  }
end

#parse_samplesObject


119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 119

def parse_samples
  groups = get_groups(/Sample Name/)

  groups.each_with_index.map do |group, pos|
    if (pos == (groups.length - 1))
      next_index = @content.length - 1
    else
      next_index = groups[pos + 1][0] - 1
    end
    [group[0], next_index]
  end.reduce({}) do |memo, group|
    memo.merge(parse_sample group)
  end
end

#parsed_contentObject


134
135
136
137
138
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 134

def parsed_content
  @parsed_content ||= parse_samples
rescue NoMethodError => e # Ugh! I want to catch these where they happen
  raise InvalidFile
end

#table_content_hash(group) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/parsers/bioanalysis_csv_parser.rb', line 32

def table_content_hash(group)
  content_hash = {}
  starting_line = group[0]
  ending_line = group[1]
  type = content[starting_line][0]
  fields = content[starting_line + 1]

  for pos in (starting_line + 2)..(ending_line) do
    values = content[pos]
    unless values.nil? && (values.length != fields.length)
      content_hash.merge!(Hash[fields.zip(values)])
    end
  end
  content_hash
end