Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
🎨 Modularize and capitalize (#24)
Browse files Browse the repository at this point in the history
* 🎨 Modularize and capitalize

* 🍱 Add a migration script

* 💚 Apply migration & fix

* 📝 Fix nb
  • Loading branch information
falexwolf authored Nov 3, 2022
1 parent c0b54bf commit 70483b8
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 120 deletions.
47 changes: 7 additions & 40 deletions docs/guide/models.ipynb → docs/guide/tables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "2ee23aa0-22e1-45ae-8fe4-392702f95de5",
"metadata": {},
"source": [
"# Models"
"# Tables"
]
},
{
Expand All @@ -16,53 +16,20 @@
"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,
"id": "06582ada",
"metadata": {},
"outputs": [],
"source": [
"gene()"
"Gene()"
]
},
{
Expand All @@ -72,7 +39,7 @@
"metadata": {},
"outputs": [],
"source": [
"protein()"
"Protein()"
]
},
{
Expand All @@ -82,7 +49,7 @@
"metadata": {},
"outputs": [],
"source": [
"species()"
"Species()"
]
}
],
Expand Down
66 changes: 29 additions & 37 deletions lnschema_bionty/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Schema module for universal biological entities (`zdno`).
"""Biological entities (`zdno`).
Import the package::
Expand All @@ -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
78 changes: 36 additions & 42 deletions lnschema_bionty/_core.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -77,62 +85,48 @@ 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)
ontology_id: Optional[str] = Field(default=None, index=True, unique=True)
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)
ontology_id: str = Field(default=None, index=True, unique=True)
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")
19 changes: 19 additions & 0 deletions lnschema_bionty/dev/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lnschema_bionty/_id.py → lnschema_bionty/dev/_id.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lnschema_core.id import base62
from lnschema_core.dev.id import base62


def featureset() -> str:
Expand Down
24 changes: 24 additions & 0 deletions lnschema_bionty/dev/_versions.py
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
Loading

0 comments on commit 70483b8

Please sign in to comment.