class Mspire::Mascot::Dat::Index

makes a byte index (not line index)

Constants

INDEX_EXT

Attributes

byte_num[RW]

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

query_num_to_byte[RW]

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

query_nums[RW]

an array of the query nums

Public Class Methods

index_filename(file) click to toggle source
# File lib/mspire/mascot/dat/index.rb, line 12
def index_filename(file)
  file + Dat::INDEX_EXT
end
new() click to toggle source

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

# File lib/mspire/mascot/dat/index.rb, line 31
def initialize
  @byte_num = {}
  @query_num_to_byte = []
  @query_nums = []
end

Public Instance Methods

[](key) click to toggle source

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.

# 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) click to toggle source

returns self

# 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) click to toggle source
# File lib/mspire/mascot/dat/index.rb, line 62
def from_file!(filename)
  File.open(filename) {|io| from_io!(io) }
end
from_io!(io) click to toggle source

returns self

# 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?() click to toggle source
# File lib/mspire/mascot/dat/index.rb, line 37
def has_queries?
  @query_nums.size > 0
end
query(n) click to toggle source

nil if the query is out of bounds

# File lib/mspire/mascot/dat/index.rb, line 106
def query(n)
  @query_num_to_byte[n]
end
write(filename) click to toggle source
# 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