diff --git a/docs/guide/models.ipynb b/docs/guide/tables.ipynb similarity index 63% rename from docs/guide/models.ipynb rename to docs/guide/tables.ipynb index 021bacd..238e9b9 100644 --- a/docs/guide/models.ipynb +++ b/docs/guide/tables.ipynb @@ -5,7 +5,7 @@ "id": "2ee23aa0-22e1-45ae-8fe4-392702f95de5", "metadata": {}, "source": [ - "# Models" + "# Tables" ] }, { @@ -16,45 +16,12 @@ "outputs": [], "source": [ "from lnschema_bionty import (\n", - " featureset,\n", - " featureset_gene,\n", - " featureset_protein,\n", - " gene,\n", - " protein,\n", - " species,\n", + " Gene,\n", + " Protein,\n", + " Species,\n", ")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "f62cc02e-c0a9-4f55-b806-ec851659d56d", - "metadata": {}, - "outputs": [], - "source": [ - "featureset()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "89141bf7-4d8c-4baa-a15d-a76f829097e0", - "metadata": {}, - "outputs": [], - "source": [ - "featureset_gene()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "86ab44aa-0c5f-4b67-9196-fda8e0b6bb3b", - "metadata": {}, - "outputs": [], - "source": [ - "featureset_protein()" - ] - }, { "cell_type": "code", "execution_count": null, @@ -62,7 +29,7 @@ "metadata": {}, "outputs": [], "source": [ - "gene()" + "Gene()" ] }, { @@ -72,7 +39,7 @@ "metadata": {}, "outputs": [], "source": [ - "protein()" + "Protein()" ] }, { @@ -82,7 +49,7 @@ "metadata": {}, "outputs": [], "source": [ - "species()" + "Species()" ] } ], diff --git a/lnschema_bionty/__init__.py b/lnschema_bionty/__init__.py index f0fb2f8..37fa0c3 100644 --- a/lnschema_bionty/__init__.py +++ b/lnschema_bionty/__init__.py @@ -1,4 +1,4 @@ -"""Schema module for universal biological entities (`zdno`). +"""Biological entities (`zdno`). Import the package:: @@ -9,58 +9,50 @@ .. autosummary:: :toctree: . - gene - protein - cell_marker - cell_type - species - tissue - disease - featureset + Gene + Protein + CellMarker + CellType + Species + Tissue + Disease -Link tables: +Feature sets: .. autosummary:: :toctree: . - featureset_gene - featureset_protein - featureset_cell_marker + Featureset + FeaturesetGene + FeaturesetProtein + FeaturesetCellMarker -Tracking versions & migrations: +Development tools: .. autosummary:: :toctree: . - version_zdno - migration_zdno - -Auxiliary modules: - -.. autosummary:: - :toctree: . - - id + dev """ # This is lnschema-module zdno. _schema_id = "zdno" +_name = "bionty" _migration = "267d12e6f6f1" __version__ = "0.4.5" -from . import id # noqa +from . import dev # noqa from ._core import ( # noqa - cell_marker, - cell_type, - disease, - featureset, - featureset_cell_marker, - featureset_gene, - featureset_protein, - gene, - migration_zdno, - protein, - species, - tissue, - version_zdno, + CellMarker, + CellType, + Disease, + Featureset, + FeaturesetCellMarker, + FeaturesetGene, + FeaturesetProtein, + Gene, + Protein, + Species, + Tissue, ) +from .dev import id # backward compat diff --git a/lnschema_bionty/_core.py b/lnschema_bionty/_core.py index f015825..8416c14 100644 --- a/lnschema_bionty/_core.py +++ b/lnschema_bionty/_core.py @@ -1,13 +1,15 @@ -from datetime import datetime as datetime from typing import Optional # noqa -from lnschema_core._timestamps import CreatedAt -from sqlmodel import Field, SQLModel +from lnschema_core.dev.sqlmodel import schema_sqlmodel +from sqlmodel import Field -from . import id as idg +from . import _name as schema_name +from .dev import id as idg +SQLModel, prefix, schema_arg = schema_sqlmodel(schema_name) -class species(SQLModel, table=True): # type: ignore + +class Species(SQLModel, table=True): # type: ignore """Species.""" id: str = Field(default_factory=idg.species, primary_key=True) @@ -17,28 +19,34 @@ class species(SQLModel, table=True): # type: ignore short_name: Optional[str] = None -class featureset_gene(SQLModel, table=True): # type: ignore +class FeaturesetGene(SQLModel, table=True): # type: ignore """Link table.""" - featureset_id: str = Field(foreign_key="featureset.id", primary_key=True) - gene_id: str = Field(foreign_key="gene.id", primary_key=True) + __tablename__ = "{prefix}featureset_gene" + + featureset_id: str = Field(foreign_key="bionty.featureset.id", primary_key=True) + gene_id: str = Field(foreign_key="bionty.gene.id", primary_key=True) -class featureset_protein(SQLModel, table=True): # type: ignore +class FeaturesetProtein(SQLModel, table=True): # type: ignore """Link table.""" - featureset_id: str = Field(foreign_key="featureset.id", primary_key=True) - protein_id: str = Field(foreign_key="protein.id", primary_key=True) + __tablename__ = "{prefix}featureset_protein" + featureset_id: str = Field(foreign_key="bionty.featureset.id", primary_key=True) + protein_id: str = Field(foreign_key="bionty.protein.id", primary_key=True) -class featureset_cell_marker(SQLModel, table=True): # type: ignore + +class FeaturesetCellMarker(SQLModel, table=True): # type: ignore """Link table.""" - featureset_id: str = Field(foreign_key="featureset.id", primary_key=True) - cell_marker_id: str = Field(foreign_key="cell_marker.id", primary_key=True) + __tablename__ = "{prefix}featureset_cell_marker" + + featureset_id: str = Field(foreign_key="bionty.featureset.id", primary_key=True) + cell_marker_id: str = Field(foreign_key="bionty.cell_marker.id", primary_key=True) -class featureset(SQLModel, table=True): # type: ignore +class Featureset(SQLModel, table=True): # type: ignore """Sets of biological features. See the corresponding link tables. @@ -49,7 +57,7 @@ class featureset(SQLModel, table=True): # type: ignore name: str = Field(default=None, unique=True) -class gene(SQLModel, table=True): # type: ignore +class Gene(SQLModel, table=True): # type: ignore """Genes.""" id: str = Field(default_factory=idg.gene, primary_key=True) @@ -63,12 +71,12 @@ class gene(SQLModel, table=True): # type: ignore omim_id: Optional[int] = Field(default=None, index=True) synonyms: Optional[str] = Field(default=None, index=True) species_id: Optional[str] = Field( - default=None, foreign_key="species.id", index=True + default=None, foreign_key="bionty.species.id", index=True ) version: Optional[str] = None -class protein(SQLModel, table=True): # type: ignore +class Protein(SQLModel, table=True): # type: ignore """Proteins.""" id: str = Field(default_factory=idg.protein, primary_key=True) @@ -77,14 +85,14 @@ class protein(SQLModel, table=True): # type: ignore uniprotkb_name: str = Field(default=None, index=True) protein_names: Optional[str] = Field(default=None, index=True) length: Optional[int] = None - species_id: str = Field(default=None, foreign_key="species.id") + species_id: str = Field(default=None, foreign_key="bionty.species.id") gene_symbols: Optional[str] = None gene_synonyms: Optional[str] = None ensembl_transcript_ids: Optional[str] = Field(default=None, index=True) ncbi_gene_ids: Optional[str] = Field(default=None, index=True) -class tissue(SQLModel, table=True): # type: ignore +class Tissue(SQLModel, table=True): # type: ignore """Tissues.""" id: str = Field(default_factory=idg.tissue, primary_key=True) @@ -92,15 +100,17 @@ class tissue(SQLModel, table=True): # type: ignore name: str = Field(default=None, index=True) -class cell_type(SQLModel, table=True): # type: ignore +class CellType(SQLModel, table=True): # type: ignore """Cell types.""" + __tablename__ = "{prefix}cell_type" + id: str = Field(default_factory=idg.cell_type, primary_key=True) ontology_id: str = Field(default=None, index=True, unique=True) name: str = Field(default=None, index=True) -class disease(SQLModel, table=True): # type: ignore +class Disease(SQLModel, table=True): # type: ignore """Diseases.""" id: str = Field(default_factory=idg.tissue, primary_key=True) @@ -108,31 +118,15 @@ class disease(SQLModel, table=True): # type: ignore name: str = Field(default=None, index=True) -class cell_marker(SQLModel, table=True): # type: ignore +class CellMarker(SQLModel, table=True): # type: ignore """Cell markers: protein complexes.""" + __tablename__ = "{prefix}cell_marker" + id: str = Field(default_factory=idg.cell_marker, primary_key=True) name: str = Field(default=None, index=True, unique=True) gene_symbols: Optional[str] = None # TODO: link table ncbi_gene_ids: Optional[str] = None # TODO: link table protein_names: Optional[str] = None # TODO: link table uniprotkb_ids: Optional[str] = None # TODO: link table - species_id: str = Field(default=None, foreign_key="species.id") - - -class version_zdno(SQLModel, table=True): # type: ignore - """Schema versions.""" - - v: str = Field(primary_key=True) - migration: Optional[str] = None - user_id: str = Field(foreign_key="user.id") - created_at: datetime = CreatedAt - - -class migration_zdno(SQLModel, table=True): # type: ignore - """Latest migration. - - This stores the reference to the latest migration script deployed. - """ - - version_num: Optional[str] = Field(primary_key=True) + species_id: str = Field(default=None, foreign_key="bionty.species.id") diff --git a/lnschema_bionty/dev/__init__.py b/lnschema_bionty/dev/__init__.py new file mode 100644 index 0000000..f6fb937 --- /dev/null +++ b/lnschema_bionty/dev/__init__.py @@ -0,0 +1,19 @@ +"""Development tools. + +Tracking versions & migrations: + +.. autosummary:: + :toctree: . + + version_zdno + migration_zdno + +Auxiliary modules: + +.. autosummary:: + :toctree: . + + id +""" +from . import id +from ._versions import migration_zdno, version_zdno diff --git a/lnschema_bionty/_id.py b/lnschema_bionty/dev/_id.py similarity index 94% rename from lnschema_bionty/_id.py rename to lnschema_bionty/dev/_id.py index 09712a9..6c8a05d 100644 --- a/lnschema_bionty/_id.py +++ b/lnschema_bionty/dev/_id.py @@ -1,4 +1,4 @@ -from lnschema_core.id import base62 +from lnschema_core.dev.id import base62 def featureset() -> str: diff --git a/lnschema_bionty/dev/_versions.py b/lnschema_bionty/dev/_versions.py new file mode 100644 index 0000000..04f840f --- /dev/null +++ b/lnschema_bionty/dev/_versions.py @@ -0,0 +1,24 @@ +from datetime import datetime as datetime +from typing import Optional + +from lnschema_core._timestamps import CreatedAt +from lnschema_core._users import CreatedBy +from sqlmodel import Field, SQLModel + + +class version_zdno(SQLModel, table=True): # type: ignore + """Schema versions.""" + + v: str = Field(primary_key=True) + migration: Optional[str] = None + user_id: str = CreatedBy + created_at: datetime = CreatedAt + + +class migration_zdno(SQLModel, table=True): # type: ignore + """Latest migration. + + This stores the reference to the latest migration script deployed. + """ + + version_num: Optional[str] = Field(primary_key=True) diff --git a/lnschema_bionty/id.py b/lnschema_bionty/dev/id.py similarity index 100% rename from lnschema_bionty/id.py rename to lnschema_bionty/dev/id.py diff --git a/lnschema_bionty/migrations/versions/2022-11-03-afda12fc80a8-v0_5_0.py b/lnschema_bionty/migrations/versions/2022-11-03-afda12fc80a8-v0_5_0.py new file mode 100644 index 0000000..798ff88 --- /dev/null +++ b/lnschema_bionty/migrations/versions/2022-11-03-afda12fc80a8-v0_5_0.py @@ -0,0 +1,55 @@ +"""v0.5.0. + +Revision ID: 98da12fc80a8 +Revises: 267d12e6f6f1 +Create Date: 2022-10-31 +""" +import sqlalchemy as sa # noqa +import sqlmodel # noqa +from alembic import op + +revision = "afda12fc80a8" +down_revision = "267d12e6f6f1" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + bind = op.get_bind() + if bind.engine.name == "sqlite": + op.rename_table(old_table_name="gene", new_table_name="bionty.gene") + op.rename_table(old_table_name="protein", new_table_name="bionty.protein") + op.rename_table( + old_table_name="cell_marker", new_table_name="bionty.cell_marker" + ) + op.rename_table(old_table_name="cell_type", new_table_name="bionty.cell_type") + op.rename_table(old_table_name="species", new_table_name="bionty.species") + op.rename_table(old_table_name="tissue", new_table_name="bionty.tissue") + op.rename_table(old_table_name="disease", new_table_name="bionty.disease") + op.rename_table(old_table_name="featureset", new_table_name="bionty.featureset") + op.rename_table( + old_table_name="featureset_gene", new_table_name="bionty.featureset_gene" + ) + op.rename_table( + old_table_name="featureset_protein", + new_table_name="bionty.featureset_protein", + ) + op.rename_table( + old_table_name="featureset_cell_marker", + new_table_name="bionty.featureset_cell_marker", + ) + else: + op.execute("alter table public.gene set schema bionty") + op.execute("alter table public.protein set schema bionty") + op.execute("alter table public.cell_marker set schema bionty") + op.execute("alter table public.species set schema bionty") + op.execute("alter table public.tissue set schema bionty") + op.execute("alter table public.disease set schema bionty") + op.execute("alter table public.featureset set schema bionty") + op.execute("alter table public.featureset_gene set schema bionty") + op.execute("alter table public.featureset_protein set schema bionty") + op.execute("alter table public.featureset_cell_marker set schema bionty") + + +def downgrade() -> None: + pass diff --git a/tests/testdb.lndb b/tests/testdb.lndb index 653c9f7..ee563ef 100644 Binary files a/tests/testdb.lndb and b/tests/testdb.lndb differ