nextflow.enable.dsl=2 process CORTO { container "${params.container}" containerOptions "${params.containerOptions}" publishDir "${params.outdir}/${params.project_name}", mode: 'copy' debug true // maxForks 1 stageInMode 'copy' input: path gene_expression_matrix path centroid_list path cnv_data // This could be optional // Define output channels output: path "regulon.rda", emit: regulon path "*.csv", emit: csv_regulon path "*.log", optional: true, emit: logs // if you have log files script: """ #!/bin/bash # Create an R script cat < corto_analysis.R # Print the R version print(R.version.string) # Load the corto library library(corto) # Function to load data loadData <- function(file_name, expected_var) { load(file_name) if (exists(expected_var)) { data <- get(expected_var) } else { stop(paste("Object", expected_var, "not found in", file_name)) } return(data) } # Load the input matrix inmat <- loadData("${gene_expression_matrix}", "inmat") print("Dimensions of inmat before any operation:") print(dim(inmat)) # Load the centroids centroids <- loadData("${centroid_list}", "centroids") print("Length of centroids:") print(length(centroids)) # Run corto with specified parameters regulon <- corto(inmat, centroids=centroids, nbootstraps=10, p=1e-30, nthreads=2) # Save the regulon object for later use save(regulon, file="regulon.rda") # Transform regulon into a data frame regulon_to_df <- function(regulon) { result_df <- data.frame(TF = character(), Target = character(), TFMode = numeric(), Likelihood = numeric(), stringsAsFactors = FALSE) for (tf in names(regulon)) { tf_data <- regulon[[tf]] if (is.null(tf_data\$tfmode) || is.null(tf_data\$likelihood)) next for (i in seq_along(tf_data\$tfmode)) { tf_mode <- tf_data\$tfmode[[i]] likelihood <- tf_data\$likelihood[[i]] target_name <- names(tf_data\$tfmode)[i] tf_df <- data.frame(TF = tf, Target = target_name, TFMode = tf_mode, Likelihood = likelihood, stringsAsFactors = FALSE) result_df <- rbind(result_df, tf_df) } } return(result_df) } # Check if regulon is a list and transform it if (is.list(regulon)) { regulon_df <- regulon_to_df(regulon) write.csv(regulon_df, file="regulon.csv", row.names=FALSE) } else { warning("Regulon object is not a list. Custom transformation needed.") } EOF # Execute the R script Rscript corto_analysis.R """ }