INNER CODE UNIT · Python
_batch_labels
antigenomics/vdjtools · python/vdjtools/cli/__init__.py:410
def _batch_labels(batches: str, samples: "list[Path]") -> "list[str]":
"""A batch label per sample, from a comma-separated list or a two-column TSV."""
p = Path(batches)
if p.exists():
meta = pl.read_csv(p, separator="\t")
cols = {c.lower(): c for c in meta.columns}
sid_col = cols.get("sample_id") or cols.get("sample")
b_col = cols.get("batch")
if not sid_col or not b_col:
_err(f"{batches!r} needs sample_id and batch columns; have {meta.columns}")
m = dict(zip(meta[sid_col].to_list(), meta[b_col].to_list()))
missing = [s.stem for s in samples if s.stem not in m]
if missing:
_err(f"no batch for {missing[:5]} in {batches!r} (matched on file stem)")
return [str(m[s.stem]) for s in samples]
labels = [b.strip() for b in batches.split(",") if b.strip()]
if len(labels) != len(samples):
_err(f"--batches has {len(labels)} labels for {len(samples)} samples")