-
Notifications
You must be signed in to change notification settings - Fork 0
/
CycloPs.py
executable file
·1584 lines (1412 loc) · 55.6 KB
/
CycloPs.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
# Experimental user interface for peptide library generation
#
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import os
import tempfile
import locale
import operator
# import pdb
from pyvirtualdisplay import Display
# from subprocess import call
locale.setlocale(locale.LC_ALL, "")
# logging
import sys
# sys.stderr = open('my_stderr.txt', 'w')
# sys.stdout = open('my_stdout.txt', 'w')
# from PIL import Image as PIL
from PIL import ImageTk as piltk
# import pybel
# import openbabel
# import rdkit
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import AllChem
# import PepLibGen
# Check which PepLibGen is used - useful for building with py2app/py2exe
# print "PepLibGen", PepLibGen.__file__
from PepLibGen.StructGen import zinc_peptides
from PepLibGen.StructGen import synthrules
from PepLibGen.StructGen import StructGen as sg
from PepLibGen.StructGen import aminoacids as aa
from PepLibGen.Analysis import chem_analysis as ca
# Hack to make RDKIT play nice when packaged...
if "RDBASE" not in list(os.environ.keys()):
os.environ["RDBASE"] = os.getcwd()
zinc_def_file = "zinc_output"
if not os.path.exists(zinc_def_file):
zinc_def_file = os.path.join(os.path.dirname(sys.argv[0]), zinc_def_file)
# Warn if trying to generate more than this number of peptides
MAX_PEPTIDES = 1000000
class PepApp(tk.Frame):
"""
Main class of pepgui.py, where the main application window is defined
"""
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid(sticky="NSEW")
self.single_frame = SinglePepFrame()
self.single_frame.grid(row=1, column=0)
self.lib_frame = PepLibraryFrame()
self.lib_frame.grid(
row=1,
column=0,
columnspan=2,
sticky=tk.N + tk.S + tk.E + tk.W,
padx=10,
pady=10,
)
self.lib_frame.grid_remove()
self.chooseFrame()
self.master.title("CycloPs")
self.temp_holder, self.temp_filename = tempfile.mkstemp()
# Make resizeable
top = self.winfo_toplevel()
top.resizable(0, 0)
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
# self.rowconfigure(0, weight =1)
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.columnconfigure(1, weight=1)
self.topLevelMenu()
# Variables
self.on_off = dict(
[(amino, tk.IntVar()) for amino in list(aa.all_aminos.keys())]
)
def topLevelMenu(self):
"""
Create top level menu
"""
top = self.winfo_toplevel()
self.topMenu = tk.Menu(top)
top["menu"] = self.topMenu
self.optionsMenu = tk.Menu(self.topMenu)
self.topMenu.add_cascade(
label="Modify Amino-acid library", menu=self.optionsMenu
)
self.optionsMenu.add_command(label="Exit", command=self.quit)
# Add option to edit available amino acids.
self.optionsMenu.add_command(
label="Choose L-Amino Acids", command=self.naturalAminoChooser
)
self.optionsMenu.add_command(
label="Choose D-Amino Acids", command=self.dAminoChooser
)
self.optionsMenu.add_command(
label="Choose special Amino Acids", command=self.specialAminoChooser
)
self.optionsMenu.add_command(label="ZINC amino acids", command=self.zinc_aminos)
def destroy(self):
tk.Frame.destroy(self)
try:
os.close(self.temp_handle)
os.remove(self.temp_filename)
except:
pass
def chooseFrame(self):
"""
Buttons to choose between single peptide frame or library frame.
Will not show both frames at once - hide one, and show one.
"""
self.show_pep_frame = tk.Button(
self, text="Peptide Generator", command=self.displayPepFrame, height=1
)
self.show_pep_frame.grid(row=0, column=0, sticky=tk.E + tk.N + tk.S)
self.show_lib_frame = tk.Button(
self,
text="Peptide Library Generator",
command=self.displayLibFrame,
height=1,
)
self.show_lib_frame.grid(row=0, column=1, sticky=tk.W + tk.N + tk.S)
self.show_pep_frame.config(relief=tk.SUNKEN)
def displayPepFrame(self):
"""
Hides the library generation frame, shows the peptide generation frame
"""
self.lib_frame.grid_remove()
self.single_frame.grid()
self.show_pep_frame.config(relief=tk.SUNKEN)
self.show_lib_frame.config(relief=tk.RAISED)
def displayLibFrame(self):
"""
Hides the peptide generation frame, shows the library generation frame
"""
self.single_frame.grid_remove()
self.lib_frame.grid()
self.show_lib_frame.config(relief=tk.SUNKEN)
self.show_pep_frame.config(relief=tk.RAISED)
def aminoChooser(self, amino_type):
"""
Presents window for selection of amino acids used to generate peptides
"""
self.chooser = tk.Toplevel(padx=50)
self.chooser.title("Amino Chooser")
self.chooser.resizable(1, 0)
self.chooser_frame = tk.LabelFrame(self.chooser)
if amino_type == aa.aminos:
self.chooser_frame.config(text="Natural Amino Acids")
elif amino_type == aa.d_aminos:
self.chooser_frame.config(text="D-Amino Acids")
elif amino_type == aa.special_aminos:
self.chooser_frame.config(text="Special Amino Acids")
self.chooser_frame.grid(sticky=tk.E + tk.W)
self.checkbuttons = dict([(amino, None) for amino in sg.all_aminos])
for amino in sorted(sg.all_aminos.keys()):
if amino in list(sg.aminodata.keys()):
# print '%s is in aminodata' % (amino)
self.on_off[amino].set(1)
self.checkbuttons[amino] = tk.Checkbutton(
self.chooser_frame,
text=amino,
variable=self.on_off[amino],
command=self.add_remove_amino,
)
if amino in amino_type:
self.checkbuttons[amino].grid(sticky=tk.W)
all_toggle = lambda: self.toggleAminoSet(amino_type)
self.choose_all = tk.Button(
self.chooser_frame,
command=all_toggle,
text="Choose/Unchoose all.",
height=1,
)
self.choose_all.grid()
def toggleAminoSet(self, amino_set):
# pdb.set_trace()
state = [self.on_off[a].get() for a in list(amino_set.keys())]
if not len(state) == len([s for s in state if s]):
# If not all are on, turn them on
[self.on_off[name].set(1) for name in amino_set]
else:
[self.on_off[name].set(0) for name in amino_set]
self.add_remove_amino()
def naturalAminoChooser(self):
"""
Presents window to add/remove natural amino acids from working library
"""
self.aminoChooser(aa.aminos)
# print len(aa.aminos)
def dAminoChooser(self):
"""
Presents window to add/remove d amino acids from working library
"""
self.aminoChooser(aa.d_aminos)
# print len(aa.d_aminos)
def specialAminoChooser(self):
"""
Presents window to add/remove d amino acids from working library
"""
self.aminoChooser(aa.special_aminos)
# print len(aa.special_aminos)
def add_remove_amino(self):
"""
Adds/removes amino acid from library on checkbox tick.
Syncs state of checkboxes with amino-acid library.
"""
for name in list(sg.all_aminos.keys()):
if self.on_off[name].get() and name not in list(sg.aminodata.keys()):
# print name
sg.add_amino(name)
elif not self.on_off[name].get() and name in list(sg.aminodata.keys()):
sg.remove_amino(name)
# print len(sg.aminodata)
def zinc_aminos(self):
test = ZincChooser(self)
class SinglePepFrame(tk.Frame):
"""
Holds the interface to work with a single peptide
"""
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid(sticky="NSEW")
self.createSinglePepWidgets()
self.temp_holder, self.temp_filename = tempfile.mkstemp()
def destroy(self):
tk.Frame.destroy(self)
try:
os.close(self.temp_handle)
os.remove(self.temp_filename)
except:
pass
def createSinglePepWidgets(self):
"""
Create widgets inside a frame that deal with generating a single peptide
"""
# Variables
self.seq_holder = tk.StringVar() # Holds peptide sequence
self.old_sequence = "" # Used to set constraint to linear
self.num_resis = tk.IntVar() # Value used for list selection
self.num_resis.set(5)
# when new sequence is entered
self.cur_constraint = tk.StringVar() # Selected constraint
self.num_menu_items = 0
self.labels = [] # Keep references to menu labels for easy deletion
self.smiles_holder = tk.StringVar()
self.imagedata = "" # Reference to image to hold it around...
# Can be synthesised...
self.can_synthesise = tk.StringVar()
# Defaults to menus
self.text_entry = tk.IntVar()
self.text_entry.set(0)
self.logp = tk.StringVar()
self.druglike = tk.StringVar()
# Sequence entry form
self.seq_frame = tk.LabelFrame(self, text="Enter Sequence Here")
self.seq_frame.grid(row=0, columnspan=3, sticky=tk.E + tk.W)
self.seq_frame.columnconfigure(0, weight=1)
self.SequenceEntryWidgetInitialise()
self.SequenceEntryUpdate()
# Peptide builder
self.pep_build_button = tk.Button(
self.seq_frame, text="Peptide Builder", command=self.CallPeptideBuilder
)
self.pep_build_button.grid()
# Constraint menu
self.cur_constraint.set("linear")
self.constButton = tk.Menubutton(
self.seq_frame, text="Select desired constraint", relief="raised"
)
self.constButton.grid(row=0, column=self.num_resis.get())
self.constButton.menu = tk.Menu(self.constButton, tearoff=0)
self.constButton["menu"] = self.constButton.menu
self.constButton.menu.add_radiobutton(
label="linear",
value="linear",
variable=self.cur_constraint,
command=self.SmilesHandlerWrapper,
)
self.constButton.bind("<1>", self.PopulateConstraintMenu)
# Label to display SMILES
self.smiles_frame = tk.LabelFrame(self, text="Sequence SMILES")
self.smiles_frame.grid(
row=1, column=0, columnspan=3, sticky=tk.N + tk.E + tk.S + tk.W
)
self.smiles_frame.columnconfigure(0, weight=1)
self.smiles_label = tk.Entry(
self.smiles_frame,
state="readonly",
readonlybackground="white",
textvariable=self.smiles_holder,
)
self.smiles_label.grid(sticky=tk.W + tk.E)
# Label to display around synthesis rules
self.synth_frame = tk.LabelFrame(self, text="Synthesisability")
self.synth_frame.grid(
row=2, column=0, columnspan=3, sticky=tk.N + tk.E + tk.S + tk.W
)
self.synth_frame.columnconfigure(0, weight=1)
self.synth_label = tk.Label(
self.synth_frame, textvariable=self.can_synthesise, justify="left"
)
self.synth_label.grid(sticky=tk.W + tk.E)
# Label to display around partition coefficient
self.logp_frame = tk.LabelFrame(
self, text="Estimated Octanol-Water Partion Coefficient (logP)"
)
self.logp_frame.grid(row=3, column=0, columnspan=3, sticky=tk.W + tk.E)
self.logp_frame.columnconfigure(0, weight=1)
self.logp_label = tk.Entry(
self.logp_frame,
textvariable=self.logp,
justify=tk.LEFT,
state="readonly",
readonlybackground="white",
relief="flat",
)
self.logp_label.grid(sticky=tk.E + tk.W)
# Label to display around Druglike criteria
self.druglike_frame = tk.LabelFrame(self, text="Druglike Properties")
self.druglike_frame.grid(row=4, column=0, columnspan=3, sticky=tk.E + tk.W)
self.druglike_frame.columnconfigure(0, weight=1)
# Label to display around Molecule Image
self.image_frame = tk.LabelFrame(self, text="Structure")
self.image_frame.grid(
row=0, column=4, columnspan=3, rowspan=5, sticky=tk.N + tk.E + tk.S + tk.W
)
self.image_frame.columnconfigure(0, weight=1)
def CallPeptideBuilder(self):
"""
Starts the peptide builder
"""
def close_handler():
try:
self.SmilesHandlerWrapper()
finally:
self.builder.destroy()
self.builder = PeptideBuilder(self, seq_holder=self.seq_holder)
self.builder.protocol("WM_DELETE_WINDOW", close_handler)
def SequenceEntryWidgetInitialise(self):
"""
Draws the sequence entry widgets
Both entry box, and menu lists
"""
# Text entry box
self.sequenceEntry = tk.Entry(self.seq_frame, textvariable=self.seq_holder)
def SequenceEntryUpdate(self):
"""
Chooses which entry widget is displayed with self.text_entry
"""
self.sequenceEntry.grid(row=0, sticky=tk.N + tk.S + tk.E + tk.W)
self.sequenceEntry.bind("<KeyPress-Return>", self.SmilesHandler)
def UpdateSequenceEntryBoxes(self, event):
"""
Updates the list of aminos in the sequence entry boxes.
"""
for i in self.sequence_menu:
self.sequence_menu[i].menu.delete(0, tk.END)
for amino in sg.aminodata:
self.sequence_menu[i].menu.add_radiobutton(
label=amino,
value=amino,
variable=self.sequence_menu_variables[i],
command=self.UpdateSeq,
)
def UpdateSeq(self):
"""
Sets the sequence StringVar from the entry menu lists
"""
new_seq = ""
for i in self.sequence_menu_variables:
if self.sequence_menu_variables[i].get() != "Select Amino Acid":
new_seq += "," + self.sequence_menu_variables[i].get()
self.seq_holder.set(new_seq.strip(","))
# print self.seq_holder.get()
self.SmilesHandler("")
def PopulateConstraintMenu(self, event):
"""
Sets the options in the drop down list to be the available constraint
options.
"""
seq = self.seq_holder.get()
seq = seq.strip("\n")
if "," in seq or "-" in seq or "ZINC" in seq:
seq = seq.split(",")
# print seq
all_constraints = sg.what_constraints(seq)
# Make return options more readable in menu
for lab in self.labels:
self.constButton.menu.delete(lab)
self.labels = []
for con in all_constraints:
if "SS" in con:
lab = "Disulphide Bond " + con
self.constButton.menu.add_radiobutton(
label=lab,
value=con,
variable=self.cur_constraint,
command=self.SmilesHandlerWrapper,
)
self.labels.append(lab)
if "HT" in con:
lab = "Head-Tail Bond"
self.constButton.menu.add_radiobutton(
label=lab,
value=con,
variable=self.cur_constraint,
command=self.SmilesHandlerWrapper,
)
self.labels.append(lab)
if "SC" in con:
lab = "Side-Chain Bond " + con
self.constButton.menu.add_radiobutton(
label=lab,
value=con,
variable=self.cur_constraint,
command=self.SmilesHandlerWrapper,
)
self.labels.append(lab)
def SmilesHandlerWrapper(self):
"""
Calls self SmilesHandler when constraint is chosen
"""
self.SmilesHandler("")
def SmilesHandler(self, event=""):
"""
Sets the appropriate label text to the SMILES of the input peptide
sequence.
Also displays the molecule image and synthesis rules
"""
# If this is the first time the SMILES has been written - set the
# constraint to a default of 'linear' as it is the only one that
# must really exist
if self.seq_holder.get() != self.old_sequence:
self.cur_constraint.set("linear")
if (
"," in self.seq_holder.get()
or "-" in self.seq_holder.get()
or "ZINC" in self.seq_holder.get()
):
seq = self.seq_holder.get().replace("\n", "").split(",")
else:
seq = self.seq_holder.get()
# print seq
# print 'Constraint', self.cur_constraint.get()
try:
if self.cur_constraint.get() == "linear":
# print 'Smiles',
self.smiles_holder.set(sg.linear_peptide_smiles(seq))
else:
constraint = self.cur_constraint.get()
# print 'Smiles, const', seq, constraint
self.smiles_holder.set(
sg.constrained_peptide_smiles(seq, constraint)[2]
)
self.old_sequence = self.seq_holder.get()
self.DrawMoleculeImage()
self.update_synthesis_rules()
self.update_logp()
self.update_druglike()
# print self.smiles_holder.get()
mol = Chem.MolFromSmiles(self.smiles_holder.get())
self.smiles_holder.set(Chem.MolToSmiles(mol, True))
except sg.UndefinedAminoError:
# print 'SMILES GETTER ERROR'
self.smiles_holder.set("None")
def DrawMoleculeImage(self):
"""
Adds label displaying a 2D model of the (constrained) peptide
"""
try:
self.image_label.destroy()
except AttributeError: # If it doesn't already exist
pass
try:
# self.mol_obj = pybel.readstring('smi',self.smiles_holder.get())
# self.mol_obj.draw(show=False, filename=self.temp_filename)
# image = PIL.open(self.temp_filename)
self.mol_obj = Chem.MolFromSmiles(self.smiles_holder.get())
image = Draw.MolToImage(self.mol_obj, size=(350, 350))
self.largeimage = Draw.MolToImage(self.mol_obj, size=(1000, 1000))
self.image = image # Save a reference, so it can be saved
self.imagedata = piltk.PhotoImage(image)
self.image_label = tk.Label(self.image_frame, image=self.imagedata)
self.image_label.grid(row=0, columnspan=2)
# Add savebutton
self.image_save = tk.Button(
self.image_frame, text="Save Molecule", command=self.SaveMoleculeDrawing
)
self.image_save.grid(row=1, column=0, sticky=tk.E + tk.W)
# Add 3D structure savebutton
self.struct_save = tk.Button(
self.image_frame, text="Write 3D structure", command=self.Write3D
)
self.struct_save.grid(row=1, column=1, columnspan=2)
except IOError as e: # Catch empty smiles strings
pass
def SaveMoleculeDrawing(self):
"""
Saves molecule drawing to a file
"""
try:
self.largeimage.save(
tkinter.filedialog.asksaveasfilename(
defaultextension=".png",
initialfile=self.seq_holder.get().strip()
+ "-"
+ self.cur_constraint.get(),
initialdir=os.path.expanduser("~"),
)
)
except KeyError:
self.largeimage.save(
tkinter.filedialog.asksaveasfilename(
initialfile=self.seq_holder.get().strip()
+ "-"
+ self.cur_constraint.get()
+ ".png",
initialdir=os.path.expanduser("~"),
)
)
def Write3D(self):
"""
Writes 3D structure to a file
"""
# self.mol_obj.make3D()
AllChem.EmbedMolecule(self.mol_obj)
AllChem.UFFOptimizeMolecule(self.mol_obj)
outfile = tkinter.filedialog.asksaveasfilename(
defaultextension=".sdf",
initialfile=self.seq_holder.get().strip() + "-" + self.cur_constraint.get(),
)
out = Chem.MolToMolBlock(self.mol_obj)
try:
handle = open(outfile, "w")
handle.write(out)
finally:
handle.close()
def update_synthesis_rules(self):
"""
Display whether a peptide is synthesisable or not, and why
"""
pep = self.seq_holder.get()
self.synth_label.config(bg="white")
self.synth_label.config(justify="left")
smiles = self.smiles_holder.get()
# pdb.set_trace()
if self.cur_constraint.get() != "linear":
bond_def = self.cur_constraint.get()[2:]
else:
bond_def = ""
if "," in pep or "-" in pep or "ZINC" in pep:
pep = pep.strip("\n").split(",")
# print pep
new_pep = "".join([sg.aminodata[resi]["Letter"] for resi in pep])
if len(new_pep) == len(pep):
pep = new_pep
try:
test_res = synthrules.run_all_tests(smiles, pep, bond_def)
results = "\n".join(test_res)
self.synth_label.config(fg="red")
self.can_synthesise.set(results)
except TypeError as e:
self.synth_label.config(fg="blue")
self.can_synthesise.set("PASSED")
else:
self.synth_label.config(fg="green")
self.can_synthesise.set("Unknown")
else:
try:
results = "\n".join(synthrules.run_all_tests(smiles, pep, bond_def))
self.synth_label.config(fg="red")
self.can_synthesise.set(results)
except TypeError:
self.synth_label.config(fg="blue")
self.can_synthesise.set("PASSED")
def update_logp(self):
"""
Display molecule LogP value
"""
self.logp.set(ca.log_partition_coefficient(self.smiles_holder.get()))
def update_druglike(self):
"""
Show which druglike rules the molecule passes and fails
"""
try:
for label in self.druglike_labels:
label.destroy()
except AttributeError: # First run, doesn't exist
pass
passed, failed = ca.lipinski_trial(self.smiles_holder.get())
self.passed = []
self.failed = []
self.druglike_labels = []
for result in passed:
self.passed.append(tk.StringVar())
self.passed[-1].set("PASSED: " + result)
lab = tk.Entry(
self.druglike_frame,
textvariable=self.passed[-1],
fg="blue",
state="readonly",
readonlybackground="white",
relief="flat",
)
lab.grid(sticky=tk.E + tk.W)
self.druglike_labels.append(lab)
for result in failed:
self.failed.append(tk.StringVar())
self.failed[-1].set("FAILED: " + result)
lab = tk.Entry(
self.druglike_frame,
textvariable=self.failed[-1],
fg="red",
state="readonly",
readonlybackground="white",
relief="flat",
)
lab.grid(sticky=tk.E + tk.W)
self.druglike_labels.append(lab)
class PepLibraryFrame(tk.Frame):
"""
Holds the interface to work with a peptide library
"""
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.createPepLibraryWidgets()
def createPepLibraryWidgets(self):
"""
Create widgets inside a frame that generate a library of peptides
"""
# Main frame
for row in range(1, 8):
self.rowconfigure(row, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
# Variables
self.seqs_from_file = tk.StringVar()
self.lib_seq_holder = tk.StringVar()
self.lib_len = tk.IntVar()
self.gen_linears = tk.IntVar()
self.num_lin = tk.IntVar()
# self.num_lin.set('Linear peptides... ')
self.gen_SS = tk.IntVar()
self.num_SS = tk.IntVar()
# self.num_SS.set('Disulphide bonded peptides... ')
self.gen_HT = tk.IntVar()
self.num_HT = tk.IntVar()
# self.num_HT.set('Head-Tail bonded peptides... ')
self.gen_SCSC = tk.IntVar()
self.num_SCSC = tk.IntVar()
# self.num_SCSC.set('Side-chain to side-chain bonded peptides... ')
self.gen_SCNT = tk.IntVar()
self.num_SCNT = tk.IntVar()
# self.num_SCNT.set('Side-chain to N-terminal bonded peptides... ')
self.gen_SCCT = tk.IntVar()
self.num_SCCT = tk.IntVar()
# self.num_SCCT.set('Side-chain to C-terminal bonded peptides... ')
self.gen_linears.set(1)
self.smiles_gen = [] # Holder for all library smiles
# Include/remove non synthesisable peptides
self.synth_filter = tk.IntVar()
# Include/remove druglike peptides
self.druglike_only = tk.IntVar()
# N-methylate peptides
self.nmethylate = tk.IntVar()
# Unreadable peptides found in input file
self.unreadable_peptides = tk.IntVar()
# Sequence entry form
self.lib_entry_frame = tk.LabelFrame(self, text="Enter library pattern")
self.lib_entry_frame.grid(row=0, columnspan=2, sticky=tk.N + tk.S + tk.E + tk.W)
self.libEntry = tk.Entry(
self.lib_entry_frame, textvariable=self.lib_seq_holder, width=60
)
self.libEntry.grid(sticky=tk.N + tk.S + tk.E + tk.W, columnspan=2)
self.libEntry.bind("<Return>", self.countPosPeptides)
self.libOptionText = tk.Label(
self.lib_entry_frame,
text="OR - choose a file containing peptide definitions "
"(This will override library pattern and constraint choices.)",
)
self.libOptionText.grid(sticky=tk.N + tk.S + tk.E + tk.W, columnspan=2)
self.fileEntry = tk.Entry(
self.lib_entry_frame, textvariable=self.seqs_from_file, width=60
)
self.fileEntry.grid(sticky=tk.N + tk.S + tk.E + tk.W, columnspan=2)
self.fileEntryButton = tk.Button(
self.lib_entry_frame, text="Choose file", command=self.choosePeptideFile
)
self.fileEntryButton.grid(column=5, row=2)
# When return is pressed, count the peptides
self.fileEntry.bind("<Return>", self.tryPeptideFile)
# When Delete is pressed, if the widget is empty, reactive the
# library pattern and constraint widgets
self.fileEntry.bind("<Delete>", self.reactivateLibraryPanel)
self.fileEntry.bind("<BackSpace>", self.reactivateLibraryPanel)
# Choose constraints...
self.lib_constraint_frame = tk.LabelFrame(
self, text="Select which types of peptides will be included in the library"
)
self.lib_constraint_frame.grid(
row=1, rowspan=6, sticky=tk.N + tk.S + tk.E + tk.W
)
self.lin_button = tk.Checkbutton(
self.lib_constraint_frame,
text="Linear",
variable=self.gen_linears,
command=self.constraintSelectHandler,
)
self.lin_button.grid(sticky=tk.W)
self.SS_button = tk.Checkbutton(
self.lib_constraint_frame,
text="Disulphide bonded",
variable=self.gen_SS,
command=self.constraintSelectHandler,
)
self.SS_button.grid(sticky=tk.W)
self.HT_button = tk.Checkbutton(
self.lib_constraint_frame,
text="Head-tail bonded",
variable=self.gen_HT,
command=self.constraintSelectHandler,
)
self.HT_button.grid(sticky=tk.W)
self.SCSC_button = tk.Checkbutton(
self.lib_constraint_frame,
text="Side-chain to side-chain bonded",
variable=self.gen_SCSC,
command=self.constraintSelectHandler,
)
self.SCSC_button.grid(sticky=tk.W)
self.SCNT_button = tk.Checkbutton(
self.lib_constraint_frame,
text="Side-chain to N-terminus bond",
variable=self.gen_SCNT,
command=self.constraintSelectHandler,
)
self.SCNT_button.grid(sticky=tk.W)
self.SCCT_button = tk.Checkbutton(
self.lib_constraint_frame,
text="Side-chain to C-terminus bonded",
variable=self.gen_SCCT,
command=self.constraintSelectHandler,
)
self.SCCT_button.grid(sticky=tk.W)
# Display number of possible sequences
self.all_info_frame = tk.LabelFrame(
self, text="Maximum number of linear combinations"
)
self.all_info_frame.grid(row=7, columnspan=2, sticky=tk.N + tk.S + tk.E + tk.W)
self.lib_info_panel = tk.Entry(
self.all_info_frame,
state="readonly",
readonlybackground="white",
textvariable=self.lib_len,
)
self.lib_info_panel.grid(column=0, row=0, sticky=tk.N + tk.S + tk.E + tk.W)
self.warning_label = tk.Label(
self.all_info_frame,
text="Warning! Very large library - may crash while generating",
fg="red",
)
# Generate SMILES button (next to possible sequences)
self.generate_lib = tk.Button(
self.all_info_frame,
text="Generate Library SMILES anyway...",
command=self.genLibrarySMILES,
)
self.generate_lib.grid(row=0, column=1, sticky=tk.N + tk.S + tk.E + tk.W)
self.generate_lib.grid_remove()
# Display number of generated peptides
self.gen_info_frame = tk.LabelFrame(
self, text="Number of peptides generated for library"
)
self.gen_info_frame.grid(
column=1, row=1, rowspan=6, sticky=tk.N + tk.S + tk.E + tk.W
)
self.lin_text_panel = tk.Label(
self.gen_info_frame, text="Linear peptides... ", anchor=tk.W
)
self.lin_text_panel.grid(sticky=tk.W, row=0, column=0)
self.lin_info_panel = tk.Label(self.gen_info_frame, textvariable=self.num_lin)
self.lin_info_panel.grid(sticky=tk.W, row=0, column=1)
self.SS_text_panel = tk.Label(
self.gen_info_frame, text="Disulphide bonded peptides... ", anchor=tk.W
)
self.SS_text_panel.grid(sticky=tk.W, row=1, column=0)
self.SS_info_panel = tk.Label(self.gen_info_frame, textvariable=self.num_SS)
self.SS_info_panel.grid(sticky=tk.W, row=1, column=1)
self.HT_text_panel = tk.Label(
self.gen_info_frame, text="Head-tail bonded peptides... ", anchor=tk.W
)
self.HT_text_panel.grid(sticky=tk.W, row=2, column=0)
self.HT_info_panel = tk.Label(self.gen_info_frame, textvariable=self.num_HT)
self.HT_info_panel.grid(sticky=tk.W, row=2, column=1)
self.SCSC_text_panel = tk.Label(
self.gen_info_frame,
text="Side-chain to Side-chain bonded peptides... ",
anchor=tk.W,
)
self.SCSC_text_panel.grid(sticky=tk.W, row=3, column=0)
self.SCSC_info_panel = tk.Label(self.gen_info_frame, textvariable=self.num_SCSC)
self.SCSC_info_panel.grid(sticky=tk.W, row=3, column=1)
self.SCNT_text_panel = tk.Label(
self.gen_info_frame,
text="Side-chain to N-Terminus bonded peptides... ",
anchor=tk.W,
)
self.SCNT_text_panel.grid(sticky=tk.W, row=4, column=0)
self.SCNT_info_panel = tk.Label(self.gen_info_frame, textvariable=self.num_SCNT)
self.SCNT_info_panel.grid(sticky=tk.W, row=4, column=1)
self.SCCT_text_panel = tk.Label(
self.gen_info_frame,
text="Side-chain to C-Terminus bonded peptides... ",
anchor=tk.W,
)
self.SCCT_text_panel.grid(sticky=tk.W, row=5, column=0)
self.SCCT_info_panel = tk.Label(self.gen_info_frame, textvariable=self.num_SCCT)
self.SCCT_info_panel.grid(sticky=tk.W, row=5, column=1)
# Checkbutton to filter by synthrules
self.synth_check = tk.Checkbutton(
self,
text="Filter out peptides that break synthesis rules",
variable=self.synth_filter,
command=self.constraintSelectHandler,
)
self.synth_check.grid(row=8, columnspan=2, sticky=tk.W)
# Checkbutton to filter for druglike compounds
self.druglike_check = tk.Checkbutton(
self,
text="Filter out non-druglike peptides "
"(Most peptides over 3 residues in length are not druglike)",
variable=self.druglike_only,
command=self.constraintSelectHandler,
)
self.druglike_check.grid(row=9, columnspan=2, sticky=tk.W)
# Checkbutton for N-methylating peptides
self.nmethylation = tk.Checkbutton(
self,
text="N-methylate peptide backbone and n-terminus",
variable=self.nmethylate,
command=lambda: 0,
)
self.nmethylation.grid(row=10, columnspan=2, sticky=tk.W)
# Write Indicator
self.writelabel = tk.Label(self)
self.writelabel.grid(row=14, sticky=tk.E + tk.W, columnspan=2)
# Write SMILES button
self.write_lib = tk.Button(
self, text="Write SMILES to file", command=self.writeLibrarySMILES
)
self.write_lib.grid(columnspan=2, row=11, sticky=tk.N + tk.S + tk.E + tk.W)
# Write 2D button
self.write_png = tk.Button(
self,
text="Write 2D drawings of library to folder",
command=self.drawLibrary,
)
self.write_png.grid(columnspan=2, row=12, sticky=tk.N + tk.S + tk.E + tk.W)
# Write 3D buttons
self.write_pdbs = tk.Button(
self,
text="Write 3D structures of library to folder",
command=self.writeLibrary3D,
)
self.write_pdbs.grid(row=13, column=0, sticky=tk.N + tk.S + tk.E + tk.W)
self.write_pdb = tk.Button(
self,
text="Write 3D structures of library to single .sdf file",
command=self.writeLibrary3Dtofile,
)
self.write_pdb.grid(row=13, column=1, sticky=tk.N + tk.S + tk.E + tk.W)
def tryPeptideFile(self, event):
"""Attempt to process the input peptide file, deactivate pattern
and constraint widgets."""
self.countPosPeptides("")
self.libEntry.configure(state=tk.DISABLED)
for checkbutton in [
self.lin_button,
self.SS_button,
self.HT_button,
self.SCSC_button,
self.SCNT_button,
self.SCCT_button,
]:
checkbutton.configure(state=tk.DISABLED)
def choosePeptideFile(self):
"""Set the file to generate peptides from and try to process it."""
fhandle = tkinter.filedialog.askopenfile(
title="Choose a peptide definition file"
)