Squashed initial commit
This commit is contained in:
54
qhtcp-workflow/apps/r/CompileGTF.R
Normal file
54
qhtcp-workflow/apps/r/CompileGTF.R
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env Rscript
|
||||
|
||||
# Load the openxlsx package
|
||||
library(openxlsx)
|
||||
|
||||
args <- commandArgs(TRUE)
|
||||
|
||||
outDir <- args[1]
|
||||
|
||||
# Function to combine CSV and TXT files into a workbook with named sheets
|
||||
combineFilesToWorkbook <- function(file_list, output_file) {
|
||||
# Create a new workbook
|
||||
wb <- createWorkbook()
|
||||
|
||||
# Loop through the file list
|
||||
for (i in seq_along(file_list)) {
|
||||
file <- file_list[i]
|
||||
|
||||
# Extract the sheet name from the file name based on the extension
|
||||
ext <- tools::file_ext(file)
|
||||
sheet_name <- tools::file_path_sans_ext(basename(file))
|
||||
|
||||
# Read the data based on the file extension
|
||||
if (ext %in% c("csv", "txt")) {
|
||||
if (ext == "csv") {
|
||||
data <- read.csv(file)
|
||||
} else if (ext == "txt") {
|
||||
data <- readLines(file)
|
||||
}
|
||||
|
||||
# Create a new sheet in the workbook with the extracted sheet name
|
||||
addWorksheet(wb, sheetName = sheet_name)
|
||||
|
||||
# Write the data to the sheet
|
||||
writeData(wb, sheet = sheet_name, x = data)
|
||||
}
|
||||
}
|
||||
|
||||
# Save the workbook as an Excel file
|
||||
saveWorkbook(wb, output_file)
|
||||
}
|
||||
|
||||
Component <- file.path(outDir, "Component", "ComponentResults.txt")
|
||||
Function <- file.path(outDir, "Function", "FunctionResults.txt")
|
||||
Process <- file.path(outDir, "Process", "ProcessResults.txt")
|
||||
|
||||
# Specify the list of input files (both CSV and TXT)
|
||||
file_list <- c(Component, Process, Function)
|
||||
|
||||
# Specify the output file name
|
||||
output_file <- file.path(outDir, "GTFCombined.xlsx")
|
||||
|
||||
# Call the function to combine the files into a workbook with named sheets
|
||||
combineFilesToWorkbook(file_list, output_file)
|
||||
Reference in New Issue
Block a user