#!/usr/bin/env Rscript # Load necessary libraries suppressPackageStartupMessages(require(optparse)) suppressPackageStartupMessages(require(Seurat)) suppressPackageStartupMessages(require(Matrix)) # Load common functions source("./lib.R") # Function to initialize and parse options initialize_options <- function() { option_list <- list( make_option(c("-i", "--input"), action = "store", default = NA, type = 'character', help = "Seurat scRNA-seq RDS file"), make_option(c("-m", "--marker"), action = "store", default = NA, type = 'character', help = "Seurat marker genes RDS or T(C)SV file"), make_option(c("-o", "--output"), action = "store", default = ".", type = 'character', help = 'Output directory') ) parser <- OptionParser(option_list = option_list) # Check if no arguments are provided if (length(commandArgs(trailingOnly = TRUE)) == 0) { print_help(parser) stop("No arguments provided.") } opts <- parse_args(parser) return(opts) } # Function to check and create output directory if not exists check_create_output_dir <- function(output_dir) { if (!file.exists(output_dir)) { dir.create(output_dir) } } # Function to read input data files read_input_data <- function(input_path, marker_path) { seurat_object <- readRDS(input_path) # Determine the file extension of the marker file marker_file_extension <- tools::file_ext(marker_path) # Read marker data based on file extension if (marker_file_extension == "rds") { markers <- readRDS(marker_path) } else if (marker_file_extension == "tsv") { markers <- read.table(marker_path, header = TRUE, sep = "\t", row.names = 1) } else if (marker_file_extension == "csv") { # TODO: Need Auto Detect if row.names = 1 markers <- read.table(marker_path, header = TRUE, sep = ",") } else { stop("Invalid marker file extension. Please provide a .rds or .tsv file.") } return(list(seurat_object, markers)) } # Main function to process data and write outputs process_data <- function(seurat_object, markers, output_dir) { # Define output file separator file_sep <- '/' # Process and write PCA data pca <- Embeddings(seurat_object, reduction = "pca")[, 1:5] writeTable(pca, gzfile(paste(output_dir, "pca.csv.gz", sep = file_sep))) sink(gzfile(paste(output_dir, "pca.feat.txt.gz", sep = file_sep))) print(seurat_object[["pca"]]) sink() # Process and write meta data meta <- cbind(Embeddings(seurat_object$umap), seurat_object@meta.data) writeTable(meta, gzfile(paste(output_dir, "meta.csv.gz", sep = file_sep))) # Writing data.tidx file output_file <- paste(output_dir, "data.tidx", sep = file_sep) file_conn <- write_header(output_file, rownames(seurat_object), colnames(seurat_object)) # data <- t(GetAssayData(seurat_object, slot = "data")) # TODO : "RNA" is a hard code here, need to detect active assay name # data <- t(seurat_object[["RNA"]]$data) assay <- GetAssay(seurat_object) data <- t(assay$data) d <- dim(data) row_counts <- array(dim = d[2]) for (i in 1:(d[2] - 1)) { row_counts[i] <- data@p[i + 1] - data@p[i] } row_counts[d[2]] <- length(data@i) - data@p[d[2]] writeBin(as.integer(row_counts), file_conn, size = 4, useBytes = TRUE) writeBin(as.integer(data@i), file_conn, size = 4) writeBin(data@x, file_conn, size = 4) close(file_conn) # Set identity and write marker data seurat_object <- SetIdent(seurat_object, value = "seurat_clusters") writeTable(markers, gzfile(paste(output_dir, "markers.csv.gz", sep = file_sep))) # Generate and save UMAP plot as screenshot.png suppressPackageStartupMessages(require(ggplot2)) umap_plot <- DimPlot(seurat_object, reduction = "umap", group.by = "seurat_clusters") screenshot_path <- paste(output_dir, "screenshot.png", sep = file_sep) ggsave(filename = screenshot_path, plot = umap_plot) } # Main script execution opts <- initialize_options() check_create_output_dir(opts$output) data <- read_input_data(opts$input, opts$marker) process_data(data[[1]], data[[2]], opts$output)