Class: Mspire::Mascot::Dat::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/mspire/mascot/dat/index.rb

Overview

makes a byte index (not line index)

Constant Summary collapse

INDEX_EXT =
'.byteindex'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndex

if handed an index_bytefile it will open the filename and use that for the index



31
32
33
34
35
# File 'lib/mspire/mascot/dat/index.rb', line 31

def initialize
  @byte_num = {}
  @query_num_to_byte = []
  @query_nums = []
end

Instance Attribute Details

#byte_numObject

the hash holding the start byte for each section (besides the queries). Keyed by symbol.



19
20
21
# File 'lib/mspire/mascot/dat/index.rb', line 19

def byte_num
  @byte_num
end

#query_num_to_byteObject

the array holding the start byte for each query. It is indexed by query number, so the first



23
24
25
# File 'lib/mspire/mascot/dat/index.rb', line 23

def query_num_to_byte
  @query_num_to_byte
end

#query_numsObject

an array of the query nums



26
27
28
# File 'lib/mspire/mascot/dat/index.rb', line 26

def query_nums
  @query_nums
end

Class Method Details

.index_filename(file) ⇒ Object



12
13
14
# File 'lib/mspire/mascot/dat/index.rb', line 12

def index_filename(file)
  file + Dat::INDEX_EXT
end

Instance Method Details

#[](key) ⇒ Object

given a string or symbol, looks up the start line. Given an Integer, assumes it is a query num and returns the start line of the query number.



97
98
99
100
101
102
103
# File 'lib/mspire/mascot/dat/index.rb', line 97

def [](key)
  if key.is_a?(Integer) 
    @query_num_to_byte[key]
  else
    @byte_num[key.to_sym]
  end
end

#from_byteindex!(filename) ⇒ Object

returns self



42
43
44
45
46
47
48
49
# File 'lib/mspire/mascot/dat/index.rb', line 42

def from_byteindex!(filename)
  hash = JSON.parse!( IO.read(filename) )
  [:byte_num, :query_num_to_byte, :query_nums].each do |key|
    self.send("#{key}=", hash[key.to_s])
  end
  @byte_num.keys.each {|k| @byte_num[k.to_sym] = @byte_num.delete(k) }
  self
end

#from_file!(filename) ⇒ Object



62
63
64
# File 'lib/mspire/mascot/dat/index.rb', line 62

def from_file!(filename)
  File.open(filename) {|io| from_io!(io) }
end

#from_io!(io) ⇒ Object

returns self



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
# File 'lib/mspire/mascot/dat/index.rb', line 67

def from_io!(io)
  io.rewind
  while line=io.gets
    io.each_line do |line|
      if md=/^Content-Type: application\/x-Mascot; name=["'](\w+)["']/.match(line)
        head = md[1]
        io.gets # the newline
        pos = io.pos

        if qmd=/query(\d+)/.match(head)
          query_num = qmd[1].to_i
          @query_nums << query_num                
          @query_num_to_byte[query_num] = pos
        else
          @byte_num[head.to_sym] = pos
        end
      end
    end
  end
  io.rewind

  @query_nums.freeze
  @query_num_to_byte.freeze
  @byte_num.freeze
  self
end

#has_queries?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/mspire/mascot/dat/index.rb', line 37

def has_queries?
  @query_nums.size > 0
end

#query(n) ⇒ Object

nil if the query is out of bounds



106
107
108
# File 'lib/mspire/mascot/dat/index.rb', line 106

def query(n)
  @query_num_to_byte[n]
end

#write(filename) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/mspire/mascot/dat/index.rb', line 51

def write(filename)
  File.open(filename,'w') do |io|
    JSON.dump(
      {
      byte_num: byte_num,
      query_num_to_byte: query_num_to_byte,
      query_nums: query_nums,
    }, io)
  end
end