-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmzmlplot.py
360 lines (298 loc) · 15.7 KB
/
mzmlplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import os
import sys
from PyQt6 import uic
from PyQt6.QtGui import *
from PyQt6.QtCore import pyqtSlot, Qt, pyqtSignal
from PyQt6.QtWidgets import *
from PyQt6.uic import loadUi
import pyopenms as oms
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pandas as pd
from mzmlplotter_ui_v110 import Ui_MainWindow
from scipy.signal import find_peaks
class MainWindow(QMainWindow): #setup GUI window
def __init__ (self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#connect buttons to functions
self.ui.fileselectButton.clicked.connect(self.file_finder)
self.ui.plotButton.clicked.connect(self.plot)
self.show()
def extract_intensity(self, spectrum, mz_value):
try:
#Peak pick for closest peak to desired m/z value
closest_peak = min(spectrum, key=lambda x: abs(x.getMZ() - mz_value))
#return intensity of the closest peak
return closest_peak.getIntensity()
except ValueError: #if a peak is not present, an empty line will be added to the dataframe
return None
def file_finder(self):
#User input for input file location - must be mzml currently
file_dialog_result = QFileDialog.getOpenFileName()
selected_file_path = file_dialog_result[0]
if selected_file_path:
self.ui.fileLineedit.setText(selected_file_path)
def plot(self): #connect mode select dropdown menu to each plotting mode
modeboxindex = self.ui.modeBox.currentIndex()
if modeboxindex == 0:
self.plotsinglescan()
elif modeboxindex == 1:
self.plotmergedscans()
elif modeboxindex == 2:
self.plotmergedms2()
elif modeboxindex == 3:
self.plottic()
elif modeboxindex == 4:
self.ploteic()
def update_annotations(self, ax, peaks_df): #function to dynamically add annotations depending on zoom level
#get current xy range from plot
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Clear existing annotations
for artist in ax.get_children():
if isinstance(artist, plt.Annotation):
artist.remove()
# Filter the xy DataFrame based on the current view limits
visible_peaks = peaks_df[(peaks_df['m/z'] >= xlim[0]) & (peaks_df['m/z'] <= xlim[1]) & (peaks_df['intensity'] >= ylim[0]) & (peaks_df['intensity'] <= ylim[1])]
if not visible_peaks.empty:
# Calculate the maximum intensity in the current view
max_intensity = visible_peaks['intensity'].max()
# Set the annotation threshold
threshold_percentage = self.ui.annothresh_Spinbox.value()/100
threshold_value = max_intensity * threshold_percentage
# Filter peaks based on the threshold value
filtered_peaks = visible_peaks[visible_peaks['intensity'] >= threshold_value]
# Annotate the top N filtered peaks
N = self.ui.maxanno_Spinbox.value()
top_filtered_peaks = filtered_peaks.nlargest(N, 'intensity')
for i, row in top_filtered_peaks.iterrows():
ax.annotate(f'{row["m/z"]:.5f}', xy=(row['m/z'], row['intensity']),
xytext=(10, 10), textcoords='offset points', fontsize=8, color='red', arrowprops=dict(facecolor='black', arrowstyle='-|>'))
def plotsinglescan(self): #choose a single scan from the mzml to plot
file = self.ui.fileLineedit.text()
#create an MSExperiment object to load the mzml file into
inp = oms.MSExperiment()
oms.MzMLFile().load(file, inp)
#grabs target scan number from GUI
scan = [self.ui.scannumberSpinbox.value()]
#Create a new MSExperiment object to extract the scan to
filtered = oms.MSExperiment()
#Add the selected scan to the filtered MSExperiment object
for k, s in enumerate(inp):
if k in scan:
filtered.addSpectrum(s)
#Set up the initial plot window
fig, axs = plt.subplots(1)
fig.set_figheight(4)
plt.subplots_adjust(hspace=1)
#setup the axis params for the figure
s = filtered[0]
#selects a line plot or stem plot (with no markers) depending on user selection of profile or centroid data
#line width and line colour taken from user GUI inputs
if self.ui.profileButton.isChecked():
axs.plot(s.get_peaks()[0],s.get_peaks()[1], self.ui.colourBox.currentText(), linewidth = self.ui.widthBox.value())
elif self.ui.centroidButton.isChecked():
markerline, stemlines, baseline = axs.stem(s.get_peaks()[0],s.get_peaks()[1], self.ui.colourBox.currentText(), markerfmt='none')
plt.setp(stemlines, 'linewidth', self.ui.widthBox.value())
#Gets the y-axis upper and lower limits from the GUI
axs.set_ylim(self.ui.lowySpinbox.value(), self.ui.highySpinbox.value())
#Get the x-axis upper and lower limits from the GUI
axs.set_xlim(self.ui.lowxSpinbox.value(), self.ui.highxSpinbox.value())
#Sets the x/y-axis labels
axs.set_xlabel("m/z")
axs.set_ylabel("Counts")
#sets the y-axis tick labels to scientific notation
axs.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0e'))
#sets the figure to a tight layout
fig.tight_layout()
#shows the figure where you can zoom/resize at will, and save to a file (recommended to save as a .svg for figure scaling)
plt.show()
def plotmergedscans(self):
try:
file = self.ui.fileLineedit.text()
if not file:
raise ValueError("File path is empty.")
# Load MS data and store as MSExperiment object
exp = oms.MSExperiment()
oms.MzMLFile().load(file, exp)
spectra = exp.getSpectra()
# Collecting only MS1 spectra
spectra_ms1 = [s for s in spectra if s.getMSLevel() == 1]
print(f"Number of MS1 spectra before merge are {len(spectra_ms1)}")
# Merges blocks of MS1
merger = oms.SpectraMerger()
param = merger.getParameters()
param.setValue("block_method:rt_block_size", len(exp.getSpectra()))
merger.setParameters(param)
merger.mergeSpectraBlockWise(exp)
# Adjust block size to 10 spectra and merge
param.setValue("block_method:rt_block_size", 10)
merger.setParameters(param)
merger.mergeSpectraBlockWise(exp)
spectraMerged = exp.getSpectra()
spectraMerged_ms1_10scans = [s for s in spectraMerged if s.getMSLevel() == 1]
# Extract m/z and intensity values
mz_values = []
intensity_values = []
for spectrum in spectraMerged_ms1_10scans:
mz, intensity = spectrum.get_peaks()
mz_values.extend(mz)
intensity_values.extend(intensity)
df = pd.DataFrame({'m/z': mz_values, 'intensity': intensity_values})
# Detect peaks
peaks, _ = find_peaks(df['intensity'])
# Create a DataFrame for the peaks
peaks_df = df.iloc[peaks]
# Set up the plot window - not too important as it can be resized at will when open
fig, axs = plt.subplots(1)
fig.set_figheight(4)
plt.subplots_adjust(hspace=1)
# Setup the axis params for the figure
s = spectraMerged[0]
# Plot profile or centroid data
if self.ui.profileButton.isChecked():
axs.plot(s.get_peaks()[0], s.get_peaks()[1], self.ui.colourBox.currentText(), linewidth=self.ui.widthBox.value())
elif self.ui.centroidButton.isChecked():
markerline, stemlines, baseline = axs.stem(s.get_peaks()[0], s.get_peaks()[1], self.ui.colourBox.currentText(), markerfmt='none')
plt.setp(stemlines, 'linewidth', self.ui.widthBox.value())
# Set the y-axis upper and lower limits
axs.set_ylim(self.ui.lowySpinbox.value(), self.ui.highySpinbox.value())
# Set the x-axis upper and lower limit
axs.set_xlim(self.ui.lowxSpinbox.value(), self.ui.highxSpinbox.value())
# Sets the x/y-axis labels
axs.set_xlabel("m/z")
axs.set_ylabel("Counts")
# Sets the y-axis tick labels to scientific notation
axs.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0e'))
# Sets the figure to a tight layout
fig.tight_layout()
if self.ui.anno_Checkbox.isChecked(): #add annotations
self.update_annotations(axs, peaks_df)
print('hello')
# Connect the update function to the 'xlim_changed' and 'ylim_changed' events
axs.callbacks.connect('xlim_changed', lambda event: self.update_annotations(axs, peaks_df))
axs.callbacks.connect('ylim_changed', lambda event: self.update_annotations(axs, peaks_df))
plt.show()
except Exception as e:
print(f"An error occurred: {e}")
def plotmergedms2(self):
try:
file = self.ui.fileLineedit.text()
if not file:
raise ValueError("File path is empty.")
# load MS data and store as MSExperiment object
exp = oms.MSExperiment()
oms.MzMLFile().load(file, exp)
spectra = exp.getSpectra()
# Collecting only MS2 spectra
spectra_ms1 = [s for s in spectra if s.getMSLevel() == 2]
print(f"Number of MS1 spectra before merge are {len(spectra_ms1)}")
# merges blocks of MS2
merger = oms.SpectraMerger()
merger.mergeSpectraPrecursors(exp)
merger.mergeSpectraBlockWise(exp)
param = merger.getParameters()
param.setValue("precursor_method:rt_tolerance", 60.0)
param.setValue("precursor_method:mz_tolerance", 1e-3)
param.setValue("block_method:rt_block_size", 49)
merger.setParameters(param)
merger.mergeSpectraBlockWise(exp)
spectraMerged = exp.getSpectra()
spectraMerged_ms1_10scans = [s for s in spectraMerged if s.getMSLevel() == 2]
# Extract m/z and intensity values
mz_values = []
intensity_values = []
for spectrum in spectraMerged_ms1_10scans:
mz, intensity = spectrum.get_peaks()
mz_values.extend(mz)
intensity_values.extend(intensity)
df = pd.DataFrame({'m/z': mz_values, 'intensity': intensity_values})
# Detect peaks
peaks, _ = find_peaks(df['intensity'])
# Create a DataFrame for the peaks
peaks_df = df.iloc[peaks]
# Set up the plot window - not too important as it can be resized at will when open
fig, axs = plt.subplots(1)
fig.set_figheight(4)
plt.subplots_adjust(hspace=1)
# Setup the axis params for the figure
s = spectraMerged[0]
# Plot profile or centroid data
if self.ui.profileButton.isChecked():
axs.plot(s.get_peaks()[0], s.get_peaks()[1], self.ui.colourBox.currentText(), linewidth=self.ui.widthBox.value())
elif self.ui.centroidButton.isChecked():
markerline, stemlines, baseline = axs.stem(s.get_peaks()[0], s.get_peaks()[1], self.ui.colourBox.currentText(), markerfmt='none')
plt.setp(stemlines, 'linewidth', self.ui.widthBox.value())
# Set the y-axis upper and lower limits
axs.set_ylim(self.ui.lowySpinbox.value(), self.ui.highySpinbox.value())
# Set the x-axis upper and lower limit
axs.set_xlim(self.ui.lowxSpinbox.value(), self.ui.highxSpinbox.value())
# Sets the x/y-axis labels
axs.set_xlabel("m/z")
axs.set_ylabel("Counts")
# Sets the y-axis tick labels to scientific notation
axs.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0e'))
# Sets the figure to a tight layout
fig.tight_layout()
if self.ui.anno_Checkbox.isChecked():
self.update_annotations(axs, peaks_df)
print('hello')
# Connect the update function to the 'xlim_changed' and 'ylim_changed' events
axs.callbacks.connect('xlim_changed', lambda event: self.update_annotations(axs, peaks_df))
axs.callbacks.connect('ylim_changed', lambda event: self.update_annotations(axs, peaks_df))
plt.show()
except Exception as e:
print(f"An error occurred: {e}")
def plottic(self):
file = self.ui.fileLineedit.text()
exp1 = oms.MSExperiment()
oms.MzMLFile().load(file, exp1)
fig, axs = plt.subplots()
fig.set_figheight(4)
plt.subplots_adjust(hspace=1)
for chrom in exp1.getChromatograms():
retention_times, intensities = chrom.get_peaks()
axs.plot(retention_times, intensities, self.ui.colourBox.currentText(), linewidth = self.ui.widthBox.value())
axs.set_xlabel("time (s)")
axs.set_ylabel("Counts")
#set the y-axis upper and lower limits - accepts raw integers or scientific notation
axs.set_ylim(self.ui.lowySpinbox.value(), self.ui.highySpinbox.value())
#Set the x-axis upper and lower limit - accepts raw integers or scientific notation
axs.set_xlim(self.ui.lowscanSpinbox.value(), self.ui.highscanSpinbox.value())
axs.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0e'))
plt.tight_layout()
plt.show()
def ploteic(self):
file = self.ui.fileLineedit.text()
# load MS data and store as MSExperiment object
exp1 = oms.MSExperiment()
oms.MzMLFile().load(file, exp1)
mz_values = [self.ui.mzSpinbox.value()]
print(len(exp1.getSpectra()))
intensity_values = []
for i in range(0, len(exp1.getSpectra())):
current_spectrum = exp1.getSpectra()[i]
for mz_value in mz_values:
intensity = self.extract_intensity(current_spectrum, mz_value)
if intensity is not None: # if intensity is none, the value will not be added to the list and the corresponding scan in the final output will read 0
intensity_values.append(intensity)
fig, axs = plt.subplots()
fig.set_figheight(4)
plt.subplots_adjust(hspace=1)
axs.plot(intensity_values, self.ui.colourBox.currentText(), linewidth = self.ui.widthBox.value())
axs.set_xlabel("Scan")
axs.set_ylabel("Counts")
#set the y-axis upper and lower limits - accepts raw integers or scientific notation
axs.set_ylim(self.ui.lowySpinbox.value(), self.ui.highySpinbox.value())
#Set the x-axis upper and lower limit - accepts raw integers or scientific notation
axs.set_xlim(self.ui.lowscanSpinbox.value(), self.ui.highscanSpinbox.value())
axs.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0e'))
plt.tight_layout()
plt.legend()
plt.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
GUI = MainWindow()
sys.exit(app.exec())