Sample-annotation architecture
Public object model
parser = AnnotationCompiler(...).compile(annotation_source)
annotation = parser.parse(parsed)
result = annotation.annotate()
The compiler accepts CSV, TSV or an in-memory Polars frame. It selects SDRF when it finds source name and comment[data file]. The SDRF parser checks for label-free rows, keys them by data file and accepts the extensionless data-file basename as an alias. Other sources use prolfquapp's single identifier column and optional _alias or _aliases columns.
The source-bound parser reads the identifiers, matches them against the supplied ParsedLevels and validates the retention policy. It then returns an Annotation holding that result and its completed AnnotationMatches. Calling annotate() applies those matches. Parsing, matching, policy validation and application have separate implementations.
Values and behavior
AnnotationTable, AnnotationCoverage, LevelAnnotationMatch, AnnotationMatches and AnnotationResult hold the data as frozen values. Policies decide which observations to retain. Each source module supplies its convention's diagnostics.
unmatched="keep"retains every observation and fills unmatched annotation fields with nullunmatched="error"requires every observation to matchunmatched="drop"keeps matched observations in their original order. With this policy,includecan name a Boolean column with a value for every matched observation. The selection keeps rows where that column is true
Every policy requires at least one match at each level. Selection filters obs, obsm and both axes of obsp, and renumbers the retained obs_pos values in each long layer. Feature axes, the result hierarchy, collection annotation tables, feature relations and export_uns are preserved. Coverage describes matching before selection.
Matching
Parser V2 saves the rule's sample_annotation.matching at apb.parse.provenance.sample_annotation_matching in the level part. Annotation builds a matcher per level from that declaration and defaults to exact matching. Optional mass_spec_basename normalization strips directories and recognized mass-spec suffixes from comparison keys. The original keys remain in the result. Colliding normalized identifiers, incompatible key lengths and annotation columns that would overwrite existing obs columns raise errors.
Fuzzy matching reserves exact pairs first. It compares the remaining identifiers with Python's SequenceMatcher(autojunk=False) over lowercase alphanumeric tokens joined by spaces. Each candidate row takes its best score across its identifiers. A fuzzy pair must be the best match in both directions, meet cutoff and lead each runner-up by at least margin. A missing runner-up has score zero. The matcher keeps up to near_miss_limit candidates for each unmatched observation. Matching implementation
Dependency direction
apb2/
├── api.py AnnotationCompiler, AnnotationError, AnnotationResult, SdrfSource
├── cli/annotation.py `apb2 annotate`: result I/O + orchestration
└── annotation/
├── compiler.py generic delimited-source composition
├── contracts.py Annotation and AnnotationParser protocols
├── prolfquapp.py source-bound parser + bound annotation
├── sdrf.py SDRF source view, source-bound parser + bound annotation
├── source/ CSV/TSV decoding
├── application/ retention and selection behavior
├── matching/ exact/fuzzy matching
└── data/ innermost values and errors
Modules directly in annotation/ compose child packages, which share the values in data/. Annotation computation uses the parser's ParsedLevels model. The CLI adapter reads stored results and writes the annotated result through the public result I/O functions.
External packages use apb2.api.AnnotationCompiler. For example, apb-proteobench compiles its module samples as a prolfquapp table with unmatched="error". SdrfSource.read(path) exposes SDRF header lookup and data-file basenames.
Storage behavior
Annotation adds columns to each level's obs and writes records under the convention key, sdrf or prolfquapp. APB metadata specification
- The root record sets
schema_versionto the string"4". Itsprovenance.annotation.sourceis null for an in-memory frame. For a file it containsrole("sample_annotation"),file_name(basename),size_bytes,sha256,compressionandformat. SDRF also recordsprovenance.annotation.columnsas{header, column}pairs for the added fields - The level record writes the coverage counts, mismatch examples, near misses, fuzzy corrections and added column names to
result.annotation. Itssummaryreportsannotation_only_rows,quantification_only_samplesandfuzzy_corrections
Each application replaces the convention's root provenance.annotation and the level's result and summary entries. Parse provenance stays in its own namespace.