Class: Array

Inherits:
Object show all
Defined in:
lib/rgfa.rb,
lib/rgfa/line.rb,
lib/rgfa/cigar.rb,
lib/rgfa/byte_array.rb,
lib/rgfa/field_array.rb,
lib/rgfa/field_writer.rb,
lib/rgfa/segment_info.rb,
lib/rgfa/numeric_array.rb,
lib/rgfa/field_validator.rb

Overview

Method to create a numeric array from an array

Instance Method Summary collapse

Instance Method Details

#default_gfa_datatypeRGFA::Line::FIELD_DATATYPE

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Optional field GFA datatype to use, if none is provided



90
91
92
# File 'lib/rgfa/field_writer.rb', line 90

def default_gfa_datatype
  (all?{|i|i.kind_of?(Integer)} or all?{|i|i.kind_of?(Float)}) ? :B : :J
end

#rgfa_field_array?Boolean

Is this possibly a RGFA::FieldArray instance?

(i.e. are the two last elements a datatype symbol and a zero byte?)

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/rgfa/field_array.rb', line 71

def rgfa_field_array?
  self[-1] == "\0" and
    RGFA::Line::OPTFIELD_DATATYPE.include?(self[-2].to_sym)
end

#to_byte_arrayRGFA::ByteArray

Create a RGFA::ByteArray from an Array instance

Returns:



55
56
57
# File 'lib/rgfa/byte_array.rb', line 55

def to_byte_array
  RGFA::ByteArray.new(self)
end

#to_cigarRGFA::CIGAR

Create a RGFA::CIGAR instance from the content of the array.

Returns:



139
140
141
# File 'lib/rgfa/cigar.rb', line 139

def to_cigar
  RGFA::CIGAR.new(self)
end

#to_cigar_operationRGFA::CIGAR::Operation

Create a RGFA::CIGAR::Operation instance from the content of the array.



144
145
146
# File 'lib/rgfa/cigar.rb', line 144

def to_cigar_operation
  RGFA::CIGAR::Operation.new(Integer(self[0]), self[1].to_sym)
end

#to_gfa_field(datatype: default_gfa_datatype) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Representation of the data for GFA fields; this method does not (in general) validate the string. The method can be overwritten for a given class, and may take the #default_gfa_datatype into consideration.

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rgfa/field_writer.rb', line 70

def to_gfa_field(datatype: default_gfa_datatype)
  case datatype
  when :B
    to_numeric_array.to_s
  when :J
    to_json
  when :cig
    to_cigar.to_s
  when :cgs
    map{|cig|cig.to_cigar.to_s}.join(",")
  when :lbs
    map{|os|os.to_oriented_segment.to_s}.join(",")
  when :H
    to_byte_array.to_s
  else
    map(&:to_s).join(",")
  end
end

#to_numeric_array(validate: true) ⇒ RGFA::NumericArray

Create a numeric array from an Array instance

Parameters:

  • validate (Boolean) (defaults to: true)

    (default: true) if true, validate the range of the numeric values, according to the array subtype

Returns:

Raises:



148
149
150
151
152
# File 'lib/rgfa/numeric_array.rb', line 148

def to_numeric_array(validate: true)
  na = RGFA::NumericArray.new(self)
  na.validate! if validate
  na
end

#to_oriented_segmentRGFA::OrientedSegment

Create and validate a segment end from an array

Returns:

Raises:



145
146
147
# File 'lib/rgfa/segment_info.rb', line 145

def to_oriented_segment
  to_segment_info(RGFA::OrientedSegment)
end

#to_rgfa(validate: 2) ⇒ RGFA

Converts an Array of strings or RGFA::Line instances into a RGFA instance.

Parameters:

  • validate (Integer) (defaults to: 2)

    (defaults to: 2) the validation level; see “Validation level” under RGFA::Line#initialize.

Returns:



374
375
376
377
378
379
# File 'lib/rgfa.rb', line 374

def to_rgfa(validate: 2)
  gfa = RGFA.new(validate: validate)
  each {|line| gfa << line}
  gfa.validate! if validate >= 1
  return gfa
end

#to_rgfa_field_array(datatype = nil) ⇒ Object

Create a RGFA::FieldArray from an array

Parameters:



78
79
80
81
82
83
84
85
86
# File 'lib/rgfa/field_array.rb', line 78

def to_rgfa_field_array(datatype=nil)
  if self.rgfa_field_array?
    RGFA::FieldArray.new(self[-2].to_sym, self[0..-3])
  elsif datatype.nil?
    raise RGFA::FieldArray::Error, "no datatype specified"
  else
    RGFA::FieldArray.new(datatype, self)
  end
end

#to_rgfa_line(validate: 2) ⇒ subclass of RGFA::Line

Note:

This method modifies the content of the array; if you still need the array, you must create a copy before calling it

Parses an array containing the fields of a RGFA file line and creates an object of the correct record type child class of RGFA::Line

Parameters:

  • validate (Integer) (defaults to: 2)

    (defaults to: 2) see RGFA::Line#initialize

Returns:

Raises:

  • (RGFA::Error)

    if the fields do not comply to the RGFA specification



723
724
725
# File 'lib/rgfa/line.rb', line 723

def to_rgfa_line(validate: 2)
  RGFA::Line.subclass(shift).new(self, validate: validate)
end

#to_segment_endRGFA::SegmentEnd

Create and validate a segment end from an array

Returns:

Raises:



138
139
140
# File 'lib/rgfa/segment_info.rb', line 138

def to_segment_end
  to_segment_info(RGFA::SegmentEnd)
end

#validate_gfa_field!(datatype, fieldname = nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates the object according to the provided datatype

Parameters:

Raises:



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
# File 'lib/rgfa/field_validator.rb', line 116

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