4.3. Example Scripts¶
4.3.1. Parsing a mzML file (new syntax)¶
-
simple_parser.main(mzml_file)[source]¶ Basic example script to demonstrate the usage of pymzML. Requires a mzML file as first argument.
- usage:
- ./simple_parser.py <path_to_mzml_file>
Note
This script uses the new syntax with the MS level being a property of the spectrum class ( Spectrum.ms_level ). The old syntax can be found in the script simple_parser.py where the MS level can be queried as a key (Spectrum[‘ms level’])
#!/usr/bin/env python3.4
import pymzml
import sys
def main(mzml_file):
'''
Basic example script to demonstrate the usage of pymzML. Requires a mzML
file as first argument.
usage:
./simple_parser.py <path_to_mzml_file>
Note:
This script uses the new syntax with the MS level being a property of
the spectrum class ( Spectrum.ms_level ). The old syntax can be found in
the script simple_parser.py where the MS level can be queried as a key
(Spectrum['ms level'])
'''
run = pymzml.run.Reader(mzml_file)
for n, spec in enumerate( run ):
print(
'Spectrum {0}, MS level {ms_level}'.format(
n,
ms_level = spec.ms_level
)
)
print(
'Parsed {0} spectra from file {1}'.format(
n,
mzml_file
)
)
print()
if __name__ == '__main__':
if len(sys.argv) < 2:
print(main.__doc__)
exit()
mzml_file = sys.argv[1]
main(mzml_file)
4.3.2. Parsing a mzML file (old syntax)¶
-
simple_parser_v2.main(mzml_file)[source]¶ Basic example script to demonstrate the usage of pymzML. Requires a mzML file as first argument.
- usage:
- ./simple_parser_v2.py <path_to_mzml_file>
Note
This script uses the old syntax where the MS level can be queried as a key (Spectrum[‘ms level’]). The current syntax can be found in simple_parser.py
#!/usr/bin/env python3.4
import pymzml
import sys
def main(mzml_file):
'''
Basic example script to demonstrate the usage of pymzML. Requires a mzML
file as first argument.
usage:
./simple_parser_v2.py <path_to_mzml_file>
Note:
This script uses the old syntax where the MS level can be queried as a
key (Spectrum['ms level']). The current syntax can be found in
simple_parser.py
'''
run = pymzml.run.Reader(mzml_file)
# print( run[10000].keys() )
for n, spec in enumerate( run ):
print(
'Spectrum {0}, MS level {ms_level}'.format(
n,
ms_level = spec['ms level']
),
end = '\r'
)
print(
'Parsed {0} spectra from file {1}'.format(
n,
mzml_file
)
)
print()
if __name__ == '__main__':
if len(sys.argv) < 2:
print(main.__doc__)
exit()
mzml_file = sys.argv[1]
main(mzml_file)
4.3.3. Plotting a chromatogram¶
-
plot_chromatogram.main(mzml_file)[source]¶ Plots a chromatogram for the given mzML file. File is saved as ‘chromatogram_<mzml_file>.html’.
- usage:
- ./plot_chromatogram.py <path_to_mzml_file>
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import pymzml
import os
from pymzml.plot import Factory
def main(mzml_file):
'''
Plots a chromatogram for the given mzML file. File is saved as
'chromatogram_<mzml_file>.html'.
usage:
./plot_chromatogram.py <path_to_mzml_file>
'''
run = pymzml.run.Reader( mzml_file )
mzml_basename = os.path.basename( mzml_file )
pf = Factory()
pf.new_plot()
pf.add(
run['TIC'].peaks,
color = (0, 0, 0),
style = 'lines',
title = mzml_basename
)
pf.save(
'chromatogram_{0}.html'.format(
mzml_basename
),
layout = {
'xaxis' : {
'title' : 'Retention time'
},
'yaxis' : {
'title' : 'TIC'
}
}
)
return
if __name__ == '__main__':
if len(sys.argv) < 2:
print(main.__doc__)
exit()
mzml_file = sys.argv[1]
main(mzml_file)
4.3.4. Extracting highest peaks¶
-
highest_peaks.main()[source]¶ Testscript to isolate the n-highest peaks from an example file.
Usage:
./highest_peaks.pyParses the file ‘../tests/data/example.mzML’ and extracts the 2 highest intensities from each spectrum.
#!/usr/bin/env python3.4
import pymzml
from collections import defaultdict as ddict
import os
def main():
"""
Testscript to isolate the n-highest peaks from an example file.
Usage:
./highest_peaks.py
Parses the file '../tests/data/example.mzML' and extracts the 2 highest
intensities from each spectrum.
"""
example_file = os.path.join(
os.path.dirname(__file__),
os.pardir,
'tests',
'data',
'example.mzML'
)
run = pymzml.run.Reader(
example_file
)
highest_i_dict = ddict(list)
number_of_peaks_to_extract = 2
for spectrum in run:
# print( spectrum.ID )
if spectrum.ms_level == 1:
for mz, i in spectrum.highest_peaks(number_of_peaks_to_extract):
highest_i_dict[ spectrum.ID ].append(i)
for spectrum_id, highest_peak_list in highest_i_dict.items():
assert len(highest_peak_list) == number_of_peaks_to_extract
print(
'Spectrum {0}; highest intensities: {1}'.format(
spectrum_id,
highest_peak_list
)
)
if __name__ == '__main__':
main()
4.3.5. Compare spectra¶
-
compare_spectra.main(verbose=False)[source]¶ Compare multiple spectra and return the cosine distance between them. The returned value is between 0 and 1, a returned value of 1 represents highest similarity.
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-
from __future__ import print_function
import pymzml
import os
def main(verbose = False):
"""
Compare multiple spectra and return the cosine distance between them.
The returned value is between 0 and 1, a returned value of 1
represents highest similarity.
"""
example_file = os.path.join(
os.path.dirname(__file__),
os.pardir,
'tests',
'data',
'example.mzML'
)
print(
"""
comparing specs
"""
)
# print(example_file)
run = pymzml.run.Reader(example_file)
tmp = []
for spec in run:
if spec.ms_level == 1:
print(
"Parsing spectrum lvl 1 has id {0}".format(
spec.ID
)
)
tmp.append( spec )
if len(tmp) >= 3:
break
print(
"Print total number of specs collected {0}".format(
len(tmp)
)
)
for compare_tuples in [ (0, 1), (0, 2), (1, 2) ]:
print(
"Cosine between spectra {0} & {1} is {2:1.4f}".format(
compare_tuples[0] + 1,
compare_tuples[1] + 1,
tmp[ compare_tuples[0] ].similarity_to( tmp[ compare_tuples[1] ] )
)
)
print(
"Cosine score between first spectrum against itself: {0:1.4f}".format(
tmp[0].similarity_to(tmp[0])
)
)
if __name__ == '__main__':
main()
4.3.6. Find m/z values¶
-
has_peak.main()[source]¶ Testscript to demonstrate functionality of function
spec.Spectrum.has_peak()
#!/usr/bin/env python3
from __future__ import print_function
import pymzml
import os
def main():
"""
Testscript to demonstrate functionality of function :py:func:`spec.Spectrum.has_peak`
"""
example_file = os.path.join(
os.path.dirname(__file__),
os.pardir,
'tests',
'data',
'example.mzML'
)
mz_to_find = 820.7711792
run = pymzml.run.Reader(example_file,)
for spectrum in run:
found_peaks = spectrum.has_peak(mz_to_find)
if found_peaks != []:
print(
'Found peaks: {0} in spectrum {1}'.format(
found_peaks,
spectrum.ID
)
)
if __name__ == '__main__':
main()