Class: Mspire::Mzml::IndexList

Inherits:
Array
  • Object
show all
Defined in:
lib/mspire/mzml/index_list.rb

Overview

A simple array of indices but #[] has been overloaded to find an index by name

index_list[0]  # the first index
index_list.map(&:name) # -> [:spectrum, :chromatogram] 
index_list[:spectrum]  # the spectrum index
index_list[:chromatogram]  # the chromatogram index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#in_groups

Class Method Details

.create_index_list(io) ⇒ Mspire::Mzml::IndexList

Reads through and captures start bytes



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mspire/mzml/index_list.rb', line 79

def create_index_list(io)
  indices_hash = io.bookmark(true) do |inner_io|   # sets to beginning of file
    indices = {:spectrum => {}, :chromatogram => {}}
    byte_total = 0
    io.each do |line|
      if md=%r{<(spectrum|chromatogram).*?id=['"](.*?)['"][ >]}.match(line)
        indices[md[1].to_sym][md[2]] = byte_total + md.pre_match.bytesize
      end
      byte_total += line.bytesize
    end
    indices
  end

  indices = indices_hash.map do |sym, hash|
    indices = Index.new ; ids = []
    hash.each {|id, startbyte| ids << id ; indices << startbyte }
    indices.ids = ids ; indices.name = sym
    indices
  end
  # we only return an index if there were some guys there
  indices.delete_if {|ind| ind.size == 0 }
  IndexList.new(indices)
end

.from_io(io) ⇒ Object

either reads in from file or creates an IndexList



39
40
41
# File 'lib/mspire/mzml/index_list.rb', line 39

def from_io(io)
  read_index_list(io) || create_index_list(io)
end

.index_offset(io, tag = 'indexListOffset', bytes_backwards = 200) ⇒ Object

returns an Integer or nil if not found does a single jump backwards from the tail of the file looking for an xml element based on tag. If it is not found, returns nil



46
47
48
49
50
51
# File 'lib/mspire/mzml/index_list.rb', line 46

def index_offset(io, tag='indexListOffset', bytes_backwards=200)
  tag_re = %r{<#{tag}>([\-\d]+)</#{tag}>}
    io.pos = (io.size - 1) - bytes_backwards
  md = io.readlines("\n").map {|line| line.match(tag_re) }.compact.shift
  md[1].to_i if md
end

.read_index_list(io) ⇒ Mspire::Mzml::IndexList

mzML

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mspire/mzml/index_list.rb', line 55

def read_index_list(io)
  if (offset = index_offset(io))
    io.seek(offset)
    xml = Nokogiri::XML.parse(io.read, nil, @encoding, Parser::NOBLANKS)
    index_list = xml.root
    num_indices = index_list['count'].to_i
    array = index_list.children.map do |index_n|
      #index = Index.new(index_n['name'])
      index = Index.new
      index.name = index_n['name'].to_sym
      ids = []
      index_n.children.map do |offset_n| 
        index << offset_n.text.to_i 
        ids << offset_n['idRef']
      end
      index.ids = ids
      index
    end
    IndexList.new(array)
  end
end

Instance Method Details

#[](int_or_symbol) ⇒ Mspire::Mzml::Index

Returns an index object.

Parameters:

  • an (Object)

    Integer (index number) or a Symbol (:spectrum or :chromatogram)

Returns:



18
19
20
21
22
23
24
# File 'lib/mspire/mzml/index_list.rb', line 18

def [](int_or_symbol)
  if int_or_symbol.is_a?(Integer)
    old_bracket_slice(int_or_symbol)
  else
    self.find {|index| index.name == int_or_symbol }
  end
end

#each_pair(&block) ⇒ Object

returns each name and associated index object



31
32
33
34
# File 'lib/mspire/mzml/index_list.rb', line 31

def each_pair(&block)
  block or return enum_for __method__
  each {|index| block.call([index.name, index]) }
end

#keysObject



26
27
28
# File 'lib/mspire/mzml/index_list.rb', line 26

def keys
  self.map(&:name)
end

#old_bracket_sliceObject



13
# File 'lib/mspire/mzml/index_list.rb', line 13

alias_method :old_bracket_slice, :'[]'