-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestart_Genetic_Algorithm.py
748 lines (605 loc) · 32.3 KB
/
Restart_Genetic_Algorithm.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
"""
File to restart genetic algorithm
"""
################# IMPORTS ###################
import Genetic_Algorithm_Functions as GAF
from rdkit import Chem
from rdkit.Chem import Draw
from random import sample
from rdkit.Chem import AllChem
from rdkit.Chem.Draw import MolDrawing, DrawingOptions
from random import choice as rnd
from os.path import join
from time import sleep
import sys
from rdkit.Chem.Draw import rdMolDraw2D
from MoleculeDifferenceViewer import view_difference
from copy import deepcopy
import ast
from operator import itemgetter
import os
import glob
import pandas as pd
import random
import math
import numpy as np
import shutil
import traceback
from gt4sd.properties import PropertyPredictorRegistry
import sys
######### Genetic Algorithm Parameters ############
fragments = ['CCCC', 'CCCCC', 'C(CC)CCC', 'CCC(CCC)CC', 'CCCC(C)CC', 'CCCCCCCCC', 'CCCCCCCC', 'CCCCCC', 'C(CCCCC)C']
Mutations = ['AddAtom', 'ReplaceAtom', 'ReplaceBond', 'RemoveAtom', 'AddFragment', 'RemoveFragment']
CopyCommand = 'cp'
Silent = True # Edit outputs to only print if this flag is False
NumElite = 25
IDcounter = 1
FirstGenerationAttempts = 0
MasterMoleculeList = [] #Keeping track of all generated molecules
FirstGenSimList = []
MaxNumHeavyAtoms = 45
MinNumHeavyAtoms = 5
MutationRate = 0.4
showdiff = False # Whether or not to display illustration of each mutation
GenerationSize = 50
LOPLS = False # Whether or not to use OPLS or LOPLS, False uses OPLS
MaxGenerations = 100
MaxMutationAttempts = 200
Fails = 0
NumRuns = 5
NumAtoms = 10000
Agent = 'Agent1'
STARTINGDIR = deepcopy(os.getcwd())
PYTHONPATH = 'python3'
generation = 3
### BOND TYPES
BondTypes = [Chem.BondType.SINGLE, Chem.BondType.DOUBLE]
### ATOM NUMBERS
Atoms = ['C', 'O']
AtomMolObjects = [Chem.MolFromSmiles(x) for x in Atoms]
AtomicNumbers = []
# Getting Atomic Numbers for Addable Atoms
for Object in AtomMolObjects:
for atom in Object.GetAtoms():
AtomicNumbers.append(atom.GetAtomicNum())
### Wait until array jobs have finished
MoveOn = False
while MoveOn == False:
GAF.runcmd(f'qstat > sims.txt')
sims = []
try:
with open(join(STARTINGDIR, 'sims.txt'), 'r') as file:
next(file) #Avoiding the first two lines
next(file)
filelist = file.readlines()
for sim in filelist:
if Agent in sim:
sims.append(sim)
print(sim)
except:
pass
# Check if array jobs have finished
if len(sims) != 0:
print('Waiting for 10 mins')
sleep(600)
else:
MoveOn = True
### Reformat directories so that properties can be calculated
GenDirectory = join(STARTINGDIR, 'Molecules', f'Generation_{generation}')
# List of simulation directories
directories_with_generation = GAF.list_generation_directories(GenDirectory, 'Run')
Num = 1
for RunDir in directories_with_generation:
try:
os.chdir(join(GenDirectory, RunDir))
PDBNamePath = GAF.find_files_with_extension(os.getcwd(), '.pdb')[0]
PDBName = GAF.extract_molecule_name(PDBNamePath)
source_directory = join(GenDirectory, RunDir)
destination_directory = join(GenDirectory, PDBName, RunDir)
GAF.move_directory(source_directory, destination_directory)
Num +=1
except Exception as E:
print(E)
traceback.print_exc()
pass
GAF.runcmd('rm -r Run*')
# Get PDBs from directories
MOLSMILESList = []
MOLIDList = GAF.list_generation_directories(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'), 'Molecule')
print(MOLIDList)
for MoleculeDir in MOLIDList:
Path = join(STARTINGDIR, 'Molecules', f'Generation_{generation}', MoleculeDir, f'{MoleculeDir}.pdb')
print(Path)
MolObject = Chem.MolFromPDBFile(Path)
SMILES = Chem.MolToSmiles(MolObject)
MOLSMILESList.append(SMILES)
# Master Dataframe where molecules from all generations will be stored
MoleculeDatabase = pd.DataFrame(columns=['SMILES', 'MolObject', 'MutationList', 'HeavyAtoms', 'ID', 'Charge', 'MolMass', 'Predecessor', 'Score', 'Density100C', 'DViscosity40C',
'DViscosity100C', 'KViscosity40C', 'KViscosity100C', 'KVI', 'DVI', 'Toxicity', 'SCScore', 'Density40C', 'SimilarityScore'])
# Generation Dataframe to store molecules from each generation
GenerationDatabase = pd.DataFrame(columns=['SMILES', 'MolObject', 'MutationList', 'HeavyAtoms', 'ID', 'Charge', 'MolMass', 'Predecessor', 'Score', 'Density100C', 'DViscosity40C',
'DViscosity100C', 'KViscosity40C', 'KViscosity100C', 'KVI', 'DVI', 'Toxicity', 'SCScore', 'Density40C', 'SimilarityScore'])
# Here is where we will get the various values generated from the MD simulations
GenSimList = list(zip(MOLIDList, MOLSMILESList))
print(GenSimList)
for Molecule, MOLSMILES in GenSimList:
try:
# Create a function to wait until all simulations from this generation are finished
os.chdir(join(STARTINGDIR, 'Molecules', f'Generation_{generation}', Molecule))
CWD = os.getcwd()
print('Getting Similarity Scores')
### Similarity Scores
Scores = GAF.TanimotoSimilarity(MOLSMILES, MOLSMILESList)
AvScore = 1 - (sum(Scores) / GenerationSize) # The higher the score, the less similar the molecule is to others
print('Getting SCScore')
### SCScore
SCScore = GAF.SCScore(MOLSMILES)
SCScoreNorm = SCScore/5
### Toxicity
ToxNorm = GAF.Toxicity(MOLSMILES)
print('Getting Density')
DirRuns = GAF.list_generation_directories(CWD, 'Run')
ExampleRun = DirRuns[0]
for run in DirRuns:
try:
DensityFile40 = f'{CWD}/{ExampleRun}/eqmDensity_{Molecule}_T313KP1atm.out'
DensityFile100 = f'{CWD}/{ExampleRun}/eqmDensity_{Molecule}_T373KP1atm.out'
except:
continue
### Viscosity
DVisc40 = GAF.GetVisc(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'), Molecule, 313)
DVisc100 = GAF.GetVisc(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'), Molecule, 373)
Dens40 = GAF.GetDens(DensityFile40)
Dens100 = GAF.GetDens(DensityFile100)
## Viscosity Index
KVI = GAF.GetKVI(DVisc40, DVisc100, Dens40, Dens100, STARTINGDIR)
DVI = GAF.GetDVI(DVisc40, DVisc100)
#Update Molecule Database
IDNumber = int(Molecule.split('_')[-1])
MoleculeDatabase.at[IDNumber - 1, 'SMILES'] = MOLSMILES
MoleculeDatabase.at[IDNumber - 1, 'ID'] = Molecule
MoleculeDatabase.at[IDNumber - 1, 'Density100C'] = Dens100
MoleculeDatabase.at[IDNumber - 1, 'Density40C'] = Dens40
MoleculeDatabase.at[IDNumber - 1, 'DViscosity40C'] = DVisc40
MoleculeDatabase.at[IDNumber - 1, 'DViscosity100C'] = DVisc100
MoleculeDatabase.at[IDNumber - 1, 'KViscosity40C'] = GAF.GetKVisc(DVisc=DVisc40, Dens=Dens40)
MoleculeDatabase.at[IDNumber - 1, 'KViscosity100C'] = GAF.GetKVisc(DVisc=DVisc100, Dens=Dens100)
MoleculeDatabase.at[IDNumber - 1, 'KVI'] = KVI
MoleculeDatabase.at[IDNumber - 1, 'DVI'] = DVI
MoleculeDatabase.at[IDNumber - 1, 'Toxicity'] = ToxNorm
MoleculeDatabase.at[IDNumber - 1, 'SCScore'] = SCScoreNorm
MoleculeDatabase.at[IDNumber - 1, 'SimilarityScore'] = SCScoreNorm
#Update Generation Database
GenerationDatabase.at[IDNumber - 1, 'SMILES'] = MOLSMILES
GenerationDatabase.at[IDNumber - 1, 'ID'] = Molecule
GenerationDatabase.at[IDNumber - 1, 'Density100C'] = Dens100
GenerationDatabase.at[IDNumber - 1, 'Density40C'] = Dens40
GenerationDatabase.at[IDNumber - 1, 'DViscosity40C'] = DVisc40
GenerationDatabase.at[IDNumber - 1, 'DViscosity100C'] = DVisc100
GenerationDatabase.at[IDNumber - 1, 'KViscosity40C'] = GAF.GetKVisc(DVisc=DVisc40, Dens=Dens40)
GenerationDatabase.at[IDNumber - 1, 'KViscosity100C'] = GAF.GetKVisc(DVisc=DVisc100, Dens=Dens100)
GenerationDatabase.at[IDNumber - 1, 'KVI'] = KVI
GenerationDatabase.at[IDNumber - 1, 'DVI'] = DVI
GenerationDatabase.at[IDNumber - 1, 'Toxicity'] = ToxNorm
GenerationDatabase.at[IDNumber - 1, 'SCScore'] = SCScoreNorm
GenerationDatabase.at[IDNumber - 1, 'SimilarityScore'] = SCScoreNorm
except Exception as E:
print(E)
traceback.print_exc()
pass
#### Generate Score
ViscScores = MoleculeDatabase['DViscosity40C'].tolist()
SCScores = MoleculeDatabase['SCScore'].tolist()
DVIScores = MoleculeDatabase['DVI'].tolist()
KVIScores = MoleculeDatabase['KVI'].tolist()
ToxicityScores = MoleculeDatabase['Toxicity'].tolist()
SimilarityScores = MoleculeDatabase['SimilarityScore'].tolist()
MoleculeNames = MoleculeDatabase['ID'].tolist()
ViscosityScore = list(zip(MoleculeNames, ViscScores))
MolecularComplexityScore = list(zip(MoleculeNames, SCScores))
DVIScore = list(zip(MoleculeNames, DVIScores))
ToxicityScore = list(zip(MoleculeNames, ToxicityScores))
ViscosityScore = [(x[0], 0) if math.isnan(x[1]) else x for x in ViscosityScore]
DVIScore = [(x[0], 0) if math.isnan(x[1]) else x for x in DVIScore]
# Apply the normalization function
Viscosity_normalized_molecule_scores = [(1-x[1]) for x in GAF.min_max_normalize(ViscosityScore)]
DVI_normalized_molecule_scores = [x[1] for x in GAF.min_max_normalize(DVIScore)]
MoleculeDatabase['ViscNormalisedScore'] = Viscosity_normalized_molecule_scores
MoleculeDatabase['DVINormalisedScore'] = DVI_normalized_molecule_scores
MoleculeDatabase['TotalScore'] = MoleculeDatabase['Toxicity'] + MoleculeDatabase['SCScore'] + MoleculeDatabase['DVINormalisedScore'] + MoleculeDatabase['ViscNormalisedScore']
MoleculeDatabase['NichedScore'] = MoleculeDatabase['TotalScore'] / MoleculeDatabase['SimilarityScore']
print(MoleculeDatabase)
#Make a pandas object with just the scores and the molecule ID
GenerationMolecules = pd.Series(MoleculeDatabase.NichedScore.values, index=MoleculeDatabase.ID).dropna()
print(GenerationMolecules)
GenerationMolecules = GenerationMolecules.to_dict()
print(GenerationMolecules)
# Sort dictiornary according to target score
ScoreSortedMolecules = sorted(GenerationMolecules.items(), key=lambda item:item[1], reverse=True)
#Convert tuple elements in sorted list back to lists
ScoreSortedMolecules = [list(x) for x in ScoreSortedMolecules]
print(ScoreSortedMolecules)
# Constructing entries for use in subsequent generation
for entry in ScoreSortedMolecules:
Key = int(entry[0].split('_')[-1]) - 1
entry.insert(1, MoleculeDatabase.loc[Key]['MolObject'])
entry.insert(2, MoleculeDatabase.loc[Key]['MutationList'])
entry.insert(3, MoleculeDatabase.loc[Key]['HeavyAtoms'])
entry.insert(4, MoleculeDatabase.loc[Key]['SMILES'])
try:
MoleculeDatabase.drop("Unnamed: 0", axis=1, inplace=True)
except:
pass
#Save the update Master database and generation database
MoleculeDatabase.to_csv(f'{STARTINGDIR}/MoleculeDatabase_Generation_{generation}.csv', index=False)
MoleculeDatabase.to_csv(f'{STARTINGDIR}/Generation_{generation}_Database.csv', index=False)
generation_Initial = int(generation)
generation_Initial +=1
print(len(ScoreSortedMolecules))
################################## Subsequent generations #################################################
for generation in range(generation_Initial, MaxGenerations + 1):
GenerationTotalAttempts = 0
GenSimList = []
IDcounter = 1
# Generation Dataframe to store molecules from each generation
GenerationDatabase = pd.DataFrame(columns=['SMILES', 'MolObject', 'MutationList', 'HeavyAtoms', 'ID', 'Charge', 'MolMass', 'Predecessor', 'Score', 'Density100C', 'DViscosity40C',
'DViscosity100C', 'KViscosity40C', 'KViscosity100C', 'KVI', 'DVI', 'Toxicity', 'SCScore', 'Density40C', 'SimilarityScore'])
MoleculeDatabase = pd.DataFrame(columns=['SMILES', 'MolObject', 'MutationList', 'HeavyAtoms', 'ID', 'Charge', 'MolMass', 'Predecessor', 'Score', 'Density100C', 'DViscosity40C',
'DViscosity100C', 'KViscosity40C', 'KViscosity100C', 'KVI', 'DVI', 'Toxicity', 'SCScore', 'Density40C', 'SimilarityScore'])
os.chdir(STARTINGDIR)
# Store x best performing molecules (x=NumElite in list for next generation, without mutating them)
GenerationMoleculeList = ScoreSortedMolecules[:NumElite]
os.chdir(join(os.getcwd(), 'Molecules'))
GAF.runcmd(f'mkdir Generation_{generation}')
os.chdir(STARTINGDIR)
for x in list(range(0, 100)): #Start by mutating best performing molecules from previous generation and work down
MutMol = None
attempts = 0
# Stop appending mutated molecules once generation reaches desired size
if len(GenerationMoleculeList) == GenerationSize:
break
# Attempt crossover/mutation on each molecule, not moving on until a valid mutation has been suggested
while MutMol == None:
attempts += 1
GenerationTotalAttempts += 1
# Limit number of attempts at mutation, if max attempts exceeded, break loop to attempt on next molecule
if attempts >= MaxMutationAttempts:
Fails += 1
break
# Get two parents using 3-way tournament selection
Parent1 = GAF.KTournament(ScoreSortedMolecules[:NumElite])[0]
Parent2 = GAF.KTournament(ScoreSortedMolecules[:NumElite])[0]
# Attempt crossover
try:
result = GAF.Mol_Crossover(Chem.MolFromSmiles(Parent1), Chem.MolFromSmiles(Parent2))
StartingMolecule = result[0]
except Exception as E:
continue
# Number of heavy atoms
try:
print(result)
NumHeavyAtoms = result[0].GetNumHeavyAtoms()
except:
continue
# Molecule ID
Name = f'Generation_{generation}_Molecule_{IDcounter}'
if NumHeavyAtoms > MaxNumHeavyAtoms * 0.8:
MutationList = ['RemoveAtom', 'ReplaceAtom', 'ReplaceBond', 'RemoveFragment']
else:
MutationList = Mutations
print(f'\n#################################################################\nNumber of attempts: {attempts}')
print(f'Total Crossover and/or Mutation Attempts: {GenerationTotalAttempts}')
print(f'GENERATION: {generation}')
#Decide whether to mutate molecule based on mutation rate
if random.random() <= MutationRate:
Mutate = True
print('Attempting to Mutate')
else:
Mutate = False
Mutation = None
if Mutate:
# Randomly select a mutation, here is where we can set mutation probabilities
Mutation = rnd(MutationList)
# Initialise Aromatic Ring
AromaticMolecule = fragments[-1]
try:
#Perform mutation, return Mut_Mol, Mut_Mol_Sanitized, MutMolSMILES, StartingMoleculeUnedited
result = GAF.Mutate(StartingMolecule, Mutation, AromaticMolecule, AtomicNumbers, BondTypes, Atoms, showdiff, fragments)
except:
continue
if GAF.GenMolChecks(result, MasterMoleculeList, MaxNumHeavyAtoms, MinNumHeavyAtoms, MaxAromRings=2) == None:
MutMol = None
else:
HeavyAtoms = result[0].GetNumHeavyAtoms() # Get number of heavy atoms in molecule
MutMol = result[0] # Get Mol object of mutated molecule
MolMass = GAF.GetMolMass(MutMol) # Get estimate of of molecular mass
MutMolSMILES = result[2] # SMILES of mutated molecule
print(f'Final SMILES: {result[2]}')
try: # Try to generate all necessary files to simulate molecule
# Set feature definition file path to OPLS or LOPLS depending on user choice
Name = f'Generation_{generation}_Molecule_{IDcounter}' # Set name of Molecule as its SMILES string
# Set feature definition file path to OPLS or LOPLS depending on user choice
if LOPLS:
LTCOMMAND = f"{join(os.getcwd(), 'rdlt.py')} --smi '{MutMolSMILES}' -n {Name} -l -c"
else:
LTCOMMAND = f"{join(os.getcwd(), 'rdlt.py')} --smi '{MutMolSMILES}' -n {Name} -c"
#Attempt to parameterise with OPLS
GAF.runcmd(f'{PYTHONPATH} {LTCOMMAND} > {STARTINGDIR}/{Name}.lt')
#Get molecule charge
charge = GAF.GetMolCharge(f'{os.getcwd()}/{Name}.lt')
#If successful, generate a PDB of molecule to use with Packmol
try:
GAF.GeneratePDB(MutMolSMILES, PATH=join(STARTINGDIR, f'{Name}.pdb'))
except:
continue
# Go into directory for this generation
os.chdir(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'))
Foldername = f'{Name}'
# Make a directory for the current molecule if it can be parameterised
GAF.runcmd(f'mkdir {Foldername}')
# Enter molecule specific directory
os.chdir(join(os.getcwd(), Foldername))
#Check if file has already been made, skip if so, being sure not to make duplicate, otherwise move file to correct directory
CWD = os.getcwd() #Need to declare otherwise will get CWD from location function is being called from
#Copy molecule pdb to molecule directory
PDBFile = join(STARTINGDIR, f'{Name}.pdb')
GAF.runcmd(f'{CopyCommand} "{PDBFile}" {join(CWD, f"{Name}.pdb")}')
#Copy molecule lt file to molecule directory
LTFile = join(STARTINGDIR, f'{Name}.lt')
GAF.runcmd(f'{CopyCommand} "{LTFile}" {join(CWD, f"{Name}.lt")}')
#Get estimate for Number of molecules
HMutMol = Chem.AddHs(MutMol)
NumMols = int(NumAtoms/HMutMol.GetNumAtoms()) # Maybe add field seeing how many mols were added to box
# Estimate starting box length
BoxL = GAF.CalcBoxLen(MolMass=MolMass, TargetDens=0.8, NumMols=NumMols)
# Make packmol files
GAF.MakePackmolFile(Name, CWD, NumMols=NumMols, BoxL=BoxL)
# Make Moltemplate files
GAF.MakeMoltemplateFile(Name, CWD, NumMols=NumMols, BoxL=BoxL)
if PYTHONPATH == 'python3':
GAF.runcmd(f'packmol < {Name}.inp')
GAF.runcmd(f'moltemplate.sh -pdb {Name}_PackmolFile.pdb {Name}_system.lt')
# Check that Moltemplate has generated all necessary files
assert os.path.exists(join(CWD, f'{Name}_system.in.settings')), 'Settings file not generated'
assert os.path.exists(join(CWD, f'{Name}_system.in.charges')), 'Charges file not generated'
assert os.path.exists(join(CWD, f'{Name}_system.data')), 'Data file not generated'
# Return to starting directory
os.chdir(STARTINGDIR)
# Add candidate and it's data to master list
MoleculeDatabase = GAF.DataUpdate(MoleculeDatabase, IDCounter=IDcounter, MutMolSMILES=MutMolSMILES, MutMol=MutMol, HeavyAtoms=HeavyAtoms,
MutationList=Mutation, ID=Name, Charge=charge, MolMass=MolMass, Predecessor=[Parent1, Parent2])
GenerationDatabase = GAF.DataUpdate(GenerationDatabase, IDCounter=IDcounter, MutMolSMILES=MutMolSMILES, MutMol=MutMol, HeavyAtoms=HeavyAtoms,
MutationList=Mutation, ID=Name, Charge=charge, MolMass=MolMass, Predecessor=[Parent1, Parent2])
# Generate list of molecules to simulate in this generation
GenSimList.append([Name, MutMolSMILES, BoxL, NumMols])
if MutMolSMILES in MasterMoleculeList:
continue
else:
MasterMoleculeList.append(MutMolSMILES) #Keep track of already generated molecules
GenerationMoleculeList.append(MutMolSMILES) #Keep track of already generated molecules
print(f'Final Molecule SMILES: {MutMolSMILES}')
IDcounter += 1
except Exception as E:
print(E)
traceback.print_exc()
os.chdir(STARTINGDIR)
continue
#### Create duplicate trajectories for each molecule
RunNum = 1
if len(GenSimList) != 25:
print(GenSimList)
print(len(GenSimList))
break
for MolParam in GenSimList:
Name = MolParam[0]
MutMolSMILES = MolParam[1]
BoxL = MolParam[2]
NumMols = MolParam[3]
for x in list(range(NumRuns)):
os.chdir(STARTINGDIR)
try:
# Go into directory for this generation
os.chdir(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'))
Foldername = f'Run_{RunNum}'
RunNum +=1
# Make a directory for the current molecule if it can be parameterised
GAF.runcmd(f'mkdir {Foldername}')
FilesDir = join(STARTINGDIR, 'Molecules', f'Generation_{generation}', f'{Name}')
CurDir = join(STARTINGDIR, 'Molecules', f'Generation_{generation}', f'{Foldername}')
SettingsFilePath = join(STARTINGDIR, 'settings.txt')
#Move files from master Dir
GAF.runcmd(f'cp -r {FilesDir}/* {CurDir}')
GAF.runcmd(f'cp {SettingsFilePath} {CurDir}')
# Make LAMMPS files
os.chdir(CurDir)
GAF.MakeLAMMPSFile(Name, CurDir, Temp=313, GKRuntime=1500000, Run=Foldername)
GAF.MakeLAMMPSFile(Name, CurDir, Temp=373, GKRuntime=1500000, Run=Foldername)
except Exception as E:
print(E)
traceback.print_exc()
pass
#This is where we should call the simulation script
CWD = join(STARTINGDIR, 'Molecules', f'Generation_{generation}')
os.chdir(CWD)
# Create array job for 40C viscosity
GAF.CreateArrayJob(STARTINGDIR, CWD, NumRuns, Generation=generation, SimName='313K.lammps', GenerationSize=GenerationSize, Agent=Agent, NumElite=NumElite)
# Create array job for 100C viscosity
GAF.CreateArrayJob(STARTINGDIR, CWD, NumRuns, Generation=generation, SimName='373K.lammps', GenerationSize=GenerationSize, Agent=Agent, NumElite=NumElite)
if PYTHONPATH == 'python3':
GAF.runcmd(f'qsub {join(CWD, f"{Agent}_313K.lammps.pbs")}')
GAF.runcmd(f'qsub {join(CWD, f"{Agent}_373K.lammps.pbs")}')
os.chdir(STARTINGDIR)
### REMOVE UNNECESSARY FILES
directory = STARTINGDIR
pattern = f'Generation_{generation}_Molecule_*' # Example: 'file_*.txt' to match files like file_1.txt, file_2.txt, etc.
# Get a list of all files matching the pattern
files_to_remove = glob.glob(os.path.join(directory, pattern))
# Remove each file
for file_path in files_to_remove:
try:
os.remove(file_path)
print(f'Removed: {file_path}')
except Exception as e:
print(f'Error removing {file_path}: {e}')
### Wait until array jobs have finished
MoveOn = False
while MoveOn == False:
GAF.runcmd(f'qstat > sims.txt')
sims = []
try:
with open(join(STARTINGDIR, 'sims.txt'), 'r') as file:
next(file) #Avoiding the first two lines
next(file)
filelist = file.readlines()
for sim in filelist:
if Agent in sim:
sims.append(sim)
print(sim)
except:
pass
# Check if array jobs have finished
if len(sims) != 0:
print('Waiting for 10 mins')
sleep(600)
else:
MoveOn = True
### Reformat directories so that properties can be calculated
GenDirectory = join(STARTINGDIR, 'Molecules', f'Generation_{generation}')
# List of simulation directories
directories_with_generation = GAF.list_generation_directories(GenDirectory, 'Run')
Num = 1
for RunDir in directories_with_generation:
try:
os.chdir(join(GenDirectory, RunDir))
PDBNamePath = GAF.find_files_with_extension(os.getcwd(), '.pdb')[0]
PDBName = GAF.extract_molecule_name(PDBNamePath)
source_directory = join(GenDirectory, RunDir)
destination_directory = join(GenDirectory, PDBName, RunDir)
GAF.move_directory(source_directory, destination_directory)
Num +=1
except Exception as E:
print(E)
traceback.print_exc()
pass
GAF.runcmd('rm -r Run*')
# Here is where we will get the various values generated from the MD simulations
# Get PDBs from directories
MOLSMILESList = []
MOLIDList = GAF.list_generation_directories(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'), 'Molecule')
for MoleculeDir in MOLIDList:
Path = join(STARTINGDIR, 'Molecules', f'Generation_{generation}', MoleculeDir, f'{MoleculeDir}.pdb')
print(Path)
MolObject = Chem.MolFromPDBFile(Path)
SMILES = Chem.MolToSmiles(MolObject)
MOLSMILESList.append(SMILES)
# Here is where we will get the various values generated from the MD simulations
GenSimList = list(zip(MOLIDList, MOLSMILESList))
print(GenSimList)
for Molecule, MOLSMILES in GenSimList:
print(Molecule)
try:
# Create a function to wait until all simulations from this generation are finished
os.chdir(join(STARTINGDIR, 'Molecules', f'Generation_{generation}', Molecule))
CWD = os.getcwd()
print('Getting Similarity Scores')
### Similarity Scores
Scores = GAF.TanimotoSimilarity(MOLSMILES, MOLSMILESList)
AvScore = 1 - (sum(Scores) / GenerationSize) # The higher the score, the less similar the molecule is to others
print('Getting SCScore')
### SCScore
SCScore = GAF.SCScore(MOLSMILES)
SCScoreNorm = SCScore/5
### Toxicity
print('Getting Toxicity')
ToxNorm = GAF.Toxicity(MOLSMILES)
print('Getting Density')
DirRuns = GAF.list_generation_directories(CWD, 'Run')
ExampleRun = DirRuns[0]
for run in DirRuns:
try:
DensityFile40 = f'{CWD}/{ExampleRun}/eqmDensity_{Molecule}_T313KP1atm.out'
DensityFile100 = f'{CWD}/{ExampleRun}/eqmDensity_{Molecule}_T373KP1atm.out'
except:
continue
print('Getting Viscosity')
### Viscosity
DVisc40 = GAF.GetVisc(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'), Molecule, 313)
DVisc100 = GAF.GetVisc(join(STARTINGDIR, 'Molecules', f'Generation_{generation}'), Molecule, 373)
Dens40 = GAF.GetDens(DensityFile40)
Dens100 = GAF.GetDens(DensityFile100)
print('Getting VI')
## Viscosity Index
KVI = GAF.GetKVI(DVisc40, DVisc100, Dens40, Dens100, STARTINGDIR)
DVI = GAF.GetDVI(DVisc40, DVisc100)
#Update Molecule Database
IDNumber = int(Molecule.split('_')[-1])
MoleculeDatabase.at[IDNumber - 1, 'Density100C'] = Dens100
MoleculeDatabase.at[IDNumber - 1, 'Density40C'] = Dens40
MoleculeDatabase.at[IDNumber - 1, 'DViscosity40C'] = DVisc40
MoleculeDatabase.at[IDNumber - 1, 'DViscosity100C'] = DVisc100
MoleculeDatabase.at[IDNumber - 1, 'KViscosity40C'] = GAF.GetKVisc(DVisc=DVisc40, Dens=Dens40)
MoleculeDatabase.at[IDNumber - 1, 'KViscosity100C'] = GAF.GetKVisc(DVisc=DVisc100, Dens=Dens100)
MoleculeDatabase.at[IDNumber - 1, 'KVI'] = KVI
MoleculeDatabase.at[IDNumber - 1, 'DVI'] = DVI
MoleculeDatabase.at[IDNumber - 1, 'Toxicity'] = ToxNorm
MoleculeDatabase.at[IDNumber - 1, 'SCScore'] = SCScoreNorm
MoleculeDatabase.at[IDNumber - 1, 'SimilarityScore'] = SCScoreNorm
#Update Generation Database
IDNumber = int(Molecule.split('_')[-1])
GenerationDatabase.at[IDNumber - 1, 'Density100C'] = Dens100
GenerationDatabase.at[IDNumber - 1, 'Density40C'] = Dens40
GenerationDatabase.at[IDNumber - 1, 'DViscosity40C'] = DVisc40
GenerationDatabase.at[IDNumber - 1, 'DViscosity100C'] = DVisc100
GenerationDatabase.at[IDNumber - 1, 'KViscosity40C'] = GAF.GetKVisc(DVisc=DVisc40, Dens=Dens40)
GenerationDatabase.at[IDNumber - 1, 'KViscosity100C'] = GAF.GetKVisc(DVisc=DVisc100, Dens=Dens100)
GenerationDatabase.at[IDNumber - 1, 'KVI'] = KVI
GenerationDatabase.at[IDNumber - 1, 'DVI'] = DVI
GenerationDatabase.at[IDNumber - 1, 'Toxicity'] = ToxNorm
GenerationDatabase.at[IDNumber - 1, 'SCScore'] = SCScoreNorm
GenerationDatabase.at[IDNumber - 1, 'SimilarityScore'] = SCScoreNorm
except Exception as E:
print(E)
traceback.print_exc()
pass
#### Generate Score
ViscScores = MoleculeDatabase['DViscosity40C'].tolist()
SCScores = MoleculeDatabase['SCScore'].tolist()
DVIScores = MoleculeDatabase['DVI'].tolist()
KVIScores = MoleculeDatabase['KVI'].tolist()
ToxicityScores = MoleculeDatabase['Toxicity'].tolist()
SimilarityScores = MoleculeDatabase['SimilarityScore'].tolist()
MoleculeNames = MoleculeDatabase['ID'].tolist()
ViscosityScore = list(zip(MoleculeNames, ViscScores))
MolecularComplexityScore = list(zip(MoleculeNames, SCScores))
DVIScore = list(zip(MoleculeNames, DVIScores))
ToxicityScore = list(zip(MoleculeNames, ToxicityScores))
ViscosityScore = [(x[0], 0) if math.isnan(x[1]) else x for x in ViscosityScore]
DVIScore = [(x[0], 0) if math.isnan(x[1]) else x for x in DVIScore]
# Apply the normalization function
Viscosity_normalized_molecule_scores = [(1-x[1]) for x in GAF.min_max_normalize(ViscosityScore)]
DVI_normalized_molecule_scores = [x[1] for x in GAF.min_max_normalize(DVIScore)]
MoleculeDatabase['ViscNormalisedScore'] = Viscosity_normalized_molecule_scores
MoleculeDatabase['DVINormalisedScore'] = DVI_normalized_molecule_scores
MoleculeDatabase['TotalScore'] = MoleculeDatabase['Toxicity'] + MoleculeDatabase['SCScore'] + MoleculeDatabase['DVINormalisedScore'] + MoleculeDatabase['ViscNormalisedScore']
MoleculeDatabase['NichedScore'] = MoleculeDatabase['TotalScore'] / MoleculeDatabase['SimilarityScore']
#Make a pandas object with just the scores and the molecule ID
GenerationMolecules = pd.Series(MoleculeDatabase.NichedScore.values, index=MoleculeDatabase.ID)
GenerationMolecules = GenerationMolecules.to_dict()
# Sort dictiornary according to target score
ScoreSortedMolecules = sorted(GenerationMolecules.items(), key=lambda item:item[1], reverse=True)
#Convert tuple elements in sorted list back to lists
ScoreSortedMolecules = [list(x) for x in ScoreSortedMolecules]
# Constructing entries for use in subsequent generation
for entry in ScoreSortedMolecules:
Key = int(entry[0].split('_')[-1]) - 1
entry.insert(1, MoleculeDatabase.loc[Key]['MolObject'])
entry.insert(2, MoleculeDatabase.loc[Key]['MutationList'])
entry.insert(3, MoleculeDatabase.loc[Key]['HeavyAtoms'])
entry.insert(4, MoleculeDatabase.loc[Key]['SMILES'])
try:
MoleculeDatabase.drop("Unnamed: 0", axis=1, inplace=True)
except:
pass
#Save the update Master database and generation database
MoleculeDatabase.to_csv(f'{STARTINGDIR}/MoleculeDatabase_Generation_{generation}.csv', index=False)
MoleculeDatabase.to_csv(f'{STARTINGDIR}/Generation_{generation}_Database.csv', index=False)