Skip to content

Commit

Permalink
betters warning files
Browse files Browse the repository at this point in the history
  • Loading branch information
ale94mleon committed Dec 20, 2022
1 parent 41db635 commit ce98b28
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
3 changes: 2 additions & 1 deletion docs/source/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [3.2.4] - 2020.12.20
## [3.2.6] - 2020.12.20

### Fixed

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

## [3.2.2] - 2020.12.12

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, 4)
__version_tuple__ = version_tuple = (3, 2, 5)
__version__ = version = '.'.join([str(i) for i in __version_tuple__])
7 changes: 5 additions & 2 deletions moldrug/constraintconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def generate_conformers(mol: Chem.rdchem.Mol,
mol with with generated conformers. In case some Exception ocurred, it returns mol without conformers and write in the
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 SMILES to be fixed are not given, assume to the MCS
if ref_smi:
if not Chem.MolFromSmiles(ref_smi):
Expand Down Expand Up @@ -167,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, 'generate_conformers_error.pbz2')}")
compressed_pickle('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
12 changes: 8 additions & 4 deletions moldrug/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ def __vinadock(
"""

constraint_type = constraint_type.lower()

# Creating the error directory if needed
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 @@ -314,8 +318,8 @@ def __vinadock(
'boxcenter': boxcenter,
'boxsize': boxsize,
}
utils.compressed_pickle(f'idx_{Individual.idx}_conf_{conf.GetId()}_error', error)
warnings.warn(f"\nVina failed! Check: idx_{Individual.idx}_conf_{conf.GetId()}_error.pbz2 file.\n")
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 @@ -361,8 +365,8 @@ def __vinadock(
'boxcenter': boxcenter,
'boxsize': boxsize,
}
utils.compressed_pickle(f'{Individual.idx}_error', error)
warnings.warn(f"\nVina failed! Check: {Individual.idx}_error.pbz2 file.\n")
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')
return vina_score_pdbqt
Expand Down
18 changes: 18 additions & 0 deletions moldrug/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,19 @@ 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
Parameters
----------
error_path : str
Where the error are storged.
"""
if os.path.isdir(error_path):
if os.listdir(error_path):
shutil.make_archive('error', 'gztar', error_path)
shutil.rmtree(error_path)


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

# Clean directory
costfunc_jobs_tmp_dir.cleanup()
# Tar errors
__tar_errors__('error')

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

# Tar errors
__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
20 changes: 9 additions & 11 deletions tests/test_moldrug.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def test_single_receptor_command_line():

os.chdir(cwd)


def test_multi_receptor(maxiter = 1, popsize = 2, njobs = 3, NumbCalls = 1):
out = utils.GA(
seed_mol=[Chem.MolFromSmiles(ligands.r_x0161), Chem.MolFromSmiles(ligands.r_x0161)],
Expand Down Expand Up @@ -144,8 +143,6 @@ def test_multi_receptor(maxiter = 1, popsize = 2, njobs = 3, NumbCalls = 1):
vina_out.chunks[0].write()
os.chdir(cwd)



def test_local_command_line():
Config = {
"main": {
Expand Down Expand Up @@ -186,7 +183,6 @@ def test_local_command_line():
print(result.to_dataframe())
os.chdir(cwd)


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

# Clean
os.remove('0_error.pbz2')

utils.__tar_errors__()
os.remove('error.tar.gz')

def test_home():
home.home(dataDir='data')


def test_get_sim_utils():
from rdkit.Chem import AllChem
mols = []
Expand All @@ -268,14 +263,12 @@ def test_get_sim_utils():
ref_fps.append(AllChem.GetMorganFingerprintAsBitVect(mol, 2))
utils.get_sim(mols, ref_fps)


def test_lipinski():
mol = Chem.MolFromSmiles('CCCO')
utils.lipinski_filter(Chem.MolFromSmiles('BrCC(COCN)CC(Br)CC(Cl)CCc1ccccc1CCCC(NCCCO)'))
utils.lipinski_filter(mol)
utils.lipinski_profile(mol)


def test_Individual():
I1 = utils.Individual(Chem.MolFromSmiles('CC'), cost=10)
I2 = utils.Individual(Chem.MolFromSmiles('CCO'), cost=2)
Expand All @@ -297,8 +290,6 @@ def test_Individual():
assert divmod(I1, I2) == (5,0)
assert I1**I2 == 100



def test_miscellanea():
obj0 = []
for i in range(0,50):
Expand Down Expand Up @@ -339,6 +330,7 @@ def test_miscellanea():
# print(os.listdir(tmp_path.name))

# print(local.to_dataframe())

def test_constraintconf():
from moldrug.constraintconf import constraintconf
with Chem.SDWriter(os.path.join(tmp_path.name, 'fix.sdf')) as w:
Expand All @@ -353,6 +345,9 @@ def test_constraintconf():
fix= os.path.join(tmp_path.name, 'fix.sdf'),
out = os.path.join(tmp_path.name, 'conf.sdf')
)
# Clean
utils.__tar_errors__()

def test_generate_conformers():
from moldrug.constraintconf import generate_conformers
from rdkit.Chem import AllChem
Expand All @@ -363,6 +358,9 @@ def test_generate_conformers():
AllChem.MMFFOptimizeMolecule(ref)
generate_conformers(Chem.RemoveHs(mol), Chem.RemoveHs(ref), 50)

# Clean
utils.__tar_errors__()


if __name__ == '__main__':
pass

0 comments on commit ce98b28

Please sign in to comment.