smartsexplore.database.util module

Utility functions for easy access to the SMARTSexplore database and connected SQLAlchemy sessions.

exception smartsexplore.database.util.NoSMARTSException

Bases: Exception

Raised by write_smarts_to_tempfile() if no SMARTS are available to write.

smartsexplore.database.util.close_session(e=None)

Closes the current SQLAlchemy session to the app database, if one was opened.

smartsexplore.database.util.get_db(db_url=None)

Gets an SQLAlchemy session given a valid database URL.

Must be called from within a Flask appcontext.

Returns

A tuple of (engine object, sessionmaker instance).

smartsexplore.database.util.get_session()

Gets an SQLAlchemy session to the app database, as defined by the DATABASE app config key.

Must be called from within a Flask appcontext.

If called within a Flask context where the flask.g object is available (e.g., a running server), it gets the session from the flask.g object, or creates and stores it on the flask.g object. This session will be a scoped session (see SQLAlchemy ORM docs).

If called within a context where the flask.g object is unavailable (e.g., management commands that are not tied to a running server, and only have the appcontext available), returns a non-scoped session and does not interact with flask.g.

Returns

A (potentially scoped) SQLAlchemy session to the app database, see above.

smartsexplore.database.util.init_db()None

Initializes the app database appropriately. Currently just means that the ORM model tables are created.

Must be used within the Flask appcontext.

smartsexplore.database.util.molecules_to_temporary_smiles_file(molecules) -> (<function NamedTemporaryFile at 0x7f6f14dede50>, typing.Dict[int, int])

Writes a list of Molecule objects to a temporary .smiles file, and returns a handle to that file.

Will write each Molecule as one line consisting of the molecule’s SMILES pattern and its ID as the “label” of its output SMILES line, so that the resultant file can be passed to external tools and the output of those tools can be linked back to the corresponding Molecule objects (as long as those external tools print the “label” of the processed molecule, of course).

Returns

A tuple of:

  • The handle to the written temporary .smiles file

  • A dictionary mapping (line number) to (Molecule id)

smartsexplore.database.util.write_smarts_to_tempfile()tempfile.NamedTemporaryFile

Retrieves all SMARTS patterns currently stored in the database, and writes them out into a temporary file, using the IDs from the database as labels for each SMARTS object.

Returns

A file handle to the written temporary .smarts file.

Raises

NoSMARTSException, if there are no SMARTS to be written.