Skip to content

feat(schema): mandatory identity ids (feature_id/psm_id/pg_id) + optional cross-refs - #232

Merged
ypriverol merged 24 commits into
devfrom
feat/mandatory-identity-ids
Aug 5, 2026
Merged

ypriverol merged 24 commits into
devfrom
feat/mandatory-identity-ids

Conversation

@ypriverol

Copy link
Copy Markdown
Contributor

Summary

Implements the agreed identity design from #229: every feature, psm, and pg row carries a single mandatory int64 identity column — feature_id / psm_id / pg_id — that is the primary key of its view. The id is derived by the writer as an opaque hash of a footer-declared identity_composite of existing columns (or kept as-is when a producer supplies its own). Converters are essentially unchanged — they emit the composite fields and the writer computes the id.

How it works

  • qpx/core/data/identity.py — derive_id(values) = signed 64-bit BLAKE2b of a canonical serialization of the composite (unit-separator joined, NUL sentinel for null). Opaque; uniqueness is guaranteed by the PK validator, not by construction.
  • Schemas (feature/psm/pg, + docs/spec mirror): id column added as the sole primary_key; new identity_composite key declares the derivation input:
    • feature → [peptidoform, charge, run_file_name, rt]
    • psm → [peptidoform, charge, run_file_name, scan]
    • pg → [anchor_protein, grouped_runs, label]
  • Writer (writers/base.py): derives the id on all three write paths (records / pa.Table / DataFrame) only when absent (producer-supplied ids preserved), and stamps primary_key + identity_composite into the parquet footer so each file self-describes its key. Views without an identity_composite (mz/run/sample/…) are untouched.
  • Validation: the id is the PK, so existing primary-key uniqueness catches hash collisions; the required/non-null id gives presence.

Optional cross-reference columns

Added (nullable, unpopulated by default): feature.psm_ids (list<int64>), feature.pg_ids (list<int64>), psm.feature_id (int64). These let the views reference each other by id (feature ↔ PSM ↔ protein group), resolving the hard-coded feature↔PSM link in #182; a converter populates them where it can resolve the mapping.

Scope / follow-ups

Verification

  • Full suite: 806 passed (adds tests/unit/test_identity.py).
  • ruff check . + ruff format --check . clean.
  • Smoke-tested: written files carry a non-null, unique feature_id == derive_id(composite), cross-ref columns present, footer stamps primary_key/identity_composite, validation passes.

Related: #229 (design), #230 (MBR columns), #182 (feature↔PSM link), #231 (mzTab removal).

…onal cross-refs

Every feature/psm/pg row now carries a single required int64 identity column that
is the primary key of its view. The writer derives it as an opaque BLAKE2b hash of
a footer-declared identity_composite of existing columns (or keeps a producer-supplied
id), and stamps primary_key + identity_composite into the parquet footer so each file
self-describes its key. Primary-key uniqueness validation catches any hash collision.

- qpx/core/data/identity.py: derive_id() + canonical serialization
- feature/psm/pg schemas (+ docs/spec mirror): id column as sole PK, identity_composite,
  and optional cross-ref columns feature.psm_ids / feature.pg_ids / psm.feature_id
- writers/base.py: derive the id on the records / table / dataframe paths; footer stamp
- loader/schema: parse + expose identity_composite
- diann/dataset: null-fill the new columns on their table/SQL build paths
- tests: tests/unit/test_identity.py + updates; full suite green (806 passed)

Implements the agreed design in #229 (cross-refs optional; producer-specific
identity_composite tuples to follow as their columns land, e.g. #230).
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 62fbaf57-754d-4ec8-902f-358dac73a9d0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codacy-production

codacy-production Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 162 complexity · 0 duplication

Metric Results
Complexity 162
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

…ce count

Adds Writer(override_provided_ids=True): when a producer supplies an id, re-derive
the library id from the identity_composite and preserve the original as a
provided_<id> cv_param, incrementing overridden_id_count so the converter can record
the override in provenance (intended for consensusXML once it reads OpenMS UniqueIds).
Default off — provided ids are kept. PK clashes stay warning-level in non-strict
validation (during/after conversion). Adds override tests; suite 808 passed.
… + referential validation

