Skill Tracer
Skills › Scientific Agent Skills › bulk-rnaseq › _tabular.py

_tabular.py

21 lines · skills/bulk-rnaseq/scripts/_tabular.py at 5f2bc03

1"""Check table headers before pandas can silently rename duplicate columns."""
2
3import csv
4from itertools import chain
5
6
7def validate_header(path, delimiter=",", comment=None):
8 with path.open(encoding="utf-8-sig", newline="") as handle:
9 first = next((line for line in handle if line.strip()
10 and not (comment and line.startswith(comment))), "")
11 if not first:
12 return # The main reader reports an empty file.
13 try:
14 if delimiter is None:
15 delimiter = csv.Sniffer().sniff(first, delimiters=",\t").delimiter
16 header = next(csv.reader(chain([first], handle), delimiter=delimiter))
17 except csv.Error as exc:
18 raise ValueError(f"Invalid table header: {exc}") from exc
19 normalized = [label.strip() for label in header]
20 if len(normalized) != len(set(normalized)):
21 raise ValueError("Duplicate column labels in input header; refusing automatic renaming")