divbase-lib 0.1.0.dev3__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (21) hide show
  1. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/.gitignore +25 -2
  2. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/PKG-INFO +8 -7
  3. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/pyproject.toml +6 -5
  4. divbase_lib-0.1.1/src/divbase_lib/__init__.py +1 -0
  5. divbase_lib-0.1.1/src/divbase_lib/api_schemas/announcements.py +18 -0
  6. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/api_schemas/auth.py +3 -0
  7. divbase_lib-0.1.1/src/divbase_lib/api_schemas/personal_access_tokens.py +42 -0
  8. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/api_schemas/project_versions.py +34 -6
  9. divbase_lib-0.1.1/src/divbase_lib/api_schemas/queries.py +210 -0
  10. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/api_schemas/s3.py +171 -18
  11. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/api_schemas/task_history.py +8 -7
  12. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/api_schemas/vcf_dimensions.py +27 -2
  13. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/divbase_constants.py +24 -3
  14. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/exceptions.py +42 -9
  15. divbase_lib-0.1.1/src/divbase_lib/metadata_validator.py +827 -0
  16. divbase_lib-0.1.1/src/divbase_lib/utils.py +142 -0
  17. divbase_lib-0.1.0.dev3/src/divbase_lib/__init__.py +0 -1
  18. divbase_lib-0.1.0.dev3/src/divbase_lib/api_schemas/queries.py +0 -65
  19. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/README.md +0 -0
  20. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/api_schemas/__init__.py +0 -0
  21. {divbase_lib-0.1.0.dev3 → divbase_lib-0.1.1}/src/divbase_lib/s3_checksums.py +0 -0
@@ -13,7 +13,7 @@ __pycache__/
13
13
 
14
14
  # project specific files
15
15
  /sample_metadata.tsv
16
- sample_metadata_*.tsv
16
+ /sample_metadata_*.tsv
17
17
  *.vcf
18
18
  *.vcf.gz
19
19
  *.vcf.gz.csi
