File: field_validator.rb

package info (click to toggle)
ruby-rgfa 1.3.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: ruby: 5,666; makefile: 9
file content (272 lines) | stat: -rw-r--r-- 8,484 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
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
127
128
129
130
131
132
133
134
135
136
137
138
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require_relative "field_parser"
require_relative "line"

#
# Methods to validate the string representations of the GFA fields data
# @api private
#
module RGFA::FieldValidator

  # Validation regular expressions, derived from the GFA specification
  DATASTRING_VALIDATION_REGEXP = {
    :A => /^[!-~]$/,         # Printable character
    :i => /^[-+]?[0-9]+$/,   # Signed integer
    :f => /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/,
                           # Single-precision floating number
    :Z => /^[ !-~]+$/,       # Printable string, including space
    :J => /^[ !-~]+$/,       # JSON, excluding new-line and tab characters
    :H => /^[0-9A-F]+$/,     # Byte array in the Hex format
    :B => /^[cCsSiIf](,[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)+$/,
                           # Integer or numeric array
    :lbl => /^[!-)+-<>-~][!-~]*$/,       # segment/path label
    :orn => /^\+|-$/,                    # segment orientation
    :lbs => /^[!-)+-<>-~][!-~]*[+-](,[!-)+-<>-~][!-~]*[+-])+$/,
                           # multiple labels with orientations, comma-sep
    :seq => /^\*$|^[A-Za-z=.]+$/,          # nucleotide sequence
    :pos => /^[0-9]*$/,                  # positive integer
    :cig => /^(\*|(([0-9]+[MIDNSHPX=])+))$/, # CIGAR string
    :cgs => /^(\*|(([0-9]+[MIDNSHPX=])+))(,(\*|(([0-9]+[MIDNSHPX=])+)))*$/,
                                       # multiple CIGARs, comma-sep
    :cmt => /.*/, # content of comment line, everything is allowed
  }

  # Validates the string according to the provided datatype
  # @param datatype [RGFA::Line::FIELD_DATATYPE]
  # @param fieldname [#to_s] Fieldname to use in the error msg
  # @raise [RGFA::FieldParser::FormatError] if the string does not match
  #   the regexp for the provided datatype
  # @return [void]
  # @api private
  def validate_gfa_field!(datatype, fieldname=nil)
    regexp = DATASTRING_VALIDATION_REGEXP[datatype]
    raise RGFA::FieldParser::UnknownDatatypeError if regexp.nil?
    if (regexp !~ self)
      fieldname ||= "Value"
      raise RGFA::FieldParser::FormatError,
        "Wrong format for field #{fieldname}: \n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}\n"+
        "Expected regex: #{regexp}\n"
    end
  end

  # Validates segment names, to check that they do not contain + or -
  # followed by comma
  # @raise [RGFA::FieldParser::FormatError] if the segment name is invalid
  # @return [void]
  # @api private
  def validate_segment_name!
    if self =~ /.*[+-],.*/
      raise RGFA::FieldParser::FormatError,
      "Segment names are not allowed to contain +/- followed by comma "+
      "(found: #{self})"
    end
  end

end

class String
  include RGFA::FieldValidator
end

class Object
  # @!macro [new] validate_gfa_field
  #   Validates the object according to the provided datatype
  #   @param datatype [RGFA::Line::FIELD_DATATYPE]
  #   @param fieldname [#to_s] Fieldname to use in the error msg
  #   @raise [RGFA::FieldParser::FormatError] if the object type or content
  #     is not compatible to the provided datatype
  #   @return [void]
  #   @api private
  def validate_gfa_field!(datatype, fieldname=nil)
    raise RGFA::FieldParser::FormatError,
      "Wrong type (#{self.class}) for field #{fieldname}\n"+
      "Content: #{self.inspect}\n"+
      "Datatype: #{datatype}"
  end
end

class Symbol
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :lbl and datatype != :orn and datatype != :Z
      raise RGFA::FieldParser::FormatError,
        "Wrong type (#{self.class}) for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}"
    end
    self.to_s.validate_gfa_field!(datatype)
  end
end

class Hash
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :J
      raise RGFA::FieldParser::FormatError,
        "Wrong type (#{self.class}) for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}"
    end
  end
end

class Array
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    begin
      case datatype
      when :J
        return
      when :Z
        return
      when :lbs
        map!(&:to_oriented_segment).each(&:validate!)
        return
      when :cig
        to_cigar.validate!
        return
      when :cgs
        map(&:to_cigar).each(&:validate!)
        return
      when :B
        to_numeric_array.validate!
        return
      when :H
        to_byte_array.validate!
        return
      end
    rescue => err
      raise RGFA::FieldParser::FormatError,
        "Invalid content for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}\n"+
        "Error: #{err}"
    end
    raise RGFA::FieldParser::FormatError,
        "Wrong type (#{self.class}) for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}"
  end
end

class RGFA::ByteArray
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :H
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: #{self.inspect}\n"+
          "Datatype: #{datatype}"
    end
    begin
      validate!
    rescue => err
      raise RGFA::FieldParser::FormatError,
        "Invalid content for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}\n"+
        "Error: #{err}"
    end
  end
end

class RGFA::CIGAR
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :cig
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: #{self.inspect}\n"+
          "Datatype: #{datatype}"
    end
    begin
      validate!
    rescue => err
      raise RGFA::FieldParser::FormatError,
        "Invalid content for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}\n"+
        "Error: #{err}"
    end
  end
end

class RGFA::NumericArray
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :B
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: #{self.inspect}\n"+
          "Datatype: #{datatype}"
    end
    begin
      validate!
    rescue => err
      raise RGFA::FieldParser::FormatError,
        "Invalid content for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}\n"+
        "Error: #{err}"
    end
  end
end

class Float
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :f and datatype != :Z
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: #{self.inspect}\n"+
          "Datatype: #{datatype}"
    end
  end
end

class Fixnum
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if (datatype == :pos and self < 0)
      raise RGFA::FieldParser::FormatError,
        "Invalid content for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}"
    elsif ![:i, :f, :Z].include?(datatype)
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: #{self.inspect}\n"+
          "Datatype: #{datatype}"
    end
  end
end

class Integer
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if (datatype == :pos and self < 0)
      raise RGFA::FieldParser::FormatError,
        "Invalid content for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}"
    elsif ![:i, :f, :Z].include?(datatype)
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: #{self.inspect}\n"+
          "Datatype: #{datatype}"
    end
  end
end

class RGFA::Line::Segment
  # @!macro validate_gfa_field
  def validate_gfa_field!(datatype, fieldname=nil)
    if datatype != :lbl
      raise RGFA::FieldParser::FormatError,
          "Wrong type (#{self.class}) for field #{fieldname}\n"+
          "Content: <RGFA::Segment:#{self.to_s}>\n"+
          "Datatype: #{datatype}"
    end
  end
end