Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function timeouts, process attempts #1

Merged
merged 6 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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