-
Notifications
You must be signed in to change notification settings - Fork 12
/
RGA.py
1238 lines (1123 loc) · 52.4 KB
/
RGA.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 and config
- policy network
- for i in 1,...,generation
- crossover
- RGA use policy network to select ligand ***
- crossover
- docking
- RGA update policy network ***
- mutation
- RGA use policy network to select ligand ***
- mutation
- docking
- RGA update policy network ***
- elites
'''
import argparse
PARSER = argparse.ArgumentParser()
# Allows the run commands to be submitted via a .json file.
PARSER.add_argument(
"--json",
"-j",
metavar="param.json",
help="Name of a json file containing all parameters. \
Overrides other arguments.",
)
# Allows the run in debug mode. Doesn't delete temp files.
PARSER.add_argument(
"--debug_mode",
"-d",
action="store_true",
default=False,
help="Run Autogrow in Debug mode. This keeps all \
temporary files and adds extra print statements.",
)
# receptor information
PARSER.add_argument(
"--filename_of_receptor",
"-r",
metavar="receptor.pdb",
default='./tutorial/PARP/4r6eA_PARP1_prepared.pdb',
help="The path to the receptor file. Should be .pdb file.",
)
PARSER.add_argument(
"--center_x",
"-x",
type=float,
default=-70.76,
help="x-coordinate for the center of the pocket to be tested by docking. (Angstrom)",
)
PARSER.add_argument(
"--center_y",
"-y",
type=float,
default=21.82,
help="y-coordinate for the center of the pocket to be tested by docking. (Angstrom)",
)
PARSER.add_argument(
"--center_z",
"-z",
type=float,
default=28.33,
help="z-coordinate for the center of the pocket to be tested by docking. (Angstrom)",
)
PARSER.add_argument(
"--size_x",
type=float,
default=25.0,
help="dimension of box to dock into in the x-axis (Angstrom)",
)
PARSER.add_argument(
"--size_y",
type=float,
default=20.0,
help="dimension of box to dock into in the y-axis (Angstrom)",
)
PARSER.add_argument(
"--size_z",
type=float,
default=25.0,
help="dimension of box to dock into in the z-axis (Angstrom)",
)
# Input/Output directories
PARSER.add_argument(
"--root_output_folder",
"-o",
type=str,
help="The Path to the folder which all output files will be placed.",
)
PARSER.add_argument(
"--source_compound_file",
"-s",
type=str,
default='./source_compounds/naphthalene_smiles.smi',
help="PATH to the file containing the source compounds. It must be \
tab-delineated .smi file. These ligands will seed the first generation.",
)
PARSER.add_argument(
"--filter_source_compounds",
choices=[True, False, "True", "False", "true", "false"],
default=True,
help="If True source ligands from source_compound_file will be \
filter using the user defined filter choices prior to the 1st generation being \
created. If False, ligands which would fail the ligand filters could seed \
the 1st generation. Default is True.",
)
PARSER.add_argument(
"--use_docked_source_compounds",
choices=[True, False, "True", "False", "true", "false"],
default=False,
help="If True source ligands will be docked prior to seeding generation 1. \
If True and the source_compound file already has docking/fitness metric score \
in -2 column of .smi file, it will not redock but reuse the scores from \
the source_compound_file.\
If True and no fitness metric score in -2 column of .smi file, it will \
dock each ligand from the source_compound_file and displayed as generation 0.\
If False, generation 1 will be randomly seeded by the source compounds with \
no preference and there will be no generation 0. \
If performing multiple simulations using same source compounds and protein, \
we recommend running once this and using the generation 0 ranked file as the \
source_compound_file for future simulations. \
Default is True.",
)
PARSER.add_argument(
"--start_a_new_run",
action="store_true",
default=False,
help="If False make a new folder and start a fresh simulation with Generation 0. \
If True find the last generation in the root_output_folder and continue to fill.\
Default is False.",
)
# SmilesMerge Settings
PARSER.add_argument(
"--max_time_MCS_prescreen",
type=int,
default=1,
help="amount time the pre-screen MCS times out. Time out doesnt prevent \
mcs matching just takes what it has up to that point",
)
PARSER.add_argument(
"--max_time_MCS_thorough",
type=int,
default=1,
help="amount time the thorough MCS times out. Time out doesnt prevent \
mcs matching just takes what it has up to that point",
)
PARSER.add_argument(
"--min_atom_match_MCS",
type=int,
default=4,
help="Determines the minimum number of atoms in common for a substructurematch. \
The higher the more restrictive, but the more likely for two ligands not to match",
)
PARSER.add_argument(
"--protanate_step",
action="store_true",
default=False,
help="Indicates if Smilesmerge uses protanated mols (if true) or deprot \
(if False) SmilesMerge is 10x faster when deprotanated",
)
# Mutation Settings
PARSER.add_argument(
"--rxn_library",
choices=["click_chem_rxns", "robust_rxns", "all_rxns", "Custom"],
default="all_rxns",
help="This set of reactions to be used in Mutation. \
If Custom, one must also provide rxn_file Path and function_group_library path",
)
PARSER.add_argument(
"--rxn_library_file",
type=str,
default="",
help="This PATH to a Custom json file of SMARTS reactions to use for Mutation. \
Only provide if using the Custom option for rxn_library.",
)
PARSER.add_argument(
"--function_group_library",
type=str,
default="",
help="This PATH for a dictionary of functional groups to be used for Mutation. \
Only provide if using the Custom option for rxn_library.",
)
PARSER.add_argument(
"--complementary_mol_directory",
type=str,
default="",
help="This PATH to the directory containing all the molecules being used \
to react with. The directory should contain .smi files contain SMILES of \
molecules containing the functional group represented by that file. Each file \
should be named with the same title as the functional groups described in \
rxn_library_file & function_group_library +.smi \
All Functional groups specified function_group_library must have its \
own .smi file. We recommend you filter these dictionaries prior to Autogrow \
for the Drug-likeliness and size filters you will Run Autogrow with.",
)
# processors and multithread mode
PARSER.add_argument(
"--number_of_processors",
"-p",
type=int,
metavar="N",
default=1,
help="Number of processors to use for parallel calculations. Set to -1 for all available CPUs.",
)
PARSER.add_argument(
"--multithread_mode",
default="multithreading",
choices=["mpi", "multithreading", "serial"],
help="Determine what style \
multithreading: mpi, multithreading, or serial. serial will override \
number_of_processors and force it to be on a single processor.",
)
# Genetic Algorithm Options
PARSER.add_argument(
"--selector_choice",
choices=["Roulette_Selector", "Rank_Selector", "Tournament_Selector"],
default="Roulette_Selector",
help="This determines whether the fitness criteria are chosen by a Weighted Roulette, \
Ranked, or Tournament style Selector. The Rank option is a non-redundant selector.\
Roulette and Tournament chose without replacement and are stoichastic options. \
Warning do not use Rank_Selector for small runs as there is potential that \
the number of desired ligands exceed the number of ligands to chose from.",
)
PARSER.add_argument(
"--tourn_size",
type=float,
default=0.1,
help="If using the Tournament_Selector this determines the size of each \
tournament. The number of ligands used for each tournament will the \
tourn_size * the number of considered ligands.",
)
# Seeding next gen and diversity
PARSER.add_argument(
"--top_mols_to_seed_next_generation_first_generation",
type=int,
help="Number of mols that seed next generation, for the first generation.\
Should be less than number_of_crossovers_first_generation + number_of_mutations_first_generation\
If not defined it will default to top_mols_to_seed_next_generation",
)
PARSER.add_argument(
"--top_mols_to_seed_next_generation",
type=int,
default=10,
help="Number of mols that seed next generation, for all generations after the first.\
Should be less than number_of_crossovers_first_generation \
+ number_of_mutations_first_generation",
)
PARSER.add_argument(
"--diversity_mols_to_seed_first_generation",
type=int,
default=10,
help="Should be less than number_of_crossovers_first_generation \
+ number_of_mutations_first_generation",
)
PARSER.add_argument(
"--diversity_seed_depreciation_per_gen",
type=int,
default=2,
help="Each gen diversity_mols_to_seed_first_generation will decrease this amount",
)
# Populations settings
PARSER.add_argument(
"--num_generations",
type=int,
default=10,
help="The number of generations to be created.",
)
PARSER.add_argument(
"--number_of_crossovers_first_generation",
type=int,
help="The number of ligands which will be created via crossovers in the \
first generation. If not defined it will default to number_of_crossovers",
)
PARSER.add_argument(
"--number_of_mutants_first_generation",
type=int,
help="The number of ligands which will be created via mutation in \
the first generation. If not defined it will default to number_of_mutants",
)
PARSER.add_argument(
"--number_elitism_advance_from_previous_gen_first_generation",
type=int,
help="The number of ligands chosen for elitism for the first generation \
These will advance from the previous generation directly into the next \
generation. This is purely advancing based on Docking/Rescore fitness. \
This does not select for diversity. If not defined it will default to \
number_elitism_advance_from_previous_gen",
)
PARSER.add_argument(
"--number_of_crossovers",
type=int,
default=10,
help="The number of ligands which will be created via crossover in each \
generation besides the first",
)
PARSER.add_argument(
"--number_of_mutants",
type=int,
default=10,
help="The number of ligands which will be created via mutation in each \
generation besides the first.",
)
PARSER.add_argument(
"--number_elitism_advance_from_previous_gen",
type=int,
default=10,
help="The number of ligands chosen for elitism. These will advance from \
the previous generation directly into the next generation. \
This is purely advancing based on Docking/Rescore \
fitness. This does not select for diversity.",
)
PARSER.add_argument(
"--redock_elite_from_previous_gen",
choices=[True, False, "True", "False", "true", "false"],
default=False,
help="If True than ligands chosen via Elitism (ie advanced from last generation) \
will be passed through Gypsum and docked again. This provides a better exploration of conformer space \
but also requires more computation time. If False, advancing ligands are simply carried forward by \
copying the PDBQT files.",
)
####### FILTER VARIABLES
PARSER.add_argument(
"--LipinskiStrictFilter",
action="store_true",
default=False,
help="Lipinski filters for orally available drugs following Lipinski rule of fives. \
Filters by molecular weight, logP and number of hydrogen bond donors and acceptors. \
Strict implementation means a ligand must pass all requirements.",
)
PARSER.add_argument(
"--LipinskiLenientFilter",
action="store_true",
default=False,
help="Lipinski filters for orally available drugs following Lipinski rule of fives. \
Filters by molecular weight, logP and number of hydrogen bond donors and acceptors. \
Lenient implementation means a ligand may fail all but one requirement and still passes.",
)
PARSER.add_argument(
"--GhoseFilter",
action="store_true",
default=False,
help="Ghose filters for drug-likeliness; filters by molecular weight,\
logP and number of atoms.",
)
PARSER.add_argument(
"--GhoseModifiedFilter",
action="store_true",
default=False,
help="Ghose filters for drug-likeliness; filters by molecular weight,\
logP and number of atoms. This is the same as the GhoseFilter, but \
the upper-bound of the molecular weight restrict is loosened from \
480Da to 500Da. This is intended to be run with Lipinski Filter and \
to match AutoGrow 3's Ghose Filter.",
)
PARSER.add_argument(
"--MozziconacciFilter",
action="store_true",
default=False,
help="Mozziconacci filters for drug-likeliness; filters by the number of \
rotatable bonds, rings, oxygens, and halogens.",
)
PARSER.add_argument(
"--VandeWaterbeemdFilter",
action="store_true",
default=False,
help="VandeWaterbeemd filters for drug likely to be blood brain barrier permeable. \
Filters by the number of molecular weight and Polar Sureface Area (PSA).",
)
PARSER.add_argument(
"--PAINSFilter",
action="store_true",
default=False,
help="PAINS filters against Pan Assay Interference Compounds using \
substructure a search.",
)
PARSER.add_argument(
"--NIHFilter",
action="store_true",
default=False,
help="NIH filters against molecules with undersirable functional groups \
using substructure a search.",
)
PARSER.add_argument(
"--BRENKFilter",
action="store_true",
default=False,
help="BRENK filter for lead-likeliness, by matching common false positive \
molecules to the current mol.",
)
PARSER.add_argument(
"--No_Filters",
action="store_true",
default=False,
help="No filters will be applied to compounds.",
)
PARSER.add_argument(
"--alternative_filter",
action="append",
help="If you want to add Custom filters to the filter child classes \
Must be a list of lists \
[[name_filter1, Path/to/name_filter1.py],[name_filter2, Path/to/name_filter2.py]]",
)
# dependency variables
# DOCUMENT THE file conversion for docking inputs
PARSER.add_argument(
"--conversion_choice",
choices=["MGLToolsConversion", "ObabelConversion", "Custom"],
default="MGLToolsConversion",
help="Determines how .pdb files will be converted \
to the final format for docking. For Autodock Vina and QuickVina style docking software, \
files must be in .pdbqt format. MGLToolsConversion: uses MGLTools and is the \
recommended converter. MGLTools conversion is required for NNScore1/2 rescoring. \
ObabelConversion: uses commandline obabel. Easier to install but Vina docking has \
been optimized with MGLTools conversion.",
)
PARSER.add_argument(
"--custom_conversion_script",
metavar="custom_conversion_script",
default="",
help="The path to a python script for which is used to convert \
ligands. This is required for custom conversion_choice choices. \
Must be a list of strings \
[name_custom_conversion_class, Path/to/name_custom_conversion_class.py]",
)
PARSER.add_argument(
"--mgltools_directory",
metavar="mgltools_directory",
help="Required if using MGLTools conversion option \
(conversion_choice=MGLToolsConversion) \
Path may look like: /home/user/MGLTools-1.5.6/",
)
PARSER.add_argument(
"--mgl_python",
metavar="mgl_python",
required=False,
help="/home/user/MGLTools-1.5.4/bin/pythonsh",
)
PARSER.add_argument(
"--prepare_ligand4.py",
metavar="prepare_ligand4.py",
required=False,
help="/home/user/MGLTools-1.5.4/MGLToolsPckgs/AutoDockTools/Utilities24/prepare_ligand4.py",
)
PARSER.add_argument(
"--prepare_receptor4.py",
metavar="prepare_receptor4.py",
required=False,
help="/home/userMGLTools-1.5.4/MGLToolsPckgs/AutoDockTools/Utilities24/prepare_receptor4.py",
)
PARSER.add_argument(
"--obabel_path",
help="required if using obabel conversion \
option (conversion_choice=ObabelConversion).\
Path may look like PATH/envs/py37/bin/obabel; \
may be found on Linux by running: which obabel",
)
###################################
######### docking #################
###################################
PARSER.add_argument(
"--dock_choice",
metavar="dock_choice",
default="QuickVina2Docking",
choices=["VinaDocking", "QuickVina2Docking", "Custom"],
help="dock_choice assigns which docking software module to use.",
)
PARSER.add_argument(
"--docking_executable",
metavar="docking_executable",
default=None,
help="path to the docking_executable",
)
PARSER.add_argument(
"--docking_exhaustiveness",
metavar="docking_exhaustiveness",
default=None,
help="exhaustiveness of the global search (roughly proportional to time. \
see docking software for settings. Unless specified Autogrow uses the \
docking softwares default setting. For AutoDock Vina 1.1.2 that is 8",
)
PARSER.add_argument(
"--docking_num_modes",
metavar="docking_num_modes",
default=None,
help=" maximum number of binding modes to generate in docking. \
See docking software for settings. Unless specified Autogrow uses the \
docking softwares default setting. For AutoDock Vina 1.1.2 that is 9",
)
PARSER.add_argument(
"--docking_timeout_limit",
type=float,
default=120,
help="The maximum amount of time allowed to dock a single ligand into a \
pocket in seconds. Many factors influence the time required to dock, such as: \
processor speed, the docking software, rotatable bonds, exhaustiveness docking,\
and number of docking modes... \
The default docking_timeout_limit is 120 seconds, which is excess for most \
docking events using QuickVina2Docking under default settings. If run with \
more exhaustive settings or with highly flexible ligands, consider increasing \
docking_timeout_limit to accommodate. Default docking_timeout_limit is 120 seconds",
)
PARSER.add_argument(
"--custom_docking_script",
metavar="custom_docking_script",
default="",
help="The name and path to a python script for which is used to \
dock ligands. This is required for Custom docking choices Must be a list of \
strings [name_custom_conversion_class, Path/to/name_custom_conversion_class.py]",
)
# scoring
PARSER.add_argument(
"--scoring_choice",
metavar="scoring_choice",
choices=["VINA", "NN1", "NN2", "Custom"],
default="VINA",
help="The scoring_choice to use to assess the ligands docking fitness. \
Default is using Vina/QuickVina2 ligand affinity while NN1/NN2 use a Neural Network \
to assess the docking pose. Custom requires providing a file path for a Custom \
scoring function. If Custom scoring function, confirm it selects properly, \
Autogrow is largely set to select for a more negative score.",
)
PARSER.add_argument(
"--rescore_lig_efficiency",
action="store_true",
default=False,
help="This will divide the final scoring_choice output by the number of \
non-Hydrogen atoms in the ligand. This adjusted ligand efficiency score will \
override the scoring_choice value. This is compatible with all scoring_choice options.",
)
PARSER.add_argument(
"--custom_scoring_script",
metavar="custom_scoring_script",
type=str,
default="",
help="The path to a python script for which is used to \
assess the ligands docking fitness. Autogrow is largely set to select for a most \
negative scores (ie binding affinity the more negative is best). Must be a list of \
strings [name_custom_conversion_class, Path/to/name_custom_conversion_class.py]",
)
# gypsum # max variance is the number of conformers made per ligand
PARSER.add_argument(
"--max_variants_per_compound",
type=int,
default=3,
help="number of conformers made per ligand. \
See Gypsum-DL publication for details",
)
PARSER.add_argument(
"--gypsum_thoroughness",
"-t",
type=int,
default = 3,
help="How widely Gypsum-DL will search for \
low-energy conformers. Larger values increase \
run times but can produce better results. \
See Gypsum-DL publication for details",
)
PARSER.add_argument(
"--min_ph",
metavar="MIN",
type=float,
default=6.4,
help="Minimum pH to consider.See Gypsum-DL \
and Dimorphite-D publication for details.",
)
PARSER.add_argument(
"--max_ph",
metavar="MAX",
type=float,
default=8.4,
help="Maximum pH to consider.See Gypsum-DL \
and Dimorphite-D publication for details.",
)
PARSER.add_argument(
"--pka_precision",
metavar="D",
type=float,
default=1.0,
help="Size of pH substructure ranges. See Dimorphite-DL \
publication for details.",
)
PARSER.add_argument(
"--gypsum_timeout_limit",
type=float,
default=15,
help="Maximum time gypsum is allowed to run for a given ligand in seconds. \
On average Gypsum-DL takes on several seconds to run for a given ligand, but \
factors such as mol size, rotatable bonds, processor speed, and gypsum \
settings (ie gypsum_thoroughness or max_variants_per_compound) will change \
how long it takes to run. If increasing gypsum settings it is best to increase \
the gypsum_timeout_limit. Default gypsum_timeout_limit is 15 seconds",
)
# Reduce files down. This compiles and compresses the files in the PDBs folder
# (contains docking outputs, pdb, pdbqt...). This reduces the data size and
# makes data transfer quicker, but requires running the
# file_concatenation_and_compression.py in the Utility script folder to
# separate these files out for readability.
PARSER.add_argument(
"--reduce_files_sizes",
choices=[True, False, "True", "False", "true", "false"],
default=True,
help="Run this combines all files in the PDBs folder into a \
single text file. Useful when data needs to be transferred.",
)
# Make a line plot of the simulation at the end of the run.
PARSER.add_argument(
"--generate_plot",
choices=[True, False, "True", "False", "true", "false"],
default=True,
help="Make a line plot of the simulation at the end of the run.",
)
# mpi mode pre-Run so there are python cache files without EOF Errors
PARSER.add_argument(
"--cache_prerun",
"-c",
action="store_true",
help="Run this before running gypsum in mpi-mode.",
)
args_dict = vars(PARSER.parse_args())
from autogrow.user_vars import multiprocess_handling, define_defaults, determine_bash_timeout_vs_gtimeout
# args_dict = define_defaults()
import numpy as np
from tqdm import tqdm
from collections import defaultdict
import os, json, pickle, time, sys, copy, random
INPUTS = copy.deepcopy(args_dict)
for k, v in args_dict.items():
if v is None:
del INPUTS[k]
if args_dict["cache_prerun"] is False:
# load the commandline parameters
from autogrow.user_vars import load_in_commandline_parameters
args_dict, printout = load_in_commandline_parameters(INPUTS)
args_dict = multiprocess_handling(args_dict)
timeout_option = determine_bash_timeout_vs_gtimeout()
if timeout_option in ["timeout", "gtimeout"]:
args_dict["timeout_vs_gtimeout"] = timeout_option
else:
raise Exception("Something is very wrong. This OS may not be supported by Autogrow or you may need to execute through Bash.")
vars = args_dict
topk = 10
############## canonical ##############
from rdkit import Chem
def canonicalize(smiles):
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
return Chem.MolToSmiles(mol, isomericSmiles=True)
else:
return None
##########################################################
### A. mutate
import autogrow.operators.mutation.smiles_click_chem.smiles_click_chem as SmileClickClass
rxn_library_variables = [vars["rxn_library"], vars["rxn_library_file"], vars["function_group_library"], vars["complementary_mol_directory"]] # Package user vars specifying Reaction library for mutation
new_mutation_smiles_list = [] # List of SMILES from mutation
a_smiles_click_chem_object = SmileClickClass.SmilesClickChem(rxn_library_variables, new_mutation_smiles_list, vars["filter_object_dict"])
##########################################################
##########################################################
### B. crossover
## crossover between 2 ligands: find common structure
import autogrow.operators.crossover.smiles_merge.smiles_merge as smiles_merge
import autogrow.operators.crossover.execute_crossover as execute_crossover
import autogrow.operators.filter.execute_filters as Filter
##########################################################
##########################################################
# C. smiles2docking
# - smiles2pdbqt: smiles -> sdf -> pdb -> pdbqt
# - docking pdbqt
########### smiles -> sdf -> pdb -> pdbqt -> pdbqt.vina
import autogrow.operators.convert_files.conversion_to_3d as conversion_to_3d
# conversion_to_3d.convert_smi_to_sdfs_with_gypsum
# conversion_to_3d.convert_sdf_to_pdbs
# convert_sdf_to_pdbs(vars, gen_folder_path, sdfs_folder_path)
# conversion_to_3d.convert_single_sdf_to_pdb
# convert_ligand_pdb_file_to_pdbqt #### in run_docking_common lig_convert_multithread
def smiles_to_sdfs(vars, gen_smiles_file, smile_file_directory):
# adapted from conversion_to_3d.convert_smi_to_sdfs_with_gypsum
max_variants_per_compound = vars["max_variants_per_compound"]
gypsum_thoroughness = vars["gypsum_thoroughness"]
min_ph = vars["min_ph"]
max_ph = vars["max_ph"]
pka_precision = vars["pka_precision"]
gypsum_timeout_limit = vars["gypsum_timeout_limit"]
# Make a new folder to put gypsum .smi's and json. Name folder gypsum_submission_files.
folder_path = "{}gypsum_submission_files{}".format(smile_file_directory, os.sep)
if os.path.exists(folder_path) is False:
os.makedirs(folder_path)
# Make Output for Gypsum folder (where .sdf's go)
gypsum_output_folder_path = "{}_SDF{}".format(smile_file_directory, os.sep)
if os.path.exists(gypsum_output_folder_path) is False:
os.makedirs(gypsum_output_folder_path)
# Make a folder to put the log files into within the 3D_SDFs folder
gypsum_log_path = "{}log{}".format(gypsum_output_folder_path, os.sep)
if os.path.exists(gypsum_log_path) is False:
os.makedirs(gypsum_log_path)
# Make All of the json files to submit to gypsum
list_of_gypsum_params = conversion_to_3d.make_smi_and_gyspum_params(
gen_smiles_file,
folder_path,
gypsum_output_folder_path,
max_variants_per_compound, gypsum_thoroughness,
min_ph, max_ph, pka_precision, )
# create a the job_inputs to run gypsum in multithread
job_input = tuple([(gypsum_log_path, gypsum_params, gypsum_timeout_limit) for gypsum_params in list_of_gypsum_params])
sys.stdout.flush()
failed_to_convert = vars["parallelizer"].run(job_input, conversion_to_3d.run_gypsum_multiprocessing)
sys.stdout.flush()
### fail: return smiles
### success: return None
lig_failed_to_convert = [x for x in failed_to_convert if x is not None]
lig_failed_to_convert = list(set(lig_failed_to_convert))
if len(lig_failed_to_convert) > 0:
print("The Following ligands Failed to convert in Gypsum")
print("Likely due to a Timeout")
print(lig_failed_to_convert)
sys.stdout.flush()
return gypsum_output_folder_path
from autogrow.docking.execute_docking import pick_run_conversion_class_dict, pick_docking_class_dict, lig_convert_multithread
def pdb_to_pdbqt(vars, pdb_dir):
### adapted from run_docking_common
dock_choice = vars["dock_choice"]
conversion_choice = vars["conversion_choice"]
receptor = vars["filename_of_receptor"]
# Use a temp vars dict so you don't put mpi multiprocess info through itself...
temp_vars = {}
for key in list(vars.keys()):
if key == "parallelizer":
continue
temp_vars[key] = vars[key]
file_conversion_class_object = pick_run_conversion_class_dict(conversion_choice)
file_conversion_class_object = file_conversion_class_object(temp_vars, receptor, test_boot=False)
dock_class = pick_docking_class_dict(dock_choice)
docking_object = dock_class(temp_vars, receptor, file_conversion_class_object, test_boot=False)
if vars["docking_executable"] is None:
docking_executable = docking_object.get_docking_executable_file(temp_vars)
vars["docking_executable"] = docking_executable
##### vina or Qvina
# Find PDB's
pdbs_in_folder = docking_object.find_pdb_ligands(pdb_dir)
print(' pdb files:', pdbs_in_folder[:2], pdb_dir, len(pdbs_in_folder))
job_input_convert_lig = tuple([(docking_object, pdb) for pdb in pdbs_in_folder])
# print(" Convert Ligand from PDB to PDBQT format")
smiles_names_failed_to_convert = vars["parallelizer"].run(job_input_convert_lig, lig_convert_multithread)
pdbqts_in_folder = docking_object.find_converted_ligands(pdb_dir)
print(' pdbqt file: ', len(pdbqts_in_folder), pdbqts_in_folder[:2])
return docking_object
from autogrow.docking.execute_docking import run_dock_multithread, run_docking_common
import autogrow.docking.scoring.execute_scoring_mol as Scoring
import autogrow.docking.ranking.ranking_mol as Ranking
def docking_pdbqt(vars, docking_object, pdbqt_folder, full_smiles_file):
pdbqts_in_folder = docking_object.find_converted_ligands(pdbqt_folder)
job_input_dock_lig = tuple([tuple([docking_object, pdbqt]) for pdbqt in pdbqts_in_folder])
smiles_names_failed_to_dock = vars["parallelizer"].run(job_input_dock_lig, run_dock_multithread)
### main docking, (including delete failed docking file)
deleted_smiles_names_list_dock = [x for x in smiles_names_failed_to_dock if x is not None]
deleted_smiles_names_list_dock = list(set(deleted_smiles_names_list_dock))
print("THE FOLLOWING LIGANDS WHICH FAILED TO DOCK:", deleted_smiles_names_list_dock)
# print("#################### \n Begin Ranking and Saving results")
# folder_with_pdbqts = current_generation_dir + "PDBs" + os.sep
# Run any compatible Scoring Function
print(full_smiles_file, pdbqt_folder)
smiles_list = Scoring.run_scoring_common(vars, full_smiles_file, pdbqt_folder)
print('---------', smiles_list[:3], 'smiles_list[:3] --------------')
# Output format of the .smi file will be: SMILES Full_lig_name
# shorthandname ...AnyCustominfo... Fitness_metric diversity
# Normally the docking score is the fitness metric but if we use a
# Custom metric than dock score gets moved to index -3 and the new
# fitness metric gets -2
# sort list by the affinity of each sublist (which is the last index of sublist)
smiles_list.sort(key=lambda x: float(x[-1]), reverse=False)
# ["[N-]=[NH+]/N=C/c1[nH+]nc(-c2cccc3ccccc23)o1", "naphthalene_35", "naphthalene_35", "naphthalene_35__3", -9.2]
# score the diversity of each ligand compared to the rest of the ligands in the group this adds on a float in the last column for the
# sum of pairwise comparisons the lower the diversity score the more unique a molecule is from the other mols in the same generation
smiles_list = Ranking.score_and_append_diversity_scores(smiles_list)
# ["[N-]=[NH+]/N=C/c1[nH+]nc(-c2cccc3ccccc23)o1", "naphthalene_35", "naphthalene_35", "naphthalene_35__3", -9.2, 40.14 (diversity)]
pdbqts_in_folder = [pdbqt + '.vina' for pdbqt in pdbqts_in_folder if os.path.exists(pdbqt + '.vina')]
print('pdbqts [:4]', pdbqts_in_folder[:3], len(pdbqts_in_folder))
id2pdbqt = defaultdict(lambda:[])
for pdbqt in pdbqts_in_folder:
smiles_id = pdbqt.split('/')[-1].split('__')[0]
id2pdbqt[smiles_id].append(pdbqt)
for idx,ss in enumerate(smiles_list):
smiles_id = ss[1]
smiles_list[idx].append(id2pdbqt[smiles_id])
# ["[N-]=[NH+]/N=C/c1[nH+]nc(-c2cccc3ccccc23)o1", "naphthalene_35", "naphthalene_35", "naphthalene_35__3",
# '-9.2', 40.14 (diversity), ['results_xxxx/xxxx__1.pdbqt', 'results_xxxx/xxxxx__2.pdbqt']]
return smiles_list
def docking(smiles_folder, smiles_file, args_dict):
sdfs_folder_path = smiles_folder.strip('/') + '_SDF/'
pdb_dir = smiles_folder.strip('/') + '_PDB/'
smiles_to_sdfs(args_dict, gen_smiles_file=os.path.join(smiles_folder,smiles_file), smile_file_directory=smiles_folder)
conversion_to_3d.convert_sdf_to_pdbs(args_dict, gen_folder_path=smiles_folder, sdfs_folder_path=sdfs_folder_path)
docking_object = pdb_to_pdbqt(vars = args_dict, pdb_dir = pdb_dir)
smiles_list = docking_pdbqt(args_dict, docking_object, pdb_dir, os.path.join(smiles_folder, smiles_file))
return smiles_list
##########################################################
# ['N=[N+]=[N+]=C(Cc1ccc2ccccc2c1)[N+](=O)[O-]', 'N=[N+]=Nc1c(N=[N+]=N)c(N=[N+]=N)c2ccccc2c1O', ...]
############# receptor ##############
receptor_info_list = [
('4r6e', './pdb/4r6e.pdb', -70.76, 21.82, 28.33, 15.0, 15.0, 15.0),
('3pbl', './pdb/3pbl.pdb', 9, 22.5, 26, 15, 15, 15),
('1iep', './pdb/1iep.pdb', 15.6138918, 53.38013513, 15.454837, 15, 15, 15), ]
# ('2rgp', './pdb/2rgp.pdb', 16.29212, 34.870818, 92.0353, 15, 15, 15),
# ('3eml', './pdb/3eml.pdb', -9.06363, -7.1446, 55.86259999, 15, 15, 15),
# ('3ny8', './pdb/3ny8.pdb', 2.2488, 4.68495, 51.39820000000001, 15, 15, 15),
# ('4rlu', './pdb/4rlu.pdb', -0.73599, 22.75547, -31.23689, 15, 15, 15),
# ('4unn', './pdb/4unn.pdb', 5.684346153, 18.1917, -7.3715, 15, 15, 15),
# ('5mo4', './pdb/5mo4.pdb', -44.901, 20.490354, 8.48335, 15, 15, 15),
# ('7l11', './pdb/7l11.pdb', -21.81481, -4.21606, -27.98378, 15, 15, 15), ]
def update_receptor_info(vars, receptor_info):
name_of_receptor, filename_of_receptor, center_x, center_y, center_z, size_x, size_y, size_z = receptor_info
vars['name_of_receptor'] = name_of_receptor
vars['filename_of_receptor'] = filename_of_receptor
vars['center_x'] = center_x
vars['center_y'] = center_y
vars['center_z'] = center_z
vars['size_x'] = size_x
vars['size_y'] = size_y
vars['size_z'] = size_z
return vars
smiles2info = defaultdict(lambda: dict())
id2smiles = dict()
def random_generate_id(id2smiles):
while True:
smiles_id = str(random.randint(1000000, 9999999))
if smiles_id not in id2smiles:
return smiles_id
##########################################################
################ initialize population ################
source_compound_file = args_dict['source_compound_file']
smiles_file = 'smiles.txt'
with open(source_compound_file, 'r') as fin:
smiles_list = fin.readlines()
initial_smiles_list = [smiles.split()[0] for smiles in smiles_list]
####### docking initial smiles list
for receptor_info in receptor_info_list:
vars = update_receptor_info(vars, receptor_info)
name_of_receptor = receptor_info[0]
print("---------- 0.1. save smiles ----------") ###### new_smiles_set -> smiles_file
meta_result_folder = './results_' + name_of_receptor + '_'
results_folder = meta_result_folder + "000" ### 'results_4r6e_000'
new_smiles_list = initial_smiles_list[:30] #### debug
if not os.path.exists(results_folder):
os.makedirs(results_folder)
full_smiles_file = os.path.join(results_folder, smiles_file)
with open(full_smiles_file, 'w') as fout:
for smiles in new_smiles_list:
smiles_id = random_generate_id(id2smiles)
fout.write(smiles + '\t' + smiles_id + '\n')
id2smiles[smiles_id] = smiles
print("---------- 0.2. docking ----------")
smiles_list = docking(smiles_folder = results_folder, smiles_file = smiles_file, args_dict = vars)
# ["[N-]=[NH+]/N=C/c1[nH+]nc(-c2cccc3ccccc23)o1", "naphthalene_35", "naphthalene_35", "naphthalene_35__3",
# '-9.2', 40.14 (diversity), ['results_xxxx/xxxx__1.pdbqt', 'results_xxxx/xxxxx__2.pdbqt']]
for info in smiles_list:
smiles, smiles_id, binding_score, pdbqt_list = info[0], info[1], float(info[-3]), info[-1]
smiles2info[name_of_receptor][smiles] = [smiles_id, binding_score, pdbqt_list]
print('------ 0.3. top-K smiles for next generation -------')
new_smiles_list = [(smiles, smiles2info[name_of_receptor][smiles][1], smiles2info[name_of_receptor][smiles][2]) \
for smiles in new_smiles_list if smiles in smiles2info[name_of_receptor]]
new_smiles_list.sort(key=lambda x:x[1])
new_smiles_list = new_smiles_list[:topk]
smiles_info_list = [(smiles,pdbqt_list) for smiles,binding_score,pdbqt_list in new_smiles_list]
smiles2info[name_of_receptor]['smiles_info_list'] = copy.deepcopy(smiles_info_list)
##########################################################
################# model ################
import torch
from model import Ligand2D, Ligand2D_product, ENN, featurize_receptor_and_ligand
# featurize_receptor_and_ligand(pdbfile, centers, pocket_size, pdbqt_file,)
# pdbtofeature(pdbfile, centers, pocket_size) & pdbqtvina2feature(pdbqt_file)
# crossover_policy_net_1 = Ligand2D() ##### TODO pocket & center
# crossover_policy_net_2 = Ligand2D_product()
# crossover_policy_net_1 = ENN()
# crossover_policy_net_2 = ENN()
crossover_policy_net_1 = torch.load('save_model/crossover_policy_net_1.ckpt')
crossover_policy_net_2 = torch.load('save_model/crossover_policy_net_2.ckpt')
crossover_budget = 20
crossover_optimizer = torch.optim.Adam(list(crossover_policy_net_1.parameters()) + list(crossover_policy_net_2.parameters()), lr=1e-3)
# mutation_policy_net_1 = ENN()
# mutation_policy_net_2 = Ligand2D_product()
mutation_policy_net_1 = torch.load('save_model/mutation_policy_net_1.ckpt')
mutation_policy_net_2 = torch.load('save_model/mutation_policy_net_2.ckpt')
mutation_budget = 20
mutation_optimizer = torch.optim.Adam(list(mutation_policy_net_1.parameters()) + list(mutation_policy_net_2.parameters()), lr=1e-3)
################# model ################
crossover_train_data = defaultdict(lambda: dict())
mutation_train_data = defaultdict(lambda: dict())
canonicalize = lambda x:x
crossover_done_set = defaultdict(lambda: set())
mutation_done_set = defaultdict(lambda: set())
########################## main loop ############################
for num_gen in tqdm(range(args_dict['num_generations'])):
for receptor_info in receptor_info_list:
##### input: smiles_list (including pdbqtvina, from previous-generation) & receptor
vars = update_receptor_info(vars, receptor_info)
name_of_receptor = vars['name_of_receptor']
smiles_info_list = copy.deepcopy(smiles2info[name_of_receptor]['smiles_info_list']) ###### [(smiles_1, pdbqtvina_list_1), (smiles_2, pdbqtvina_list_2), ...]
print('===== 1. beginning of the generation: smiles_info_list =====', len(smiles_info_list), smiles_info_list[:5], )
for info in smiles_info_list:
pdbqtvina = info[1][0]
assert os.path.exists(pdbqtvina)
smiles_list = [smiles for smiles, pdbqtvina_list in smiles_info_list]
smiles2pdbqtvina_local = {smiles:pdbqtvina_list[0] for smiles,pdbqtvina_list in smiles_info_list}
if len(smiles_list) <=3:
continue
new_smiles_set = set()
print("---------- 2. RGA: crossover ----------")
pdbqtvina_list = [smiles2pdbqtvina_local[smiles] for smiles in smiles_list]
print('length of ligand:', len(pdbqtvina_list))
if num_gen > -1:
##### evaluate probability distribution in RGA
_, crossover_sample_probability_list = crossover_policy_net_1.forward_ligand_list(
name_of_receptor = vars['name_of_receptor'],
pdbqtvina_list = pdbqtvina_list)
else:
crossover_sample_probability_list = [1.0/len(pdbqtvina_list) for i in pdbqtvina_list]
sampled_idx = random.choices(list(range(len(crossover_sample_probability_list))),
weights = crossover_sample_probability_list,
k = crossover_budget)
####### RGA outer loop, first ligand ########
for idx in tqdm(sampled_idx):
selected_smiles_1 = smiles_list[idx]
mol = execute_crossover.convert_mol_from_smiles(selected_smiles_1)
holdout_smiles_list = [smiles for smiles in smiles_list if smiles!=selected_smiles_1]
if holdout_smiles_list == []:
continue
holdout_pdbqtvina_list = [smiles2pdbqtvina_local[smiles] for smiles in holdout_smiles_list]
if num_gen > -1:
##### evaluate probability distribution in RGA
_, crossover_sample_probability_list_2 = crossover_policy_net_2.forward_ligand_list(