Class: RGFA::Line::Segment

Inherits:
RGFA::Line show all
Defined in:
lib/rgfa/line/segment.rb,
lib/rgfa/field_writer.rb,
lib/rgfa/field_validator.rb

Overview

A segment line of a RGFA file

Defined Under Namespace

Classes: InconsistentLengthError, UndefinedLengthError

Constant Summary collapse

RECORD_TYPE =
:S
REQFIELDS =
[:name, :sequence]
PREDEFINED_OPTFIELDS =
[:LN, :RC, :FC, :KC, :SH, :UR]
DATATYPE =
{
  :name => :lbl,
  :sequence => :seq,
  :LN => :i,
  :RC => :i,
  :FC => :i,
  :KC => :i,
  :SH => :H,
  :UR => :Z
}

Constants inherited from RGFA::Line

DELAYED_PARSING_DATATYPES, DIRECTION, FIELD_DATATYPE, OPTFIELD_DATATYPE, ORIENTATION, RECORD_TYPES, RECORD_TYPE_LABELS, REQFIELD_DATATYPE, SEPARATOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RGFA::Line

#==, #clone, #delete, #field_to_s, #fieldnames, #get, #get!, #get_datatype, #initialize, #method_missing, #optional_fieldnames, #real!, #record_type, #required_fieldnames, #respond_to?, #set, #set_datatype, subclass, #tags, #to_a, #to_rgfa_line, #validate!, #validate_field!, #virtual?

Constructor Details

This class inherits a constructor from RGFA::Line

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RGFA::Line

Instance Attribute Details

#containmentsHash{RGFA::Line::DIRECTION => Hash{RGFA::Line::ORIENTATION => Array<RGFA::Line::Containment>}}

References to the containments in which the segment is involved. The references are in four arrays which are accessed from a nested hash table. The first key is the direction (from or to), the second is the orientation (+ or -).

Examples:

segment.containments[:from][:+]

Returns:



47
48
49
50
51
# File 'lib/rgfa/line/segment.rb', line 47

def containments
  @containments ||= {:from => {:+ => [], :- => []},
                     :to   => {:+ => [], :- => []}}
  @containments
end

References to the links in which the segment is involved.

The references are in four arrays which are accessed from a nested hash table. The first key is the direction (from or to), the second is the orientation (+ or -).

Examples:

segment.links[:from][:+]

Returns:



34
35
36
37
38
# File 'lib/rgfa/line/segment.rb', line 34

def links
  @links ||= {:from => {:+ => [], :- => []},
              :to   => {:+ => [], :- => []}}
  @links
end

#pathsHash{RGFA::Line::ORIENTATION => Array<RGFA::Line::Path>}

References to the containments in which the segment is involved.

The references are in two arrays which are accessed from a hash table. The key is the orientation (+ or -).

Examples:

segment.paths[:+]

Returns:



63
64
65
66
# File 'lib/rgfa/line/segment.rb', line 63

def paths
  @paths ||= {:+ => [], :- => []}
  @paths
end

Instance Method Details

#all_connectionsObject

Note:

the list shall be considered read-only, as this is a copy of the original arrays of references, concatenated to each other.

All links and containments where the segment is involved.



87
88
89
# File 'lib/rgfa/line/segment.rb', line 87

def all_connections
  all_links + all_containments
end

#all_containmentsObject

Note:

the list shall be considered read-only, as this is a copy of the original arrays of references, concatenated to each other.

All containments where a segment is involved.



73
74
75
76
# File 'lib/rgfa/line/segment.rb', line 73

def all_containments
  l = self.containments
  l[:from][:+] + l[:from][:-] + l[:to][:+] + l[:to][:-]
end
Note:

the list shall be considered read-only, as this is a copy of the original arrays of references, concatenated to each other.

All links where the segment is involved.



80
81
82
83
# File 'lib/rgfa/line/segment.rb', line 80

def all_links
  l = self.links
  l[:from][:+] + l[:from][:-] + l[:to][:+] + l[:to][:-]
end

#all_pathsObject

Note:

the list shall be considered read-only, as this is a copy of the original arrays of references, concatenated to each other.

All paths where the segment is involved.



93
94
95
96
# File 'lib/rgfa/line/segment.rb', line 93

def all_paths
  pt = self.paths
  pt[:+] + pt[:-]
end

#all_referencesObject

Note:

the list shall be considered read-only, as this is a copy of the original arrays of references, concatenated to each other.

All paths, links and containments where the segment is involved.



