Skip to content

Commit

Permalink
fix circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrOertlin committed Mar 18, 2024
1 parent 916b323 commit b3d5a2f
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 142 deletions.
3 changes: 2 additions & 1 deletion genotype_api/api/endpoints/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
get_analyses_with_skip_and_limit,
)
from genotype_api.database.crud.update import refresh_sample_status
from genotype_api.database.models import Analysis, AnalysisRead, AnalysisReadWithGenotype, User
from genotype_api.database.models import Analysis, User
from genotype_api.dto.dto import AnalysisRead, AnalysisReadWithGenotype
from genotype_api.database.session_handler import get_session
from genotype_api.file_parsing.files import check_file
from genotype_api.file_parsing.vcf import SequenceAnalysis
Expand Down
4 changes: 3 additions & 1 deletion genotype_api/api/endpoints/plates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
from genotype_api.database.models import (
Analysis,
Plate,
User,
)
from genotype_api.dto.dto import (
PlateCreate,
PlateReadWithAnalyses,
PlateReadWithAnalysisDetail,
PlateReadWithAnalysisDetailSingle,
User,
)
from genotype_api.database.session_handler import get_session
from genotype_api.file_parsing.excel import GenotypeAnalysis
Expand Down
3 changes: 1 addition & 2 deletions genotype_api/api/endpoints/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
from genotype_api.database.models import (
Analysis,
Sample,
SampleRead,
SampleReadWithAnalysisDeep,
User,
)
from genotype_api.dto.dto import SampleRead, SampleReadWithAnalysisDeep
from genotype_api.database.session_handler import get_session
from genotype_api.models import MatchResult, SampleDetail
from genotype_api.security import get_active_user
Expand Down
3 changes: 2 additions & 1 deletion genotype_api/api/endpoints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from starlette.responses import JSONResponse

from genotype_api.database.crud.read import get_user
from genotype_api.database.models import User, UserCreate, UserRead, UserReadWithPlates
from genotype_api.database.models import User
from genotype_api.dto.dto import UserRead, UserCreate, UserReadWithPlates
from genotype_api.database.session_handler import get_session
from genotype_api.security import get_active_user

Expand Down
3 changes: 2 additions & 1 deletion genotype_api/database/crud/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from sqlmodel import Session
from sqlmodel.sql.expression import Select, SelectOfScalar

from genotype_api.database.models import Analysis, Plate, PlateCreate, Sample, User, UserCreate
from genotype_api.database.models import Analysis, Plate, Sample, User
from genotype_api.dto.dto import UserCreate, PlateCreate

SelectOfScalar.inherit_cache = True
Select.inherit_cache = True
Expand Down
2 changes: 1 addition & 1 deletion genotype_api/database/crud/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlmodel import Session
from sqlmodel.sql.expression import Select, SelectOfScalar

from genotype_api.database.models import Analysis, Plate
from genotype_api.database.models import Analysis, Plate, Sample

SelectOfScalar.inherit_cache = True
Select.inherit_cache = True
Expand Down
2 changes: 1 addition & 1 deletion genotype_api/database/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
Plate,
Sample,
User,
PlateReadWithAnalysisDetailSingle,
)
from genotype_api.dto.dto import PlateReadWithAnalysisDetailSingle

SelectOfScalar.inherit_cache = True
Select.inherit_cache = True
Expand Down
5 changes: 3 additions & 2 deletions genotype_api/database/crud/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from genotype_api.database.crud.read import get_sample
from genotype_api.database.filter_models.plate_models import PlateSignOff
from genotype_api.database.filter_models.sample_models import SampleSexesUpdate
from genotype_api.match import check_sample
from genotype_api.database.models import Sample, Plate
from sqlmodel.sql.expression import Select, SelectOfScalar

from genotype_api.service.match_genotype_service.match_genotype import MatchGenotypeService

SelectOfScalar.inherit_cache = True
Select.inherit_cache = True

Expand All @@ -16,7 +17,7 @@ def refresh_sample_status(sample: Sample, session: Session) -> Sample:
if len(sample.analyses) != 2:
sample.status = None
else:
results = check_sample(sample=sample)
results = MatchGenotypeService.check_sample(sample=sample)
sample.status = "fail" if "fail" in results.dict().values() else "pass"

session.add(sample)
Expand Down
131 changes: 1 addition & 130 deletions genotype_api/database/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from collections import Counter
from datetime import datetime

from pydantic import EmailStr, constr, validator
from pydantic import EmailStr, constr
from sqlalchemy import Index
from sqlmodel import Field, Relationship, SQLModel

from genotype_api.constants import SEXES, STATUS, TYPES
from genotype_api.models import PlateStatusCounts, SampleDetail
from genotype_api.service.match_genotype_service.utils import check_snps, check_sex


