Class: MzML::Doc

Inherits:
File
  • Object
show all
Defined in:
lib/mzml/doc.rb

Overview

The main mzML parser class, it is a subclass of the File class from the Ruby standard library in that it places a read cursor on the mzML file, and will skip around using byte-offsets. We utilize the index at the end of mzML files to facilitate random access of spectra.

The #each method will cycle through all of the spectrum in a file, starting from the first one each time. If you would rather access the spectra randomly, the #spectrum_list attribute contains the ordered list of specturm identifiers. You can access the MzML::Spectrum objects by feeding these identifiers to the #spectrum method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mz_fname) ⇒ Doc

Open a file handle to a mzML document



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mzml/doc.rb', line 54

def initialize(mz_fname)
  unless mz_fname =~ /\.mzML$/
    raise MzML::UnsupportedFileFormat.new  "File extension must be .\"mzML\""
  end
  super(mz_fname, "r")
  @fname = mz_fname
  @index = parse_index_list
  @spectrum_count = @spectrum_list.length
  @chromatogram_count = @chromatogram_list.length
  @current_spectrum_index = 0
end

Instance Attribute Details

#chromatogram_countObject (readonly)

Returns the value of attribute chromatogram_count.



65
66
67
# File 'lib/mzml/doc.rb', line 65

def chromatogram_count
  @chromatogram_count
end

#chromatogram_listObject (readonly)

Returns the value of attribute chromatogram_list.



65
66
67
# File 'lib/mzml/doc.rb', line 65

def chromatogram_list
  @chromatogram_list
end

#fnameObject (readonly)

Returns the value of attribute fname.



65
66
67
# File 'lib/mzml/doc.rb', line 65

def fname
  @fname
end

#indexObject (readonly)

Returns the value of attribute index.



65
66
67
# File 'lib/mzml/doc.rb', line 65

def index
  @index
end

#spectrum_countObject (readonly)

Returns the value of attribute spectrum_count.



65
66
67
# File 'lib/mzml/doc.rb', line 65

def spectrum_count
  @spectrum_count
end

#spectrum_listObject (readonly)

Returns the value of attribute spectrum_list.



65
66
67
# File 'lib/mzml/doc.rb', line 65

def spectrum_list
  @spectrum_list
end

Instance Method Details

#chromatogram(chromatogram_id) ⇒ MzML::Chromatogram

Fetch a Chromatogram from the file, given the identifier

Parameters:

  • chromatogram_id

    String

Returns:



70
71
72
73
74
75
76
77
# File 'lib/mzml/doc.rb', line 70

def chromatogram(chromatogram_id)
  if @index[:chromatogram].has_key? chromatogram_id
    self.seek @index[:chromatogram][chromatogram_id]
    return MzML::Chromatogram.new(parse_next)
  else
    raise MzML::BadIdentifier.new("Invalid ID '#{chromatogram_id}'")
  end
end

#each(&block) ⇒ Object Also known as: each_spectrum



88
89
90
91
92
93
# File 'lib/mzml/doc.rb', line 88

def each &block
  @spectrum_list.each do |spectrum_id|
    block.call(self.spectrum(spectrum_id))
    @current_spectrum_index += 1
  end
end

#next(&block) ⇒ Object Also known as: next_spectrum



96
97
98
99
100
101
102
103
# File 'lib/mzml/doc.rb', line 96

def next &block
  if @current_spectrum_index < @spectrum_list.length
    @current_spectrum_index += 1
    self.spectrum(@spectrum_list[@current_spectrum_index - 1])
  else
    nil
  end
end

#rewindObject



106
107
108
109
# File 'lib/mzml/doc.rb', line 106

def rewind
  super
  @current_spectrum_index = 0
end

#spectrum(spectrum_id) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/mzml/doc.rb', line 79

def spectrum(spectrum_id)
  if @index[:spectrum].has_key? spectrum_id
    self.seek @index[:spectrum][spectrum_id]
    return MzML::Spectrum.new(parse_next())
  else
    raise MzML::BadIdentifier.new("Invalid ID '#{spectrum_id}'")
  end
end