Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nmdc_server/attribute_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def get_attribute_units(table: str, attribute: str) -> Optional[PlainUnit]:
return _unit_info.get(table, {}).get(attribute)


def extract_quantity(obj: dict, table: str, attribute: str) -> Optional[float]:
def extract_quantity(obj: dict, table: str, attribute: str, value_field: str = "has_numeric_value", ) -> Optional[float]:
"""Extract units from https://microbiomedata.github.io/nmdc-schema/QuantityValue/"""
expected_units = get_attribute_units(table, attribute)
value = obj.get("has_numeric_value", None)
value = obj.get(value_field, None)
units = obj.get("has_unit", None)
if value is None:
return None
Expand Down
9 changes: 8 additions & 1 deletion nmdc_server/ingest/biosample.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ def load_biosample(db: Session, obj: Dict[str, Any]):

obj["study_id"] = associated_studies[0]
depth_obj = obj.get("depth", {})
obj["depth"] = extract_quantity(depth_obj, "biosample", "depth")

# Handle depth, which may be a range or a single value
if "has_numeric_value" in depth_obj:
obj["depth_min"] = extract_quantity(depth_obj, "biosample", "depth", "has_numeric_value")
obj["depth_max"] = extract_quantity(depth_obj, "biosample", "depth", "has_numeric_value")
elif "has_minimum_numeric_value" in depth_obj and "has_maximum_numeric_value" in depth_obj:
obj["depth_min"] = extract_quantity(depth_obj, "biosample", "depth", "has_minimum_numeric_value")
obj["depth_max"] = extract_quantity(depth_obj, "biosample", "depth", "has_maximum_numeric_value")

biosample = Biosample(**obj)

Expand Down
34 changes: 34 additions & 0 deletions nmdc_server/migrations/versions/dc2a4cca7c66_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""empty message

Revision ID: dc2a4cca7c66
Revises: 9a0ff33a4e6b
Create Date: 2025-09-11 21:38:15.666750

"""
from typing import Optional

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'dc2a4cca7c66'
down_revision: Optional[str] = '9a0ff33a4e6b'
branch_labels: Optional[str] = None
depends_on: Optional[str] = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('biosample', sa.Column('depth_min', sa.Float(), nullable=True))
op.add_column('biosample', sa.Column('depth_max', sa.Float(), nullable=True))
op.drop_column('biosample', 'depth')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('biosample', sa.Column('depth', postgresql.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True))
op.drop_column('biosample', 'depth_max')
op.drop_column('biosample', 'depth_min')
# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion nmdc_server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ class Biosample(Base, AnnotatedModel):
add_date = Column(DateTime, nullable=True)
mod_date = Column(DateTime, nullable=True)
collection_date = Column(DateTime, nullable=True)
depth = Column(Float, nullable=True)
depth_min = Column(Float, nullable=True)
depth_max = Column(Float, nullable=True)
env_broad_scale_id = Column(String, ForeignKey(EnvoTerm.id), nullable=True)
env_local_scale_id = Column(String, ForeignKey(EnvoTerm.id), nullable=True)
env_medium_id = Column(String, ForeignKey(EnvoTerm.id), nullable=True)
Expand Down
Loading