Skip to content

Commit 231ed67

Browse files
committed
Initial implementation of brain-atlas endpoints
See #168 for the specification. Briefly; a `BrainAtlas` is an named entity that has an asset `annotation.nrrd` attached to it. It is associated with a particular `BrainRegionHierarchy` In addition, `BrainAtlasRegion` gives metadata for all the regions within a `BrainAtlas`; * their volume in the `annotation.nrrd` if they are a leaf, -1 otherwise. * an attached asset with the .obj mesh
1 parent 9061703 commit 231ed67

18 files changed

+2152
-620
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
"""brainatlas
2+
3+
Revision ID: f750b8010fb9
4+
Revises: 719dc38efbd6
5+
Create Date: 2025-05-23 08:56:31.033885
6+
7+
"""
8+
9+
from typing import Sequence, Union
10+
11+
from alembic import op
12+
import sqlalchemy as sa
13+
from alembic_postgresql_enum import TableReference
14+
from alembic_utils.pg_trigger import PGTrigger
15+
from sqlalchemy import text as sql_text
16+
from sqlalchemy.dialects import postgresql
17+
18+
from sqlalchemy import Text
19+
import app.db.types
20+
21+
# revision identifiers, used by Alembic.
22+
revision: str = "f750b8010fb9"
23+
down_revision: Union[str, None] = "719dc38efbd6"
24+
branch_labels: Union[str, Sequence[str], None] = None
25+
depends_on: Union[str, Sequence[str], None] = None
26+
27+
28+
def upgrade() -> None:
29+
# ### commands auto generated by Alembic - please adjust! ###
30+
op.create_table(
31+
"brain_atlas_region",
32+
sa.Column("id", sa.Uuid(), nullable=False),
33+
sa.Column("volume", sa.Float(), nullable=True),
34+
sa.Column("leaf_region", sa.Boolean(), nullable=False),
35+
sa.Column("brain_atlas_id", sa.Uuid(), nullable=False),
36+
sa.Column("brain_region_id", sa.Uuid(), nullable=False),
37+
sa.ForeignKeyConstraint(
38+
["brain_atlas_id"],
39+
["brain_atlas.id"],
40+
name=op.f("fk_brain_atlas_region_brain_atlas_id_brain_atlas"),
41+
),
42+
sa.ForeignKeyConstraint(
43+
["brain_region_id"],
44+
["brain_region.id"],
45+
name=op.f("fk_brain_atlas_region_brain_region_id_brain_region"),
46+
),
47+
sa.ForeignKeyConstraint(
48+
["id"], ["entity.id"], name=op.f("fk_brain_atlas_region_id_entity")
49+
),
50+
sa.PrimaryKeyConstraint("id", name=op.f("pk_brain_atlas_region")),
51+
)
52+
op.create_index(
53+
op.f("ix_brain_atlas_region_brain_atlas_id"),
54+
"brain_atlas_region",
55+
["brain_atlas_id"],
56+
unique=False,
57+
)
58+
op.create_index(
59+
op.f("ix_brain_atlas_region_brain_region_id"),
60+
"brain_atlas_region",
61+
["brain_region_id"],
62+
unique=False,
63+
)
64+
op.drop_index("ix_mesh_brain_region_id", table_name="mesh")
65+
op.drop_index("ix_mesh_description_vector", table_name="mesh", postgresql_using="gin")
66+
op.drop_index("ix_mesh_name", table_name="mesh")
67+
op.drop_table("mesh")
68+
op.add_column("brain_atlas", sa.Column("hierarchy_id", sa.Uuid(), nullable=False))
69+
op.drop_index("ix_brain_atlas_brain_region_id", table_name="brain_atlas")
70+
op.create_index(
71+
op.f("ix_brain_atlas_hierarchy_id"), "brain_atlas", ["hierarchy_id"], unique=False
72+
)
73+
op.drop_constraint(
74+
"fk_brain_atlas_brain_region_id_brain_region", "brain_atlas", type_="foreignkey"
75+
)
76+
op.create_foreign_key(
77+
op.f("fk_brain_atlas_hierarchy_id_brain_region_hierarchy"),
78+
"brain_atlas",
79+
"brain_region_hierarchy",
80+
["hierarchy_id"],
81+
["id"],
82+
)
83+
op.drop_column("brain_atlas", "brain_region_id")
84+
op.sync_enum_values(
85+
enum_schema="public",
86+
enum_name="entitytype",
87+
new_values=[
88+
"analysis_software_source_code",
89+
"brain_atlas",
90+
"brain_atlas_region",
91+
"emodel",
92+
"cell_composition",
93+
"experimental_bouton_density",
94+
"experimental_neuron_density",
95+
"experimental_synapses_per_connection",
96+
"memodel",
97+
"mesh",
98+
"me_type_density",
99+
"reconstruction_morphology",
100+
"electrical_cell_recording",
101+
"electrical_recording_stimulus",
102+
"single_neuron_simulation",
103+
"single_neuron_synaptome",
104+
"single_neuron_synaptome_simulation",
105+
"ion_channel_model",
106+
"subject",
107+
"validation_result",
108+
],
109+
affected_columns=[
110+
TableReference(table_schema="public", table_name="entity", column_name="type")
111+
],
112+
enum_values_to_rename=[],
113+
)
114+
# ### end Alembic commands ###
115+
116+
117+
def downgrade() -> None:
118+
# ### commands auto generated by Alembic - please adjust! ###
119+
public_mesh_mesh_description_vector = PGTrigger(
120+
schema="public",
121+
signature="mesh_description_vector",
122+
on_entity="public.mesh",
123+
is_constraint=False,
124+
definition="BEFORE INSERT OR UPDATE ON public.mesh FOR EACH ROW EXECUTE FUNCTION tsvector_update_trigger('description_vector', 'pg_catalog.english', 'description', 'name')",
125+
)
126+
op.create_entity(public_mesh_mesh_description_vector)
127+
128+
op.sync_enum_values(
129+
enum_schema="public",
130+
enum_name="entitytype",
131+
new_values=[
132+
"analysis_software_source_code",
133+
"brain_atlas",
134+
"emodel",
135+
"cell_composition",
136+
"experimental_bouton_density",
137+
"experimental_neuron_density",
138+
"experimental_synapses_per_connection",
139+
"memodel",
140+
"mesh",
141+
"me_type_density",
142+
"reconstruction_morphology",
143+
"electrical_cell_recording",
144+
"electrical_recording_stimulus",
145+
"single_neuron_simulation",
146+
"single_neuron_synaptome",
147+
"single_neuron_synaptome_simulation",
148+
"ion_channel_model",
149+
"subject",
150+
"validation_result",
151+
],
152+
affected_columns=[
153+
TableReference(table_schema="public", table_name="entity", column_name="type")
154+
],
155+
enum_values_to_rename=[],
156+
)
157+
op.add_column(
158+
"brain_atlas", sa.Column("brain_region_id", sa.UUID(), autoincrement=False, nullable=False)
159+
)
160+
op.drop_constraint(
161+
op.f("fk_brain_atlas_hierarchy_id_brain_region_hierarchy"),
162+
"brain_atlas",
163+
type_="foreignkey",
164+
)
165+
op.create_foreign_key(
166+
"fk_brain_atlas_brain_region_id_brain_region",
167+
"brain_atlas",
168+
"brain_region",
169+
["brain_region_id"],
170+
["id"],
171+
)
172+
op.drop_index(op.f("ix_brain_atlas_hierarchy_id"), table_name="brain_atlas")
173+
op.create_index(
174+
"ix_brain_atlas_brain_region_id", "brain_atlas", ["brain_region_id"], unique=False
175+
)
176+
op.drop_column("brain_atlas", "hierarchy_id")
177+
op.create_table(
178+
"mesh",
179+
sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
180+
sa.Column("brain_region_id", sa.UUID(), autoincrement=False, nullable=False),
181+
sa.Column("name", sa.VARCHAR(), autoincrement=False, nullable=False),
182+
sa.Column("description", sa.VARCHAR(), autoincrement=False, nullable=False),
183+
sa.Column("description_vector", postgresql.TSVECTOR(), autoincrement=False, nullable=True),
184+
sa.ForeignKeyConstraint(
185+
["brain_region_id"], ["brain_region.id"], name="fk_mesh_brain_region_id_brain_region"
186+
),
187+
sa.ForeignKeyConstraint(["id"], ["entity.id"], name="fk_mesh_id_entity"),
188+
sa.PrimaryKeyConstraint("id", name="pk_mesh"),
189+
)
190+
op.create_index("ix_mesh_name", "mesh", ["name"], unique=False)
191+
op.create_index(
192+
"ix_mesh_description_vector",
193+
"mesh",
194+
["description_vector"],
195+
unique=False,
196+
postgresql_using="gin",
197+
)
198+
op.create_index("ix_mesh_brain_region_id", "mesh", ["brain_region_id"], unique=False)
199+
op.drop_index(op.f("ix_brain_atlas_region_brain_region_id"), table_name="brain_atlas_region")
200+
op.drop_index(op.f("ix_brain_atlas_region_brain_atlas_id"), table_name="brain_atlas_region")
201+
op.drop_table("brain_atlas_region")
202+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)