Skip to content

Commit

Permalink
added license table to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed May 29, 2024
1 parent 6d60e2b commit 64bb042
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bbconf/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

POSTGRES_DIALECT = "postgresql+psycopg"

# set if the db schema was created
first_db_init: bool = False


class SchemaError(Exception):
def __init__(self):
Expand Down Expand Up @@ -49,8 +52,10 @@ def receive_after_create(target, connection, tables, **kw):
"""
listen for the 'after_create' event
"""
global first_db_init
if tables:
_LOGGER.info("A table was created")
first_db_init = True
else:
_LOGGER.info("A table was not created")

Expand Down Expand Up @@ -103,6 +108,10 @@ class Bed(Base):
tokenized: Mapped["TokenizedBed"] = relationship(
back_populates="bed", cascade="all, delete-orphan"
)
license: Mapped["str"] = mapped_column(
ForeignKey("licenses.id", ondelete="CASCADE"), nullable=True, index=True
)
license_map: Mapped["License"] = relationship("License", back_populates="bed")


class BedStats(Base):
Expand Down Expand Up @@ -260,6 +269,19 @@ class TokenizedBed(Base):
)


class License(Base):
__tablename__ = "licenses"

id: Mapped[str] = mapped_column(primary_key=True, index=True)
shorthand: Mapped[str] = mapped_column(nullable=True, comment="License shorthand")
label: Mapped[str] = mapped_column(nullable=False, comment="License label")
description: Mapped[str] = mapped_column(
nullable=False, comment="License description"
)

bed: Mapped[List["Bed"]] = relationship("Bed", back_populates="license_map")


@listens_for(Universes, "after_insert")
@listens_for(Universes, "after_update")
def add_bed_universe(mapper, connection, target):
Expand Down Expand Up @@ -331,6 +353,11 @@ def create_schema(self, engine=None):
if not engine:
engine = self._engine
Base.metadata.create_all(engine)

global first_db_init
if first_db_init:
self._upload_licenses()

return None

def delete_schema(self, engine=None) -> None:
Expand Down Expand Up @@ -398,3 +425,17 @@ def create_schema_graph(self, output_file: str = "schema.svg"):
graph = create_schema_graph(engine=self.engine, metadata=Base.metadata)
graph.write(output_file, format="svg", prog="dot")
return None

def _upload_licenses(self):
"""
Upload licenses to the database.
"""

import pandas as pd

csv_file_path = "https://raw.githubusercontent.com/EBISPOT/DUO/master/duo.csv"
df = pd.read_csv(csv_file_path)

with Session(self.engine) as session:
df.to_sql("licenses", self.engine, if_exists="append", index=False)
session.commit()
1 change: 1 addition & 0 deletions requirements/requirements-all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pyyaml >= 6.0.1 # for s3fs because of the errors
s3fs >= 2024.3.1
# quick fix for gesnim
scipy <= 1.11.0
pandas

0 comments on commit 64bb042

Please sign in to comment.