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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mzml/doc.rb', line 62

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
  @current_chromatogram_index = 0
end

Instance Attribute Details

#chromatogram_countObject (readonly)

Returns the value of attribute chromatogram_count.



74
75
76
# File 'lib/mzml/doc.rb', line 74

def chromatogram_count
  @chromatogram_count
end

#chromatogram_listObject (readonly)

Returns the value of attribute chromatogram_list.



74
75
76
# File 'lib/mzml/doc.rb', line 74

def chromatogram_list
  @chromatogram_list
end

#fnameObject (readonly)

Returns the value of attribute fname.



74
75
76
# File 'lib/mzml/doc.rb', line 74

def fname
  @fname
end

#indexObject (readonly)

Returns the value of attribute index.



74
75
76
# File 'lib/mzml/doc.rb', line 74

def index
  @index
end

#spectrum_countObject (readonly)

Returns the value of attribute spectrum_count.



74
75
76
# File 'lib/mzml/doc.rb', line 74

def spectrum_count
  @spectrum_count
end

#spectrum_listObject (readonly)

Returns the value of attribute spectrum_list.



74
75
76
# File 'lib/mzml/doc.rb', line 74

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:



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

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



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

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

#each_chromatogram(&block) ⇒ Object



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

def each_chromatogram &block
  @chromatogram_list.each do |chromatogram_id|
    block.call(self.chromatogram(chromatogram_id))
    @current_chromatogram_index += 1
  end
end

#nextObject Also known as: next_spectrum



112
113
114
115
116
117
118
119
# File 'lib/mzml/doc.rb', line 112

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

#next_chromatogramObject



122
123
124
125
126
127
128
129
# File 'lib/mzml/doc.rb', line 122

def next_chromatogram
  if @current_chromatogram_index < @chromatogram_list.length
    @current_chromatogram_index += 1
    self.chromatogram(@chromatogram_list[@current_chromatogram_index - 1])
  else
    nil
  end
end

#rewindObject



131
132
133
134
135
136
# File 'lib/mzml/doc.rb', line 131

def rewind
  super
  @current_spectrum_index = 0
  @current_chromatogram_index = 0
  self.pos
end

#spectrum(spectrum_id) ⇒ Object



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

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