Class: GFA::RecordSet

Inherits:
Object
  • Object
show all
Defined in:
lib/gfa/record_set.rb

Defined Under Namespace

Classes: CommentSet, ContainmentSet, HeaderSet, JumpSet, LinkSet, PathSet, SegmentSet, WalkSet

Constant Summary collapse

INDEX_FIELD =
nil
TYPES =
GFA::Record.TYPES.map { |i| :"#{i}Set" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gfa = nil) ⇒ RecordSet

Returns a new instance of RecordSet.



28
29
30
31
32
33
# File 'lib/gfa/record_set.rb', line 28

def initialize(gfa = nil)
  @set      = []
  @index    = {}
  @position = {}
  @gfa      = gfa || GFA.new
end

Instance Attribute Details

#gfaObject (readonly)

Instance-level



26
27
28
# File 'lib/gfa/record_set.rb', line 26

def gfa
  @gfa
end

#indexObject (readonly)

Instance-level



26
27
28
# File 'lib/gfa/record_set.rb', line 26

def index
  @index
end

#position(v) ⇒ Object (readonly)

Instance-level



26
27
28
# File 'lib/gfa/record_set.rb', line 26

def position
  @position
end

#setObject (readonly)

Instance-level



26
27
28
# File 'lib/gfa/record_set.rb', line 26

def set
  @set
end

Class Method Details

.code_class(code) ⇒ Object



13
14
15
16
17
# File 'lib/gfa/record_set.rb', line 13

def self.code_class(code)
  name = GFA::Record.CODES[code.to_sym]
  raise "Unknown record type: #{code}." if name.nil?
  name_class(name)
end

.name_class(name) ⇒ Object



19
20
21
22
# File 'lib/gfa/record_set.rb', line 19

def self.name_class(name)
  name = "#{name}Set" unless name =~ /Set$/
  const_get(name)
end

Instance Method Details

#<<(v) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/gfa/record_set.rb', line 66

def <<(v)
  v = v.split("\t") if v.is_a? String
  v = GFA::Record.code_class(code).new(*v) if v.is_a? Array
  raise "Not a GFA Record: #{v}" unless v.is_a? GFA::Record
  raise "Wrong type of record: #{v.type}" if v.type != type

  @set << v
  @position[index_id(v)] = set.size - 1
  index!(v)
end

#[](k) ⇒ Object



35
36
37
38
# File 'lib/gfa/record_set.rb', line 35

def [](k)
  return set[k] if k.is_a?(Integer)
  find_index(k)
end

#codeObject



44
45
46
# File 'lib/gfa/record_set.rb', line 44

def code
  self.class.const_get(:CODE)
end

#eql?(rec) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


60
61
62
# File 'lib/gfa/record_set.rb', line 60

def eql?(rec)
  hash == rec.hash
end

#find_index(k) ⇒ Object



109
110
111
112
# File 'lib/gfa/record_set.rb', line 109

def find_index(k)
  k = k.value if k.is_a? GFA::Field
  @index[k]
end

#index!(v) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/gfa/record_set.rb', line 90

def index!(v)
  save_index(index_id(v), v) if index_field

  # Whenever present, index also by ID
  if gfa.opts[:index_id] && v[:ID] && v[:ID].value =~ index_id(v)
    save_index(v[:ID].value, v)
  end
end

#index_fieldObject



48
49
50
# File 'lib/gfa/record_set.rb', line 48

def index_field
  self.class.const_get(:INDEX_FIELD)
end

#index_id(v) ⇒ Object



86
87
88
# File 'lib/gfa/record_set.rb', line 86

def index_id(v)
  v[index_field]&.value
end

#indexed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/gfa/record_set.rb', line 77

def indexed?
  (empty? || !index_field) ? gfa.opts[:index] : !index.empty?
end

#merge!(record_set) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/gfa/record_set.rb', line 120

def merge!(record_set)
  raise "Not a record set" unless record_set.is_a?(GFA::RecordSet)
  if record_set.type != type
    raise "Wrong type of record set: #{record_set.type}"
  end

  record_set.set.each { |i| @set << i }
  record_set.index.each { |k, v| save_index(k, v) }
end

#rebuild_index!Object



81
82
83
84
# File 'lib/gfa/record_set.rb', line 81

def rebuild_index!
  @index = {}
  set.each { |v| index!(v) }
end

#save_index(k, v) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/gfa/record_set.rb', line 99

def save_index(k, v)
  return unless gfa.opts[:index] && k

  if @index[k]
    f = index_field.is_a?(Integer) ? '' : "#{index_field}: "
    raise "#{type} already registered: #{f}#{k}"
  end
  @index[k] = v
end

#to_sObject



56
57
58
# File 'lib/gfa/record_set.rb', line 56

def to_s
  set.map(&:to_s).join("\n")
end

#typeObject



40
41
42
# File 'lib/gfa/record_set.rb', line 40

def type
  GFA::Record.CODES[code]
end