From e3e3fb29b5e609723ece8b709fdebda6822eb891 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 12 Aug 2024 09:46:18 -0500 Subject: Add utility to transpose CSVs, renaming the original file. --- r_qtl/r_qtl2.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'r_qtl') diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index c6282c5..fc6feb5 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -1,5 +1,6 @@ """The R/qtl2 parsing and processing code.""" import io +import os import csv import json from pathlib import Path @@ -54,7 +55,7 @@ def transpose_csv( inpath: Path, linesplitterfn: Callable, linejoinerfn: Callable, - outpath: Path): + outpath: Path) -> Path: """Transpose a file: Make its rows into columns and its columns into rows. This function will create a new file, `outfile`, with the same content as @@ -80,6 +81,27 @@ def transpose_csv( for line in transposed_data: outfile.write(line) + return outpath + + +def transpose_csv_with_rename(inpath: Path, + linesplitterfn: Callable, + linejoinerfn: Callable) -> Path: + """Renames input file and creates new transposed file with the original name + of the input file. + + Parameters + ---------- + inpath: Path to the input file. Should be a pathlib.Path object. + linesplitterfn: A function to use for splitting each line into columns + linejoinerfn: A function to use to rebuild the lines + """ + transposedfilepath = Path(inpath) + origbkp = inpath.parent.joinpath(f"{inpath.stem}___original{inpath.suffix}") + os.rename(inpath, origbkp) + return transpose_csv( + origbkp, linesplitterfn, linejoinerfn, transposedfilepath) + def __control_data_from_zipfile__(zfile: ZipFile) -> dict: """Retrieve the control file from the zip file info.""" -- cgit v1.2.3