aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-08-12 09:46:18 -0500
committerFrederick Muriuki Muriithi2024-08-12 16:15:13 -0500
commite3e3fb29b5e609723ece8b709fdebda6822eb891 (patch)
treef7195b23ae62b43faf6fe387dfc592c7660fe899
parent71c4a871ff01f6b23ab6a5872f7fcf1a018ab86a (diff)
downloadgn-uploader-e3e3fb29b5e609723ece8b709fdebda6822eb891.tar.gz
Add utility to transpose CSVs, renaming the original file.
-rw-r--r--r_qtl/r_qtl2.py24
1 files changed, 23 insertions, 1 deletions
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."""