diff --git a/fgpyo/util/metric.py b/fgpyo/util/metric.py index d3e8d8e6..abceaa20 100644 --- a/fgpyo/util/metric.py +++ b/fgpyo/util/metric.py @@ -214,7 +214,7 @@ def read( cls, path: Path, ignore_extra_fields: bool = True, - comment_prefix: str = "#", + comment_prefix: Optional[str] = None, ) -> Iterator["Metric[MetricType]"]: """Reads in zero or more metrics from the given path. @@ -397,7 +397,7 @@ def fast_concat(*inputs: Path, output: Path) -> None: def _read_header( reader: TextIOWrapper, delimiter: str = "\t", - comment_prefix: str = "#", + comment_prefix: Optional[str] = None, ) -> MetricFileHeader: """ Read the header from an open file. @@ -422,12 +422,17 @@ def _read_header( preamble: List[str] = [] for line in reader: - if line.strip().startswith(comment_prefix) or line.strip() == "": - # Skip any commented or empty lines before the header - preamble.append(line.strip()) + line = line.strip() + + if line == "": + # Skip any empty lines before the header + preamble.append(line) + elif comment_prefix is not None and line.startswith(comment_prefix): + # Skip any commented lines before the header + preamble.append(line) else: # The first line with any other content is assumed to be the header - fieldnames = line.strip().split(delimiter) + fieldnames = line.split(delimiter) break else: # If the file was empty, kick back an empty header diff --git a/tests/fgpyo/util/test_metric.py b/tests/fgpyo/util/test_metric.py index aed6a11a..0f0f8049 100644 --- a/tests/fgpyo/util/test_metric.py +++ b/tests/fgpyo/util/test_metric.py @@ -926,4 +926,4 @@ def test_read_validates_no_header( metrics_file.writelines(lines) with pytest.raises(ValueError, match="No header found"): - [m for m in data_and_classes.DummyMetric.read(metrics_path)] + [m for m in data_and_classes.DummyMetric.read(metrics_path, comment_prefix="#")]