From 94fe97b01a3887209f4785a3d7bce6291ed3cf3d Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 19 May 2025 10:24:09 -0500 Subject: Read headers from a CSV file, whether transposed or not. --- r_qtl/r_qtl2.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'r_qtl/r_qtl2.py') diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index dfa84ba..dbf5a7b 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -580,3 +580,25 @@ def read_csv_file(filepath: Union[str, Path], if line.startswith(comment_char): continue yield tuple(field.strip() for field in line.split(separator)) + + +def read_csv_file_headers( + filepath: Union[str, Path], + transposed: bool, + separator: str = ",", + comment_char: str = "#" +) -> tuple[str, ...]: + """Read the 'true' headers of a CSV file.""" + headers = tuple() + for line in read_text_file(filepath): + if line.startswith(comment_char): + continue + + line = tuple(field.strip() for field in line.split(separator)) + if not transposed: + return line + + headers = headers + (line[0],) + continue + + return headers -- cgit v1.2.3