pymzml / pymzML

pymzML - an interface between Python and mzML Mass spectrometry Files
https://pymzml.readthedocs.io/en/latest/
MIT License
162 stars 92 forks source link

Method pymzml.run.Reader.get_chromatogram_count() does not return chromatogram count #153

Closed fkromer closed 5 years ago

fkromer commented 5 years ago

Describe the bug

pymzml.run.Reader.get_chromatogram_count() throws a KeyError KeyError: 'chromatogram_count' when I try to get the chromatogram count for the exemplary data tests/data/example.mzML.

To Reproduce

With the data file data example.mzML in the same directory as a Jupyter notebook with following cell:

import pymzml
import os

current_dir = os.getcwd()
mzml_file_path = current_dir + '/example.mzML'
reader = pymzml.run.Reader( mzml_file_path )
reader.get_chromatogram_count()

throws

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-36-46dfe74fa1c6> in <module>
----> 1 reader.get_chromatogram_count()

/usr/local/lib/python3.6/dist-packages/pymzml/run.py in get_chromatogram_count(self)
    405             chromatogram count (int): Number of chromatograms in file.
    406         """
--> 407         return self.info["chromatogram_count"]
    408 
    409 

KeyError: 'chromatogram_count'

Expected behavior

Either (a) count of chromatograms or (b) human readable exception message about API miss-use.

Screenshots

n.a.

Desktop (please complete the following information):

Additional context

n.a.

MKoesters commented 5 years ago

Hey,

thanks for reporting this issue. I think this feature was developed for mzML files containing only chromatograms, at least it was implemented like this since v0.7.9 Since your files probably contains spectra, _init_iter finds the spectrum list and then stops parsing, else we would need to parse the whole file down to the chromatogram list. In files without spectra and only chromatograms this method works fine.

To 'fix' this, I would set the 'chromatogram_count' to None in the beginning and let it be overwritten if the file happens to be a chromatogram only file.

I'll push a fix later this day and may think about a more clever solution to find the chromatogram count without parsing the whole file.

Best, Manuel

fkromer commented 5 years ago

Hi Manuel,

thanks for the fast response. The files I will be dealing with won't contain chromatograms. I used the file in the examples for evaluation of and playing around with pymzml functionality. I do not need a fast fix.

Best, Florian