Source code for schrodinger.application.matsci.smartsutilsgui
"""
GUI elements for working with SMARTS patterns
Copyright Schrodinger, LLC. All rights reserved.
"""
import warnings
from schrodinger.application.matsci import smartsutils
from schrodinger.infra import mm
from schrodinger.Qt import QtGui
from schrodinger.structutils import analyze
[docs]class SMARTSNameValidator(QtGui.QValidator):
"""
Ensures that the line edit contains only valid SMARTS name characters
"""
[docs] def validate(self, value, position):
"""
See PyQt documentation for arguments and return values
"""
if not value:
return self.Intermediate, value, position
if smartsutils.validate_name(value):
return self.Acceptable, value, position
else:
return self.Invalid, value, position
[docs]def getSMARTSFromWS(maestro,
warning,
smart_edit,
canvas_api=False,
fall_back=False,
check_connectivity=True,
allow_intermolecular=False):
"""
Get the SMARTS pattern for the selected atoms in the workspace and
insert it into the SMARTS entry
:type maestro: `schrodinger.maestro.maestro`
:param maestro: maestro provides structure and selected atom index
:type warning: function
:param warning: prints warning message
:type smart_edit: `schrodinger.ui.qt.swidgets.SMARTSEdit`
:param smart_edit: setText() sets the name of the smart pattern in GUI
:type canvas_api: bool
:param canvas_api: whether to use analyze.generate_smarts or
analyze.generate_smarts_canvas
:type fall_back: bool
:param fall_back: whether to fall back on using analyze.generate_smarts
if analyze.generate_smarts_canvas fails, used only if canvas_api is
True
:type check_connectivity: bool
:param check_connectivity:
If True, check for whether the atoms given are connected and raise a
ValueError if they are not. SMARTS generation will give bogus
results for unconnected atoms.
:type allow_intermolecular: bool
:param allow_intermolecular: if check_connectivity is False this controls whether
matches must be intramolecular or allowed to be intermolecular
"""
selection = maestro.selected_atoms_get()
if not selection:
warning('No workspace atoms selected')
return
try:
struct = maestro.get_included_entry()
except RuntimeError:
warning("There has to be at least one and "
"only one included entry in the Workspace.")
return
if not check_connectivity and not allow_intermolecular:
if len(set(struct.atom[i].molecule_number for i in selection)) > 1:
warning('Selection includes atoms from different molecules.')
return
use_default = not canvas_api
if canvas_api:
try:
smarts = analyze.generate_smarts_canvas(
struct,
atom_subset=selection,
check_connectivity=check_connectivity)
except (RuntimeError, ValueError) as msg:
if not fall_back:
warning(str(msg))
return
else:
use_default = True
if use_default:
try:
# Changed default of generate_smarts_canvas to generate_smarts
# due to CANVAS-5621
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message="analyze.generate_smarts")
smarts = analyze.generate_smarts(
struct,
atom_subset=selection,
check_connectivity=check_connectivity)
except ValueError as msg:
warning(str(msg))
return
except mm.MmException:
# Something went very wrong with obtaining the SMARTS pattern, but
# we don't know what and the Exception message is not user
# appropriate
warning('Unable to obtain a SMARTS pattern for the selected atoms')
return
smart_edit.setText(smarts)