MS::Reader::MzXML - A simple but complete mzXML parser


    use MS::Reader::MzXML;

    my $run = MS::Reader::MzXML->new('run.mzXML');

    while (my $spectrum = $run->next_spectrum) {
        # only want MS1
        next if ($spectrum->ms_level > 1);

        my $rt = $spectrum->rt;
        # see MS::Reader::MzXML::Spectrum and MS::Spectrum for all available
        # methods


    $spectrum = $run->fetch_spectrum(0);  # first spectrum
    $spectrum = $run->find_by_time(1500); # in seconds


MS::Reader::MzXML is a parser for the mzXML format for raw mass spectrometry data. It aims to provide complete access to the data contents while not being overburdened by detailed class infrastructure. Convenience methods are provided for accessing commonly used data. Users who want to extract data not accessible through the available methods should examine the data structure of the parsed object. The dump() method of MS::Reader::XML, from which this class inherits, provides an easy method of doing so.


MS::Reader::MzXML is a subclass of MS::Reader::XML, which in turn inherits from MS::Reader, and inherits the methods of these parental classes. Please see the documentation for those classes for details of available methods not detailed below.



    my $run = MS::Reader::MzXML->new( $fn,
        use_cache => 0,
        paranoid  => 0,

Takes an input filename (required) and optional argument hash and returns an MS::Reader::MzXML object. This constructor is inherited directly from MS::Reader. Available options include:

  • use_cache — cache fetched records in memory for repeat access (default: FALSE)

  • paranoid — when loading index from disk, recalculates MD5 checksum each time to make sure raw file hasn't changed. This adds (typically) a few seconds to load times. By default, only file size and mtime are checked.


    while (my $s = $run->next_spectrum) {
        # do something

Returns an MS::Reader::MzXML::Spectrum object representing the next spectrum in the file, or undef if the end of records has been reached. Typically used to iterate over each spectrum in the run.


    my $s = $run->fetch_spectrum($idx);

Takes a single argument (zero-based spectrum index) and returns an MS::Reader::MzXML::Spectrum object representing the spectrum at that index. Throws an exception if the index is out of range.



Takes a single argument (zero-based spectrum index) and sets the spectrum record iterator to that index (for subsequent calls to next_spectrum).


    my $idx = $run->find_by_time($rt);

Takes a single argument (retention time in SECONDS) and returns the index of the nearest spectrum with retention time equal to or greater than that given. Throws an exception if the given retention time is out of range.

NOTE: The first time this method is called, the spectral indices are sorted by retention time for subsequent access. This can be a bit slow. The retention time index is saved and subsequent calls should be relatively quick. This is done because the mzXML specification doesn't guarantee that the spectra are ordered by RT (even though they invariably are).


    my $n = $run->n_spectra;

Returns the number of spectra present in the file.


The mzXML format allows for nested <scan> elements (e.g. nesting MS2 scans within the parent MS1). However, this is not currently supported by the parser and will throw an exception if detected. It is possible (and arguably preferable) to represent such files using a flat scan list - this is how msconvert currently formats its mzXML output. Lack of support is due to lack of demand - if this feature is desired it could be implemented with minimal trouble.

The API is in alpha stage and is not guaranteed to be stable.

Please reports bugs or feature requests through the issue tracker at



Jeremy Volkening <>


Copyright 2015-2016 Jeremy Volkening

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <>.