Skip to content

Commit

Permalink
fixing handling of error writing to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
ale94mleon committed Dec 21, 2022
1 parent 5c7f1d9 commit b751132
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
20 changes: 18 additions & 2 deletions docs/source/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [3.3.0]

### Changed

- `moldrug.utils.__tar_errors__` by `moldrug.utils.tar_errors`.
- Default value of `moldrug.utils.tar_errors` is `error` instead of `.error`.
- `moldrug.constraintconf.generate_conformers` outputs warnings and errors to `error` instead of `.error`.
- `moldrug.fitness.__vinadock` by `moldrug.fitness._vinadock`.
- `moldrug.fitness._vinadock` outputs warnings and errors to `error` instead of `.error`.

### Fixed

- The use of `moldrug.utils.tar_errors` inside of `moldrug.utils.Local` and `moldrug.utils.GA`.
- Clean code.

## [3.2.5] - 2020.12.20

### Fixed
Expand Down Expand Up @@ -279,8 +294,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Minor code cleaning.
- Better code covered during testing

[unreleased]: https://github.com/ale94mleon/MolDrug/compare/3.2.5...HEAD
[3.2.4]: https://github.com/ale94mleon/MolDrug/compare/3.2.2...3.2.5
[unreleased]: https://github.com/ale94mleon/MolDrug/compare/3.3.0...HEAD
[3.3.0]: https://github.com/ale94mleon/MolDrug/compare/3.2.5...3.3.0
[3.2.5]: https://github.com/ale94mleon/MolDrug/compare/3.2.2...3.2.5
[3.2.2]: https://github.com/ale94mleon/MolDrug/compare/3.2.0...3.2.2
[3.2.0]: https://github.com/ale94mleon/MolDrug/compare/3.1.0...3.2.0
[3.1.0]: https://github.com/ale94mleon/MolDrug/compare/3.0.3...3.1.0
Expand Down
2 changes: 1 addition & 1 deletion moldrug/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# We will use semantic version (major, minor, patch)
__version_tuple__ = version_tuple = (3, 2, 5)
__version_tuple__ = version_tuple = (3, 3, 0)
__version__ = version = '.'.join([str(i) for i in __version_tuple__])
8 changes: 4 additions & 4 deletions moldrug/constraintconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def generate_conformers(mol: Chem.rdchem.Mol,
current directory generate_conformers_error.log with the nature of the Exception.
"""
# Creating the error directory if needed
if not os.path.isdir('.error'):
os.makedirs('.error')
if not os.path.isdir('error'):
os.makedirs('error')
# if SMILES to be fixed are not given, assume to the MCS
if ref_smi:
if not Chem.MolFromSmiles(ref_smi):
Expand Down Expand Up @@ -170,8 +170,8 @@ def generate_conformers(mol: Chem.rdchem.Mol,
except Exception as e:
print(e)
cwd = os.getcwd()
warnings.warn(f"generate_conformers failed. Check the file {os.path.join(cwd, '.error/generate_conformers_error.pbz2')}")
compressed_pickle('.error/generate_conformers_error', e)
warnings.warn(f"generate_conformers failed. Check the file {os.path.join(cwd, 'error/generate_conformers_error.pbz2')}")
compressed_pickle('error/generate_conformers_error', e)
mol.RemoveAllConformers()
return mol

Expand Down
10 changes: 5 additions & 5 deletions moldrug/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def __vinadock(
constraint_type = constraint_type.lower()

# Creating the error directory if needed
if not os.path.isdir('.error'):
os.makedirs('.error')
if not os.path.isdir('error'):
os.makedirs('error')
# Creating the working directory if needed
if not os.path.exists(wd):
os.makedirs(wd)
Expand Down Expand Up @@ -297,7 +297,7 @@ def __vinadock(
# Make a copy to the vina command string and add the out (is needed) and ligand options
cmd_vina_str_tmp = cmd_vina_str[:]
cmd_vina_str_tmp += f" --ligand {os.path.join(wd, f'{Individual.idx}_conf_{conf.GetId()}.pdbqt')}"

if constraint_type == 'local_only':
cmd_vina_str_tmp += f" --out {os.path.join(wd, f'{Individual.idx}_conf_{conf.GetId()}_out.pdbqt')}"
try:
Expand All @@ -318,7 +318,7 @@ def __vinadock(
'boxcenter': boxcenter,
'boxsize': boxsize,
}
utils.compressed_pickle(f'.error/idx_{Individual.idx}_conf_{conf.GetId()}_error', error)
utils.compressed_pickle(f'error/idx_{Individual.idx}_conf_{conf.GetId()}_error', error)
warnings.warn(f"\nVina failed! Check: idx_{Individual.idx}_conf_{conf.GetId()}_error.pbz2 file in error.\n")
vina_score_pdbqt = (np.inf, preparator.write_pdbqt_string())
return vina_score_pdbqt
Expand Down Expand Up @@ -365,7 +365,7 @@ def __vinadock(
'boxcenter': boxcenter,
'boxsize': boxsize,
}
utils.compressed_pickle(f'.error/{Individual.idx}_error', error)
utils.compressed_pickle(f'error/{Individual.idx}_error', error)
warnings.warn(f"\nVina failed! Check: {Individual.idx}_error.pbz2 file in error.\n")

vina_score_pdbqt = (np.inf, 'VinaFailed')
Expand Down
25 changes: 14 additions & 11 deletions moldrug/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def import_sascorer():
"""
# In order to import sascorer from RDConfig.RDContribDir
from rdkit.Chem import RDConfig
import os, importlib.util as importlib_util
import importlib.util as importlib_util
spec=importlib_util.spec_from_file_location('sascorer', os.path.join(RDConfig.RDContribDir, 'SA_Score', 'sascorer.py'))
sascorer = importlib_util.module_from_spec(spec)
spec.loader.exec_module(sascorer)
Expand Down Expand Up @@ -826,7 +826,7 @@ def make_sdf(individuals:List[Individual], sdf_name = 'out'):
print(f"{individual} does not have a valid pdbqt: {individual.pdbqt}.")
print(f"File {sdf_name}.sdf was createad!")

def __make_kwargs_copy__(costfunc, costfunc_kwargs,):
def _make_kwargs_copy(costfunc, costfunc_kwargs,):
"""Make a copy of the self.costfunc_kwargs.
It creates a temporal directory.
Expand All @@ -841,13 +841,15 @@ def __make_kwargs_copy__(costfunc, costfunc_kwargs,):
kwargs_copy['wd'] = costfunc_jobs_tmp_dir.name
return kwargs_copy, costfunc_jobs_tmp_dir

def __tar_errors__(error_path:str = '.error'):
"""Clena of errors the working directory
def tar_errors(error_path:str = 'error'):
"""Clean errors in the working directory.
Convert to error.tar.gz the error_path
and delete the directory.
Parameters
----------
error_path : str
Where the error are storged.
Where the errors are storged.
"""
if os.path.isdir(error_path):
if os.listdir(error_path):
Expand Down Expand Up @@ -946,7 +948,7 @@ def __call__(self, njobs:int = 1, pick:int = None):
# Creating the arguments
args_list = []
# Make a copy of the self.costfunc_kwargs
kwargs_copy, costfunc_jobs_tmp_dir = __make_kwargs_copy__(self.costfunc, self.costfunc_kwargs)
kwargs_copy, costfunc_jobs_tmp_dir = _make_kwargs_copy(self.costfunc, self.costfunc_kwargs)

for individual in self.pop:
args_list.append((individual, kwargs_copy))
Expand All @@ -959,7 +961,7 @@ def __call__(self, njobs:int = 1, pick:int = None):
# Clean directory
costfunc_jobs_tmp_dir.cleanup()
# Tar errors
__tar_errors__('error')
tar_errors('error')

# Printing how long was the simulation
print(f"Finished at {datetime.datetime.now().strftime('%c')}.\n")
Expand Down Expand Up @@ -1231,7 +1233,7 @@ def __call__(self, njobs:int = 1):
args_list = []
# Make a copy of the self.costfunc_kwargs
# Make a copy of the self.costfunc_kwargs
kwargs_copy, costfunc_jobs_tmp_dir = __make_kwargs_copy__(self.costfunc, self.costfunc_kwargs)
kwargs_copy, costfunc_jobs_tmp_dir = _make_kwargs_copy(self.costfunc, self.costfunc_kwargs)

for individual in self.pop:
args_list.append((individual, kwargs_copy))
Expand All @@ -1254,7 +1256,7 @@ def __call__(self, njobs:int = 1):

# Clean directory
costfunc_jobs_tmp_dir.cleanup()

# Adding generation information
for individual in self.pop:
individual.genID = self.NumGens
Expand Down Expand Up @@ -1323,7 +1325,7 @@ def __call__(self, njobs:int = 1):
# Creating the arguments
args_list = []
# Make a copy of the self.costfunc_kwargs
kwargs_copy, costfunc_jobs_tmp_dir = __make_kwargs_copy__(self.costfunc, self.costfunc_kwargs)
kwargs_copy, costfunc_jobs_tmp_dir = _make_kwargs_copy(self.costfunc, self.costfunc_kwargs)

NumbOfSawIndividuals = len(self.SawIndividuals)
for (i, individual) in enumerate(popc):
Expand All @@ -1348,6 +1350,7 @@ def __call__(self, njobs:int = 1):
f"=========Parellel=========:\n {e1}\n"\
f"==========Serial==========:\n {e2}"
)