class GenotypeBase(SQLModel):
Expand Down Expand Up @@ -36,14 +34,6 @@ def is_ok(self) -> bool:
return "0" not in self.alleles


class GenotypeRead(GenotypeBase):
id: int


class GenotypeCreate(GenotypeBase):
pass


class AnalysisBase(SQLModel):
type: TYPES
source: str | None
Expand All @@ -68,14 +58,6 @@ def check_no_calls(self) -> dict[str, int]:
return Counter(calls)


class AnalysisRead(AnalysisBase):
id: int


class AnalysisCreate(AnalysisBase):
pass


class SampleSlim(SQLModel):
status: STATUS | None
comment: str | None
Expand Down Expand Up @@ -113,14 +95,6 @@ def sequence_analysis(self) -> Analysis | None:
return None


class SampleRead(SampleBase):
id: constr(max_length=32)


class SampleCreate(SampleBase):
pass


class SNPBase(SQLModel):
ref: constr(max_length=1) | None
chrom: constr(max_length=5) | None
Expand All @@ -134,10 +108,6 @@ class SNP(SNPBase, table=True):
id: constr(max_length=32) | None = Field(default=None, primary_key=True)


class SNPRead(SNPBase):
id: constr(max_length=32)


class UserBase(SQLModel):
email: EmailStr = Field(index=True, unique=True)
name: str | None = ""
Expand All @@ -149,14 +119,6 @@ class User(UserBase, table=True):
plates: list["Plate"] = Relationship(back_populates="user")


class UserRead(UserBase):
id: int


class UserCreate(UserBase):
pass


class PlateBase(SQLModel):
created_at: datetime | None = datetime.now()
plate_id: constr(max_length=16) = Field(index=True, unique=True)
Expand All @@ -171,94 +133,3 @@ class Plate(PlateBase, table=True):
id: int | None = Field(default=None, primary_key=True)
user: "User" = Relationship(back_populates="plates")
analyses: list["Analysis"] = Relationship(back_populates="plate")


class PlateRead(PlateBase):
id: str
user: UserRead | None


class PlateCreate(PlateBase):
analyses: list[Analysis] | None = []


class UserReadWithPlates(UserRead):
plates: list[Plate] | None = []


class SampleReadWithAnalysis(SampleRead):
analyses: list[AnalysisRead] | None = []


class AnalysisReadWithGenotype(AnalysisRead):
genotypes: list[Genotype] | None = []


class SampleReadWithAnalysisDeep(SampleRead):
analyses: list[AnalysisReadWithGenotype] | None = []
detail: SampleDetail | None

@validator("detail")
def get_detail(cls, value, values) -> SampleDetail:
analyses = values.get("analyses")
if len(analyses) != 2:
return SampleDetail()
genotype_analysis = [analysis for analysis in analyses if analysis.type == "genotype"][0]
sequence_analysis = [analysis for analysis in analyses if analysis.type == "sequence"][0]
status = check_snps(
genotype_analysis=genotype_analysis, sequence_analysis=sequence_analysis
)
sex = check_sex(
sample_sex=values.get("sex"),
genotype_analysis=genotype_analysis,
sequence_analysis=sequence_analysis,
)

return SampleDetail(**status, sex=sex)

class Config:
validate_all = True


class AnalysisReadWithSample(AnalysisRead):
sample: SampleSlim | None


class AnalysisReadWithSampleDeep(AnalysisRead):
sample: SampleReadWithAnalysisDeep | None


class PlateReadWithAnalyses(PlateRead):
analyses: list[AnalysisReadWithSample] | None = []


class PlateReadWithAnalysisDetail(PlateRead):
analyses: list[AnalysisReadWithSample] | None = []
detail: PlateStatusCounts | None

@validator("detail")
def check_detail(cls, value, values):
analyses = values.get("analyses")
statuses = [str(analysis.sample.status) for analysis in analyses]
commented = sum(1 for analysis in analyses if analysis.sample.comment)
status_counts = Counter(statuses)
return PlateStatusCounts(**status_counts, total=len(analyses), commented=commented)

class Config:
validate_all = True


class PlateReadWithAnalysisDetailSingle(PlateRead):
analyses: list[AnalysisReadWithSample] | None = []
detail: PlateStatusCounts | None

@validator("detail")
def check_detail(cls, value, values):
analyses = values.get("analyses")
statuses = [str(analysis.sample.status) for analysis in analyses]
commented = sum(1 for analysis in analyses if analysis.sample.comment)
status_counts = Counter(statuses)
return PlateStatusCounts(**status_counts, total=len(analyses), commented=commented)

class Config:
validate_all = True
Loading

0 comments on commit b3d5a2f

Please sign in to comment.