Source code for app.model.orm.community

from typing import List

import sqlalchemy as sql
from sqlalchemy.orm import (
    mapped_column,
    relationship,
    Mapped,
)

from app.model.orm.orm_base import OrmBase


[docs] class Community(OrmBase): "A collection of strains measured in a particular study"
[docs] __tablename__ = "Communities"
[docs] id: Mapped[int] = mapped_column(primary_key=True)
[docs] name: Mapped[str] = mapped_column(sql.String(100), nullable=False)
# Note: convert to studyUniqueID or delete
[docs] studyId: Mapped[str] = mapped_column(sql.ForeignKey('Studies.publicId'), nullable=False)
[docs] study: Mapped['Study'] = relationship(back_populates='communities')
[docs] experiments: Mapped[List['Experiment']] = relationship(back_populates='community')
[docs] communityStrains: Mapped[List['CommunityStrain']] = relationship( back_populates='community', cascade='all, delete-orphan', )
[docs] strains: Mapped[List['StudyStrain']] = relationship( secondary='CommunityStrains', viewonly=True, )
[docs] def diff(self, other): strains = frozenset(self.strains) other_strains = frozenset(other.strains) return { 'added': other_strains - strains, 'removed': strains - other_strains, }