-
Notifications
You must be signed in to change notification settings - Fork 0
/
IonPickerGUI.py
307 lines (237 loc) · 10.7 KB
/
IonPickerGUI.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
input_file = "A1.csv"
MAXERROR = 10
"""
Script for ion selection for Joachm
Feb 22 2016
Author: Sean O'Callaghan
spoc@unimelb.edu.au
"""
from Tkinter import Tk, BOTH, Listbox, StringVar, END, Scrollbar, RIGHT, \
VERTICAL, Y, TOP, LEFT, IntVar
from ttk import Frame, Label, Style, Entry, Button, Checkbutton
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import matplotlib.pyplot as plt
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from IonPickerEngine import deisotope, check_for_gaps
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.title("Jo Ion Picker")
self.pack(fill=BOTH, expand=1)
self.compounds = deisotope(filename=input_file)
self.error_label = Label(self, text="Error in ppm")
self.error_label.place(x=20, y=730)
self.entry = Entry(self, text='error in ppm')
self.entry.place(x=20, y=750)
self.entry.insert(10,'5')
self.b = Button(self, text="ReCalc Error", width=15, \
command=self.callback)
self.b.place(x=20, y=770)
self.b_output = Button(self, text="Export", width=10, command=self.write_output)
self.b_output.place(x=20, y=800)
#self.b_output = Button(self, text="Allowed", width=10, command=self.only_allowed_ions)
#self.b_output.place(x=20, y=830)
self.gaps=IntVar()
self.check_gaps = Checkbutton(self, text='Remove Gaps', variable=self.gaps,onvalue=1, offvalue=0, command=self.remove_gaps_call)
#self.check.pack()
self.check_gaps.place(x=20, y=830)
self.scrollbar = Scrollbar(self, orient=VERTICAL)
self.lb = Listbox(self, height=46, yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.lb.yview)
self.scrollbar.pack(side=LEFT, fill=Y)
for compound in self.compounds:
print "found", compound.get_base_peak()
mzs = compound.get_mz_list()
num_mzs = len(mzs)
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
self.lb.bind("<<ListboxSelect>>", self.onSelect)
self.lb.place(x=20, y=20)
self.var = StringVar()
#self.label = Label(self, text=0, textvariable=self.var)
#self.label.place(x=20, y=710)
self.mz_label = Label(self, text="M/Z Num Ions")
self.mz_label.place(x=20, y=0)
f = Figure(figsize=(8,11), dpi=100)
self.ax = f.add_subplot(111)
mass_list = self.compounds[0].get_mz_list()
print mass_list
intensity_list = self.compounds[0].get_intensity_list()
mass_spec_plot = self.ax.bar(mass_list, intensity_list,\
width=0.05)
min_mz = mass_list[0]
max_mz = mass_list[-1]
self.ax.set_xlim([min_mz-1, max_mz+1])
self.ax.set_ylim([0, 1.1*max(intensity_list)])
self.ax.set_xticks(mass_list)
self.ax.set_xticklabels(mass_list, rotation=45)
self.ax.set_title("Base Ion:" + str(mass_list[0]))
self.canvas = FigureCanvasTkAgg(f, master=self)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=RIGHT)
def onSelect(self, val):
sender = val.widget
idx = sender.curselection()
value = sender.get(idx)
self.var.set(value)
mz_to_search = value.split()[0]
self.ax.clear()
for i, compound in enumerate(self.compounds):
if float(mz_to_search) == compound.get_base_peak():
index = i
mass_list = self.compounds[index].get_mz_list()
print mass_list
intensity_list = self.compounds[index].get_intensity_list()
mass_spec_plot = self.ax.bar(mass_list, intensity_list,\
width=0.05)
min_mz = mass_list[0]
max_mz = mass_list[-1]
self.ax.set_xlim([min_mz-1, max_mz+1])
self.ax.set_ylim([0, 1.1*max(intensity_list)])
self.ax.set_xticks(mass_list)
self.ax.set_xticklabels([str(mass) for mass in mass_list], rotation=45)
self.ax.set_title("Base Ion:" + str(mass_list[0]))
self.canvas.draw()
def only_allowed_ions(self):
ion_list = []
fp = open('ions.csv', 'r')
lines = fp.readlines()
for line in lines:
ion_list.append(float(line))
#print ion_list
self.compounds = deisotope(filename=input_file,max_error = float(self.entry.get()))
new_compound_list = []
for compound in self.compounds:
for ion in ion_list:
error_Da = float(self.entry.get())*compound.get_base_peak()/1e6
if compound.get_base_peak() > ion-error_Da \
and compound.get_base_peak() < ion + error_Da:
new_compound_list.append(compound)
for compound in new_compound_list:
print "compound:",compound.get_base_peak()
self.lb.delete(0, END)
for compound in new_compound_list:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
def callback(self):
self.compounds = deisotope(filename=input_file, max_error = float(self.entry.get()))
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
print self.entry.get()
def remove_gaps_call(self):
self.compounds = deisotope(filename=input_file,max_error = float(self.entry.get()))
check_for_gaps(self.compounds)
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
print self.entry.get()
def do_list_update(self):
l_multi = self.multi.get()
l_missing = self.missing.get()
l_m1gtm2 = self.m1gtm2.get()
# possible situations: 000, 001,010,100, 011,101, 110, 111
if l_multi == 0 and l_missing == 0 and l_m1gtm2 ==0:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 0 and l_missing == 0 and l_m1gtm2 ==1:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
intensities = compound.get_intensity_list()
if intensities[-1] <= intensities[0]:
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 0 and l_missing == 1 and l_m1gtm2 ==0:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
if mzs[-1] - mzs[0] <1.75: # margin of error allowed here
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 1 and l_missing == 0 and l_m1gtm2 ==0:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
if num_mzs >1:
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 0 and l_missing == 1 and l_m1gtm2 ==1:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
intensities = compound.get_intensity_list()
num_mzs = len(mzs)
if mzs[-1] - mzs[0] <1.75 and intensities[-1] <= intensities[0]:
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 1 and l_missing == 0 and l_m1gtm2 ==1:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
intensities = compound.get_intensity_list()
num_mzs = len(mzs)
if num_mzs >1 and intensities[-1] <= intensities[0]:
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 1 and l_missing == 1 and l_m1gtm2 ==0:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
num_mzs = len(mzs)
if num_mzs >1 and mzs[-1] - mzs[0] <1.75:
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
elif l_multi == 1 and l_missing == 1 and l_m1gtm2 ==1:
self.lb.delete(0, END)
for compound in self.compounds:
mzs = compound.get_mz_list()
intensities = compound.get_intensity_list()
num_mzs = len(mzs)
if num_mzs >1 and mzs[-1] - mzs[0] <1.75 and \
intensities[1] <= intensities[0]:
entry = str(mzs[0]) + " " + str(num_mzs)
self.lb.insert(END, entry)
else:
pass # error!
def write_output(self):
op = open('edited_output.csv', 'w')
op.write('mz, intensity, mz, intensity, mz, intensity\n')
items = self.lb.get(0,END)
output_list = []
for item in items:
mz_val = item.split(' ')[0]
for compound in self.compounds:
if float(mz_val) == compound.get_base_peak():
mzs = compound.get_mz_list()
intensities = compound.get_intensity_list()
for i, mz in enumerate(mzs):
op.write(str(mz) + ',' + str(intensities[i]) + ',')
op.write('\n')
op.close()
def main():
root = Tk()
ex = Example(root)
root.geometry("1050x900+300+300")
root.mainloop()
if __name__ == '__main__':
main()