diff --git a/DESCRIPTION b/DESCRIPTION index 30c07d57e2b50a57f594da9df52ee9422f21c526..35342786cd6f9294fcc61d3747df5b5cf08fe9ea 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,11 +1,16 @@ Package: rlabkeyCLI Type: Package -Title: What the Package Does (Title Case) -Version: 0.1.0 -Author: Who wrote it -Maintainer: The package maintainer <yourself@somewhere.net> -Description: More about what it does (maybe more than one line) - Use four spaces when indenting paragraphs within the Description. -License: What license is it under? +Title: rlabkey CLI R package +Version: 1.0.0 +Author: El Karchi Jad +Maintainer: jad.el-karchi@u-bordeaux.fr +Description: RLabKey CLI is a command-line interface designed for data managers to easily interact with the LabKey platform. + It allows fetching, pushing, and managing datasets and files using the Rlabkey package. +License: NONE +Imports: + Rlabkey (>= 3.2.2), + readxl (>= 1.4.3), + devtools (>= 2.4.5), Encoding: UTF-8 LazyData: true +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index d75f824ec6278db24891505b14ab3d915514dba7..9842004e591aa78b1bb4b53391481b79e4e06d02 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,5 @@ -exportPattern("^[[:alpha:]]+") +# Generated by roxygen2: do not edit by hand + +export(fetch) +export(push) +export(status) diff --git a/R/fetch.R b/R/fetch.R index 65e5e80deea944775fa96ea03f0f37faa493ca3e..8d975b4cff7184109595c42ce4b51272d1aa24dc 100644 --- a/R/fetch.R +++ b/R/fetch.R @@ -1,6 +1,3 @@ -# Load necessary library -library("Rlabkey") - # Base URLs BASE_URL <- labkey.getBaseUrl() @@ -14,7 +11,11 @@ check_path_exists <- function(folder_path, remote_file_path) { # Download an entire project download_project <- function(folder_path) { + local_base_dir <- file.path(Sys.getenv("HOME"), ".rlabkey") + if (!dir.exists(local_base_dir)) { + dir.create(local_base_dir, recursive = TRUE) + } message("Downloading folder to ", local_base_dir) labkey.webdav.downloadFolder( baseUrl = BASE_URL, @@ -23,19 +24,18 @@ download_project <- function(folder_path) { remoteFilePath = ".", overwriteFiles = TRUE, mergeFolders = TRUE, - fileSet = "@files", - showProgressBar = TRUE + fileSet = "@files" ) } # Download a specific file -download_file <- function(folder_path, file_name) { - local_file_path <- file.path(Sys.getenv("HOME"), ".rlabkey", "_webdav", folder_path, file_name) +download_file <- function(folder_path, remote_file_path) { + local_file_path <- file.path(Sys.getenv("HOME"), "_webdav", folder_path, remote_file_path) message("Downloading file to ", local_file_path) labkey.webdav.get( baseUrl = BASE_URL, folderPath = folder_path, - remoteFilePath = file_name, + remoteFilePath = remote_file_path, localFilePath = local_file_path, overwrite = TRUE, fileSet = "@files", @@ -44,46 +44,43 @@ download_file <- function(folder_path, file_name) { } # fetch function -fetch_data <- function(project_name, file_name = NULL) { +fetch_data <- function(project_name, remote_file_path) { folder_path <- paste0("/", project_name) - - if (is.null(file_name)) { + + if (is.null(remote_file_path)) { path_info <- check_path_exists(folder_path, ".") } else { - path_info <- check_path_exists(folder_path, file_name) + path_info <- check_path_exists(folder_path, remote_file_path) } - + if (!path_info$exists) { stop("The specified path does not exist on any configured server.") } - - - if (is.null(file_name)) { + + + if (is.null(remote_file_path)) { message("Fetching directory from: ", path_info$url, " ", path_info$project) download_project(project_name) } else { message("Fetching file from: ", path_info$url, path_info$project, path_info$filename) - download_file(folder_path, file_name) + download_file(folder_path, remote_file_path) } } -# main function -main <- function () { - args <- commandArgs(trailingOnly = TRUE) - if (length(args) < 1) { - stop("Usage: Rscript fetch.R <project_name> [<file_name>]") - } - - project_name <- args[1] - file_name <- if (length(args) >= 2) args[2] else NULL - - project_name - file_name - - - fetch_data(project_name, file_name) +#' Load a Matrix +#' +#' This function loads a file as a matrix. It assumes that the first column +#' contains the rownames and the subsequent columns are the sample identifiers. +#' Any rows with duplicated row names will be dropped with the first one being +#' kepted. +#' +#' @param infile Path to the input file +#' @return A matrix of the infile +#' @export +fetch <- function(projectName, remoteFilePath=NULL) { + project_name <- projectName + remote_file_path <- remoteFilePath + + fetch_data(project_name, remote_file_path) message("Data fetched successfully.") } - - -main() \ No newline at end of file diff --git a/R/push.R b/R/push.R index a4bddbf11cef1a3bf4ff81167c509584cbcf6c25..c996b84529bd8bc86a3b1a0e49a20710a0817bcc 100644 --- a/R/push.R +++ b/R/push.R @@ -1,5 +1,4 @@ -# Load necessary library -library("Rlabkey") +library(readxl) # Base URLs BASE_URL <- labkey.getBaseUrl() @@ -35,7 +34,7 @@ create_and_upload_dataset <- function(df, folder_path, schema_name, query_name) description = "" ) message("Domain created.") - + message("Uploading data ...") labkey.importRows( baseUrl = BASE_URL, @@ -44,19 +43,30 @@ create_and_upload_dataset <- function(df, folder_path, schema_name, query_name) queryName = query_name, toImport = df ) - + message(paste("Data uploaded to", folder_path, ", schema:", schema_name, ", query:", query_name)) } upload_data <- function(project_name, file_path, target_name, is_assay, is_dataset) { # Load the data - df <- read.csv(file_path, header = TRUE, sep = ",") - + file_ext <- tools::file_ext(file_path) + + # Read the file based on the extension + if (file_ext == "csv") { + df <- read.csv(file_path, header = TRUE, sep = ",") + } else if (file_ext == "tsv") { + df <- read.csv(file_path, header = TRUE, sep = "\t") + } else if (file_ext == "xlsx" || file_ext == "xls") { + df <- read_excel(file_path) + } else { + stop("Unsupported file type.") + } + # Initialize variables folder_path <- project_name schema_name <- "" query_name <- NULL - + # Define schema and queries based on the type if (is_assay) { upload_assay_data(df, folder_path, target_name) @@ -67,19 +77,25 @@ upload_data <- function(project_name, file_path, target_name, is_assay, is_datas } } -# main function -main <- function () { - args <- commandArgs(trailingOnly = TRUE) - if (length(args) < 1) { - stop("Usage: Rscript push.R <project_name> <file_path> <target_name> <is_assay> <is_dataset>") - } - - project_name <- args[1] - file_path <- if (length(args) >= 2) args[2] else NULL - target_name <- if (length(args) >= 3) args[3] else NULL - is_assay <- if (length(args) >= 4) as.logical(tolower(args[4])) else FALSE - is_dataset <- if (length(args) >= 5) as.logical(tolower(args[5])) else FALSE - +#' Load a Matrix +#' +#' This function loads a file as a matrix. It assumes that the first column +#' contains the rownames and the subsequent columns are the sample identifiers. +#' Any rows with duplicated row names will be dropped with the first one being +#' kepted. +#' +#' @param infile Path to the input file +#' @return A matrix of the infile +#' @export +push <- function (projectName, filePath, targetName, dirPath=NULL, isAssay=FALSE, isDataset=FALSE) { + + project_name <- projectName + file_path <- filePath + dir_path <- dirPath + target_name <- targetName + is_assay <- isAssay + is_dataset <- isDataset + if (!is.null(file_path) && !is.null(project_name) && !is.null(target_name) && xor(is_assay, is_dataset)) { upload_data(project_name, file_path, target_name, is_assay, is_dataset) message("Data uploaded successfully.") @@ -93,5 +109,3 @@ main <- function () { "\nis_dataset =", is_dataset)) } } - -main() \ No newline at end of file diff --git a/R/status.R b/R/status.R index 71be8e67d1f0c80987459a1253760687ae655d17..a3da47f0c9bff11591752ad60e436eb0b2857f99 100644 --- a/R/status.R +++ b/R/status.R @@ -1,12 +1,20 @@ -main <- function() { +#' Load a Matrix +#' +#' This function loads a file as a matrix. It assumes that the first column +#' contains the rownames and the subsequent columns are the sample identifiers. +#' Any rows with duplicated row names will be dropped with the first one being +#' kepted. +#' +#' @param infile Path to the input file +#' @return A matrix of the infile +#' @export +status <- function() { current_user_info <- labkey.whoAmI(baseUrl=NULL) message( paste("LabKey session status :"), - paste("Name:", current_user_info$displayName), - paste("Connected:", current_user_info$success), - paste("ID:", current_user_info$id), - paste("Email:", current_user_info$email) + paste("\n Name:", current_user_info$displayName), + paste("\n Connected:", current_user_info$success), + paste("\n ID:", current_user_info$id), + paste("\n Email:", current_user_info$email) ) } - -main() \ No newline at end of file diff --git a/man/fetch.Rd b/man/fetch.Rd index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..d1d73e3ae2c243bdae9da0fb81a62ed9bbb518a4 100644 --- a/man/fetch.Rd +++ b/man/fetch.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fetch.R +\name{fetch} +\alias{fetch} +\title{Load a Matrix} +\usage{ +fetch(projectName, remoteFilePath) +} +\arguments{ +\item{infile}{Path to the input file} +} +\value{ +A matrix of the infile +} +\description{ +This function loads a file as a matrix. It assumes that the first column +contains the rownames and the subsequent columns are the sample identifiers. +Any rows with duplicated row names will be dropped with the first one being +kepted. +} diff --git a/man/push.Rd b/man/push.Rd index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..d4d3b4263f621774ff03a0bc9603a8d76c47ad9b 100644 --- a/man/push.Rd +++ b/man/push.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/push.R +\name{push} +\alias{push} +\title{Load a Matrix} +\usage{ +push( + projectName, + filePath, + targetName, + dirPath = NULL, + isAssay = FALSE, + isDataset = FALSE +) +} +\arguments{ +\item{infile}{Path to the input file} +} +\value{ +A matrix of the infile +} +\description{ +This function loads a file as a matrix. It assumes that the first column +contains the rownames and the subsequent columns are the sample identifiers. +Any rows with duplicated row names will be dropped with the first one being +kepted. +} diff --git a/man/status.Rd b/man/status.Rd index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9576fec3383c92df9e2b3561919d980f14d982e3 100644 --- a/man/status.Rd +++ b/man/status.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/status.R +\name{status} +\alias{status} +\title{Load a Matrix} +\usage{ +status() +} +\arguments{ +\item{infile}{Path to the input file} +} +\value{ +A matrix of the infile +} +\description{ +This function loads a file as a matrix. It assumes that the first column +contains the rownames and the subsequent columns are the sample identifiers. +Any rows with duplicated row names will be dropped with the first one being +kepted. +} diff --git a/renv.lock b/renv.lock index d2b4a9cd0680ee2ccb4816f071ea1b80c1753226..db82af320ec09bc2febf14ac9974891d67795502 100644 --- a/renv.lock +++ b/renv.lock @@ -9,6 +9,104 @@ ] }, "Packages": { + "R6": { + "Package": "R6", + "Version": "2.5.1", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R" + ], + "Hash": "470851b6d5d0ac559e9d01bb352b4021" + }, + "Rcpp": { + "Package": "Rcpp", + "Version": "1.0.12", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "methods", + "utils" + ], + "Hash": "5ea2700d21e038ace58269ecdbeb9ec0" + }, + "Rlabkey": { + "Package": "Rlabkey", + "Version": "3.2.2", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "Rcpp", + "httr", + "jsonlite" + ], + "Hash": "4f57fb2006190d94ab53074a9fb7b5d4" + }, + "askpass": { + "Package": "askpass", + "Version": "1.2.0", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "sys" + ], + "Hash": "cad6cf7f1d5f6e906700b9d3e718c796" + }, + "curl": { + "Package": "curl", + "Version": "5.2.1", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R" + ], + "Hash": "411ca2c03b1ce5f548345d2fc2685f7a" + }, + "httr": { + "Package": "httr", + "Version": "1.4.7", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "R6", + "curl", + "jsonlite", + "mime", + "openssl" + ], + "Hash": "ac107251d9d9fd72f0ca8049988f1d7f" + }, + "jsonlite": { + "Package": "jsonlite", + "Version": "1.8.8", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "methods" + ], + "Hash": "e1b9c55281c5adc4dd113652d9e26768" + }, + "mime": { + "Package": "mime", + "Version": "0.12", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "tools" + ], + "Hash": "18e9c28c1d3ca1560ce30658b22ce104" + }, + "openssl": { + "Package": "openssl", + "Version": "2.2.0", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "askpass" + ], + "Hash": "2bcca3848e4734eb3b16103bc9aa4b8e" + }, "renv": { "Package": "renv", "Version": "1.0.7", @@ -18,6 +116,13 @@ "utils" ], "Hash": "397b7b2a265bc5a7a06852524dabae20" + }, + "sys": { + "Package": "sys", + "Version": "3.4.2", + "Source": "Repository", + "Repository": "CRAN", + "Hash": "3a1be13d68d47a8cd0bfd74739ca1555" } } } diff --git a/setup.R b/setup.R new file mode 100644 index 0000000000000000000000000000000000000000..8205218ba5f0cc7ba2a7f9700e97fa0d12cdd4d7 --- /dev/null +++ b/setup.R @@ -0,0 +1,5 @@ +devtools::document() + +devtools::load_all() + +devtools::install()