Skip to content

Commit

Permalink
Merge pull request #1 from ECRL/dev
Browse files Browse the repository at this point in the history
Function timeouts, process attempts
  • Loading branch information
tjkessler authored Jun 8, 2019
2 parents 95af693 + ccd4492 commit da3aa5e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion padelpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from padelpy.wrapper import padeldescriptor
from padelpy.functions import from_mdl, from_smiles
__version__ = '0.1.2'
__version__ = '0.1.3'
80 changes: 45 additions & 35 deletions padelpy/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
#
# padelpy/functions.py
# v.0.1.2
# v.0.1.3
# Developed in 2019 by Travis Kessler <[email protected]>
#
# Contains various functions commonly used with PaDEL-Descriptor
Expand All @@ -20,14 +20,15 @@


def from_smiles(smiles: str, output_csv: str=None, descriptors: bool=True,
fingerprints: bool=False) -> OrderedDict:
fingerprints: bool=False, timeout: int=4) -> OrderedDict:
''' from_smiles: converts SMILES string to QSPR descriptors/fingerprints
Args:
smiles (str): SMILES string for a given molecule
output_csv (str): if supplied, saves descriptors to this CSV file
descriptors (bool): if `True`, calculates descriptors
fingerprints (bool): if `True`, calculates fingerprints
timeout (int): maximum time, in seconds, for conversion
Returns:
OrderedDict: descriptors/fingerprint labels and values
Expand All @@ -44,22 +45,26 @@ def from_smiles(smiles: str, output_csv: str=None, descriptors: bool=True,
save_csv = False
output_csv = '{}.csv'.format(timestamp)

try:
padeldescriptor(
mol_dir='{}.smi'.format(timestamp),
d_file=output_csv,
convert3d=True,
retain3d=True,
d_2d=descriptors,
d_3d=descriptors,
fingerprints=fingerprints,
maxruntime=10000
)
except RuntimeError as exception:
remove('{}.smi'.format(timestamp))
if not save_csv:
remove(output_csv)
raise RuntimeError(exception)
for attempt in range(3):
try:
padeldescriptor(
mol_dir='{}.smi'.format(timestamp),
d_file=output_csv,
convert3d=True,
retain3d=True,
d_2d=descriptors,
d_3d=descriptors,
fingerprints=fingerprints,
maxruntime=1000*timeout
)
except RuntimeError as exception:
if attempt == 2:
remove('{}.smi'.format(timestamp))
if not save_csv:
remove(output_csv)
raise RuntimeError(exception)
else:
continue

with open(output_csv, 'r', encoding='utf-8') as desc_file:
reader = DictReader(desc_file)
Expand All @@ -75,7 +80,7 @@ def from_smiles(smiles: str, output_csv: str=None, descriptors: bool=True,


def from_mdl(mdl_file: str, output_csv: str=None, descriptors: bool=True,
fingerprints: bool=False) -> list:
fingerprints: bool=False, timeout: int=4) -> list:
''' from_mdl: converts MDL file into QSPR descriptors/fingerprints;
multiple molecules may be represented in the MDL file
Expand All @@ -84,6 +89,7 @@ def from_mdl(mdl_file: str, output_csv: str=None, descriptors: bool=True,
output_csv (str): if supplied, saves descriptors/fingerprints here
descriptors (bool): if `True`, calculates descriptors
fingerprints (bool): if `True`, calculates fingerprints
timeout (int): maximum time, in seconds, for conversion
Returns:
list: list of dicts, where each dict corresponds sequentially to a
Expand All @@ -103,22 +109,26 @@ def from_mdl(mdl_file: str, output_csv: str=None, descriptors: bool=True,
datetime.now().strftime('%Y%m%d%H%M%S%f')[:-3]
)

try:
padeldescriptor(
mol_dir=mdl_file,
d_file=output_csv,
convert3d=True,
retain3d=True,
retainorder=True,
d_2d=descriptors,
d_3d=descriptors,
fingerprints=fingerprints,
maxruntime=10000
)
except RuntimeError as exception:
if not save_csv:
remove(output_csv)
raise RuntimeError(exception)
for attempt in range(3):
try:
padeldescriptor(
mol_dir=mdl_file,
d_file=output_csv,
convert3d=True,
retain3d=True,
retainorder=True,
d_2d=descriptors,
d_3d=descriptors,
fingerprints=fingerprints,
maxruntime=1000*timeout
)
except RuntimeError as exception:
if attempt == 2:
if not save_csv:
remove(output_csv)
raise RuntimeError(exception)
else:
continue

with open(output_csv, 'r', encoding='utf-8') as desc_file:
reader = DictReader(desc_file)
Expand Down
4 changes: 2 additions & 2 deletions padelpy/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
#
# padelpy/wrapper.py
# v.0.1.2
# v.0.1.3
# Developed in 2019 by Travis Kessler <[email protected]>
#
# Contains the `padeldescriptor` function, a wrapper for PaDEL-Descriptor
Expand Down Expand Up @@ -121,6 +121,6 @@ def padeldescriptor(maxruntime: int=-1, waitingjobs: int=-1, threads: int=-1,
_, err = p.communicate()
if err != b'':
raise RuntimeError('PaDEL-Descriptor encountered an error: {}'.format(
err
err.decode('utf-8')
))
return
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='padelpy',
version='0.1.2',
version='0.1.3',
description='A Python wrapper for PaDEL-Descriptor',
url='https://github.com/ecrl/padelpy',
author='Travis Kessler',
Expand Down

0 comments on commit da3aa5e

Please sign in to comment.