Skip to content

Commit

Permalink
Catch and handle GSL error during alignment. [closes #287]
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Mar 23, 2022
1 parent a45333b commit bad708e
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions python/BioSimSpace/Align/_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ def rmsdAlign(molecule0, molecule1, mapping=None, property_map0={}, property_map
mol0 = mol0.move().align(mol1, _SireMol.AtomResultMatcher(sire_mapping)).molecule()
except Exception as e:
msg = "Failed to align molecules based on mapping: %r" % mapping
if "Could not calculate the single value decomposition" in str(e):
msg += ". Try minimising your molecular coordinates prior to aligment."
if _isVerbose():
raise _AlignmentError(msg) from e
else:
Expand Down Expand Up @@ -1401,6 +1403,9 @@ def _score_rdkit_mappings(molecule0, molecule1, rdkit_molecule0, rdkit_molecule1
# Initialise a list of to hold the score for each mapping.
scores = []

# Whether there was a GSL alignment error.
is_gsl_error = False

# Loop over all matches from mol0.
for x in range(len(matches0)):
match0 = matches0[x]
Expand Down Expand Up @@ -1441,11 +1446,15 @@ def _score_rdkit_mappings(molecule0, molecule1, rdkit_molecule0, rdkit_molecule1
try:
molecule0 = molecule0.move().align(molecule1, _SireMol.AtomResultMatcher(sire_mapping)).molecule()
except Exception as e:
msg = "Failed to align molecules when scoring based on mapping: %r" % mapping
if _isVerbose():
raise _AlignmentError(msg) from e
if "Could not calculate the single value decomposition" in str(e):
is_gsl_error = True
gsl_exception = e
else:
raise _AlignmentError(msg) from None
msg = "Failed to align molecules when scoring based on mapping: %r" % mapping
if _isVerbose():
raise _AlignmentError(msg) from e
else:
raise _AlignmentError(msg) from None
# Flexibly align molecule0 to molecule1 based on the mapping.
elif scoring_function == "RMSDFLEXALIGN":
molecule0 = flexAlign(_Molecule(molecule0), _Molecule(molecule1), mapping,
Expand Down Expand Up @@ -1473,6 +1482,16 @@ def _score_rdkit_mappings(molecule0, molecule1, rdkit_molecule0, rdkit_molecule1

# No mappings were found.
if len(mappings) == 0:
# We failed to align mappings during scoring due to convergence issues
# during the GSL single value decomposition routine.
if is_gsl_error:
msg = "Failed to align molecules when scoring. " \
"Try minimising your molecular coordinates prior calling matchAtoms."
if _isVerbose():
raise _AlignmentError(msg) from gsl_exception

This comment has been minimized.

Copy link
@kexul

kexul May 11, 2022

Contributor

Hi @lohedges , this gsl_exception is not defined 🙃

This comment has been minimized.

Copy link
@lohedges

lohedges May 11, 2022

Author Member

It is defined on line 1535, where the gsl error triggered within Sire is caught, i.e.:

                        try:
                            molecule0 = molecule0.move().align(molecule1, _SireMol.AtomResultMatcher(sire_mapping)).molecule()
                        except Exception as e:
                            if "Could not calculate the single value decomposition" in str(e):
                                is_gsl_error = True
                                gsl_exception = e

Is this not working for you? I tested it the time with the problematic input in the issue thread and it worked for me.

This comment has been minimized.

Copy link
@kexul

kexul May 11, 2022

Contributor

Sorry! My IDE just highlighted it and I thought it was an undefined variable.
Actually, it's saying:
截屏2022-05-11 21 22 02

This comment has been minimized.

Copy link
@lohedges

lohedges May 11, 2022

Author Member

I imagine it's because gsl_exception isn't assigned to anyting in the else clause, but this isn't being picked up correctly by the IDE's linter. See here for a similar example.

This comment has been minimized.

Copy link
@kexul

kexul May 11, 2022

Contributor

Thanks, that's informative. 👍 👍 👍

else:
raise _AlignmentError(msg) from None

if len(prematch) == 0:
return ([{}], [])
else:
Expand Down

0 comments on commit bad708e

Please sign in to comment.