apb-catalog 0.1.0__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 (59) hide show
  1. apb_catalog-0.1.0/LICENSE +21 -0
  2. apb_catalog-0.1.0/PKG-INFO +79 -0
  3. apb_catalog-0.1.0/README.md +55 -0
  4. apb_catalog-0.1.0/pyproject.toml +129 -0
  5. apb_catalog-0.1.0/pyproject.toml.orig +111 -0
  6. apb_catalog-0.1.0/src/apb_catalog/__init__.py +1 -0
  7. apb_catalog-0.1.0/src/apb_catalog/api.py +16 -0
  8. apb_catalog-0.1.0/src/apb_catalog/catalog.py +234 -0
  9. apb_catalog-0.1.0/src/apb_catalog/data/apb-catalog-resolution-0.2.schema.json +520 -0
  10. apb_catalog-0.1.0/src/apb_catalog/data/catalogues.json +20 -0
  11. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/alphadia.json +81 -0
  12. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/alphapept.json +40 -0
  13. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/diann.json +113 -0
  14. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/fragpipe.json +19 -0
  15. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/i2masschroq.json +19 -0
  16. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/maxquant.json +98 -0
  17. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/msangel.json +19 -0
  18. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/pb_custom.json +19 -0
  19. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/peaks.json +19 -0
  20. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/prolinestudio.json +19 -0
  21. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/quantms.json +19 -0
  22. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/sage.json +67 -0
  23. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/spectronaut.json +118 -0
  24. apb_catalog-0.1.0/src/apb_catalog/data/sources/identification_confidence/wombat.json +26 -0
  25. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/alphadia.json +79 -0
  26. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/alphapept.json +19 -0
  27. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/diann.json +151 -0
  28. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/fragpipe.json +19 -0
  29. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/i2masschroq.json +19 -0
  30. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/maxquant.json +307 -0
  31. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/msangel.json +19 -0
  32. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/pb_custom.json +19 -0
  33. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/peaks.json +19 -0
  34. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/prolinestudio.json +19 -0
  35. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/quantms.json +19 -0
  36. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/sage.json +103 -0
  37. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/spectronaut.json +156 -0
  38. apb_catalog-0.1.0/src/apb_catalog/data/sources/miape/wombat.json +103 -0
  39. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/alphadia.json +81 -0
  40. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/alphapept.json +19 -0
  41. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/diann.json +136 -0
  42. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/fragpipe.json +19 -0
  43. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/i2masschroq.json +19 -0
  44. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/maxquant.json +40 -0
  45. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/msangel.json +19 -0
  46. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/pb_custom.json +19 -0
  47. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/peaks.json +19 -0
  48. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/prolinestudio.json +19 -0
  49. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/quantms.json +19 -0
  50. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/sage.json +26 -0
  51. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/spectronaut.json +118 -0
  52. apb_catalog-0.1.0/src/apb_catalog/data/sources/proteobench_entrapment/wombat.json +26 -0
  53. apb_catalog-0.1.0/src/apb_catalog/data/vocabulary.json +47 -0
  54. apb_catalog-0.1.0/src/apb_catalog/fingerprint.py +25 -0
  55. apb_catalog-0.1.0/src/apb_catalog/py.typed +1 -0
  56. apb_catalog-0.1.0/src/apb_catalog/resolver.py +233 -0
  57. apb_catalog-0.1.0/src/apb_catalog/snapshot.py +62 -0
  58. apb_catalog-0.1.0/src/apb_catalog/source.py +166 -0
  59. apb_catalog-0.1.0/src/apb_catalog/vocabulary.py +83 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Witold Wolski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,79 @@
