Sha256: 59fbbe30615656287c9b05aa0bce126817d86ce136094e51f8df071ca178008f

Contents?: true

Size: 1.86 KB

Versions: 14

Compression:

Stored size: 1.86 KB

Contents

require 'mspire/mzml/parser'
require 'mspire/mzml/spectrum'
require 'mspire/mzml/chromatogram'

module Mspire
  class Mzml

    # an index that retrieves its objects on the fly by index from the IO object.
    class IOIndex
      include Enumerable

      attr_reader :io

      attr_reader :byte_index

      # hash of relevant hashes and objects for linking
      attr_accessor :link

      # byte_index will typically be an Mspire::Mzml::Index object.
      #
      # link will have the following keys:
      #
      #     :ref_hash
      #     :data_processing_hash
      #     :(<sample>|<chromatogram>)_default_data_processing
      #
      # may have:
      #
      #     :source_file_hash
      #
      def initialize(io, byte_index, link)
        @io, @byte_index, @link = io, byte_index, link
        @object_class = Mspire::Mzml.const_get(@byte_index.name.to_s.capitalize)
        @closetag_regexp = %r{</#{name}>}
      end

      def name
        @byte_index.name
      end

      def each(&block)
        return to_enum(__method__) unless block
        (0...byte_index.size).each do |int|
          block.call(self[int])
        end
      end

      def [](index)
        @object_class.from_xml(fetch_xml_node(index), @link)
      end

      def length
        @byte_index.length
      end
      alias_method :size, :length

      # gets the data string through to last element
      def get_xml_string(start_byte)
        @io.seek(start_byte)
        data = ""
        @io.each_line do |line|
          data << line 
          break if @closetag_regexp.match(line)
        end
        data
      end

      def xml_node_from_start_byte(start_byte)
        xml = get_xml_string(start_byte)
        Nokogiri::XML.parse(xml, nil, @encoding, Parser::NOBLANKS).root
      end

      def fetch_xml_node(index)
        xml_node_from_start_byte(byte_index[index])
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mspire-0.10.7.1 lib/mspire/mzml/io_index.rb
mspire-0.10.7 lib/mspire/mzml/io_index.rb
mspire-0.10.6 lib/mspire/mzml/io_index.rb
mspire-0.10.5 lib/mspire/mzml/io_index.rb
mspire-0.10.4 lib/mspire/mzml/io_index.rb
mspire-0.10.3 lib/mspire/mzml/io_index.rb
mspire-0.10.2 lib/mspire/mzml/io_index.rb
mspire-0.10.1 lib/mspire/mzml/io_index.rb
mspire-0.10.0 lib/mspire/mzml/io_index.rb
mspire-0.9.2 lib/mspire/mzml/io_index.rb
mspire-0.9.1 lib/mspire/mzml/io_index.rb
mspire-0.9.0 lib/mspire/mzml/io_index.rb
mspire-0.8.7 lib/mspire/mzml/io_index.rb
mspire-0.8.6.2 lib/mspire/mzml/io_index.rb