@@ -21,6 +21,10 @@ sample_metadata_*.tsv
21
21
  !tests/fixtures/*.vcf.gz
22
22
  tests/fixtures/temp*
23
23
  tests/fixtures/merged*
24
+ divbase_metadata_template*.tsv
25
+ # query results files
26
+ result_of_job_*.vcf.gz
27
+ result_of_job_*.log
24
28
 
25
29
  # query job config files
26
30
  bcftools_divbase_job_config.json
@@ -33,10 +37,29 @@ split_scaffold_files.txt
33
37
  scripts/benchmarking/*.yaml
34
38
  scripts/benchmarking/results
35
39
 
40
+ # pytest-cov generate files
41
+ .coverage
42
+ .coverage.*
43
+ htmlcov/
44
+ docker/coverage-data/
45
+
46
+ # log files written by docker compose, if env var set to do so (by default no)
47
+ docker/logs/*
48
+ !docker/logs/.gitkeep
49
+
36
50
  #MacOS artifacts
37
51
  .DS_Store
38
52
 
39
- # mkdocs build cache
53
+ # mkdocs/zensical build cache
40
54
  .cache/
55
+ site/
56
+ # documentation site's auto generated files
57
+ docs/cli/_auto_generated/*.md
58
+ docs/releases/index.md
59
+ docs/llms.txt
60
+ docs/llms-full.txt
61
+ docs/llms-users.txt
62
+
63
+
41
64
  # pypi
42
65
  dist/
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: divbase-lib
3
- Version: 0.1.0.dev3
3
+ Version: 0.1.1
4
4
  Summary: Library module for Divbase
5
- Project-URL: Homepage, https://divbase.scilifelab.se
5
+ Project-URL: Homepage, https://divbase.scilifelab-2-prod.sys.kth.se
6
6
  Project-URL: Documentation, https://scilifelabdatacentre.github.io/divbase
7
7
  Project-URL: Repository, https://github.com/ScilifelabDataCentre/divbase
8
8
  Project-URL: Issues, https://github.com/ScilifelabDataCentre/divbase/issues
@@ -18,10 +18,11 @@ Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Programming Language :: Python :: 3.14
20
20
  Requires-Python: >=3.12
21
- Requires-Dist: boto3<2,>=1.42.27
22
- Requires-Dist: pandas<3,>=2.3.3
23
- Requires-Dist: pydantic<3,>=2.12.5
24
- Requires-Dist: pyyaml<7,>=6.0.3
21
+ Requires-Dist: httpx==0.28.1
22
+ Requires-Dist: pandas==2.3.3
23
+ Requires-Dist: pydantic==2.13.5
24
+ Requires-Dist: pyyaml==6.0.3
25
+ Requires-Dist: stamina==26.1.0
25
26
  Description-Content-Type: text/markdown
26
27
 
27
28
  # divbase-lib
@@ -8,10 +8,11 @@ authors = [
8
8
  ]
9
9
  requires-python = ">=3.12"
10
10
  dependencies = [
11
- "pyyaml>=6.0.3,<7",
12
- "boto3>=1.42.27,<2",
13
- "pandas>=2.3.3,<3",
14
- "pydantic>=2.12.5,<3"
11
+ "pyyaml==6.0.3",
12
+ "pandas==2.3.3", # TODO - consider major version bump
13
+ "pydantic==2.13.5",
14
+ "httpx==0.28.1",
15
+ "stamina==26.1.0"
15
16
  ]
16
17
  dynamic = ["version"]
17
18
 
@@ -28,7 +29,7 @@ classifiers = [
28
29
  ]
29
30
 
30
31
  [project.urls]
31
- Homepage = "https://divbase.scilifelab.se"
32
+ Homepage = "https://divbase.scilifelab-2-prod.sys.kth.se"
32
33
  Documentation = "https://scilifelabdatacentre.github.io/divbase"
33
34
  Repository = "https://github.com/ScilifelabDataCentre/divbase"
34
35
  Issues = "https://github.com/ScilifelabDataCentre/divbase/issues"
@@ -0,0 +1 @@
1
+ __version__ = "0.1.1"
@@ -0,0 +1,18 @@
1
+ """
2
+ Schemas for announcements.
3
+ """
4
+
5
+ from pydantic import BaseModel, ConfigDict, Field
6
+
7
+
8
+ class AnnouncementResponse(BaseModel):
9
+ """Response model for an announcement returned by the API."""
10
+
11
+ heading: str = Field(..., description="Title of the announcement.")
12
+ message: str | None = Field(None, description="Detailed message of the announcement.")
13
+ level: str = Field(
14
+ ...,
15
+ description="The announcement level, which can control styling of announcement. Possible values are: info, success, warning, danger.",
16
+ )
17
+
18
+ model_config = ConfigDict(from_attributes=True)
@@ -1,5 +1,8 @@
1
1
  """
2
2
  Schemas for login + access and refresh tokens
3
+
4
+ NOTE: Expiry fields are unix timestamps (not datetimes like the rest of the API returns).
5
+ This is to mirror how the JWT's stores the expiry, and it is convenient as the CLI stores these expiries locally as unix timestamps too.
3
6
  """
4
7
 
5
8
  from pydantic import BaseModel, Field
@@ -0,0 +1,42 @@
1
+ """
2
+ Schemas for personal access token related endpoints.
3
+ """
4
+
5
+ from pydantic import BaseModel, Field, model_validator
6
+ from typing_extensions import Self
7
+
8
+ # This needs to stay in sync with ProjectRoles in divbase-api.
9
+ # Redefined here to avoid a circular dependency on divbase-api.
10
+ ALLOWED_PROJECT_ROLES = ["read", "query", "edit", "manage"]
11
+
12
+
13
+ class PATPermissions(BaseModel):
14
+ """
15
+ Personal Access Token permissions model.
16
+ This is stored in the db as JSONB in the PersonalAccessTokenDB.permissions.
17
+
18
+ - all_projects: True = access all the user's projects with same role as user
19
+ False = access only the projects listed in `projects`.
20
+ - projects: {str(project_id): role_string}; only enforced when all_projects=False.
21
+ Where role_string must be a valid ProjectRoles enum value.
22
+ - task_history: endpoint level scope.
23
+ For the 2 task history routes not tied to a specific project.
24
+ (these are 'task-history id' and for 'task-history user')
25
+ """
26
+
27
+ all_projects: bool = False
28
+ projects: dict[str, str] = Field(default_factory=dict)
29
+ task_history: bool = False
30
+
31
+ @model_validator(mode="after")
32
+ def no_project_scope_if_all_projects_true(self) -> Self:
33
+ if self.all_projects and self.projects:
34
+ raise ValueError("cannot specify project scope if 'all_projects' is True")
35
+ return self
36
+
37
+ @model_validator(mode="after")
38
+ def validate_project_roles(self) -> Self:
39
+ for project_id, role_str in self.projects.items():
40
+ if role_str not in ALLOWED_PROJECT_ROLES:
41
+ raise ValueError(f"invalid role '{role_str}' for project {project_id}")
42
+ return self
@@ -4,6 +4,9 @@ Schemas for project versioning routes.
4
4
  Project versions are the state of all files in a project's storage bucket at a given time point.
5
5
  """
6
6
 
7
+ from datetime import datetime
8
+ from typing import TypedDict
9
+
7
10
  from pydantic import BaseModel, Field
8
11
 
9
12
 
@@ -22,33 +25,52 @@ class DeleteVersionRequest(BaseModel):
22
25
  version_name: str = Field(..., description="Name of the version to delete")
23
26
 
24
27
 
28
+ class UpdateVersionRequest(BaseModel):
29
+ version_name: str = Field(..., description="Name of the version to update")
30
+ new_name: str | None = Field(None, description="New name for the version")
31
+ new_description: str | None = Field(None, description="New description for the version")
32
+
33
+
25
34
  # Response Models
26
35
  class ProjectBasicInfo(BaseModel):
27
36
  """Base model for describing a single project version, not for direct use in an endpoint."""
28
37
 
29
38
  name: str = Field(..., description="Version name")
30
- description: str | None = Field(..., description="Version description")
39
+ description: str | None = Field(None, description="Version description")
31
40
 
32
41
 
33
42
  class AddVersionResponse(ProjectBasicInfo):
34
43
  """Response model for adding a version."""
35
44
 
36
- created_at: str = Field(..., description="ISO timestamp when version was created")
45
+ created_at: datetime = Field(..., description="When the version was created")
37
46
 
38
47
 
39
48
  class ProjectVersionInfo(ProjectBasicInfo):
40
49
  """Basic information about a project version. You get a list of these when listing all versions in a project."""
41
50
 
42
- created_at: str = Field(..., description="ISO timestamp when version was created")
51
+ created_at: datetime = Field(..., description="When the version was created")
52
+ updated_at: datetime = Field(..., description="When the version was last updated")
43
53
  is_deleted: bool = Field(..., description="Whether this version has been soft-deleted")
44
54
 
45
55
 
56
+ # Typed dict used as this is used in the db model to define the contents of the JSONB
57
+ class FileDetails(TypedDict):
58
+ """Details about a single file in a project version, ETag (is checksum) and size is in bytes."""
59
+
60
+ version_id: str
61
+ etag: str
62
+ size: int
63
+
64
+
46
65
  class ProjectVersionDetailResponse(ProjectBasicInfo):
47
66
  """Full information about a single project version, including the files at that version."""
48
67
 
49
- created_at: str = Field(..., description="ISO timestamp when version was created")
68
+ created_at: datetime = Field(..., description="When the version was created")
69
+ updated_at: datetime = Field(..., description="When the version was last updated")
50
70
  is_deleted: bool = Field(..., description="Whether this version has been soft-deleted")
51
- files: dict[str, str] = Field(..., description="Mapping of file names to their version IDs")
71
+ files: dict[str, FileDetails] = Field(
72
+ ..., description="Info about all files at this version, including: version IDs, etags and file sizes"
73
+ )
52
74
 
53
75
 
54
76
  class DeleteVersionResponse(BaseModel):
@@ -59,4 +81,10 @@ class DeleteVersionResponse(BaseModel):
59
81
  False,
60
82
  description="Whether the version was already soft-deleted before this request",
61
83
  )
62
- date_deleted: str = Field(..., description="ISO timestamp of when the version was soft-deleted")
84
+ date_deleted: datetime = Field(..., description="When the version was soft-deleted")
85
+
86
+
87
+ class UpdateVersionResponse(ProjectBasicInfo):
88
+ """Response model for updating a version."""
89
+
90
+ created_at: datetime = Field(..., description="When the version was created")
@@ -0,0 +1,210 @@
1
+ """
2
+ Schemas for query routes.
3
+ """
4
+
5
+ from typing import Any, Self
6
+
7
+ from pydantic import BaseModel, ConfigDict, field_validator, model_validator
8
+
9
+ from divbase_lib.utils import split_semicolon_bcftools_command_segments
10
+
11
+ # Shared helper functions and models
12
+
13
+
14
+ def validate_command_not_empty(command: str) -> str:
15
+ """
16
+ Validator function to ensure that the user-submitted bcftools command string is not empty or just whitespace.
17
+ If there are multiple segments separated by semicolons, it also validates that none of the segments are empty or whitespace.
18
+
19
+ Shared helper for command field validators in BcftoolsQueryRequest.command and BcftoolsQueryKwargs.command.
20
+ """
21
+ command_string = command.strip()
22
+ if command_string == "":
23
+ raise ValueError(
24
+ "The --command option must be a non-empty bcftools view string. "
25
+ 'If you only want to subset based on samples, use --command "view -s" in combination with one of the sample-selection options: --tsv-filter, --samples, --samples-file.'
26
+ )
27
+
28
+ segments = split_semicolon_bcftools_command_segments(command_string)
29
+ for position, segment in enumerate(segments, start=1):
30
+ if segment.strip() == "":
31
+ raise ValueError(
32
+ f"The --command option has an empty pipeline segment at position {position}. "
33
+ "Use semicolons only between complete bcftools view commands."
34
+ )
35
+
36
+ return command
37
+
38
+
39
+ class SharedBaseModel(BaseModel):
40
+ """Shared pydantic BaseModel for VCF query response and kwarg schemas."""
41
+
42
+ model_config = ConfigDict(
43
+ validate_assignment=True,
44
+ json_schema_extra={
45
+ "description": ("Exactly one sample-selection mode must be provided: tsv_filter, samples, or all_samples.")
46
+ },
47
+ )
48
+
49
+
50
+ # Request models
51
+ class SampleMetadataQueryRequest(BaseModel):
52
+ """Request model for sample metadata query route."""
53
+
54
+ tsv_filter: str
55
+ metadata_tsv_name: str
56
+
57
+
58
+ class BcftoolsQueryRequest(SharedBaseModel):
59
+ """
60
+ Request model for sample metadata query route.
61
+ See ConfigDict in SharedBaseModel for validation rules around tsv_filter and samples fields.
62
+ """
63
+
64
+ tsv_filter: str | None = None # Used for metadata mode only
65
+ metadata_tsv_name: str | None = None # Used for metadata mode only
66
+ command: str # bcftools command input with --command
67
+ samples: list[str] | None = None
68
+ all_samples: bool = False
69
+
70
+ @field_validator("command")
71
+ @classmethod
72
+ def validate_command(_cls, value: str) -> str:
73
+ return validate_command_not_empty(value)
74
+
75
+ @model_validator(mode="after")
76
+ def validate_sample_selection_mode(self) -> Self:
77
+ tsv_filter = self.tsv_filter
78
+ samples = self.samples
79
+ all_samples = self.all_samples
80
+
81
+ selection_count = 0
82
+ if tsv_filter is not None:
83
+ selection_count += 1
84
+ if samples is not None:
85
+ selection_count += 1
86
+ if all_samples:
87
+ selection_count += 1
88
+
89
+ if selection_count > 1:
90
+ raise ValueError("Only one of tsv_filter, samples, or all_samples may be provided.")
91
+ if selection_count == 0:
92
+ raise ValueError("One sample-selection mode must be provided (tsv_filter, samples, or all_samples).")
93
+
94
+ if tsv_filter is not None and self.metadata_tsv_name is None:
95
+ raise ValueError("metadata_tsv_name must be provided when tsv_filter is used.")
96
+
97
+ if samples is not None and len(samples) == 0:
98
+ raise ValueError("samples must contain at least one sample ID when provided.")
99
+
100
+ return self
101
+
102
+
103
+ # Models for task kwargs and task results. Reused in task history schemas too, hence pydantic models and not just dataclasses.
104
+ class SampleMetadataQueryKwargs(BaseModel):
105
+ """Keyword arguments for sample metadata query task. Used to pass info to Celery task, and also for recording task history."""
106
+
107
+ tsv_filter: str
108
+ metadata_tsv_name: str
109
+ bucket_name: str
110
+ project_id: int
111
+ project_name: str
112
+ user_id: int
113
+
114
+
115
+ class BcftoolsQueryKwargs(SharedBaseModel):
116
+ """
117
+ Keyword arguments for BCFtools query task. Used to pass info to Celery task, and also for recording task history.
118
+ See ConfigDict in SharedBaseModel for validation rules around tsv_filter and samples fields.
119
+ """
120
+
121
+ tsv_filter: str | None = None # Used for metadata mode only
122
+ metadata_tsv_name: str | None = None # Used for metadata mode only
123
+ command: str # bcftools command input with --command
124
+ bucket_name: str
125
+ project_id: int
126
+ project_name: str
127
+ user_id: int
128
+ job_id: int
129
+ samples: list[str] | None = None
130
+ all_samples: bool = False
131
+
132
+ @field_validator("command")
133
+ @classmethod
134
+ def validate_command(_cls, value: str) -> str:
135
+ return validate_command_not_empty(value)
136
+
137
+ @model_validator(mode="after")
138
+ def validate_sample_selection_mode(self) -> Self:
139
+ tsv_filter = self.tsv_filter
140
+ samples = self.samples
141
+ all_samples = self.all_samples
142
+
143
+ selection_count = 0
144
+ if tsv_filter is not None:
145
+ selection_count += 1
146
+ if samples is not None:
147
+ selection_count += 1
148
+ if all_samples:
149
+ selection_count += 1
150
+
151
+ if selection_count > 1:
152
+ raise ValueError("Only one of tsv_filter, samples, or all_samples may be provided.")
153
+ if selection_count == 0:
154
+ # Backward compatibility for historical task kwargs created before all_samples option was implemented.
155
+ # To ensure that task-history deserialization does not break for existing tasks.
156
+ # Could be handled by a backfilling migration instead.
157
+ self.all_samples = True
158
+ return self
159
+
160
+ if tsv_filter is not None and self.metadata_tsv_name is None:
161
+ raise ValueError("metadata_tsv_name must be provided when tsv_filter is used.")
162
+
163
+ if samples is not None and len(samples) == 0:
164
+ raise ValueError("samples must contain at least one sample ID when provided.")
165
+
166
+ return self
167
+
168
+
169
+ class SampleFileMappingResult(BaseModel):
170
+ sample_id: str
171
+ filename: str
172
+
173
+ @model_validator(mode="before")
174
+ @classmethod
175
+ def map_legacy_task_history_keys(cls, data: Any) -> Any:
176
+ """
177
+ Support historical task-history payloads that stored uppercase keys.
178
+ """
179
+ if not isinstance(data, dict):
180
+ return data
181
+
182
+ normalized = dict(data)
183
+ if "sample_id" not in normalized:
184
+ if "Sample_ID" in normalized:
185
+ normalized["sample_id"] = normalized["Sample_ID"]
186
+ elif "#Sample_ID" in normalized:
187
+ normalized["sample_id"] = normalized["#Sample_ID"]
188
+ if "filename" not in normalized and "Filename" in normalized:
189
+ normalized["filename"] = normalized["Filename"]
190
+
191
+ return normalized
192
+
193
+
194
+ class SampleMetadataQueryTaskResult(BaseModel):
195
+ """Metadata query task result details. Based on the return of tasks.sample_metadata_query."""
196
+
197
+ sample_and_filename_subset: list[SampleFileMappingResult]
198
+ unique_sample_ids: list[str]
199
+ unique_filenames: list[str]
200
+ query_message: str
201
+ warnings: list[str] = []
202
+ status: str | None = None
203
+
204
+
205
+ class BcftoolsQueryTaskResult(BaseModel):
206
+ """BCFtools query task result details. Based on the return of tasks.bcftools_query."""
207
+
208
+ output_file: str
209
+ status: str | None = None
210
+ log_file: str | None = None