-
Notifications
You must be signed in to change notification settings - Fork 104
/
PyDP4_GUI.py
executable file
·2671 lines (1577 loc) · 80.6 KB
/
PyDP4_GUI.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
#!/usr/bin/env python
from PyQt5 import QtWidgets, QtCore, QtGui
import os
import shutil
import PyDP4
import queue
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import numpy as np
from scipy.stats import norm
from PyQt5.QtSvg import QSvgWidget
from rdkit.Chem import AllChem as Chem
from rdkit.Chem.Draw import rdMolDraw2D
import pickle
from matplotlib import cm
from pathlib import Path
class Window(QtWidgets.QMainWindow):
# create signal to start background job
signal_start_PyDP4 = QtCore.pyqtSignal()
# signal_finished_PyDP4 = QtCore.pyqtSignal()
def __init__(self):
super(Window, self).__init__()
self.table_widget = TabWidget(self)
self.setCentralWidget(self.table_widget)
self.resize(self.table_widget.Tab1.Overall_widget.sizeHint())
#self.resize(self.table_widget.sizeHint())
self.show()
# check if DP4outfile is there
class TabWidget(QtWidgets.QWidget):
def __init__(self, parent):
super(QtWidgets.QWidget, self).__init__(parent)
self.layouttabs = QtWidgets.QVBoxLayout()
# Initialize tab screen
self.tabs = QtWidgets.QTabWidget()
self.tab1 = QtWidgets.QWidget()
self.Tab1 = CalculationTab()
self.tabs.addTab(self.Tab1, "Calculation")
self.tab1.layout = QtWidgets.QVBoxLayout()
# Add tabs to widget
self.layouttabs.addWidget(self.tabs)
self.setLayout(self.layouttabs)
def addplottabs(self):
self.tab2 = QtWidgets.QWidget()
self.tab3 = QtWidgets.QWidget()
self.tab4 = QtWidgets.QWidget()
self.tab5 = QtWidgets.QWidget()
self.tab6 = QtWidgets.QWidget()
self.tabs.addTab(self.tab5, "Conformers")
#only add proton/carbon plot tabs if the raw data is there
NMR_list = [str(self.Tab1.NMR_list.item(i).text()) for i in range(self.Tab1.NMR_list.count())]
if 'Proton' in NMR_list or 'Proton.dx' in NMR_list or 'proton' in NMR_list or 'proton.dx' in NMR_list:
self.tabs.addTab(self.tab2, "Proton Plot")
self.Tab2 = ProtonPlotTab()
self.tab2.layout = QtWidgets.QVBoxLayout(self)
self.tab2.layout.addWidget(self.Tab2)
self.tab2.setLayout(self.tab2.layout)
if 'Carbon' in NMR_list or 'Carbon.dx' in NMR_list or 'carbon' in NMR_list or 'carbon.dx' in NMR_list:
self.tabs.addTab(self.tab3, "Carbon Plot")
self.Tab3 = CarbonPlotTab()
self.tab3.layout = QtWidgets.QVBoxLayout(self)
self.tab3.layout.addWidget(self.Tab3)
self.tab3.setLayout(self.tab3.layout)
self.Tab5 = ConformerTab()
self.tab5.layout = QtWidgets.QVBoxLayout(self)
self.tab5.layout.addWidget(self.Tab5)
self.tab5.setLayout(self.tab5.layout)
self.Tab6 = DP5Tab()
if 's' in ui.table_widget.Tab1.settings.Workflow:
self.tabs.addTab(self.tab6, "DP5")
self.tab6.layout = QtWidgets.QVBoxLayout(self)
self.tab6.layout.addWidget(self.Tab6)
self.tab6.setLayout(self.tab6.layout)
self.Tab4 = StatsTab()
if 's' in ui.table_widget.Tab1.settings.Workflow and len(ui.table_widget.Tab1.settings.InputFiles) > 1:
self.tabs.addTab(self.tab4, "DP4")
self.tab4.layout = QtWidgets.QVBoxLayout(self)
self.tab4.layout.addWidget(self.Tab4)
self.tab4.setLayout(self.tab4.layout)
self.layouttabs.addWidget(self.tabs)
self.setLayout(self.layouttabs)
ui.update()
class DP5Tab(QtWidgets.QWidget):
def __init__(self):
super(DP5Tab, self).__init__()
# main horizontal widget
self.main_layout = QtWidgets.QHBoxLayout()
self.setLayout(self.main_layout)
self.Isomers = ui.table_widget.Tab1.worker.Isomers
self.DP5data = ui.table_widget.Tab1.worker.DP5Data
self.DP4data = ui.table_widget.Tab1.worker.DP4Data
self.Isomer_DP5_table = QtWidgets.QTableWidget(self)
self.Isomer_DP5_table.setColumnCount(3)
self.Isomer_DP5_table.setHorizontalHeaderLabels(["Isomer", "DP5", "DP4"])
self.Isomer_DP5_table.setRowCount(len(self.DP5data.DP5scaledprobs))
c = 0
for scaled_DP5, DP4 in zip(self.DP5data.DP5scaledprobs, self.DP4data.DP4probs):
self.Isomer_DP5_table.setItem(c, 0, QtWidgets.QTableWidgetItem(str(c + 1)))
self.Isomer_DP5_table.setItem(c, 1, QtWidgets.QTableWidgetItem(str(round(scaled_DP5, 2))))
self.Isomer_DP5_table.setItem(c, 2, QtWidgets.QTableWidgetItem(str(round(DP4 , 3))))
c += 1
self.Isomer_DP5_table.selectRow(0)
# add error table
self.DP5_table = QtWidgets.QTableWidget(self)
self.DP5_table.setColumnCount(4)
self.DP5_table.setHorizontalHeaderLabels(
["Atom Label", "Atomic Probability", "Scaled error (ppm)", "Unscaled error (ppm)"])
self.DP5_table.setColumnWidth(0, 70)
self.DP5_table.setColumnWidth(1, 70)
self.DP5_table.setColumnWidth(2, 70)
self.DP5_table.setColumnWidth(3, 70)
#########
# add isomer canvas
self.table_widget = QtWidgets.QWidget(self)
self.image = QSvgWidget()
self.image.setMinimumWidth(self.image.height())
# organise layouts
self.table_layout = QtWidgets.QVBoxLayout()
self.table_layout.addWidget(self.Isomer_DP5_table)
self.table_layout.addWidget(self.DP5_table)
#self.image_layout.addWidget(self.image)
self.table_widget.setLayout(self.table_layout)
self.main_layout.addWidget(self.table_widget)
self.main_layout.addWidget(self.image)
# connections
self.Isomer_DP5_table.currentCellChanged.connect(self.Mol_drawer)
self.Isomer_DP5_table.currentCellChanged.connect(self.populatetable)
self.DP5_table.currentCellChanged.connect(self.Mol_drawer)
def populatetable(self):
isomerindex = self.Isomer_DP5_table.currentRow()
# which isomer is selected
self.DP5_table.setRowCount(0)
self.labels, self.shifts, self.exps, self.scaleds = self.DP5data.Clabels[isomerindex], \
self.DP5data.Cshifts[isomerindex], \
self.DP5data.Cexp[isomerindex], \
self.DP5data.Cscaled[isomerindex], \
self.scaled_probs = self.DP5data.BScaledAtomProbs[isomerindex]
self.DP5_table.setRowCount(len(self.labels))
# set info in rows and columns
c = 0
for label, shift, exp, scaled_shift, scaled_prob in zip(self.labels, self.shifts, self.exps,
self.scaleds,
self.scaled_probs):
self.DP5_table.setItem(c, 0, QtWidgets.QTableWidgetItem(label))
self.DP5_table.setItem(c, 1, QtWidgets.QTableWidgetItem(str(round(scaled_prob, 2))))
self.DP5_table.setItem(c, 2, QtWidgets.QTableWidgetItem(str(round(abs(scaled_shift - exp), 2))))
self.DP5_table.setItem(c, 3, QtWidgets.QTableWidgetItem(str(round(abs(shift - exp), 2))))
c += 1
self.DP5_table.selectRow(0)
# self.plot()
def Isomer_number(self):
Isomer_list = []
for c, i in enumerate(self.Isomers):
Isomer_list.append("Isomer " + str(c + 1))
return Isomer_list
def Mol_drawer(self):
isomerindex = self.Isomer_DP5_table.currentRow()
highlight = {}
inds = []
for label in self.DP5data.Clabels[isomerindex]:
inds.append(int(label.split("C")[1]) - 1)
for i, s in zip(inds, self.DP5data.BScaledAtomProbs[isomerindex]):
highlight[i] = cm.RdYlGn(s)
# pick selected atom
if self.DP5_table.item(self.DP5_table.currentRow(), 0):
error_ind = int(self.DP5_table.currentRow())
atom_ind = inds[error_ind]
highlight[atom_ind] = cm.viridis( self.DP5data.BScaledAtomProbs[isomerindex][error_ind])
#m = Chem.MolFromMolFile(str(ui.table_widget.Tab1.worker.settings.InputFiles[0]).split('.sdf')[0] + '.sdf', removeHs=False)
m = Chem.MolFromMolFile(str(ui.table_widget.Tab1.worker.settings.InputFilesPaths[isomerindex]).split('.sdf')[0] + '.sdf',removeHs=False)
Chem.Compute2DCoords(m)
drawer = rdMolDraw2D.MolDraw2DSVG(self.image.width(), self.image.height())
drawer.DrawMolecule(m, highlightAtoms=inds, highlightAtomColors=highlight, highlightBonds=False)
# ,addStereoAnnotation = True,addAtomIndices = True
drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:', '')
svg_bytes = bytearray(svg, encoding='utf-8')
self.image.renderer().load(svg_bytes)
ui.update()
class StatsTab(QtWidgets.QWidget):
def __init__(self):
super(StatsTab, self).__init__()
#main horizontal widget
self.main_layout = QtWidgets.QHBoxLayout()
self.setLayout(self.main_layout)
self.Isomers = ui.table_widget.Tab1.worker.Isomers
self.dp4data = ui.table_widget.Tab1.worker.DP4Data
# add isomer selection
self.IsomerSelect = QtWidgets.QComboBox(self)
self.IsomerSelect.addItems(self.Isomer_number())
# add isomer canvas
self.image = QSvgWidget()
#make vertical isomer layout
self.isomer_layout = QtWidgets.QVBoxLayout()
self.isomer_layout.addWidget(self.IsomerSelect)
self.isomer_layout.addWidget(self.image)
self.isomer_widget = QtWidgets.QWidget(self)
self.isomer_widget.setLayout(self.isomer_layout)
# make error table
# populate table based on the Isomer selected
self.Hplot = plotstats('H')
#check if there is a DP4 result for this atomt type:
if not np.isnan(ui.table_widget.Tab1.worker.DP4Data.HDP4probs).any() :
self.IsomerSelect.currentIndexChanged.connect(self.Hplot.populatetable)
self.IsomerSelect.currentIndexChanged.connect(self.RenderImage)
self.Hplot.errortable.itemSelectionChanged.connect(self.RenderImage)
self.Cplot = plotstats('C')
if not np.isnan(ui.table_widget.Tab1.worker.DP4Data.CDP4probs).any() :
self.IsomerSelect.currentIndexChanged.connect(self.Cplot.populatetable)
self.IsomerSelect.currentIndexChanged.connect(self.RenderImage)
self.Cplot.errortable.itemSelectionChanged.connect(self.RenderImage)
#make error table layout
self.error_layout = QtWidgets.QVBoxLayout()
self.error_layout.addWidget(self.Cplot.errortable)
self.error_layout.addWidget(self.Hplot.errortable)
self.error_widget = QtWidgets.QWidget(self)
self.error_widget.setLayout(self.error_layout)
#make plot layout
self.plot_layout = QtWidgets.QVBoxLayout()
self.plot_layout.addWidget(self.Cplot.statscanvas)
self.plot_layout.addWidget(self.Hplot.statscanvas)
self.plot_widget = QtWidgets.QWidget(self)
self.plot_widget.setLayout(self.plot_layout)
# add these widgets to main horizontal layout
self.isomer_widget.setMinimumWidth(400)
self.error_widget.setFixedWidth(400)
self.main_layout.addWidget(self.isomer_widget)
self.main_layout.addWidget(self.error_widget)
self.main_layout.addWidget(self.plot_widget)
def Isomer_number(self):
Isomer_list = []
for c, i in enumerate(self.Isomers):
Isomer_list.append("Isomer " + str(c + 1))
return Isomer_list
def RenderImage(self):
'''colors = [(0.12, 0.47, 0.71), (1.0, 0.5, 0.05), (0.17, 0.63, 0.17), (0.84, 0.15, 0.16), (0.58, 0.4, 0.74),
(0.55, 0.34, 0.29), (0.89, 0.47, 0.76), (0.5, 0.5, 0.5), (0.74, 0.74, 0.13), (0.09, 0.75, 0.81)]'''
isomerindex = self.IsomerSelect.currentIndex()
atom = []
highlight = {}
if self.Cplot.errortable.item(self.Cplot.errortable.currentRow(), 0):
C_atom = int(self.Cplot.errortable.item(self.Cplot.errortable.currentRow(), 0).text().split("C")[1])
atom.append(C_atom - 1 )
highlight[C_atom - 1] = (0.12, 0.47, 0.71)
if self.Hplot.errortable.item(self.Hplot.errortable.currentRow(), 0):
H_atom = int(self.Hplot.errortable.item(self.Hplot.errortable.currentRow(), 0).text().split("H")[1])
atom.append(H_atom - 1)
highlight[H_atom - 1] = (0.84, 0.15, 0.16)
m = Chem.MolFromMolFile(str(ui.table_widget.Tab1.worker.settings.InputFilesPaths[isomerindex]).split('.sdf')[0] + '.sdf',removeHs=False)
Chem.Compute2DCoords(m)
drawer = rdMolDraw2D.MolDraw2DSVG(self.image.width(), self.image.height())
drawer.DrawMolecule(m, highlightAtoms=atom, highlightAtomColors=highlight,highlightBonds = False)
drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:', '')
svg_bytes = bytearray(svg, encoding='utf-8')
self.image.renderer().load(svg_bytes)
#self.image.setGeometry(QtCore.QRect(0, 50, 500, 500))
ui.update()
class plotstats(QtWidgets.QWidget):
def __init__(self, Atom):
super(plotstats, self).__init__()
# when a cell is selected signal to plot stats
self.atom = Atom
self.findmeans()
self.dp4data = ui.table_widget.Tab1.worker.DP4Data
self.Isomers = ui.table_widget.Tab1.worker.Isomers
self.errortable = QtWidgets.QTableWidget(self)
self.errortable.setColumnCount(5)
self.errortable.setHorizontalHeaderLabels(["Atom Label", "Calc Shift", "Scaled", "Exp", "Error"])
self.errortable.setColumnWidth(0, 70)
self.errortable.setColumnWidth(1, 70)
self.errortable.setColumnWidth(2, 70)
self.errortable.setColumnWidth(3, 70)
self.errortable.setColumnWidth(4, 70)
self.statsfigure = Figure()
self.statscanvas = FigureCanvas(self.statsfigure)
# self.errortable.cellClicked.connect(self.plot)
self.errortable.itemSelectionChanged.connect(self.plot)
self.statscanvas.mpl_connect('button_press_event', self.selectpoint)
def plot(self):
r = int(self.errortable.currentRow())
if r >= 0:
error = float(self.errortable.item(r, 4).text())
# decide which stats params have been used
self.statsfigure.clear()
self.statsfig = self.statsfigure.add_subplot(111)
if self.atom == 'H':
m = abs(max([item for sublist in self.dp4data.Herrors for item in sublist], key=abs))
p_color = 3
elif self.atom == 'C':
m = abs(max([item for sublist in self.dp4data.Cerrors for item in sublist], key=abs))
p_color = 0
# plot all errors at low transparency
for e in self.errors:
self.statsfig.plot([e, e], [0, self.multipdf([float(e)])], color='C'+ str(p_color), alpha=0.25)
self.statsfig.plot(e, self.multipdf([float(e)]), 'o', color='C'+ str(p_color), alpha=0.25)
x = np.linspace(- 2 * m, 2 * m, 1000)
self.statsfig.plot(x, self.multipdf(x),color = "C1")
self.statsfig.plot([error, error], [0, self.multipdf([float(error)])], color='C'+ str(p_color), alpha=1)
self.statsfig.plot(error, self.multipdf([float(error)]), 'o', color='C'+ str(p_color), alpha= 1)
self.statsfig.set_xlim([x[0], x[-1]])
self.statsfig.set_xlabel("error (ppm)")
self.statsfig.set_ylabel("probability density")
self.statscanvas.draw()
def multipdf(self, x):
y = np.zeros(len(x))
if self.atom == 'H':
for mean, std in zip(self.Hmeans, self.Hstdevs):
y += norm.pdf(x, loc=mean, scale=std)
elif self.atom == 'C':
for mean, std in zip(self.Cmeans, self.Cstdevs):
y += norm.pdf(x, loc=mean, scale=std)
return y
def findmeans(self):
if ui.table_widget.Tab1.worker.settings.StatsParamFile == 'none':
self.Hmeans = [0]
self.Hstdevs = [0.18731058105269952]
self.Cmeans = [0]
self.Cstdevs = [2.269372270818724]
else:
self.Cmeans, self.Cstdevs, self.Hmeans, self.Hstdevs = ReadParamFile(
ui.table_widget.Tab1.worker.settings.StatsParamFile, 'g')
def populatetable(self):
# which isomer is selected
self.isomerindex = int(str(ui.table_widget.Tab4.IsomerSelect.currentText())[-1]) - 1
self.errortable.setRowCount(0)
if self.atom == 'H':
self.labels, self.shifts, self.exps, self.scaleds, self.errors = self.dp4data.Hlabels[self.isomerindex], \
self.dp4data.Hshifts[self.isomerindex], \
self.dp4data.Hexp[self.isomerindex], \
self.dp4data.Hscaled[self.isomerindex], \
self.dp4data.Herrors[self.isomerindex]
elif self.atom == 'C':
self.labels, self.shifts, self.exps, self.scaleds, self.errors = self.dp4data.Clabels[self.isomerindex], \
self.dp4data.Cshifts[self.isomerindex], \
self.dp4data.Cexp[self.isomerindex], \
self.dp4data.Cscaled[self.isomerindex], \
self.dp4data.Cerrors[self.isomerindex]
self.errortable.setRowCount(len(self.labels))
# set info in rows and columns
c = 0
for label, shift, exp, scaled, error in zip(self.labels, self.shifts, self.exps, self.scaleds, self.errors):
self.errortable.setItem(c, 0, QtWidgets.QTableWidgetItem(label))
self.errortable.setItem(c, 1, QtWidgets.QTableWidgetItem(str(round(shift, 2))))
self.errortable.setItem(c, 2, QtWidgets.QTableWidgetItem(str(round(scaled, 2))))
self.errortable.setItem(c, 3, QtWidgets.QTableWidgetItem(str(round(exp, 2))))
self.errortable.setItem(c, 4, QtWidgets.QTableWidgetItem(str(round(error, 2))))
c += 1
self.errortable.selectRow(0)
self.plot()
def selectpoint(self, event):
self.xpos = event.xdata
self.ypos = event.ydata
# find the closest point to the click
coords = self.statsfig.transData.transform((self.xpos, self.ypos))
coordinates = np.array(self.statsfig.transData.transform(list(zip(self.errors, self.multipdf(self.errors)))))
mindis = np.argmin((coordinates[:, 0] - coords[0]) ** 2 + (coordinates[:, 1] - coords[1]) ** 2)
self.errortable.selectRow(mindis)
class CalculationTab(QtWidgets.QWidget):
signal_start_PyDP4 = QtCore.pyqtSignal()
def __init__(self):
self.log_file = ''
super(CalculationTab, self).__init__()
self.cwd = Path(os.getcwd())
self.Overall_verticallayout =QtWidgets.QVBoxLayout(self)
self.Overall_widget = QtWidgets.QWidget(self)
self.title = QtWidgets.QLabel(self)
self.title.setText("DP5 GUI")
self.title.setObjectName("Title")
self.Overall_verticallayout.addWidget(self.title)
########### input
self.input_widget = QtWidgets.QWidget(self)
self.input_layout = QtWidgets.QHBoxLayout(self)
self.structure_widget = QtWidgets.QWidget(self)
self.structure_layout = QtWidgets.QVBoxLayout(self)
self.Add_structure = QtWidgets.QPushButton(self)
self.Add_structure.setObjectName("Add_structure")
self.Add_structure.setText("Add structure")
self.remove_structure = QtWidgets.QPushButton(self)
self.remove_structure.setObjectName("remove_structure")
self.remove_structure.setText("Remove selected")
self.structure_layout.addWidget(self.Add_structure)
self.structure_layout.addWidget(self.remove_structure)
self.structure_widget.setLayout(self.structure_layout)
self.input_layout.addWidget(self.structure_widget)
self.Structure_list = QtWidgets.QListWidget(self)
self.Structure_list.setMaximumHeight(self.Add_structure.sizeHint().height() * 2)
self.Structure_list.setObjectName("Structure_list")
self.input_layout.addWidget(self.Structure_list)
self.NMR_widget = QtWidgets.QWidget(self)
self.NMR_layout = QtWidgets.QVBoxLayout(self)
self.Add_NMR = QtWidgets.QPushButton(self)
self.Add_NMR.setObjectName("Add_NMR")
self.Add_NMR.setText("Add raw NMR")
self.Add_NMR_desc = QtWidgets.QPushButton(self)
self.Add_NMR_desc.setObjectName("Add_NMR")
self.Add_NMR_desc.setText("Add NMR desc")
self.remove_NMR = QtWidgets.QPushButton(self)
self.remove_NMR.setObjectName("remove_NMR")
self.remove_NMR.setText("Remove selected")
self.NMR_layout.addWidget(self.Add_NMR)
self.NMR_layout.addWidget(self.Add_NMR_desc)
self.NMR_layout.addWidget(self.remove_NMR)
self.NMR_widget.setLayout(self.NMR_layout)
self.input_layout.addWidget(self.NMR_widget)
self.NMR_list = QtWidgets.QListWidget(self)
self.NMR_list.setObjectName("NMR_list")
self.NMR_list.setMaximumHeight(self.Add_structure.sizeHint().height() * 3)
self.input_layout.addWidget(self.NMR_list)
self.Output_add = QtWidgets.QPushButton(self)
self.Output_add.setObjectName("Out add")
self.Output_add.setText("Output Folder")
self.input_layout.addWidget(self.Output_add)
self.Output_list = QtWidgets.QListWidget(self)
self.Output_list.setObjectName("Out list")
self.Output_list.setMaximumHeight(self.Add_structure.sizeHint().height() * 2)
self.input_layout.addWidget(self.Output_list)
self.input_widget.setLayout(self.input_layout)
self.Overall_verticallayout.addWidget(self.input_widget)
############ workflow
self.workflow_layout = QtWidgets.QGridLayout(self)
self.workflow_widget = QtWidgets.QWidget(self)
self.workflow_title = QtWidgets.QLabel(self)
self.workflow_title.setObjectName("workflow_title")
self.workflow_title.setText("Workflow")
self.workflow_layout.addWidget(self.workflow_title, 0, 0)
self.CleanUp_yn = QtWidgets.QCheckBox(self)
self.CleanUp_yn.setObjectName("CleanUp_yn")
self.CleanUp_yn.setText("Clean\n"
"Structures")
self.workflow_layout.addWidget(self.CleanUp_yn,1,0)
self.Gen_diastereomers_yn = QtWidgets.QCheckBox(self)
self.Gen_diastereomers_yn.setObjectName("Gen_diastereomers_yn")
self.Gen_diastereomers_yn.setText("Generate\n"
"Diastereomers")
self.workflow_layout.addWidget(self.Gen_diastereomers_yn,1,1)
self.Solvent_yn = QtWidgets.QCheckBox(self)
self.Solvent_yn.setObjectName("Solvent_yn")
self.Solvent_yn.setText("Solvent")
self.workflow_layout.addWidget(self.Solvent_yn,1,2)
self.solvent_drop = QtWidgets.QComboBox(self)
self.solvent_drop.setObjectName("solvent_drop")
solvents = ['chloroform', 'dimethylsulfoxide', 'benzene', 'methanol', 'pyridine', 'acetone']
self.solvent_drop.addItems(solvents)
self.workflow_layout.addWidget(self.solvent_drop,2,2)
self.MM_yn = QtWidgets.QCheckBox(self)
self.MM_yn.setObjectName("MM_yn")
self.MM_yn.setText("Molecular\nMechanics")
self.workflow_layout.addWidget(self.MM_yn,1,3)
self.MM_drop = QtWidgets.QComboBox(self)
self.MM_drop.setObjectName("MM_drop")
MM = ["MacroModel","Tinker"]
self.MM_drop.addItems(MM)
self.workflow_layout.addWidget(self.MM_drop,2,3)
self.MM_advanced = QtWidgets.QPushButton(self)
self.MM_advanced.setText("Advanced Settings")
self.workflow_layout.addWidget(self.MM_advanced,3,3)
self.DFT_yn = QtWidgets.QCheckBox(self)
self.DFT_yn.setObjectName("DFT_yn")
self.DFT_yn.setText("DFT\nCalculations")
self.workflow_layout.addWidget(self.DFT_yn, 1, 4)
self.DFT_drop = QtWidgets.QComboBox(self)
self.DFT_drop.setObjectName("DFT_drop")
DFT = ["Gaussian", "NWChem"]
self.DFT_drop.addItems(DFT)
self.workflow_layout.addWidget(self.DFT_drop, 2, 4)
self.DFT_advanced = QtWidgets.QPushButton(self)
self.DFT_advanced.setText("Advanced Settings")
self.workflow_layout.addWidget(self.DFT_advanced,3,4)
self.Assignment_yn = QtWidgets.QCheckBox(self)
self.Assignment_yn.setObjectName("Assignment_yn")
self.Assignment_yn.setText("NMR\nAssignment")
self.workflow_layout.addWidget(self.Assignment_yn, 1, 5)
self.DP5_stat_yn = QtWidgets.QCheckBox(self)
self.DP5_stat_yn.setObjectName("DP5_stat_yn")
self.DP5_stat_yn.setText("DP5 Statistics")
self.workflow_layout.addWidget(self.DP5_stat_yn, 1, 6)
self.DP4_stat_yn = QtWidgets.QCheckBox(self)
self.DP4_stat_yn.setObjectName("DP4_stat_yn")
self.DP4_stat_yn.setText("DP4 Statistics")
self.workflow_layout.addWidget(self.DP4_stat_yn, 1, 7)
self.Add_stats_model = QtWidgets.QPushButton(self)
self.Add_stats_model.setObjectName("Add_stats_model")
self.Add_stats_model.setText("Add stats model")
self.workflow_layout.addWidget(self.Add_stats_model, 2, 7)
self.Stats_list = QtWidgets.QListWidget(self)
self.Stats_list.setMaximumHeight(self.Add_structure.sizeHint().height())
self.Stats_list.setObjectName("Stats_list")
self.workflow_layout.addWidget(self.Stats_list, 3, 7)
self.workflow_widget.setLayout(self.workflow_layout)
self.Overall_verticallayout.addWidget(self.workflow_widget)
############ calc
self.Gobutton = QtWidgets.QPushButton(self)
self.Gobutton.setObjectName("Gobutton")
self.Gobutton.setText("Calculate")
self.Overall_verticallayout.addWidget(self.Gobutton)
self.Output_box = QtWidgets.QTextEdit(self)
self.Output_box.setObjectName("Output_box")
self.Overall_verticallayout.addWidget(self.Output_box)
self.Overall_widget.setLayout(self.Overall_verticallayout)
#################################################################################################buttons methods
# adding structures to the gui
self.NMR_paths = []
self.Structure_paths = []
self.Output_folder = Path.cwd()
self.Add_structure.clicked.connect(self.addstructure)
self.remove_structure.clicked.connect(self.removestructure)
self.Add_NMR.clicked.connect(self.addNMR)
self.Add_NMR_desc.clicked.connect(self.remove_all_NMR)
self.Add_NMR_desc.clicked.connect(self.addNMRdesc)
self.remove_NMR.clicked.connect(self.removeNMR)
self.Output_add.clicked.connect(self.addoutputfolder)
# selecting solvent check box
self.solvent_drop.setEnabled(False)
self.Solvent_yn.stateChanged.connect(self.solventtoggle)
self.MM_drop.setEnabled(False)
self.MM_advanced.setEnabled(False)
self.MM_yn.stateChanged.connect(self.MMtoggle)
self.DFT_drop.setEnabled(False)
self.DFT_advanced.setEnabled(False)
self.DFT_yn.stateChanged.connect(self.DFTtoggle)
self.Assignment_yn.stateChanged.connect(self.Assignment_toggle)
# adding a stats model
self.Add_stats_model.setEnabled(False)
self.DP4_stat_yn.stateChanged.connect(self.DP4toggle)
self.DP5_stat_yn.stateChanged.connect(self.DP5toggle)
self.Add_stats_model.clicked.connect(self.addstats)
#####################################################################################running PyDP4 in new thread
# self.Gobutton.clicked.connect(self.Gotoggle)
self.Gobutton.clicked.connect(self.get_current_values)
# make worker object
self.worker = PyDP4WorkerObject()
# make thread
self.thread = QtCore.QThread()
# move the worker object to the ne thread
self.worker.moveToThread(self.thread)
# connect workers finished signal to quit thread
self.worker.finished.connect(self.thread.quit)
# connect workers finished signal to enable go button
self.worker.finished.connect(self.Gotoggle)
self.worker.finished.connect(self.enabletabs)
# connect start background signal to background job slot
self.Gobutton.clicked.connect(self.start_PyDP4)
# start pydp4 give start signal
# go button press disables go button
self.Gobutton.clicked.connect(self.Gotoggle)
# start signal starts pydp4
self.signal_start_PyDP4.connect(self.worker.runPyDP4)
self.DFT_settings = DFT_advanced_settings()
self.DFT_advanced.clicked.connect(self.DFT_pop)
self.MM_settings = MM_advanced_settings()
self.MM_advanced.clicked.connect(self.MM_pop)
def DFT_pop(self):
self.DFT_settings.show()
def MM_pop(self):
self.MM_settings.show()
def enabletabs(self):
ui.table_widget.addplottabs()
def addoutputfolder(self):
# filename = QtWidgets.QFileDialog.getOpenFileName()
filename = QtWidgets.QFileDialog.getExistingDirectory()
# self.NMR_list.addItem(str(filename[0].split("/")[-1]))
self.Output_list.clear()
self.Output_list.addItem(filename)
self.Output_folder = Path(filename)
def get_current_values(self):
import PyDP4
import datetime
import getpass
self.settings = PyDP4.Settings()
self.settings.GUIRunning = True
now = datetime.datetime.now()
self.settings.StartTime = now.strftime('%d%b%H%M')
self.settings.user = getpass.getuser()
self.settings.DarwinScrDir.replace('/u/', self.settings.user)
# Read config file and fill in settings in from that
self.settings = PyDP4.ReadConfig(self.settings)
# add output folder
self.settings.OutputFolder = self.Output_folder
self.log_file = open(self.Output_folder / "DP4_log.log", "w+")
self.settings.InputFilesPaths = self.Structure_paths
# copy structures to output folder
for f in self.Structure_paths:
if not Path(self.Output_folder / f).exists():
shutil.copyfile(f, self.settings.OutputFolder / f)
# add structures
Smiles = []
Smarts = []
InChIs = []
for index in range(self.Structure_list.count()):
list_text = self.Structure_list.item(index).text()
if list_text != '':
# check if file is text or sdf
if list_text.endswith('.sdf'):
self.settings.InputFiles.append(list_text[:-4])
# else its a text file - work out if it contains smiles smarts or inchis
elif (list_text.endswith('Smiles')) or (list_text.endswith('smiles')):
Smiles.append(list_text)
elif (list_text.endswith('Smarts')) or (list_text.endswith('smarts')):
Smarts.append(list_text)