OCDocker.DB.DB module

Sets of classes and functions that are used for creating everything required for the database.

Usage:

import OCDocker.DB.DB as ocdb

OCDocker.DB.DB.create_tables(engine=None)[source]

Create all ORM tables bound to the provided or default engine.

If no engine is provided, the default engine must have been initialized explicitly through application/CLI bootstrap.

Parameters:

engine (Engine | None)

Return type:

None

OCDocker.DB.DB.export_db_to_csv(session=None, output_format='dataframe', output_file=None, drop_na=True, batch_size=1000)[source]

Merge data from Complexes, Ligands, and Receptors tables and export.

Parameters:
  • session (sqlalchemy.orm.session.Session, optional) – The session object to use for querying the database. If omitted, the explicitly initialized default session factory is used.

  • output_format ({'dataframe','json','csv'}) – Output format. If ‘dataframe’, returns a DataFrame; for ‘json’/’csv’ returns a string unless output_file is provided (then returns None).

  • output_file (str | None) – Optional path to write the result to disk.

  • drop_na (bool) – If True, drops rows with missing values. Defaults to True.

  • batch_size (int) – Streaming batch size for DB row iteration. Defaults to 1000.

Returns:

DataFrame or serialized string depending on output_format; None when writing to output_file.

Return type:

pandas.DataFrame | str | None

OCDocker.DB.DB.export_table_to_csv(model, filename, session=None, batch_size=1000)[source]

Export a single ORM model’s rows to CSV.

Parameters:
  • model (type[Base]) – ORM model class to export.

  • filename (str) – Output CSV file path.

  • session (sqlalchemy.orm.session.Session, optional) – SQLAlchemy session bound to the database engine. If omitted, the explicitly initialized default session factory is used.

  • batch_size (int) – Streaming batch size for DB row iteration. Defaults to 1000.

Return type:

None

OCDocker.DB.DB.setup_database(url=None, *, create_if_missing=False, engine=None)[source]

Ensure the database exists, create a new Engine, and create tables.

Parameters:
  • url (Any, optional) – Explicit SQLAlchemy URL/string. If omitted, resolves from explicitly initialized OCDocker.Initialise state or falls back to SQLite memory.

  • create_if_missing (bool, optional) – Explicitly create a missing PostgreSQL/MySQL database.

  • engine (Engine, optional) – Existing engine to bind tables to.

Returns:

Live engine connected to the configured database URL.

Return type:

sqlalchemy.engine.base.Engine