# 7. Example scripts

In this section, example scripts for the usage of pymzML can be found.

## 7.1. Finding a peak

hasPeak.py

Testscript to demonstrate functionality of function `spec.Spectrum.hasPeak()` or `spec.Spectrum.hasDeconvolutedPeak()`

Example:

    >>> import pymzml, get_example_file
    >>> example_file = get_example_file.open_example('deconvolution.mzml.gz')
    >>> run = pymzml.run.Reader(example_file, MS1_Precision = 5e-6, MSn_Precision = 20e-6)
    >>> for spectrum in run:
    ...     if spectrum["ms level"] == 2:
    ...             peak_to_find = spectrum.haspeak(1016.5404)
    ...             print(peak_to_find)
    (1016.5404, 19141.735187697403)

## 7.2. Plotting a spectrum

plotAspec.py

This function shows how to plot a simple spectrum. It can be directly plotted via this script or using the python console

Example of plotting a spectrum:

    >>> import pymzml, get_example_file
    >>> mzMLFile = 'profile-mass-spectrum.mzml'
    >>> get_example_file.open_example(mzMLFile)
    >>> run = pymzml.run.Reader("mzML_example_files/"+mzMLFile, MSn_Precision = 25e-6)
    >>> p = pymzml.plot.Factory()
    >>> for spec in run:
    >>>     p.newPlot()
    >>>     p.add(spec.peaks, color=(200,0,0), style='circles')
    >>>     p.add(spec.centroidedPeaks, color=(0,0,0), style='sticks')
    >>>     p.add(spec.reprofiledPeaks, color=(0,255,0), style='circles')
    >>>     p.save( filename="output/plotAspect.xhtml" , mzRange = [744.7,747] )

output as xhtml :)

## 7.3. Abundant precursor

find_abundant_precursors.py

Testscript to demonstrate an easy isoloation of the most abundant, isolated precusors for MSn spectra.Thus exclusion list for further MS runs can be generated. In this example all precursors which were isolated more than 5 times are found and printed.

Example:

    >>> import pymzml, get_example_file, operator.item
    >>> import collections import defaultdict as ddict
    >>> from operator import itemgetter
    >>> example_file = get_example_file.open_example('dta_example.mzML')
    >>> run = pymzml.run.Reader(example_file , MS1_Precision = 5e-6 , MSn_Precision = 20e-6 )
    >>> precursor_count_dict = ddict(int)
    >>> for spectrum in run:
    >>>    if spectrum["ms level"] == 2:
    >>>        if "precursors" in spectrum.keys():
    >>>            precursor_count_dict[round(float(spectrum["precursors"][0]["mz"]),3)] += 1
    >>> for precursor, frequency in sorted(precursor_count_dict.items()):
    >>>     print("{0}\t{1}".format(precursor, frequency))

## 7.4. Compare Spectra

compareSpectra.py

Compare two 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

    >>> spec1 = pymzml.spec.Spectrum(measuredPrecision = 20e-5)
    >>> spec2 = pymzml.spec.Spectrum(measuredPrecision = 20e-5)
    >>> spec1.peaks = [ ( 1500,1 ), ( 1502,2.0 ), (1300,1 )]
    >>> spec2.peaks = [ ( 1500,1 ), ( 1502,1.3 ), (1400,2 )]
    >>> spec1.similarityTo( spec2 )
    0.5682164685724541
    >>> spec1.similarityTo( spec1 )
    1.0000000000000002

## 7.5. Query Obo files

queryOBO.py

%(prog)s [-h] [-v VERSION] query

Use this script to interogate the OBO database files.

example

$ %(prog)s ‘scan time’ MS:1000016 scan time ‘The time taken for an acquisition by scanning analyzers.’ [PSI:MS] Is a: MS:1000503 ! scan attribute

## 7.6. Highest peaks

highestPeaks.py

Testscript to isolate the n-highest peaks from an example file.

Example:

    >>> example_file = get_example_file.open_example('deconvolution.mzML.gz')
    >>> run = pymzml.run.Reader(example_file, MS1_Precision = 5e-6, MSn_Precision = 20e-6)
    >>> for spectrum in run:
    ...     if spectrum["ms level"] == 2:
    ...         if spectrum["id"] == 1770:
    ...             for mz,i  in spectrum.highestPeaks(5):
    ...                 print(mz,i)

## 7.7. extract a specific Ion Chromatogram (EIC, XIC)

extractIonChromatogram.py

Demonstration of the extraction of a specific ion chromatogram, i.e. XIC or EIC

Example:

    >>> import pymzml, get_example_file
    >>> example_file = get_example_file.open_example('small.pwiz.1.1.mzML')
    >>> run = pymzml.run.Reader(example_file, MS1_Precision = 20e-6, MSn_Precision = 20e-6)
    >>> timeDependentIntensities = []
    >>> for spectrum in run:
    ...     if spectrum['ms level'] == 1:
    ...         matchList = spectrum.hasPeak(MASS_2_FOLLOW)
    ...         if matchList != []:
    ...             for mz,I in matchList:
    ...                 timeDependentIntensities.append( [ spectrum['scan time'], I , mz ])
    >>> for rt, i, mz in timeDependentIntensities:
    ...     print('{0:5.3f} {1:13.4f}       {2:10}'.format( rt, i, mz ))

## 7.8. Accessing the original XML Tree of a spectrum

accessAllData.py

accessAllData.py

Demos the usage of the spectrum.xmlTree iterator that can be used to extract all MS:tag for a given spectrum.

Example:

    >>> example_file = get_example_file.open_example('small.pwiz.1.1.mzML')
    >>> run = pymzml.run.Reader(example_file, MSn_Precision = 250e-6)
    >>> spectrum = run[1]
    >>> for element in spectrum.xmlTree:
    ...     print('-'*40)
    ...     print(element)
    ...     print(element.get('accession') )
    ...     print(element.tag)
    ...     print(element.items())

## 7.9. Write mzML

writeExample.py

This is part is still in development.

Simple test of the mzML writing functionality.

This is very priliminary. The ‘header’ is copied into the new file with some addition in the softwareList XML tag, hence a pymzml.run.Reader Object needs to be passed over to the write function.

Example:

    >>> example_file = get_example_file.open_example('small.pwiz.1.1.mzML')
    >>> run  = pymzml.run.Reader(example_file, MS1_Precision = 5e-6)
    >>> run2 = pymzml.run.Writer(filename = 'write_test.mzML', run= run , overwrite = True)
    >>> specOfIntrest = run[2]
    >>> run2.addSpec(spec)
    >>> run2.save()