1
+ Metadata-Version: 2.4
2
+ Name: apb-catalog
3
+ Version: 0.1.0
4
+ Summary: Semantic source catalogues and column resolution for APB results
5
+ Keywords: proteomics,anndata,metadata,catalogue,mass spectrometry
6
+ Author: Witold Wolski
7
+ Author-email: Witold Wolski <wew@fgcz.ethz.ch>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
16
+ Classifier: Typing :: Typed
17
+ Requires-Dist: apb2>=0.1,<0.2
18
+ Requires-Dist: polars>=1.43,<2
19
+ Requires-Dist: pydantic>=2.10
20
+ Requires-Python: >=3.13
21
+ Project-URL: Documentation, https://anndata-omics-bridge.github.io/apb-catalog/
22
+ Project-URL: Repository, https://github.com/anndata-omics-bridge/apb-catalog
23
+ Description-Content-Type: text/markdown
24
+
25
+ # APB Catalog
26
+
27
+ Catalogues of what APB result fields mean, one set per kind of meaning, each stating its purpose and users, so the consumer asks for a meaning instead of vendor columns such as `PEP`, `EG_PEP` or `Q_Value`.
28
+
29
+ Documentation: [anndata-omics-bridge.github.io/apb-catalog](https://anndata-omics-bridge.github.io/apb-catalog/), built from [docs/](https://github.com/anndata-omics-bridge/apb-catalog/tree/main/docs) with `make docs`; start with [Get started](https://anndata-omics-bridge.github.io/apb-catalog/getting-started/).
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install apb-catalog
35
+ ```
36
+
37
+ ## Use
38
+
39
+ ```python
40
+ from apb_catalog.api import Catalog
41
+
42
+ confidence = Catalog(parsed, "identification_confidence") # parsed: apb2 ParsedLevels
43
+ confidence.layer("ion", concept="confidence") # ('pep', 'q_value') — kinds on offer, or None
44
+ pep = confidence.layer("ion", concept="confidence", kind="pep") # MaxQuant PEP, Spectronaut EG_PEP, DIA-NN PEP
45
+
46
+ miape = Catalog(parsed, "miape")
47
+ miape.var("protein", concept="miape") # ('gene_name', 'protein_group')
48
+ genes = miape.var("protein", concept="miape", kind="gene_name") # the column MIAPE-AnnData calls gene_name
49
+ ```
50
+
51
+ - [Every entry](https://anndata-omics-bridge.github.io/apb-catalog/catalogues/) of both sets
52
+ - `layer` returns the apb2 layer table, `var` the column; `None` means the vendor has no such field
53
+ - A level answers only for its own entity: ion for precursors, protein for protein groups
54
+ - Confidence lookups mean identification unless `stage="quantification"` says otherwise
55
+ - Two equally good fields, or a rule nobody reviewed, raise `UnresolvedField` naming the candidates
56
+ - `attach_snapshot(parsed, catalog.snapshot())` records bindings and lookups in `metadata["catalog"][<catalogue>]`
57
+
58
+ ## Catalogues
59
+
60
+ - `identification_confidence`: how likely each identification is correct: per-run PEP and q-value layers, per-feature PEP and Sage's MS1-peak q-value; used by apb-aggregate's confidence-weighted rollups; 12 entries
61
+ - `miape`: draft mapping onto the MIAPE-AnnData Schema 0.4.0 draft (HUPO-PSI AI Readiness Working Group): protein, peptide and peptidoform fields and the `raw` layer; ion levels have no MIAPE modality; 32 entries
62
+ - `Catalog(parsed, name).description`: the set's purpose and users, also embedded in its snapshot
63
+ - Each set lists every packaged APB2 rule level, so a vendor without a relevant field answers `missing`, never `unknown`
64
+ - Entries bind to reviewed effective-rule fingerprints (SHA-256 of canonical `rule_json`); any APB2 rule change needs re-review, enforced by the drift tests
65
+ - Vocabulary: `confidence` with `kind` (pep, q_value) and `stage`; `miape` with `kind` naming the MIAPE-AnnData field and its requirement level
66
+
67
+ ## Snapshot contract
68
+
69
+ `attach_snapshot` stores `apb-catalog-resolution` 0.2 as JSON at `ParsedLevels.metadata["catalog"][<catalogue>]`; APB2 persists it in every format, including AnnData `uns` from HDF5 result format 5. It embeds the set's description, concept definitions, level bindings, retained entries and every answer, so readers need neither this package nor its catalogues. The [JSON Schema](https://github.com/anndata-omics-bridge/apb-catalog/blob/main/src/apb_catalog/data/apb-catalog-resolution-0.2.schema.json) is published with the package; `make schema` regenerates it.
70
+
71
+ ## Development
72
+
73
+ ```bash
74
+ uv sync --group dev
75
+ make check
76
+ .venv/bin/pre-commit install --hook-type pre-commit --hook-type pre-push
77
+ ```
78
+
79
+ All Python commands run from the synchronized project `.venv`.
@@ -0,0 +1,55 @@
1
+ # APB Catalog
2
+
3
+ Catalogues of what APB result fields mean, one set per kind of meaning, each stating its purpose and users, so the consumer asks for a meaning instead of vendor columns such as `PEP`, `EG_PEP` or `Q_Value`.
4
+
5
+ Documentation: [anndata-omics-bridge.github.io/apb-catalog](https://anndata-omics-bridge.github.io/apb-catalog/), built from [docs/](https://github.com/anndata-omics-bridge/apb-catalog/tree/main/docs) with `make docs`; start with [Get started](https://anndata-omics-bridge.github.io/apb-catalog/getting-started/).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install apb-catalog
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```python
16
+ from apb_catalog.api import Catalog
17
+
18
+ confidence = Catalog(parsed, "identification_confidence") # parsed: apb2 ParsedLevels
19
+ confidence.layer("ion", concept="confidence") # ('pep', 'q_value') — kinds on offer, or None
20
+ pep = confidence.layer("ion", concept="confidence", kind="pep") # MaxQuant PEP, Spectronaut EG_PEP, DIA-NN PEP
21
+
22
+ miape = Catalog(parsed, "miape")
23
+ miape.var("protein", concept="miape") # ('gene_name', 'protein_group')
24
+ genes = miape.var("protein", concept="miape", kind="gene_name") # the column MIAPE-AnnData calls gene_name
25
+ ```
26
+
27
+ - [Every entry](https://anndata-omics-bridge.github.io/apb-catalog/catalogues/) of both sets
28
+ - `layer` returns the apb2 layer table, `var` the column; `None` means the vendor has no such field
29
+ - A level answers only for its own entity: ion for precursors, protein for protein groups
30
+ - Confidence lookups mean identification unless `stage="quantification"` says otherwise
31
+ - Two equally good fields, or a rule nobody reviewed, raise `UnresolvedField` naming the candidates
32
+ - `attach_snapshot(parsed, catalog.snapshot())` records bindings and lookups in `metadata["catalog"][<catalogue>]`
33
+
34
+ ## Catalogues
35
+
36
+ - `identification_confidence`: how likely each identification is correct: per-run PEP and q-value layers, per-feature PEP and Sage's MS1-peak q-value; used by apb-aggregate's confidence-weighted rollups; 12 entries
37
+ - `miape`: draft mapping onto the MIAPE-AnnData Schema 0.4.0 draft (HUPO-PSI AI Readiness Working Group): protein, peptide and peptidoform fields and the `raw` layer; ion levels have no MIAPE modality; 32 entries
38
+ - `Catalog(parsed, name).description`: the set's purpose and users, also embedded in its snapshot
39
+ - Each set lists every packaged APB2 rule level, so a vendor without a relevant field answers `missing`, never `unknown`
40
+ - Entries bind to reviewed effective-rule fingerprints (SHA-256 of canonical `rule_json`); any APB2 rule change needs re-review, enforced by the drift tests
41
+ - Vocabulary: `confidence` with `kind` (pep, q_value) and `stage`; `miape` with `kind` naming the MIAPE-AnnData field and its requirement level
42
+
43
+ ## Snapshot contract
44
+
45
+ `attach_snapshot` stores `apb-catalog-resolution` 0.2 as JSON at `ParsedLevels.metadata["catalog"][<catalogue>]`; APB2 persists it in every format, including AnnData `uns` from HDF5 result format 5. It embeds the set's description, concept definitions, level bindings, retained entries and every answer, so readers need neither this package nor its catalogues. The [JSON Schema](https://github.com/anndata-omics-bridge/apb-catalog/blob/main/src/apb_catalog/data/apb-catalog-resolution-0.2.schema.json) is published with the package; `make schema` regenerates it.
46
+
47
+ ## Development
48
+
49
+ ```bash
50
+ uv sync --group dev
51
+ make check
52
+ .venv/bin/pre-commit install --hook-type pre-commit --hook-type pre-push
53
+ ```
54
+
55
+ All Python commands run from the synchronized project `.venv`.
@@ -0,0 +1,129 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.9.26,<0.10.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "apb-catalog"
7
+ version = "0.1.0"
8
+ description = "Semantic source catalogues and column resolution for APB results"
9
+ readme = "README.md"
10
+ requires-python = ">=3.13"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ keywords = [
14
+ "proteomics",
15
+ "anndata",
16
+ "metadata",
17
+ "catalogue",
18
+ "mass spectrometry",
19
+ ]
20
+ classifiers = [
21
+ "Development Status :: 3 - Alpha",
22
+ "Intended Audience :: Science/Research",
23
+ "Operating System :: OS Independent",
24
+ "Programming Language :: Python :: 3",
25
+ "Programming Language :: Python :: 3.13",
26
+ "Topic :: Scientific/Engineering :: Bio-Informatics",
27
+ "Typing :: Typed",
28
+ ]
29
+ dependencies = [
30
+ "apb2>=0.1,<0.2",
31
+ "polars>=1.43,<2",
32
+ "pydantic>=2.10",
33
+ ]
34
+
35
+ [[project.authors]]
36
+ name = "Witold Wolski"
37
+ email = "wew@fgcz.ethz.ch"
38
+
39
+ [project.urls]
40
+ Documentation = "https://anndata-omics-bridge.github.io/apb-catalog/"
41
+ Repository = "https://github.com/anndata-omics-bridge/apb-catalog"
42
+
43
+ [tool.uv.sources.apb2]
44
+ path = "../apb2"
45
+ editable = true
46
+
47
+ [tool.ruff]
48
+ line-length = 100
49
+ target-version = "py313"
50
+ src = [
51
+ "src",
52
+ "tests",
53
+ ]
54
+
55
+ [tool.ruff.lint]
56
+ select = [
57
+ "ANN",
58
+ "B",
59
+ "C4",
60
+ "C90",
61
+ "E4",
62
+ "E7",
63
+ "E9",
64
+ "F",
65
+ "I",
66
+ "PGH",
67
+ "PIE",
68
+ "RUF",
69
+ "SIM",
70
+ "UP",
71
+ ]
72
+
73
+ [tool.ruff.lint.mccabe]
74
+ max-complexity = 10
75
+
76
+ [tool.ruff.format]
77
+ docstring-code-format = true
78
+
79
+ [tool.pyright]
80
+ include = [
81
+ "src",
82
+ "tests",
83
+ ]
84
+ venvPath = "."
85
+ venv = ".venv"
86
+ pythonVersion = "3.13"
87
+ typeCheckingMode = "strict"
88
+ reportImportCycles = "error"
89
+ reportMissingTypeStubs = "error"
90
+ reportUnnecessaryTypeIgnoreComment = "error"
91
+ reportImplicitOverride = "error"
92
+ enableTypeIgnoreComments = false
93
+
94
+ [tool.pytest.ini_options]
95
+ addopts = [
96
+ "--strict-config",
97
+ "--strict-markers",
98
+ "-ra",
99
+ ]
100
+ testpaths = ["tests"]
101
+ xfail_strict = true
102
+
103
+ [tool.coverage.run]
104
+ branch = true
105
+ source = ["apb_catalog"]
106
+
107
+ [tool.coverage.report]
108
+ fail_under = 80
109
+ show_missing = true
110
+ skip_covered = true
111
+
112
+ [tool.deptry]
113
+ known_first_party = ["apb_catalog"]
114
+
115
+ [dependency-groups]
116
+ dev = [
117
+ "build>=1.3,<2",
118
+ "deptry>=0.24,<1",
119
+ "pre-commit>=4,<5",
120
+ "pyright>=1.1.400,<2",
121
+ "pytest>=9,<10",
122
+ "pytest-cov>=7,<8",
123
+ "ruff>=0.15,<1",
124
+ "twine>=6,<7",
125
+ ]
126
+ docs = [
127
+ "pymdown-extensions>=11,<12",
128
+ "zensical==0.0.43",
129
+ ]
@@ -0,0 +1,111 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.9.26,<0.10.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "apb-catalog"
7
+ version = "0.1.0"
8
+ description = "Semantic source catalogues and column resolution for APB results"
9
+ readme = "README.md"
10
+ requires-python = ">=3.13"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [
14
+ { name = "Witold Wolski", email = "wew@fgcz.ethz.ch" },
15
+ ]
16
+ keywords = ["proteomics", "anndata", "metadata", "catalogue", "mass spectrometry"]
17
+ classifiers = [
18
+ "Development Status :: 3 - Alpha",
19
+ "Intended Audience :: Science/Research",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Topic :: Scientific/Engineering :: Bio-Informatics",
24
+ "Typing :: Typed",
25
+ ]
26
+ dependencies = [
27
+ "apb2>=0.1,<0.2",
28
+ "polars>=1.43,<2",
29
+ "pydantic>=2.10",
30
+ ]
31
+
32
+ [project.urls]
33
+ Documentation = "https://anndata-omics-bridge.github.io/apb-catalog/"
34
+ Repository = "https://github.com/anndata-omics-bridge/apb-catalog"
35
+
36
+ [tool.uv.sources]
37
+ apb2 = { path = "../apb2", editable = true }
38
+
39
+ [dependency-groups]
40
+ dev = [
41
+ "build>=1.3,<2",
42
+ "deptry>=0.24,<1",
43
+ "pre-commit>=4,<5",
44
+ "pyright>=1.1.400,<2",
45
+ "pytest>=9,<10",
46
+ "pytest-cov>=7,<8",
47
+ "ruff>=0.15,<1",
48
+ "twine>=6,<7",
49
+ ]
50
+ docs = [
51
+ "pymdown-extensions>=11,<12",
52
+ "zensical==0.0.43",
53
+ ]
54
+
55
+ [tool.ruff]
56
+ line-length = 100
57
+ target-version = "py313"
58
+ src = ["src", "tests"]
59
+
60
+ [tool.ruff.lint]
61
+ select = [
62
+ "ANN",
63
+ "B",
64
+ "C4",
65
+ "C90",
66
+ "E4",
67
+ "E7",
68
+ "E9",
69
+ "F",
70
+ "I",
71
+ "PGH",
72
+ "PIE",
73
+ "RUF",
74
+ "SIM",
75
+ "UP",
76
+ ]
77
+
78
+ [tool.ruff.lint.mccabe]
79
+ max-complexity = 10
80
+
81
+ [tool.ruff.format]
82
+ docstring-code-format = true
83
+
84
+ [tool.pyright]
85
+ include = ["src", "tests"]
86
+ venvPath = "."
87
+ venv = ".venv"
88
+ pythonVersion = "3.13"
89
+ typeCheckingMode = "strict"
90
+ reportImportCycles = "error"
91
+ reportMissingTypeStubs = "error"
92
+ reportUnnecessaryTypeIgnoreComment = "error"
93
+ reportImplicitOverride = "error"
94
+ enableTypeIgnoreComments = false
95
+
96
+ [tool.pytest.ini_options]
97
+ addopts = ["--strict-config", "--strict-markers", "-ra"]
98
+ testpaths = ["tests"]
99
+ xfail_strict = true
100
+
101
+ [tool.coverage.run]
102
+ branch = true
103
+ source = ["apb_catalog"]
104
+
105
+ [tool.coverage.report]
106
+ fail_under = 80
107
+ show_missing = true
108
+ skip_covered = true
109
+
110
+ [tool.deptry]
111
+ known_first_party = ["apb_catalog"]
@@ -0,0 +1,16 @@
1
+ """The one public APB Catalog module: semantic lookups and their stored snapshots."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from apb_catalog.catalog import Catalog, attach_snapshot, stale_levels, stored_snapshot
6
+ from apb_catalog.resolver import UnresolvedField
7
+ from apb_catalog.snapshot import ResolutionSnapshot
8
+
9
+ __all__ = [
10
+ "Catalog",
11
+ "ResolutionSnapshot",
12
+ "UnresolvedField",
13
+ "attach_snapshot",
14
+ "stale_levels",
15
+ "stored_snapshot",
16
+ ]
@@ -0,0 +1,234 @@
1
+ """Look up an APB result's fields by meaning, so callers never name vendor columns."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from dataclasses import replace
7
+ from typing import overload
8
+
9
+ import polars as pl
10
+ from apb2.api import FinalLayerTable, ParsedLevel, ParsedLevels
11
+
12
+ from apb_catalog.fingerprint import fingerprint
13
+ from apb_catalog.resolver import (
14
+ AbsentLevel,
15
+ ConceptRequest,
16
+ Resolution,
17
+ ReviewedLevel,
18
+ UnreviewedLevel,
19
+ )
20
+ from apb_catalog.snapshot import ResolutionSnapshot, this_producer
21
+ from apb_catalog.source import (
22
+ CatalogueDescription,
23
+ Location,
24
+ Reference,
25
+ SourceCatalogues,
26
+ packaged_catalogues,
27
+ )
28
+
29
+ METADATA_KEY = "catalog"
30
+ _DEFAULTS = {"stage": "identification"}
31
+
32
+
33
+ class Catalog:
34
+ """What one APB result's fields mean to one consumer, with the data handed back directly.
35
+
36
+ ``Catalog(parsed, "identification_confidence").layer("ion", concept="confidence", kind="pep")`` returns
37
+ MaxQuant's ``PEP`` or Spectronaut's ``EG_PEP`` layer, or ``None`` when the vendor reports no
38
+ PEP; ``Catalog(parsed, "miape").var("protein", concept="miape", kind="gene_name")`` returns the
39
+ column MIAPE-AnnData calls ``gene_name``. Leaving out ``kind`` lists the kinds the level holds.
40
+ A level answers only for its own entity. Confidence lookups mean identification unless
41
+ ``stage="quantification"`` says otherwise. Ambiguous or unreviewed answers raise
42
+ :class:`~apb_catalog.resolver.UnresolvedField` naming the candidates.
43
+ """
44
+
45
+ __slots__ = ("_catalogues", "_name", "_parsed", "_resolutions", "_views")
46
+
47
+ def __init__(self, parsed: ParsedLevels, catalogue: str) -> None:
48
+ self._parsed = parsed
49
+ self._name = catalogue
50
+ self._catalogues = packaged_catalogues(catalogue)
51
+ self._views: dict[str, ReviewedLevel | UnreviewedLevel] = {
52
+ name: level_view(name, level, self._catalogues) for name, level in parsed.levels.items()
53
+ }
54
+ self._resolutions: list[Resolution] = []
55
+
56
+ @property
57
+ def description(self) -> CatalogueDescription:
58
+ """What this catalogue set holds and who uses it."""
59
+ return self._catalogues.description
60
+
61
+ @overload
62
+ def layer(
63
+ self, level: str, concept: str, kind: str, **qualifiers: str
64
+ ) -> FinalLayerTable | None: ...
65
+
66
+ @overload
67
+ def layer(
68
+ self, level: str, concept: str, kind: None = None, **qualifiers: str
69
+ ) -> tuple[str, ...] | None: ...
70
+
71
+ def layer(
72
+ self, level: str, concept: str, kind: str | None = None, **qualifiers: str
73
+ ) -> FinalLayerTable | tuple[str, ...] | None:
74
+ """Return the sample-by-feature layer with this meaning, or the kinds on offer."""
75
+ if kind is None:
76
+ return self._kinds(level, "layers", concept, qualifiers)
77
+ reference = self._find(level, "layers", concept, qualifiers | {"kind": kind})
78
+ return None if reference is None else self._parsed.levels[level].layers[reference.name]
79
+
80
+ @overload
81
+ def var(self, level: str, concept: str, kind: str, **qualifiers: str) -> pl.Series | None: ...
82
+
83
+ @overload
84
+ def var(
85
+ self, level: str, concept: str, kind: None = None, **qualifiers: str
86
+ ) -> tuple[str, ...] | None: ...
87
+
88
+ def var(
89
+ self, level: str, concept: str, kind: str | None = None, **qualifiers: str
90
+ ) -> pl.Series | tuple[str, ...] | None:
91
+ """Return the per-feature column with this meaning, or the kinds on offer."""
92
+ if kind is None:
93
+ return self._kinds(level, "var", concept, qualifiers)
94
+ reference = self._find(level, "var", concept, qualifiers | {"kind": kind})
95
+ return None if reference is None else self._parsed.levels[level].var.frame[reference.name]
96
+
97
+ def fields(self) -> pl.DataFrame:
98
+ """List every catalogued field the result retains, one row per field."""
99
+ return pl.DataFrame(
100
+ [
101
+ {
102
+ "level": entry.reference.level,
103
+ "location": entry.reference.location,
104
+ "name": entry.reference.name,
105
+ "concept": entry.concept,
106
+ **entry.qualifiers,
107
+ }
108
+ for view in self._views.values()
109
+ for entry in view.entries
110
+ ]
111
+ )
112
+
113
+ def resolve(self, request: ConceptRequest) -> Resolution:
114
+ """Answer one explicit request and record it for the snapshot."""
115
+ resolution = self._answer(request)
116
+ self._resolutions.append(resolution)
117
+ return resolution
118
+
119
+ def snapshot(self) -> ResolutionSnapshot:
120
+ """Return a self-contained record of the bindings, fields and every lookup so far."""
121
+ entries = tuple(entry for view in self._views.values() for entry in view.entries)
122
+ concepts = {entry.concept for entry in entries}
123
+ concepts |= {resolution.request.concept for resolution in self._resolutions}
124
+ vocabulary = self._catalogues.vocabulary
125
+ return ResolutionSnapshot(
126
+ producer=this_producer(),
127
+ catalogue=self._name,
128
+ description=self._catalogues.description,
129
+ vocabulary_version=vocabulary.vocabulary_version,
130
+ concepts={name: vocabulary.concept(name) for name in sorted(concepts)},
131
+ levels=tuple(view.binding() for view in self._views.values()),
132
+ entries=entries,
133
+ resolutions=tuple(self._resolutions),
134
+ )
135
+
136
+ def _find(
137
+ self,
138
+ level: str,
139
+ location: Location,
140
+ concept: str,
141
+ qualifiers: dict[str, str],
142
+ ) -> Reference | None:
143
+ return self.resolve(self._request(level, location, concept, qualifiers)).found()
144
+
145
+ def _kinds(
146
+ self,
147
+ level: str,
148
+ location: Location,
149
+ concept: str,
150
+ qualifiers: dict[str, str],
151
+ ) -> tuple[str, ...] | None:
152
+ offered = self._answer(self._request(level, location, concept, qualifiers)).offered("kind")
153
+ return offered or None
154
+
155
+ def _request(
156
+ self,
157
+ level: str,
158
+ location: Location,
159
+ concept: str,
160
+ qualifiers: dict[str, str],
161
+ ) -> ConceptRequest:
162
+ declared = self._catalogues.vocabulary.concept(concept).qualifiers
163
+ described = {name: value for name, value in _DEFAULTS.items() if name in declared}
164
+ described |= qualifiers
165
+ return ConceptRequest(
166
+ concept=concept,
167
+ level=level,
168
+ location=location,
169
+ qualifiers={name: (value,) for name, value in described.items()},
170
+ )
171
+
172
+ def _answer(self, request: ConceptRequest) -> Resolution:
173
+ self._catalogues.vocabulary.concept(request.concept).check_predicate(request.qualifiers)
174
+ return self._views.get(request.level, AbsentLevel(request.level)).resolve(request)
175
+
176
+
177
+ def rule_fingerprint(level: ParsedLevel, /) -> str | None:
178
+ """Return the fingerprint of a level's stored effective rule, if it records one."""
179
+ rule_json = level.uns.get("rule_json")
180
+ if not isinstance(rule_json, str):
181
+ return None
182
+ return fingerprint(json.loads(rule_json))
183
+
184
+
185
+ def level_view(
186
+ name: str, level: ParsedLevel, catalogues: SourceCatalogues, /
187
+ ) -> ReviewedLevel | UnreviewedLevel:
188
+ """Bind one level to its reviewed rule, keeping only entries whose fields it retains."""
189
+ level_fingerprint = rule_fingerprint(level)
190
+ if level_fingerprint is None:
191
+ return UnreviewedLevel(name, None, "the level records no rule_json provenance")
192
+ reviewed = catalogues.reviewed(level_fingerprint)
193
+ if reviewed is None:
194
+ return UnreviewedLevel(
195
+ name, level_fingerprint, "no source catalogue reviewed this effective rule"
196
+ )
197
+ retained = {("layers", layer) for layer in level.layers}
198
+ retained |= {("var", column) for column in level.var.frame.columns}
199
+ entries = tuple(
200
+ entry
201
+ for entry in reviewed.entries()
202
+ if (entry.reference.location, entry.reference.name) in retained
203
+ )
204
+ return ReviewedLevel(name, level_fingerprint, reviewed, entries)
205
+
206
+
207
+ def attach_snapshot(parsed: ParsedLevels, snapshot: ResolutionSnapshot) -> ParsedLevels:
208
+ """Return a new result carrying the snapshot beside other catalogues' snapshots.
209
+
210
+ Scientific data is shared, not copied.
211
+ """
212
+ namespace = parsed.metadata.get(METADATA_KEY)
213
+ snapshots = dict(namespace) if isinstance(namespace, dict) else {}
214
+ snapshots[snapshot.catalogue] = snapshot.model_dump(mode="json")
215
+ return replace(parsed, metadata={**parsed.metadata, METADATA_KEY: snapshots})
216
+
217
+
218
+ def stored_snapshot(parsed: ParsedLevels, catalogue: str) -> ResolutionSnapshot | None:
219
+ """Return the snapshot one catalogue set left on a result, if any."""
220
+ namespace = parsed.metadata.get(METADATA_KEY)
221
+ stored = namespace.get(catalogue) if isinstance(namespace, dict) else None
222
+ if stored is None:
223
+ return None
224
+ return ResolutionSnapshot.model_validate(stored)
225
+
226
+
227
+ def stale_levels(parsed: ParsedLevels, snapshot: ResolutionSnapshot) -> tuple[str, ...]:
228
+ """Return the snapshot's levels whose current effective rule differs from the recorded one."""
229
+ return tuple(
230
+ binding.level
231
+ for binding in snapshot.levels
232
+ if binding.level not in parsed.levels
233
+ or rule_fingerprint(parsed.levels[binding.level]) != binding.fingerprint
234
+ )