Skip to content

Commit aad1b76

Browse files
committed
feat: update read_header to skip comments and empty lines
1 parent 7efec59 commit aad1b76

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

fgpyo/util/metric.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120

121121
MetricType = TypeVar("MetricType", bound="Metric")
122122

123+
DEFAULT_HEADER_COMMENT_CHAR = "#"
124+
123125

124126
@attr.s
125127
class Metric(ABC, Generic[MetricType]):
@@ -153,6 +155,7 @@ def read(
153155
cls,
154156
path: Path,
155157
ignore_extra_fields: bool = True,
158+
header_comment_char: str = DEFAULT_HEADER_COMMENT_CHAR,
156159
) -> Iterator[Any]:
157160
"""Reads in zero or more metrics from the given path.
158161
@@ -164,10 +167,12 @@ def read(
164167
Args:
165168
path: the path to the metrics file.
166169
ignore_extra_fields: True to ignore any extra columns, False to raise an exception.
170+
header_comment_char: Any lines beginning with this character will be ignored before
171+
parsing the header.
167172
"""
168173
parsers = cls._parsers()
169174
with io.to_reader(path) as reader:
170-
header: List[str] = Metric.read_header(reader)
175+
header: List[str] = Metric.read_header(reader, comment_char=header_comment_char)
171176

172177
# check the header
173178
class_fields = set(cls.header())
@@ -329,8 +334,27 @@ def fast_concat(*inputs: Path, output: Path) -> None:
329334
)
330335

331336
@staticmethod
332-
def read_header(reader: io.Reader) -> List[str]:
337+
def read_header(
338+
reader: io.Reader,
339+
comment_char: str = "#",
340+
) -> List[str]:
333341
"""
334342
Read the header from an open file.
343+
344+
Comment and empty lines will be ignored.
345+
346+
Args:
347+
reader: An open, readable file
348+
comment_char: The character which indicates the start of a comment line.
349+
350+
Returns:
351+
A list of field names found in the header line.
335352
"""
336-
return reader.readline().rstrip("\r\n").split("\t")
353+
354+
for line in reader:
355+
if not line.startswith(comment_char) and not line.strip() == "":
356+
break
357+
else:
358+
raise ValueError("No header found")
359+
360+
return line.rstrip("\r\n").split("\t")

0 commit comments

Comments
 (0)