100
101
102
# File 'lib/rgfa/line/segment.rb', line 100

def all_references
  all_connections + all_paths
end

#coverage(count_tag: :RC, unit_length: 1) ⇒ Integer?

The coverage computed from a count_tag. If unit_length is provided then: count/(length-unit_length+1), otherwise: count/length. The latter is a good approximation if length >>> unit_length.

Parameters:

  • count_tag (Symbol) (defaults to: :RC)

    (defaults to :RC) integer tag storing the count, usually :KC, :RC or :FC

  • unit_length (Integer) (defaults to: 1)

    the (average) length of a read (for :RC), fragment (for :FC) or k-mer (for :KC)

Returns:

  • (Integer)

    coverage, if count_tag and length are defined

  • (nil)

    otherwise

See Also:



155
156
157
158
159
160
161
# File 'lib/rgfa/line/segment.rb', line 155

def coverage(count_tag: :RC, unit_length: 1)
  if optional_fieldnames.include?(count_tag) and self.length
    return (self.get(count_tag).to_f)/(self.length-unit_length+1)
  else
    return nil
  end
end

#coverage!(count_tag: :RC, unit_length: 1) ⇒ Integer

The coverage computed from a count_tag. If unit_length is provided then: count/(length-unit_length+1), otherwise: count/length. The latter is a good approximation if length >>> unit_length.

Parameters:

  • count_tag (Symbol) (defaults to: :RC)

    (defaults to :RC) integer tag storing the count, usually :KC, :RC or :FC

  • unit_length (Integer) (defaults to: 1)

    the (average) length of a read (for :RC), fragment (for :FC) or k-mer (for :KC)

Returns:

  • (Integer)

    coverage, if count_tag and length are defined

Raises:

See Also:



167
168
169
170
171
172
173
174
175
176
# File 'lib/rgfa/line/segment.rb', line 167

def coverage!(count_tag: :RC, unit_length: 1)
  c = coverage(count_tag: count_tag, unit_length: unit_length)
  if c.nil?
    self.length!
    raise RGFA::Line::TagMissingError,
      "Tag #{count_tag} undefined for segment #{name}"
  else
    return c
  end
end

#lengthInteger?

Returns:

  • (Integer)

    value of LN tag, if segment has LN tag

  • (Integer)

    sequence length if no LN and sequence not “*”

  • (nil)

    if sequence is “*”

See Also:



121
122
123
124
125
126
127
128
129
# File 'lib/rgfa/line/segment.rb', line 121

def length
  if self.LN
    self.LN
  elsif sequence != "*"
    sequence.length
  else
    nil
  end
end

#length!Integer

Returns:

  • (Integer)

    value of LN tag, if segment has LN tag

  • (Integer)

    sequence length if no LN and sequence not “*”

Raises:

See Also:



136
137
138
139
140
141
# File 'lib/rgfa/line/segment.rb', line 136

def length!
  l = self.length()
  raise RGFA::Line::Segment::UndefinedLengthError,
    "No length information available" if l.nil?
  return l
end

#to_gfa_field(datatype: nil) ⇒ 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:



107
# File 'lib/rgfa/field_writer.rb', line 107

def to_gfa_field(datatype: nil); to_sym.to_s; end

#to_s(without_sequence: false) ⇒ Object

Returns string representation of the segment.

Parameters:

  • without_sequence (Boolean) (defaults to: false)

    if true, output “*” instead of sequence

Returns:

  • string representation of the segment



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rgfa/line/segment.rb', line 180

def to_s(without_sequence: false)
  if !without_sequence
    return super()
  else
    saved = self.sequence
    self.sequence = "*"
    retval = super()
    self.sequence = saved
    return retval
  end
end

#to_symSymbol

Returns name of the segment as symbol.

Returns:

  • (Symbol)

    name of the segment as symbol



193
194
195
# File 'lib/rgfa/line/segment.rb', line 193

def to_sym
  name.to_sym
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:



247
248
249
250
251
252
253
254
# File 'lib/rgfa/field_validator.rb', line 247

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

#validate_length!Object

Raises:



106
107
108
109
110
111
112
113
114
# File 'lib/rgfa/line/segment.rb', line 106

def validate_length!
  if sequence != "*" and optional_fieldnames.include?(:LN)
    if self.LN != sequence.length
      raise RGFA::Line::Segment::InconsistentLengthError,
        "Length in LN tag (#{self.LN}) "+
        "is different from length of sequence field (#{sequence.length})"
    end
  end
end