Completes the #229 identity design:

PART 1 — per-converter feature identity_composite (Yufei's measured keys, existing
columns only), passed to FeatureWriter and stamped in the footer:
  openms-consensus: [peptidoform,charge,run_file_name,rt,scan,observed_mz]
  maxquant:         [peptidoform,charge,run_file_name,rt_start,rt_stop,scan]
  diann/spectronaut/cdap: [peptidoform,charge,run_file_name]
  fragpipe: schema default (needs compensation_voltage/quantification_unit_id, #230)
  openms -out_qpx: n/a (enriches existing feature.parquet, no FeatureWriter)

PART 2 — openms-consensus populates feature.psm_ids (list) and psm.feature_id via
deterministic derive_id over the same composites the writers use, in both the
in-memory and single-pass streaming paths (equivalence preserved).

PART 3 — dataset-level referential validation: non-null psm.feature_id must resolve
to a feature.feature_id and each feature.psm_ids element to a psm.psm_id; unresolved
refs are warnings (not errors), per the agreed design. Skipped gracefully on files
lacking the columns.

Full suite 812 passed; ruff clean. Resolves the hard-coded feature<->psm link (#182).
…lse positive)

- ViewSchema: expose public primary_key property; writer uses it instead of _primary_key
- rename BaseWriter._align_table_to_schema -> align_table_to_schema (called from dataset.py)
- identity.py: build FIELD_SEP/NULL_TOKEN via chr() to avoid the 'hardcoded password' heuristic
- CHANGELOG: blank line before the Changed list (MD032)
@ypriverol

Copy link
Copy Markdown
Contributor Author

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

…, docs)

Addresses the code-review findings on this PR:
- identity.canonical(): sort list-valued composite fields (grouped_runs, scan) so
  order does not change the derived id — restores the order-insensitive identity the
  composite PK had; two rows differing only in list order now collide as duplicate_pk.
- writers/base._fill_identity_table(): honor override_provided_ids on the
  write_table/write_dataframe path too (re-derive + stash provided id as cv_param +
  count), matching the record path.
- writers/base.write_dataframe(): only backfill nullable columns (+ the derived id),
  so a genuinely missing required column errors clearly instead of deferring.
- openms_consensus: pass _PSM_IDENTITY_COMPOSITE explicitly to PsmWriter (both paths)
  so the converter is the single source, not implicit schema-default coupling.
- pg.py: update stale module docstring (pg PK is now the derived pg_id).
Adds tests for list-order aliasing and table-path override. Suite 815 passed.
Addresses the Codex adversarial-review findings on PR #232:
- [high] Deterministic ids across write paths: derive from persisted (Arrow-cast)
  values in one place (_fill_identity_table). write_batch/write_table/write_dataframe
  now yield identical ids for the same row (float32 rt no longer diverges record vs
  table path). Regression test test_ids_agree_across_write_paths.
- [high] write_dataframe / align_table_to_schema derive the id BEFORE the non-nullable
  cast (relaxed nullable-id schema), so a frame/table without a precomputed id no longer
  fails casting a null id to non-nullable. Single derivation point avoids double-stash
  in override mode.
- [medium] Injective canonical encoding: JSON (quoted/escaped) instead of a delimiter
  join, so list elements/fields containing commas/brackets cannot alias (['a,b','c'] vs
  ['a','b,c']). Order-insensitivity preserved. Test test_canonical_is_injective_json.
- [medium] Referential validation flags reciprocal feature<->psm desync (warning), only
  where the opposite direction is populated. Test test_feature_psm_reciprocal_desync_is_warning.

Not changed (intentional, per the agreed #229 design): producer-supplied ids are kept by
default (not force-verified), and PK clashes are warnings in non-strict validation.
Full suite 817 passed; ruff clean.
@ypriverol
ypriverol requested a review from Shen-YuFei August 5, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants