Skip to content

Commit 8da3671

Browse files
authored
Merge pull request #384 from bigbio/copilot/fix-comment-columns-drop
Preserve unmapped SDRF comments in run.parquet
2 parents cef79f4 + deabc84 commit 8da3671

5 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎docs/spec/schemas/run.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: run
22
file_type: run_file
33
primary_key: [run_file_name]
44
doc: "MS acquisition run metadata, one row per run"
5+
extra_columns: true
56

67
fields:
78
run_accession:

‎docs/spec/sdrf-mapping.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Run columns describe technical and instrument-related properties of the data acq
4343
| `comment[proteomics data acquisition method]` | `acquisition_method` (plain string; also resolved into `additional_terms` as ONTOLOGY_TERM) | run.parquet |
4444
| `comment[dissociation method]` | `dissociation_method` (plain string; the term is in `additional_terms`) | run.parquet |
4545
| `comment[modification parameters]` | `modification_parameters` (as list of MODIFICATION) | run.parquet |
46+
| Other `comment[X]` columns | `x_sdrf_<x>` (lower-case snake case) | run.parquet |
47+
48+
Unmapped SDRF comment columns are retained as nullable string extension columns on each run. For example, `comment[roi id]` becomes `x_sdrf_roi_id`.
4649

4750
### Factor Columns
4851

‎qpx/converters/sdrf.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ def convert_sdrf_views(sdrf_path: str, output_folder: Path, prefix: str, ontolog
8585
_DURATION = re.compile(r"(?i)(\d+(?:\.\d+)?)\s*(h|hr|hours?|m|min|mins|minutes?|s|sec|seconds?)?")
8686
_SECONDS_PER_UNIT = {"h": 3600.0, "m": 60.0, "s": 1.0}
8787
_COL_MOD_PARAM = "comment[modification parameter" # prefix, may have ]s suffix
88+
_KNOWN_RUN_COMMENT_COLUMNS = {
89+
_COL_DATA_FILE,
90+
_COL_LABEL,
91+
_COL_FRACTION,
92+
_COL_TECH_REP,
93+
_COL_INSTRUMENT,
94+
_COL_ENZYME,
95+
_COL_DISSOC,
96+
_COL_ACQUISITION,
97+
_COL_GRADIENT,
98+
_COL_CHANNEL_ROLE,
99+
_COL_REFERENCE_CHANNEL,
100+
_COL_MIXTURE,
101+
}
88102

89103
# Sample fields the schema requires; a blank value becomes "not available" rather than losing the views
90104
_REQUIRED_SAMPLE_FIELDS = {
@@ -297,6 +311,19 @@ def _term_pairs(group: pd.DataFrame, column: str) -> list[tuple[str, str]]:
297311
return [pair for pair in pairs if pair[0]]
298312

299313

314+
def _extra_run_comment_columns(columns) -> dict[str, list[str]]:
315+
"""``x_sdrf_<name>`` for each unmapped ``comment[...]`` column, with the SDRF columns that fill it."""
316+
extra: dict[str, list[str]] = {}
317+
for column in columns:
318+
match = re.fullmatch(r"comment\[(.+)\]", column)
319+
if not match or column in _KNOWN_RUN_COMMENT_COLUMNS or column.startswith(_COL_MOD_PARAM):
320+
continue
321+
name = _normalize_char_name(f"characteristics[{match.group(1)}]")
322+
if name:
323+
extra.setdefault(f"x_sdrf_{name}", []).append(column)
324+
return extra
325+
326+
300327
def _replicate_number(value: str) -> int | None:
301328
"""An SDRF replicate as an integer: ``1`` and ``1.0`` give 1, other text and fractions None."""
302329
try:
@@ -446,6 +473,9 @@ def _write_runs(
446473
# Group by run file
447474
run_groups = sdrf_df.groupby("_run_file")
448475

476+
extra_comment_cols = _extra_run_comment_columns(sdrf_df.columns)
477+
extra_column_names = sorted(extra_comment_cols)
478+
449479
records: list[dict] = []
450480
for run_file, group in run_groups:
451481
run_file = str(run_file)
@@ -471,12 +501,14 @@ def _write_runs(
471501
"label_definitions": self._label_definitions(group),
472502
"gradient_length": _gradient_seconds(group),
473503
}
504+
for output_column, source_columns in extra_comment_cols.items():
505+
rec[output_column] = _merge_characteristic_values(group, source_columns)
474506
records.append(rec)
475507

476-
with RunWriter(output_path, creator=creator) as writer:
508+
with RunWriter(output_path, creator=creator, extra_columns=extra_column_names) as writer:
477509
writer.write_batch(records)
478510

479-
self.logger.info(f"Wrote {len(records)} runs to {output_path}")
511+
self.logger.info("Wrote %d runs (%d extra columns) to %s", len(records), len(extra_column_names), output_path)
480512

481513
def _run_acquisition_fields(self, group: pd.DataFrame) -> dict:
482514
"""Extract acquisition fields as plain strings, with their resolved terms in ``additional_terms``."""

‎qpx/core/data/schemas/run.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: run
22
file_type: run_file
33
primary_key: [run_file_name]
44
doc: "MS acquisition run metadata, one row per run"
5+
extra_columns: true
56

67
fields:
78
run_accession:

‎tests/converters/test_sdrf_run_metadata.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ def test_only_sentinel_modification_columns_leave_none(tmp_path):
5252
assert run["modification_parameters"] is None
5353

5454

55+
def test_unrecognized_comment_columns_are_kept_per_run(tmp_path):
56+
"""Unmapped SDRF comments become run extension columns rather than being dropped."""
57+
rows, _ = _convert_sdrf(
58+
tmp_path,
59+
["source name", "comment[data file]", "comment[label]", "comment[roi id]", "comment[plate identifier]"],
60+
[
61+
["S1", "run_01.raw", "label free sample", "R7", "plate A"],
62+
["S2", "run_02.raw", "label free sample", "R10", "plate B"],
63+
],
64+
)
65+
66+
assert [(row["x_sdrf_roi_id"], row["x_sdrf_plate_identifier"]) for row in rows] == [
67+
("R7", "plate A"),
68+
("R10", "plate B"),
69+
]
70+
71+
5572
def test_zero_padded_source_names_are_preserved_in_sample_and_run_outputs(tmp_path):
5673
"""Source names remain distinct text identifiers, while biological replicates are integers."""
5774
sdrf = tmp_path / "zero_padded.sdrf.tsv"

0 commit comments

Comments
 (0)