| 1 |
.. code-block:: python |
| 2 |
|
| 3 |
#!/usr/bin/env python |
| 4 |
# -*- coding: utf-8 -*- |
| 5 |
|
| 6 |
import copy |
| 7 |
import os |
| 8 |
|
| 9 |
import pymzml |
| 10 |
|
| 11 |
|
| 12 |
def main(): |
| 13 |
""" |
| 14 |
This script shows how to plot multiple spectra in one plot and |
| 15 |
how to use label for the annotation of spectra. |
| 16 |
The first plot is an MS1 spectrum with the annotated precursor ion. |
| 17 |
The second plot is a zoom into the precursor isotope pattern. |
| 18 |
The third plot is an annotated fragmentation spectrum (MS2) of the |
| 19 |
peptide HLVDEPQNLIK from BSA. |
| 20 |
These examples also show the use of 'layout' to define the appearance |
| 21 |
of a plot. |
| 22 |
|
| 23 |
usage: |
| 24 |
|
| 25 |
./plot_spectrum_with_annotation.py |
| 26 |
|
| 27 |
""" |
| 28 |
|
| 29 |
# First we define some general layout attributes |
| 30 |
layout = { |
| 31 |
"xaxis": { |
| 32 |
"title": "<i>m/z</i>", |
| 33 |
"tickmode": "auto", |
| 34 |
"showticklabels": True, |
| 35 |
"ticklen": 5, |
| 36 |
"tickwidth": 1, |
| 37 |
"ticks": "outside", |
| 38 |
"showline": True, |
| 39 |
"showgrid": False, |
| 40 |
}, |
| 41 |
"yaxis": { |
| 42 |
"color": "#000000", |
| 43 |
"tickmode": "auto", |
| 44 |
"showticklabels": True, |
| 45 |
"ticklen": 5, |
| 46 |
"tickwidth": 1, |
| 47 |
"ticks": "outside", |
| 48 |
"showline": True, |
| 49 |
"showgrid": False, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
# The example BSA file will be used |
| 54 |
example_file = os.path.join( |
| 55 |
os.path.dirname(__file__), os.pardir, "tests", "data", "BSA1.mzML.gz" |
| 56 |
) |
| 57 |
|
| 58 |
# Define different precisions for MS1 and MS2 |
| 59 |
run = pymzml.run.Reader(example_file, MS_precisions={1: 5e-6, 2: 5e-4}) |
| 60 |
p = pymzml.plot.Factory() |
| 61 |
plot_layout = {} |
| 62 |
|
| 63 |
# Now that everything is set up, we can plot the MS1 spectrum |
| 64 |
# Spectrum ID: 1574 |
| 65 |
p.new_plot(title="MS1 Spectrum") |
| 66 |
ms1_spectrum = run[1574] |
| 67 |
|
| 68 |
# The measured peaks are added as first trace |
| 69 |
p.add( |
| 70 |
ms1_spectrum.peaks("centroided"), |
| 71 |
color=(0, 0, 0), |
| 72 |
opacity=1, |
| 73 |
style="sticks", |
| 74 |
name="raw data plot 1", |
| 75 |
) |
| 76 |
|
| 77 |
# The label for the precursor ion is added as a seperate trace. |
| 78 |
# Note that triangle.MS_precision is used here as a label. |
| 79 |
# By zooming in at this peak one can therefore check if the measured |
| 80 |
# peak fits into defined the mass accuracy range. |
| 81 |
precursor_mz_calc = 435.9102 |
| 82 |
p.add( |
| 83 |
[(precursor_mz_calc, "max_intensity", "theoretical precursor")], |
| 84 |
color=(255, 0, 0), |
| 85 |
opacity=0.6, |
| 86 |
style="label.triangle.MS_precision", |
| 87 |
name="theoretical precursor plot 1", |
| 88 |
) |
| 89 |
|
| 90 |
# Define the layout for the first subplot. |
| 91 |
# The x- and y-axes of subplots are numbered, starting at 1. |
| 92 |
for axis in layout.keys(): |
| 93 |
plot_layout["{0}1".format(axis)] = copy.copy(layout[axis]) |
| 94 |
|
| 95 |
# Now we can add a second plot, the same way as above but as a zoom-in. |
| 96 |
# Therefore, we define a mz_range |
| 97 |
p.new_plot(title="MS1 Spectrum Zoom") |
| 98 |
p.add( |
| 99 |
ms1_spectrum.peaks("centroided"), |
| 100 |
color=(0, 0, 0), |
| 101 |
opacity=1, |
| 102 |
style="sticks", |
| 103 |
name="raw data plot 2", |
| 104 |
plot_num=1, |
| 105 |
mz_range=[435.7, 437], |
| 106 |
) |
| 107 |
|
| 108 |
p.add( |
| 109 |
[(precursor_mz_calc, "max_intensity", "theoretical precursor")], |
| 110 |
color=(255, 0, 0), |
| 111 |
opacity=0.3, |
| 112 |
plot_num=1, |
| 113 |
style="label.triangle.MS_precision", |
| 114 |
name="theoretical precursor plot 2", |
| 115 |
) |
| 116 |
|
| 117 |
# The mz_range can be included in the layout as well. |
| 118 |
# In contrast to mz_range in the add() function, which limits the included |
| 119 |
# datapoints, the layout range only defines the area that is depicted (i.e. the zoom) |
| 120 |
for axis in layout.keys(): |
| 121 |
plot_layout["{0}2".format(axis)] = copy.copy(layout[axis]) |
| 122 |
plot_layout["xaxis2"]["autorange"] = False |
| 123 |
plot_layout["xaxis2"]["range"] = [435.7, 437] |
| 124 |
|
| 125 |
# Now the third plot will be added, a fragmentation spectrum of HLVDEPQNLIK |
| 126 |
ms2_spectrum = run[3542] |
| 127 |
|
| 128 |
# The MS_precision for the plotting option label.triangle.MS_precision |
| 129 |
# needs to be defined |
| 130 |
p.new_plot(title="MS2 Spectrum Annotated: HLVDEPQNLIK", MS_precision=5e-4) |
| 131 |
p.add( |
| 132 |
ms2_spectrum.peaks("centroided"), |
| 133 |
color=(0, 0, 0), |
| 134 |
opacity=1, |
| 135 |
style="sticks", |
| 136 |
name="raw data plot 3", |
| 137 |
plot_num=2, |
| 138 |
) |
| 139 |
|
| 140 |
theoretical_b_ions = { |
| 141 |
"b<sub>2</sub><sup>+2</sup>": 126.0788, |
| 142 |
"b<sub>3</sub><sup>+2</sup>": 175.6130, |
| 143 |
"b<sub>4</sub><sup>+2</sup>": 233.1264, |
| 144 |
"b<sub>2</sub>": 251.1503, |
| 145 |
"b<sub>5</sub><sup>+2</sup>": 297.6477, |
| 146 |
"b<sub>6</sub><sup>+2</sup>": 346.1741, |
| 147 |
"b<sub>3</sub>": 350.2187, |
| 148 |
"b<sub>7</sub><sup>+2</sup>": 410.2034, |
| 149 |
"b<sub>4</sub>": 465.2456, |
| 150 |
"b<sub>8</sub><sup>+2</sup>": 467.2249, |
| 151 |
"b<sub>9</sub><sup>+2</sup>": 523.7669, |
| 152 |
"b<sub>10</sub><sup>+2</sup>": 580.3089, |
| 153 |
"b<sub>5</sub>": 594.2882, |
| 154 |
"b<sub>6</sub>": 691.341, |
| 155 |
"b<sub>7</sub>": 819.3995, |
| 156 |
"b<sub>8</sub>": 933.4425, |
| 157 |
"b<sub>9</sub>": 1046.5265, |
| 158 |
"b<sub>10</sub>": 1159.6106, |
| 159 |
} |
| 160 |
|
| 161 |
theoretical_y_ions = { |
| 162 |
"y<sub>1</sub><sup>+2</sup>": 74.0600, |
| 163 |
"y<sub>2</sub><sup>+2</sup>": 130.6021, |
| 164 |
"y<sub>1</sub>": 147.1128, |
| 165 |
"y<sub>3</sub><sup>+2</sup>": 187.1441, |
| 166 |
"y<sub>4</sub><sup>+2</sup>": 244.1656, |
| 167 |
"y<sub>2</sub>": 260.1969, |
| 168 |
"y<sub>5</sub><sup>+2</sup>": 308.1949, |
| 169 |
"y<sub>6</sub><sup>+2</sup>": 356.7212, |
| 170 |
"y<sub>3</sub>": 373.2809, |
| 171 |
"y<sub>7</sub><sup>+2</sup>": 421.2425, |
| 172 |
"y<sub>8</sub><sup>+2</sup>": 478.7560, |
| 173 |
"y<sub>4</sub>": 487.3239, |
| 174 |
"y<sub>9</sub><sup>+2</sup>": 528.2902, |
| 175 |
"y<sub>10</sub><sup>+2</sup>": 584.8322, |
| 176 |
"y<sub>5</sub>": 615.3824, |
| 177 |
"y<sub>6</sub>": 712.4352, |
| 178 |
"y<sub>7</sub>": 841.4778, |
| 179 |
"y<sub>8</sub>": 956.5047, |
| 180 |
"y<sub>9</sub>": 1055.5732, |
| 181 |
"y<sub>10</sub>": 1168.6572, |
| 182 |
} |
| 183 |
|
| 184 |
# Check which theoretical fragments are present in the spectrum |
| 185 |
# using the has_peak() function |
| 186 |
for ion_list in [theoretical_b_ions, theoretical_y_ions]: |
| 187 |
label_list = [] |
| 188 |
for fragment in ion_list.keys(): |
| 189 |
peak = ms2_spectrum.has_peak(ion_list[fragment]) |
| 190 |
if len(peak) != 0: |
| 191 |
label_list.append((ion_list[fragment], peak[0][1], fragment)) |
| 192 |
if ion_list == theoretical_b_ions: |
| 193 |
color = (0, 0, 255) |
| 194 |
else: |
| 195 |
color = (0, 255, 0) |
| 196 |
p.add( |
| 197 |
label_list, |
| 198 |
color=color, |
| 199 |
style="label.triangle.MS_precision", |
| 200 |
name="theoretical fragment ions plot 3", |
| 201 |
) |
| 202 |
|
| 203 |
for axis in layout.keys(): |
| 204 |
plot_layout["{0}3".format(axis)] = copy.copy(layout[axis]) |
| 205 |
|
| 206 |
# Save the plot in a file using the defined plot_layout |
| 207 |
filename = "example_plot_{0}_annotation.html".format(os.path.basename(example_file)) |
| 208 |
p.save(filename=filename, layout=plot_layout) |
| 209 |
print("Plotted file: {0}".format(filename)) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
main() |