forked from michaelmarty/UniDec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatacollector.py
1251 lines (1115 loc) · 52.8 KB
/
datacollector.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import json
import time
import wx
import wx.lib.mixins.listctrl as listmix
import numpy as np
import matplotlib.cm as cm
from matplotlib.pyplot import colormaps
from matplotlib import rcParams
from pubsub import pub
import multiprocessing
from unidec_modules import UniFit, Extract2D, unidecstructure, PlotAnimations, plot1d, plot2d, miscwindows, \
MassDefects, nativez, IM_functions
from unidec_modules.PlottingWindow import PlottingWindow
import unidec_modules.unidectools as ud
from unidec_modules.AutocorrWindow import AutocorrWindow
import h5py
from unidec_modules.hdf5_tools import replace_dataset, get_dataset
__author__ = 'michael.marty'
rcParams['ps.useafm'] = True
rcParams['ps.fonttype'] = 42
rcParams['pdf.fonttype'] = 42
# TODO: Rewrite this code to clean it up...
class XValueListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.TextEditMixin):
def __init__(self, parent, id_value, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, id_value, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
listmix.TextEditMixin.__init__(self)
self.InsertColumn(0, "X Values")
self.InsertColumn(1, "# Prot")
self.InsertColumn(2, "# Lig")
self.SetColumnWidth(0, width=100) # , wx.LIST_AUTOSIZE)
self.SetColumnWidth(1, width=50) # , wx.LIST_AUTOSIZE)
self.SetColumnWidth(2, width=50) # , wx.LIST_AUTOSIZE)
self.currentItem = 0
def populate(self, listctrldata, colors=None):
self.DeleteAllItems()
listctrldata = np.array(listctrldata)
for i in range(0, len(listctrldata)):
try:
index = self.InsertItem(sys.maxint, str(listctrldata[i, 0]))
self.SetItem(index, 1, str(listctrldata[i, 1]))
self.SetItem(index, 2, str(listctrldata[i, 2]))
self.SetItemData(index, i)
except (ValueError, TypeError):
index = self.InsertItem(sys.maxint, str(listctrldata[i]))
if colors is not None:
# print listctrldata[i],colors[i]
color = wx.Colour(int(round(colors[i][0] * 255)), int(round(colors[i][1] * 255)),
int(round(colors[i][2] * 255)), alpha=255)
self.SetItemBackgroundColour(index, col=color)
self.SetItemData(index, i)
self.currentItem = 0
return self.get_maxes()
def clear_list(self):
self.DeleteAllItems()
return self.get_maxes()
def add_line(self, val=0):
index = self.InsertItem(sys.maxint, str(val))
self.SetItem(index, 1, str(1))
self.SetItem(index, 2, str(self.GetItemCount() - 1))
return self.get_maxes()
def get_list(self):
count = self.GetItemCount()
list_output = []
for i in range(0, count):
sublist = [str(self.GetItem(i, col=0).GetText()), ud.string_to_int(self.GetItem(i, col=1).GetText(), 0),
ud.string_to_int(self.GetItem(i, col=2).GetText(), 0)]
if sublist[0] != "":
list_output.append(sublist)
return list_output
def get_maxes(self):
try:
array = np.array(self.get_list())
maxp = np.amax([int(thing[1]) for thing in array])
maxl = np.amax([int(thing[2]) for thing in array])
return [maxp, maxl]
except (ValueError, TypeError):
return [0, 0]
class YValueListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.TextEditMixin):
def __init__(self, parent, id_value, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, id_value, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
listmix.TextEditMixin.__init__(self)
self.InsertColumn(0, "File Name")
self.InsertColumn(1, "Variable 1 (Ligand)")
self.InsertColumn(2, "Variable 2 (Protein)")
self.InsertColumn(3, "Charge State")
self.SetColumnWidth(0, width=300)
self.SetColumnWidth(1, width=100)
self.SetColumnWidth(2, width=100)
self.SetColumnWidth(3, width=100)
def populate(self, listctrldata, colors=None):
self.DeleteAllItems()
for i in range(0, len(listctrldata)):
index = self.InsertItem(sys.maxint, str(listctrldata[i][0]))
self.SetItem(index, 1, str(listctrldata[i][1]))
self.SetItem(index, 2, str(listctrldata[i][2]))
try:
self.SetItem(index, 3, str(listctrldata[i][3]))
except (ValueError, TypeError):
self.SetItem(index, 3, "All")
self.SetItemData(index, i)
if colors is not None:
color = wx.Colour(int(round(colors[i][0] * 255)), int(round(colors[i][1] * 255)),
int(round(colors[i][2] * 255)) , alpha=255)
self.SetItemBackgroundColour(index, col=color)
def clear_list(self):
self.DeleteAllItems()
def add_line(self, file_name="file.txt", var1="count", var2=0):
if var1 == "count":
var1 = self.GetItemCount()
index = self.InsertItem(sys.maxint, str(file_name))
self.SetItem(index, 1, str(var1))
self.SetItem(index, 2, str(var2))
self.SetItem(index, 3, str("All"))
def get_list(self):
count = self.GetItemCount()
colormap = cm.get_cmap('rainbow', count)
peakcolors = colormap(np.arange(count))
list_output = []
for i in range(0, count):
sublist = [str(self.GetItem(i, col=0).GetText()), float(self.GetItem(i, col=1).GetText()),
float(self.GetItem(i, col=2).GetText()), self.GetItem(i, col=3).GetText(), peakcolors[i][0],
peakcolors[i][1], peakcolors[i][2]]
list_output.append(sublist)
return list_output
class ListCtrlPanel(wx.Panel):
def __init__(self, parent, list_type="X", size=(200, 400)):
wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
id_value = wx.NewId()
self.selection = []
sizer = wx.BoxSizer(wx.VERTICAL)
if list_type == "X":
self.list = XValueListCtrl(self, id_value, size=size, style=wx.LC_REPORT | wx.BORDER_NONE)
elif list_type == "Y":
self.list = YValueListCtrl(self, id_value, size=size, style=wx.LC_REPORT | wx.BORDER_NONE)
else:
print "Error making ListCtrlPanel"
exit()
sizer.Add(self.list, 1, wx.EXPAND)
self.SetSizer(sizer)
self.SetAutoLayout(True)
self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.on_right_click, self.list)
self.popupID1 = wx.NewId()
self.popupID2 = wx.NewId()
self.popupID3 = wx.NewId()
self.popupID4 = wx.NewId()
self.Bind(wx.EVT_MENU, self.on_popup_one, id=self.popupID1)
self.Bind(wx.EVT_MENU, self.on_popup_two, id=self.popupID2)
self.Bind(wx.EVT_MENU, self.on_popup_three, id=self.popupID3)
self.Bind(wx.EVT_MENU, self.on_popup_four, id=self.popupID4)
def on_right_click(self, event):
if hasattr(self, "popupID1"):
menu = wx.Menu()
menu.Append(self.popupID1, "Delete")
menu.Append(self.popupID2, "Delete All")
menu.Append(self.popupID3, "Fill Down Variable 2")
menu.Append(self.popupID4, "Fill Down Charge State")
self.PopupMenu(menu)
menu.Destroy()
def on_popup_one(self, event):
# Delete
item = self.list.GetFirstSelected()
num = self.list.GetSelectedItemCount()
self.selection = []
self.selection.append(item)
for i in range(1, num):
item = self.list.GetNextSelected(item)
self.selection.append(item)
for i in range(0, num):
self.list.DeleteItem(self.selection[num - i - 1])
def on_popup_two(self, event):
self.list.DeleteAllItems()
def on_popup_three(self, event):
item = self.list.GetFirstSelected()
val = self.list.GetItem(item, col=2).GetText()
count = self.list.GetItemCount()
for i in range(0, count):
self.list.SetItem(i, 2, val)
def on_popup_four(self, event):
item = self.list.GetFirstSelected()
val = self.list.GetItem(item, col=3).GetText()
count = self.list.GetItemCount()
for i in range(0, count):
self.list.SetItem(i, 3, val)
class NetworkFrame(PlottingWindow):
def __init__(self, *args, **kwargs):
PlottingWindow.__init__(self, *args, **kwargs)
self.axes = self.figure.add_axes(self._axes)
self.flag = True
def clear_frame(self):
self.figure.clear()
self.axes = self.figure.add_axes(self._axes)
self.repaint()
datachoices = {0: "Raw Data", 1: "Processed Data", 2: "Zero Charge Mass Spectrum"}
extractchoices = {0: "Height", 1: "Local Max", 2: "Area", 3: "Center of Mass", 4: "Local Max Position",
5: "Center of Mass 50%", 6: "Center of Mass 10%"}
extractlabels = {0: "Intensity", 1: "Intensity", 2: "Area", 3: "Mass", 4: "Mass", 5: "Mass", 6: "Mass"}
modelchoices = {"Simple Single KD": "one", "Parallel KD's Chained": "parallel", "All KD's Free": "free",
"Test for Best Model": "test", "Series KD's Chained": "series"}
# noinspection PyNoneFunctionAssignment,PyNoneFunctionAssignment,PyArgumentList
class DataCollector(wx.Frame):
def __init__(self, parent, title, config=None, pks=None, *args, **kwargs):
wx.Frame.__init__(self, parent, title=title) # ,size=(200,-1))
if "directory" in kwargs:
self.directory = kwargs["directory"]
else:
self.directory = ""
self.config = config
self.pks = pks
self.gridparams = None
if self.config is None:
self.config = unidecstructure.UniDecConfig()
self.config.initialize()
print "Using Empty Structure"
self.config.publicationmode = 1
if "viridis" in colormaps():
self.config.cmap = "viridis"
else:
self.config.cmap = "jet"
self.CreateStatusBar(2)
self.SetStatusWidths([-1, 150])
pub.subscribe(self.on_motion, 'newxy')
self.filemenu = wx.Menu()
self.menuSave = self.filemenu.Append(wx.ID_SAVE, "Save", "Save Parameters")
self.menuLoad = self.filemenu.Append(wx.ID_ANY, "Load", "Load Parameters")
self.filemenu.AppendSeparator()
self.menuSaveFigPNG = self.filemenu.Append(wx.ID_ANY, "Save Figures as PNG",
"Save all figures as PNG in central directory")
self.menuSaveFigPDF = self.filemenu.Append(wx.ID_ANY, "Save Figures as PDF",
"Save all figures as PDF in central directory")
self.Bind(wx.EVT_MENU, self.on_save, self.menuSave)
self.Bind(wx.EVT_MENU, self.on_load, self.menuLoad)
self.Bind(wx.EVT_MENU, self.on_save_fig, self.menuSaveFigPNG)
self.Bind(wx.EVT_MENU, self.on_save_figPDF, self.menuSaveFigPDF)
self.toolsmenu = wx.Menu()
self.experimentalmenu = wx.Menu()
self.menuOpen = self.experimentalmenu.Append(wx.ID_ANY, "Open HDF5 File", "Open HDF5 file from MetaUniDec")
self.Bind(wx.EVT_MENU, self.on_hdf5_open, self.menuOpen)
self.experimentalmenu.AppendSeparator()
self.menuAnimation = self.experimentalmenu.Append(wx.ID_ANY, "Animate Spectra",
"Animation from spectra in list")
self.Bind(wx.EVT_MENU, self.on_animate, self.menuAnimation)
self.menuAnimation2 = self.experimentalmenu.Append(wx.ID_ANY, "Animate 2D Plots",
"Animate mass v. charge or m/z v. charge")
self.Bind(wx.EVT_MENU, self.on_animate2, self.menuAnimation2)
self.menu2dGrid = self.experimentalmenu.Append(wx.ID_ANY, "2D Grid Extraction",
"Extract a 2d Grid of intensities")
self.Bind(wx.EVT_MENU, self.on_2dgrid, self.menu2dGrid)
self.menudefect = self.experimentalmenu.Append(wx.ID_ANY, "Kendrick Mass Tools", "Mass Defect Analysis")
self.Bind(wx.EVT_MENU, self.on_defect, self.menudefect)
self.menulocalpath = self.toolsmenu.Append(wx.ID_ANY, "Convert to Local Path",
"Change file name to reflect local path for portability")
self.Bind(wx.EVT_MENU, self.on_local_path, self.menulocalpath)
self.menuabsolutepath = self.toolsmenu.Append(wx.ID_ANY, "Convert to Absolute Path",
"Change file name to reflect absolute path")
self.Bind(wx.EVT_MENU, self.on_absolute_path, self.menuabsolutepath)
self.menumsmsnorm = self.experimentalmenu.Append(wx.ID_ANY, "Normalize to MSMS",
"Normalizes mass deconvolutions to MS1 scan for variable 1 +/- variable 2")
self.Bind(wx.EVT_MENU, self.on_msms_norm, self.menumsmsnorm)
self.menuautocorr = self.experimentalmenu.Append(wx.ID_ANY, "View Autocorrelation of Sum",
"Shows autocorelation plot of sum")
self.Bind(wx.EVT_MENU, self.on_autocorr, self.menuautocorr)
self.toolsmenu.AppendSeparator()
self.menuylabel = self.toolsmenu.Append(wx.ID_ANY, "Specify Var. 1 Label", "Adds Var. 1 axis label to plot")
self.Bind(wx.EVT_MENU, self.on_ylabel, self.menuylabel)
self.menuBar = wx.MenuBar()
self.menuBar.Append(self.filemenu, "&File")
self.menuBar.Append(self.toolsmenu, "Tools")
self.menuBar.Append(self.experimentalmenu, "Experimental")
self.SetMenuBar(self.menuBar)
self.panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.inputsizer = wx.BoxSizer(wx.HORIZONTAL)
self.ypanelsizer = wx.BoxSizer(wx.VERTICAL)
self.ypanel = ListCtrlPanel(self.panel, list_type="Y", size=(600, 500))
self.ypanel.SetDropTarget(DCDropTarget(self))
self.ypanelsizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.addybutton = wx.Button(self.panel, label="Add Files")
self.Bind(wx.EVT_BUTTON, self.on_add_y, self.addybutton)
self.ypanelsizer2.Add(self.addybutton, 0, wx.EXPAND)
self.ypanelsizer2.Add(wx.StaticText(self.panel, label="Directory:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.dirinput = wx.TextCtrl(self.panel, value="", size=(100, 20))
self.ypanelsizer2.Add(self.dirinput, 1, wx.EXPAND)
self.dirbutton = wx.Button(self.panel, label="...", size=(20, 20))
self.Bind(wx.EVT_BUTTON, self.on_choose_dir, self.dirbutton)
self.ypanelsizer2.Add(self.dirbutton, 0, wx.EXPAND)
self.ypanelsizer.Add(self.ypanelsizer2, 0, wx.EXPAND)
self.ypanelsizer.Add(self.ypanel, 0, wx.EXPAND)
self.inputsizer.Add(self.ypanelsizer, 0, wx.EXPAND)
self.xpanel = ListCtrlPanel(self.panel, list_type="X", size=(200, 500))
self.xpanelsizer = wx.BoxSizer(wx.VERTICAL)
self.addxbutton = wx.Button(self.panel, label="Add X Value")
self.Bind(wx.EVT_BUTTON, self.on_add_x, self.addxbutton)
self.xpanelsizer.Add(self.addxbutton, 0, wx.EXPAND)
self.xpanelsizer.Add(self.xpanel, 0, wx.EXPAND)
self.inputsizer.Add(self.xpanelsizer, 0, wx.EXPAND)
self.plotwindow = wx.Notebook(self.panel)
self.tab1 = wx.Panel(self.plotwindow)
self.tab2 = wx.Panel(self.plotwindow)
self.tab3 = wx.Panel(self.plotwindow)
self.plot1 = plot1d.Plot1d(self.tab1)
self.plot2d = plot2d.Plot2d(self.tab2)
self.plot4 = plot1d.Plot1d(self.tab3)
miscwindows.setup_tab_box(self.tab1, self.plot1)
miscwindows.setup_tab_box(self.tab2, self.plot2d)
miscwindows.setup_tab_box(self.tab3, self.plot4)
self.plotwindow.AddPage(self.tab1, "1D Stack")
self.plotwindow.AddPage(self.tab2, "2D Grid")
self.plotwindow.AddPage(self.tab3, "Summation")
self.plot2 = plot1d.Plot1d(self.panel)
self.plotwindow2 = wx.Notebook(self.panel)
self.tab12 = wx.Panel(self.plotwindow2)
self.tab22 = wx.Panel(self.plotwindow2)
self.plot3 = NetworkFrame(self.tab12)
self.plot3h = plot1d.Plot1d(self.tab22)
miscwindows.setup_tab_box(self.tab12, self.plot3)
miscwindows.setup_tab_box(self.tab22, self.plot3h)
self.plotwindow2.AddPage(self.tab12, "Network Model")
self.plotwindow2.AddPage(self.tab22, "histogram")
# self.plot3=NetworkFrame(self.panel)
self.inputsizer.Add(self.plotwindow, 0, wx.EXPAND)
self.sizer.Add(self.inputsizer, 1, wx.EXPAND)
self.runsizer = wx.BoxSizer(wx.HORIZONTAL)
self.runsizer.Add(wx.StaticText(self.panel, label=" What to extract: "), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctldata = wx.ComboBox(self.panel, value="Raw Data", choices=datachoices.values(), style=wx.CB_READONLY)
self.runsizer.Add(self.ctldata, 0, wx.EXPAND)
self.runsizer.Add(wx.StaticText(self.panel, label=" Range:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlmin = wx.TextCtrl(self.panel, value="", size=(50, 20))
self.runsizer.Add(self.ctlmin, 0, wx.ALIGN_CENTER_VERTICAL)
self.runsizer.Add(wx.StaticText(self.panel, label=" to "), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlmax = wx.TextCtrl(self.panel, value="", size=(50, 20))
self.runsizer.Add(self.ctlmax, 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlnorm = wx.CheckBox(self.panel, label="Normalize Data")
# self.Bind(wx.EVT_CHECKBOX, self.on_norm, self.ctlnorm)
self.runsizer.Add(self.ctlnorm, 0, wx.EXPAND)
self.runbutton = wx.Button(self.panel, label="Run Extraction")
self.Bind(wx.EVT_BUTTON, self.on_run, self.runbutton)
self.runsizer.Add(self.runbutton, 0, wx.EXPAND)
self.ctlnorm2 = wx.CheckBox(self.panel, label="Normalize Extraction")
self.runsizer.Add(self.ctlnorm2, 0, wx.EXPAND)
self.runsizer.Add(wx.StaticText(self.panel, label=" How to extract: "), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlextract = wx.ComboBox(self.panel, value="Height", choices=extractchoices.values(), style=wx.CB_READONLY)
self.runsizer.Add(self.ctlextract, 0, wx.EXPAND)
self.runsizer.Add(wx.StaticText(self.panel, label=" Window:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlwindow = wx.TextCtrl(self.panel, value="", size=(50, 20))
self.runsizer.Add(self.ctlwindow, 0, wx.EXPAND)
self.runsizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.runsizer2.Add(wx.StaticText(self.panel, label="Number of Proteins:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlprot = wx.TextCtrl(self.panel, value="", size=(50, 20))
self.runsizer2.Add(self.ctlprot, 0, wx.ALIGN_CENTER_VERTICAL)
self.runsizer2.Add(wx.StaticText(self.panel, label="Number of Ligands:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctllig = wx.TextCtrl(self.panel, value="", size=(50, 20))
self.runsizer2.Add(self.ctllig, 0, wx.ALIGN_CENTER_VERTICAL)
self.runsizer2.Add(wx.StaticText(self.panel, label="Number of Bootstraps:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlbootstrap = wx.TextCtrl(self.panel, value="0", size=(50, 20))
self.runsizer2.Add(self.ctlbootstrap, 0, wx.ALIGN_CENTER_VERTICAL)
self.kdbutton = wx.Button(self.panel, label="Fit KDs")
self.Bind(wx.EVT_BUTTON, self.on_kd_fit, self.kdbutton)
self.runsizer2.Add(self.kdbutton, 0, wx.EXPAND)
choices = sorted(modelchoices.keys())
self.ctlprotmodel = wx.ComboBox(self.panel, value=choices[0], choices=choices, style=wx.CB_READONLY)
self.ctlligmodel = wx.ComboBox(self.panel, value=choices[0], choices=choices, style=wx.CB_READONLY)
self.runsizer2.Add(wx.StaticText(self.panel, label=" Protein Model: "), 0, wx.ALIGN_CENTER_VERTICAL)
self.runsizer2.Add(self.ctlprotmodel, 0, wx.EXPAND)
self.runsizer2.Add(wx.StaticText(self.panel, label=" Ligand Model: "), 0, wx.ALIGN_CENTER_VERTICAL)
self.runsizer2.Add(self.ctlligmodel, 0, wx.EXPAND)
self.runsizer2.Add(wx.StaticText(self.panel, label="Binding Sites Per Protein:"), 0, wx.ALIGN_CENTER_VERTICAL)
self.ctlmaxsites = wx.TextCtrl(self.panel, value="None", size=(50, 20))
self.runsizer2.Add(self.ctlmaxsites, 0, wx.ALIGN_CENTER_VERTICAL)
# self.runsizer2.Add(wx.StaticText(self.panel,label="Binding Sites Per Protein:"),0,wx.ALIGN_CENTER_VERTICAL)
self.ctloutliers = wx.CheckBox(self.panel, label="Remove Outliers")
self.runsizer2.Add(self.ctloutliers, 0, wx.ALIGN_CENTER_VERTICAL)
self.sizer.Add(self.runsizer, 0, wx.EXPAND)
self.sizer.Add(self.runsizer2, 0, wx.EXPAND)
self.plotsizer = wx.BoxSizer(wx.HORIZONTAL)
# self.plot3=NetworkFrame(self.panel)
self.plotsizer.Add(self.plot2, 0, wx.EXPAND)
self.plotsizer.Add(self.plotwindow2, 1, wx.EXPAND)
# self.plotsizer.Add(self.plot3,0,wx.EXPAND)
self.sizer.Add(self.plotsizer, 0, wx.EXPAND)
self.panel.SetSizer(self.sizer)
self.sizer.Fit(self)
self.xvals = []
self.yvals = []
self.range = []
self.extract = []
self.normflag = True
self.normflag2 = True
self.protflag = "free"
self.ligflag = "free"
self.datachoice = 2
self.numprot = 0
self.numlig = 0
self.bootstrap = 0
self.window = ''
self.maxsites = ''
self.extractchoice = 0
self.savename = "collection1.json"
self.localpath = 0
self.molig = None
self.xcolors = []
self.data = []
self.grid = []
self.var1 = []
self.xlabel = "Mass"
self.ylabel = ""
self.hdf5_file = ""
self.filetype = 0
self.update_set(0)
self.Centre()
self.Show(True)
if "hdf_file" in kwargs:
self.open_hdf5(kwargs["hdf_file"])
try:
self.load_x_from_peaks(0)
except (ValueError, TypeError, AttributeError):
print "Failed to load from peak list"
if __name__ == "__main__":
# self.directory="C:\\cprog\\Georg"
# self.directory="C:\\Data\\AmtB_POPC"
# self.load(os.path.join(self.directory,"AmtB_04_test.json"))
# self.directory = "C:\\Data\\AmtB_DMPC"
# self.load(os.path.join(self.directory, "AmtB_07.json"))
if False:
self.directory = "C:\\Data\\Others\\Miranda"
self.load(os.path.join(self.directory, "collection1.json"))
self.on_kd_fit(0)
try:
# testdir = "C:\Python\UniDec\unidec_src\UniDec\\x64\Release"
# testfile = "JAW.hdf5"
# self.open_hdf5(os.path.join(testdir, testfile))
# self.directory="C:\\Data\\AmtB_POPC"
# self.directory="C:\\cprog\\Shane_ND3"
# self.directory="C:\\MassLynx\\Mike.PRO\Data\\150521\\mzML\\Aqpz_05_Ramp3"
# self.load(os.path.join(self.directory,"AmtB_04_test.json"))
# self.directory = "C:\\cprog\\Jon\\Jon Titration data\\Man9 141015"
# self.load(os.path.join(self.directory, "collection1.json"))
# self.on_run(0)
# self.on_kd_fit(0)
# self.on_animate2(0)
pass
except Exception, e:
print e
pass
def load_x_from_peaks(self, e):
try:
if not ud.isempty(self.pks.peaks):
for p in self.pks.peaks:
maxes = self.xpanel.list.add_line(val=p.mass)
self.ctlprot.SetValue(str(maxes[0]))
self.ctllig.SetValue(str(maxes[1]))
except Exception, ex:
print "Unable to detect max # protein and ligands", ex
def on_hdf5_open(self, e):
dlg = wx.FileDialog(self, "Open HDF5 File", self.directory, self.hdf5_file, "*.hdf5")
if dlg.ShowModal() == wx.ID_OK:
self.hdf5_file = dlg.GetPath()
self.open_hdf5(self.hdf5_file)
dlg.Destroy()
def open_hdf5(self, path):
self.topname = "ms_dataset"
self.hdf5_file = path
hdf = h5py.File(path)
msdata = hdf.require_group(self.topname)
keys = msdata.keys()
self.indexes = []
for k in keys:
try:
self.indexes.append(int(k))
except:
pass
self.indexes = np.array(self.indexes)
self.indexes = sorted(self.indexes)
self.len = len(self.indexes)
for f in self.indexes:
msdata = hdf.get(self.topname + "/" + str(f))
self.attrs = dict(msdata.attrs.items())
if "var1" in self.attrs.keys():
var1 = self.attrs["var1"]
elif "collision_voltage" in self.attrs.keys():
var1 = self.attrs["collision_voltage"]
else:
var1 = f
self.ypanel.list.add_line(file_name=f, var1=str(var1))
pdataset = hdf.require_group("/peaks")
peaks = get_dataset(pdataset, "peakdata")
for p in peaks:
maxes = self.xpanel.list.add_line(val=p[0])
self.ctlprot.SetValue(str(maxes[0]))
self.ctllig.SetValue(str(maxes[1]))
hdf.close()
self.update_get(0)
self.directory = os.path.join(os.path.split(path)[0], "UniDec_Figures_and_Files")
self.update_set(0)
self.filetype = 1
def on_save(self, e):
self.update_get(e)
try:
exout = np.array(self.extract).tolist()
except AttributeError:
exout = []
# print "Saved: ",self.gridparams
outdict = {"x": self.xvals, "y": self.yvals, "dir": self.directory, "extractselection": self.extractchoice,
"window": self.window,
"selection": self.datachoice, "range": self.range, "normalize": self.normflag,
"normalize2": self.normflag2,
"numprot": self.numprot, "numlig": self.numlig, "bootstrap": self.bootstrap, "extract": exout,
"protflag": self.protflag,
"ligflag": self.ligflag, "maxsites": self.maxsites, "gridparams": self.gridparams,
"molig": self.molig, "filetype": self.filetype}
dlg = wx.FileDialog(self, "Save Collection in JSON Format", self.directory, self.savename, "*.json")
if dlg.ShowModal() == wx.ID_OK:
self.savename = dlg.GetPath()
with open(self.savename, "w") as outfile:
json.dump(outdict, outfile)
print "Saved: ", self.savename
dlg.Destroy()
def on_load(self, e):
dlg = wx.FileDialog(self, "Load JSON Collection", self.directory, self.savename, "*.json")
if dlg.ShowModal() == wx.ID_OK:
self.savename = dlg.GetPath()
self.load(self.savename)
dlg.Destroy()
def load(self, savename):
with open(savename, "r") as outfile:
indict = json.load(outfile)
if "x" in indict:
self.xvals = indict["x"]
if "y" in indict:
self.yvals = indict["y"]
if "dir" in indict:
self.directory = indict["dir"]
if "extractselection" in indict:
self.extractchoice = indict["extractselection"]
if "selection" in indict:
self.datachoice = indict["selection"]
if "range" in indict:
self.range = indict["range"]
if "normalize" in indict:
self.normflag = indict["normalize"]
if "normalize2" in indict:
self.normflag2 = indict["normalize2"]
if "numprot" in indict:
self.numprot = indict["numprot"]
if "numlig" in indict:
self.numlig = indict["numlig"]
if "bootstrap" in indict:
self.bootstrap = indict["bootstrap"]
if "window" in indict:
self.window = indict["window"]
if "protflag" in indict:
self.protflag = indict["protflag"]
if "ligflag" in indict:
self.ligflag = indict["ligflag"]
if "maxsites" in indict:
self.maxsites = indict["maxsites"]
if "gridparams" in indict:
self.gridparams = indict["gridparams"]
# print "Loaded: ",self.gridparams
if "molig" in indict:
self.molig = indict["molig"]
if "filetype" in indict:
self.filetype = indict["filetype"]
else:
self.filetype = 0
self.update_set(0)
print "Loaded: ", savename
self.on_run(0)
def on_add_x(self, e):
maxes = self.xpanel.list.add_line()
try:
self.ctlprot.SetValue(str(maxes[0]))
self.ctllig.SetValue(str(maxes[1]))
except (ValueError, TypeError):
print "Failed to autoupdate total # of protein and ligand, update manually in boxes."
def on_add_y(self, e):
self.update_get(e)
dlg = wx.FileDialog(self, "Load Files", self.directory, "", "*.*", wx.FD_MULTIPLE)
if dlg.ShowModal() == wx.ID_OK:
filenames = dlg.GetPaths()
for f in filenames:
self.ypanel.list.add_line(file_name=f)
self.filetype = 0
dlg.Destroy()
self.localpath = 0
def on_choose_dir(self, e):
dlg = wx.DirDialog(None, "Choose Top Directory", "", wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.directory = dlg.GetPath()
self.dirinput.SetValue(self.directory)
# print self.directory
dlg.Destroy()
def on_motion(self, xpos, ypos):
try:
if xpos is not None and ypos is not None:
self.SetStatusText("x=%.4f y=%.2f" % (xpos, ypos), number=1)
except:
pass
def update_get(self, e):
self.xvals = self.xpanel.list.get_list()
try:
maxes = self.xpanel.list.get_maxes()
self.ctlprot.SetValue(str(maxes[0]))
self.ctllig.SetValue(str(maxes[1]))
except (ValueError, TypeError):
print "Failed to autoupdate total # of protein and ligand, update manually in boxes."
self.yvals = self.ypanel.list.get_list()
self.directory = self.dirinput.GetValue()
self.normflag = self.ctlnorm.GetValue()
self.normflag2 = self.ctlnorm2.GetValue()
self.datachoice = self.ctldata.GetSelection()
self.extractchoice = self.ctlextract.GetSelection()
self.protflag = modelchoices[self.ctlprotmodel.GetStringSelection()]
self.ligflag = modelchoices[self.ctlligmodel.GetStringSelection()]
try:
self.window = float(self.ctlwindow.GetValue())
except ValueError:
pass
try:
self.bootstrap = int(self.ctlbootstrap.GetValue())
except ValueError:
self.bootstrap = 1
pass
self.range = []
try:
self.range.append(float(self.ctlmin.GetValue()))
self.range.append(float(self.ctlmax.GetValue()))
except ValueError:
pass
try:
self.numprot = int(self.ctlprot.GetValue())
self.numlig = int(self.ctllig.GetValue())
except ValueError:
pass
try:
self.maxsites = int(self.ctlmaxsites.GetValue())
except ValueError:
self.maxsites = ''
# print self.xvals
# print self.yvals
# print self.directory
def update_set(self, e):
self.ctlprotmodel.SetValue(
next((label for label, flag in modelchoices.items() if flag == self.protflag), "test"))
self.ctlligmodel.SetValue(next((label for label, flag in modelchoices.items() if flag == self.ligflag), "test"))
self.dirinput.SetValue(self.directory)
self.xpanel.list.populate(self.xvals)
self.ypanel.list.populate(self.yvals)
self.ctlnorm.SetValue(self.normflag)
self.ctlnorm2.SetValue(self.normflag2)
self.ctlprot.SetValue(str(self.numprot))
self.ctllig.SetValue(str(self.numlig))
self.ctlwindow.SetValue(str(self.window))
self.ctlbootstrap.SetValue(str(self.bootstrap))
self.ctldata.SetSelection(self.datachoice)
self.ctlextract.SetSelection(self.extractchoice)
self.ctlmaxsites.SetValue(str(self.maxsites))
if not ud.isempty(self.range):
self.ctlmin.SetValue(str(self.range[0]))
self.ctlmax.SetValue(str(self.range[1]))
def on_run(self, e, vals=None):
tstart = time.clock()
self.update_get(e)
# os.chdir(self.directory)
self.data = []
self.extract = []
self.plot1.clear_plot()
self.plot2.clear_plot()
self.var1 = []
self.grid = []
if self.filetype == 1:
hdf = h5py.File(self.hdf5_file)
ycolors = []
print "Directory:", self.directory
for k, l in enumerate(self.yvals):
if self.filetype == 1:
msdata = hdf.get(self.topname + "/" + str(l[0]))
filename = l[0]
header = os.path.splitext(filename)[0]
if self.localpath == 1 or not os.path.isabs(filename):
header = os.path.join(self.directory, header)
filename = os.path.join(self.directory, filename)
print "Loading:", filename
subheader = os.path.split(header)[1]
self.var1.append(l[1])
ycolors.append(l[4:7])
fcolor = np.array(l[4:7])
if self.datachoice == 0:
if self.filetype == 1:
data = get_dataset(msdata, "raw_data")
else:
data = np.loadtxt(filename)
self.xlabel = "m/z (Th)"
elif self.datachoice == 1:
filename = os.path.join(header + "_unidecfiles", subheader + "_input.dat")
if self.filetype == 1:
data = get_dataset(msdata, "processed_data")
else:
data = np.loadtxt(filename)
self.xlabel = "m/z (Th)"
elif self.datachoice == 2:
self.xlabel = "Mass (Da)"
zstate = l[3]
filename = os.path.join(header + "_unidecfiles", subheader + "_mass.txt")
if self.filetype == 1:
data = get_dataset(msdata, "mass_data")
else:
data = np.loadtxt(filename)
if not zstate == 'All':
try:
filename = os.path.join(header + "_unidecfiles", subheader + "_massgrid.bin")
massgrid = np.fromfile(filename, dtype=float)
configfile = os.path.join(header + "_unidecfiles", subheader + "_conf.dat")
f = open(configfile, 'r')
for line in f:
if line.startswith("startz"):
startz = int(line.split()[1])
if line.startswith("endz"):
endz = int(line.split()[1])
zlength = endz - startz + 1
massgrid = np.reshape(massgrid, (len(data), zlength))
if zstate[0] == "N":
srange = zstate[1:]
nzstart, nzend = srange.split(':')
nzstart = float(nzstart)
nzend = float(nzend)
if nzstart > nzend:
nzstart, nzend = nzend, nzstart
ztab = np.arange(startz, endz + 1)
mgrid, zgrid = np.meshgrid(data[:, 0], ztab, indexing='ij')
offsetgrid = nativez.GetOffset(mgrid, zgrid)
bool1 = offsetgrid >= nzstart
bool2 = offsetgrid <= nzend
bool3 = np.all([bool1, bool2], axis=0)
mout = np.sum(massgrid * bool3, axis=1)
data[:, 1] = mout
else:
zindex = int(zstate) - startz
data[:, 1] = massgrid[:, zindex]
except Exception, e:
print "FAILED TO EXTRACT CHARGE STATE\nUsing total for all charge states\n", e
l[3] = "All"
try:
self.ypanel.list.populate(self.yvals, colors=ycolors)
except (ValueError, TypeError, AttributeError):
self.ypanel.list.populate(self.yvals)
if not ud.isempty(self.range):
bool1 = data[:, 0] >= self.range[0]
bool2 = data[:, 0] <= self.range[1]
bool3 = np.all([bool1, bool2], axis=0)
data = data[bool3]
if self.normflag:
data[:, 1] = data[:, 1] / np.amax(data[:, 1])
if vals is not None:
data[:, 1] = data[:, 1] * vals[k]
self.data.append(data)
if not self.plot1.flag:
self.plot1.plotrefreshtop(data[:, 0], data[:, 1], "Extracted Data", "", "", filename, None,
nopaint=True,
color=fcolor, test_kda=True)
self.grid.append(data)
else:
self.plot1.plotadd(data[:, 0], data[:, 1], fcolor, filename)
try:
self.grid.append(ud.mergedata(self.grid[0], data))
except (ValueError, TypeError, AttributeError):
print "Error combining data"
xext = []
for i in xrange(0, len(self.xvals)):
s = self.xvals[i][0]
try:
window = float(self.window)
except (ValueError, TypeError):
window = None
val = ud.data_extract(data, float(s), self.extractchoice, window=window)
xext.append(val)
self.extract.append(xext)
self.ypanel.list.populate(self.yvals, colors=ycolors)
self.plot1.repaint()
tend = time.clock()
print "Extraction time: %.2gs" % (tend - tstart)
print "Plotting..."
self.extract = np.array(self.extract)
if len(self.xvals) != 0:
if self.normflag2 == 1:
sums = np.sum(self.extract, axis=1)
self.extract = [self.extract[i] / sums[i] for i in xrange(0, len(self.yvals))]
self.extract = np.array(self.extract)
colormap = cm.get_cmap('rainbow', len(self.xvals))
self.xcolors = colormap(np.arange(len(self.xvals)))
self.makeplot2()
# print np.mean(self.extract,axis=0)
np.savetxt(os.path.join(self.directory, "extracts.txt"), self.extract)
self.xpanel.list.populate(self.xvals, colors=self.xcolors)
self.make_grid_plots(e)
if self.filetype == 1:
hdf.close()
print "Extraction Complete"
def make_grid_plots(self, e):
self.grid = np.array(self.grid)
if not ud.isempty(self.grid):
try:
self.plot4.plotrefreshtop(np.unique(self.grid[0, :, 0]), np.sum(self.grid[:, :, 1], axis=0), "Total",
"", "Sum", "", self.config, test_kda=True)
np.savetxt(os.path.join(self.directory, "sums.txt"),
np.transpose([np.unique(self.grid[0, :, 0]), np.sum(self.grid[:, :, 1], axis=0)]))
except (ValueError, TypeError, AttributeError):
print "Failed total plot"
self.plot4.clear_plot()
try:
x, y = np.meshgrid(self.grid[0, :, 0], self.var1, indexing='ij')
dat = np.transpose([np.ravel(x), np.ravel(y), np.ravel(np.transpose(self.grid[:, :, 1]))])
self.plot2d.contourplot(dat, self.config, xlab=self.xlabel, ylab=self.ylabel, title="Extracted Data")
self.on_export(e)
except (ValueError, TypeError, AttributeError):
print "Failed to make 2D plot"
self.plot2d.clear_plot()
def makeplot2(self):
# This is a bit funny but the ylabel from this plot is actually the xlabel from the others or the intensity...
ylabel = extractlabels[self.extractchoice]
if ylabel is "mass":
ylabel = self.xlabel
self.plot2.clear_plot()
self.plot2._axes = [0.15, 0.1, 0.75, 0.8]
for i in xrange(0, len(self.xvals)):
color = self.xcolors[i]
if not self.plot2.flag:
self.plot2.plotrefreshtop(self.var1, self.extract[:, i], title="Extracted Data", xlabel=self.ylabel
, ylabel=ylabel, color=color, test_kda=False)
self.plot2.plotadddot(self.var1, self.extract[:, i], color, "o")
else:
self.plot2.plotadd(self.var1, self.extract[:, i], color, file)
self.plot2.plotadddot(self.var1, self.extract[:, i], color, "o")
if self.normflag2 == 1:
self.plot2.subplot1.set_ylim([0, 1])
self.plot2.repaint()
def on_save_fig(self, e):
name1 = os.path.join(self.directory, "Figure1.png")
if self.plot1.flag:
self.plot1.on_save_fig(e, name1)
print name1
name2 = os.path.join(self.directory, "Figure2.png")
if self.plot2.flag:
self.plot2.on_save_fig(e, name2)
print name2
name3 = os.path.join(self.directory, "Figure3.png")
if self.plot3.flag:
self.plot3.on_save_fig(e, name3)
print name3
name1 = os.path.join(self.directory, "Figure1_2D.png")
if self.plot2d.flag:
self.plot2d.on_save_fig(e, name1)
print name1
name3 = os.path.join(self.directory, "Figure4.png")
if self.plot4.flag:
self.plot4.on_save_fig(e, name3)
print name3
def on_save_figPDF(self, e):
name1 = os.path.join(self.directory, "Figure1.pdf")
if self.plot1.flag:
self.plot1.on_save_fig(e, name1)
print name1
name2 = os.path.join(self.directory, "Figure2.pdf")
if self.plot2.flag:
self.plot2.on_save_fig(e, name2)
print name2
name3 = os.path.join(self.directory, "Figure3.pdf")
if self.plot3.flag:
self.plot3.on_save_fig(e, name3)
print name3
name1 = os.path.join(self.directory, "Figure4.pdf")
if self.plot4.flag:
self.plot4.on_save_fig(e, name1)
print name1
name1 = os.path.join(self.directory, "Figure1_2D.pdf")
if self.plot2d.flag:
self.plot2d.on_save_fig(e, name1)
print name1
def on_kd_fit(self, e):
outlierflag = self.ctloutliers.GetValue()
self.update_get(e)
self.plot3.clear_frame()
# self.makeplot2()
nodelist = []
for i in xrange(0, len(self.xvals)):
nodelist.append([self.xvals[i][1], self.xvals[i][2]])