# Clean directory
costfunc_jobs_tmp_dir.cleanup()

Expand Down Expand Up @@ -1397,7 +1400,7 @@ def __call__(self, njobs:int = 1):
print(f"\n{50*'=+'}\n")

# Tar errors
__tar_errors__('error')
tar_errors('error')

# Printing how long was the simulation
print(f"Total time ({self.maxiter} generations): {time.time() - ts:>5.2f} (s).\nFinished at {datetime.datetime.now().strftime('%c')}.\n")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_moldrug.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_fitness_module():
boxcenter = boxes.r_x0161['A']['boxcenter'], boxsize = boxes.r_x0161['A']['boxsize'],)

# Clean
utils.__tar_errors__()
utils.tar_errors()
os.remove('error.tar.gz')

def test_home():
Expand Down Expand Up @@ -346,20 +346,20 @@ def test_constraintconf():
out = os.path.join(tmp_path.name, 'conf.sdf')
)
# Clean
utils.__tar_errors__()
utils.tar_errors()

def test_generate_conformers():
from moldrug.constraintconf import generate_conformers
from rdkit.Chem import AllChem
ref = Chem.MolFromSmiles('O=S(=O)(Nc1ccc(Cl)cc1)c1ccsc1C(O)O')
mol = Chem.MolFromSmiles('CN(C)S(=O)(=O)c1cc(NS(=O)(=O)c2ccsc2C(O)O)ccc1Cl')

AllChem.EmbedMolecule(ref)
AllChem.EmbedMolecule(ref)
AllChem.MMFFOptimizeMolecule(ref)
generate_conformers(Chem.RemoveHs(mol), Chem.RemoveHs(ref), 50)

# Clean
utils.__tar_errors__()
utils.tar_errors()


if __name__ == '__main__':
Expand Down

0 comments on commit b751132

Please sign in to comment.