Skip to content

Commit ce98b28

Browse files
committed
betters warning files
1 parent 41db635 commit ce98b28

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

docs/source/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10-
## [3.2.4] - 2020.12.20
10+
## [3.2.6] - 2020.12.20
1111

1212
### Fixed
1313

1414
- Improve docs.
1515
- Cleaning of temporal files in /tmp directory. Now it is created temporal directories in the working directory with the pattern: `.costfunc_MolDrug_XXXXXXXX`.
16+
- Cleaning errors. Now all warnings and errors are saved in .error directory and at the end they are compress to error.tar.gz
1617

1718
## [3.2.2] - 2020.12.12
1819

moldrug/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
# We will use semantic version (major, minor, patch)
4-
__version_tuple__ = version_tuple = (3, 2, 4)
4+
__version_tuple__ = version_tuple = (3, 2, 5)
55
__version__ = version = '.'.join([str(i) for i in __version_tuple__])

moldrug/constraintconf.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def generate_conformers(mol: Chem.rdchem.Mol,
126126
mol with with generated conformers. In case some Exception ocurred, it returns mol without conformers and write in the
127127
current directory generate_conformers_error.log with the nature of the Exception.
128128
"""
129+
# Creating the error directory if needed
130+
if not os.path.isdir('.error'):
131+
os.makedirs('.error')
129132
# if SMILES to be fixed are not given, assume to the MCS
130133
if ref_smi:
131134
if not Chem.MolFromSmiles(ref_smi):
@@ -167,8 +170,8 @@ def generate_conformers(mol: Chem.rdchem.Mol,
167170
except Exception as e:
168171
print(e)
169172
cwd = os.getcwd()
170-
warnings.warn(f"generate_conformers failed. Check the file {os.path.join(cwd, 'generate_conformers_error.pbz2')}")
171-
compressed_pickle('generate_conformers_error', e)
173+
warnings.warn(f"generate_conformers failed. Check the file {os.path.join(cwd, '.error/generate_conformers_error.pbz2')}")
174+
compressed_pickle('.error/generate_conformers_error', e)
172175
mol.RemoveAllConformers()
173176
return mol
174177

moldrug/fitness.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def __vinadock(
236236
"""
237237

238238
constraint_type = constraint_type.lower()
239+
240+
# Creating the error directory if needed
241+
if not os.path.isdir('.error'):
242+
os.makedirs('.error')
239243
# Creating the working directory if needed
240244
if not os.path.exists(wd):
241245
os.makedirs(wd)
@@ -314,8 +318,8 @@ def __vinadock(
314318
'boxcenter': boxcenter,
315319
'boxsize': boxsize,
316320
}
317-
utils.compressed_pickle(f'idx_{Individual.idx}_conf_{conf.GetId()}_error', error)
318-
warnings.warn(f"\nVina failed! Check: idx_{Individual.idx}_conf_{conf.GetId()}_error.pbz2 file.\n")
321+
utils.compressed_pickle(f'.error/idx_{Individual.idx}_conf_{conf.GetId()}_error', error)
322+
warnings.warn(f"\nVina failed! Check: idx_{Individual.idx}_conf_{conf.GetId()}_error.pbz2 file in error.\n")
319323
vina_score_pdbqt = (np.inf, preparator.write_pdbqt_string())
320324
return vina_score_pdbqt
321325

@@ -361,8 +365,8 @@ def __vinadock(
361365
'boxcenter': boxcenter,
362366
'boxsize': boxsize,
363367
}
364-
utils.compressed_pickle(f'{Individual.idx}_error', error)
365-
warnings.warn(f"\nVina failed! Check: {Individual.idx}_error.pbz2 file.\n")
368+
utils.compressed_pickle(f'.error/{Individual.idx}_error', error)
369+
warnings.warn(f"\nVina failed! Check: {Individual.idx}_error.pbz2 file in error.\n")
366370

367371
vina_score_pdbqt = (np.inf, 'VinaFailed')
368372
return vina_score_pdbqt

moldrug/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,19 @@ def __make_kwargs_copy__(costfunc, costfunc_kwargs,):
841841
kwargs_copy['wd'] = costfunc_jobs_tmp_dir.name
842842
return kwargs_copy, costfunc_jobs_tmp_dir
843843

844+
def __tar_errors__(error_path:str = '.error'):
845+
"""Clena of errors the working directory
846+
847+
Parameters
848+
----------
849+
error_path : str
850+
Where the error are storged.
851+
"""
852+
if os.path.isdir(error_path):
853+
if os.listdir(error_path):
854+
shutil.make_archive('error', 'gztar', error_path)
855+
shutil.rmtree(error_path)
856+
844857

845858
class Local:
846859
"""For local search
@@ -945,6 +958,8 @@ def __call__(self, njobs:int = 1, pick:int = None):
945958

946959
# Clean directory
947960
costfunc_jobs_tmp_dir.cleanup()
961+
# Tar errors
962+
__tar_errors__('error')
948963

949964
# Printing how long was the simulation
950965
print(f"Finished at {datetime.datetime.now().strftime('%c')}.\n")
@@ -1381,6 +1396,9 @@ def __call__(self, njobs:int = 1):
13811396
print(f"The cost function dropped in {self.InitIndividual - self.pop[0]} units.")
13821397
print(f"\n{50*'=+'}\n")
13831398

1399+
# Tar errors
1400+
__tar_errors__('error')
1401+
13841402
# Printing how long was the simulation
13851403
print(f"Total time ({self.maxiter} generations): {time.time() - ts:>5.2f} (s).\nFinished at {datetime.datetime.now().strftime('%c')}.\n")
13861404

tests/test_moldrug.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def test_single_receptor_command_line():
9393

9494
os.chdir(cwd)
9595

96-
9796
def test_multi_receptor(maxiter = 1, popsize = 2, njobs = 3, NumbCalls = 1):
9897
out = utils.GA(
9998
seed_mol=[Chem.MolFromSmiles(ligands.r_x0161), Chem.MolFromSmiles(ligands.r_x0161)],
@@ -144,8 +143,6 @@ def test_multi_receptor(maxiter = 1, popsize = 2, njobs = 3, NumbCalls = 1):
144143
vina_out.chunks[0].write()
145144
os.chdir(cwd)
146145

147-
148-
149146
def test_local_command_line():
150147
Config = {
151148
"main": {
@@ -186,7 +183,6 @@ def test_local_command_line():
186183
print(result.to_dataframe())
187184
os.chdir(cwd)
188185

189-
190186
@pytest.mark.filterwarnings("ignore:\nVina failed")
191187
def test_fitness_module():
192188
individual = utils.Individual(Chem.MolFromSmiles(ligands.r_x0161))
@@ -251,13 +247,12 @@ def test_fitness_module():
251247
boxcenter = boxes.r_x0161['A']['boxcenter'], boxsize = boxes.r_x0161['A']['boxsize'],)
252248

253249
# Clean
254-
os.remove('0_error.pbz2')
255-
250+
utils.__tar_errors__()
251+
os.remove('error.tar.gz')
256252

257253
def test_home():
258254
home.home(dataDir='data')
259255

260-
261256
def test_get_sim_utils():
262257
from rdkit.Chem import AllChem
263258
mols = []
@@ -268,14 +263,12 @@ def test_get_sim_utils():
268263
ref_fps.append(AllChem.GetMorganFingerprintAsBitVect(mol, 2))
269264
utils.get_sim(mols, ref_fps)
270265

271-
272266
def test_lipinski():
273267
mol = Chem.MolFromSmiles('CCCO')
274268
utils.lipinski_filter(Chem.MolFromSmiles('BrCC(COCN)CC(Br)CC(Cl)CCc1ccccc1CCCC(NCCCO)'))
275269
utils.lipinski_filter(mol)
276270
utils.lipinski_profile(mol)
277271

278-
279272
def test_Individual():
280273
I1 = utils.Individual(Chem.MolFromSmiles('CC'), cost=10)
281274
I2 = utils.Individual(Chem.MolFromSmiles('CCO'), cost=2)
@@ -297,8 +290,6 @@ def test_Individual():
297290
assert divmod(I1, I2) == (5,0)
298291
assert I1**I2 == 100
299292

300-
301-
302293
def test_miscellanea():
303294
obj0 = []
304295
for i in range(0,50):
@@ -339,6 +330,7 @@ def test_miscellanea():
339330
# print(os.listdir(tmp_path.name))
340331

341332
# print(local.to_dataframe())
333+
342334
def test_constraintconf():
343335
from moldrug.constraintconf import constraintconf
344336
with Chem.SDWriter(os.path.join(tmp_path.name, 'fix.sdf')) as w:
@@ -353,6 +345,9 @@ def test_constraintconf():
353345
fix= os.path.join(tmp_path.name, 'fix.sdf'),
354346
out = os.path.join(tmp_path.name, 'conf.sdf')
355347
)
348+
# Clean
349+
utils.__tar_errors__()
350+
356351
def test_generate_conformers():
357352
from moldrug.constraintconf import generate_conformers
358353
from rdkit.Chem import AllChem
@@ -363,6 +358,9 @@ def test_generate_conformers():
363358
AllChem.MMFFOptimizeMolecule(ref)
364359
generate_conformers(Chem.RemoveHs(mol), Chem.RemoveHs(ref), 50)
365360

361+
# Clean
362+
utils.__tar_errors__()
363+
366364

367365
if __name__ == '__main__':
368366
pass

0 commit comments

Comments
 (0)