Title: | Run Any CLI Tool on a 'Conda' Environment |
---|---|
Description: | Simplifies the execution of command line interface (CLI) tools within isolated and reproducible environments. It enables users to effortlessly manage 'Conda' environments, execute command line tools, handle dependencies, and ensure reproducibility in their data analysis workflows. |
Authors: | Lucio Queiroz [aut, cre, cph] , Claudio Zanettini [aut, ctb] |
Maintainer: | Lucio Queiroz <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-01-09 06:41:30 UTC |
Source: | https://github.com/luciorq/condathis |
Create Conda Environment with specific packages installed to be used by run()
.
create_env( packages = NULL, env_file = NULL, env_name = "condathis-env", channels = c("bioconda", "conda-forge"), method = c("native", "auto"), additional_channels = NULL, platform = NULL, verbose = "silent", overwrite = FALSE )
create_env( packages = NULL, env_file = NULL, env_name = "condathis-env", channels = c("bioconda", "conda-forge"), method = c("native", "auto"), additional_channels = NULL, platform = NULL, verbose = "silent", overwrite = FALSE )
packages |
Character vector. Names of the packages, and
version strings if necessary, e.g. 'python=3.11'. The use of the |
env_file |
Character. Path to the YAML file with Conda Environment
description. If this argument is used, the |
env_name |
Character. Name of the Conda environment where the packages are going to be installed. Defaults to 'condathis-env'. |
channels |
Character vector. Names of the channels to be included. By default 'c("bioconda", "conda-forge")' are used for solving dependencies. |
method |
Character. Beckend method to run |
additional_channels |
Character. Additional Channels to be added to the default ones. |
platform |
Character. Platform to search for |
verbose |
Character string specifying the verbosity level of the function's output. Acceptable values are:
|
overwrite |
Logical. Should environment always be overwritten?
Defaults to |
An object of class list
representing the result of the command
execution. Contains information about the standard output, standard error,
and exit status of the command.
## Not run: condathis::with_sandbox_dir({ # Create a Conda environment and install the CLI `fastqc` in it. condathis::create_env( packages = "fastqc==0.12.1", env_name = "fastqc-env", verbose = "output" ) #> ! Environment fastqc-env succesfully created. }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ # Create a Conda environment and install the CLI `fastqc` in it. condathis::create_env( packages = "fastqc==0.12.1", env_name = "fastqc-env", verbose = "output" ) #> ! Environment fastqc-env succesfully created. }) ## End(Not run)
This function checks whether a specified Conda environment already exists
in the available environments. It returns TRUE
if the environment exists
and FALSE
otherwise.
env_exists(env_name)
env_exists(env_name)
env_name |
Character. Name of the Conda environment where the packages are going to be installed. Defaults to 'condathis-env'. |
Boolean. TRUE
if the environment exists and FALSE
otherwise.
## Not run: condathis::with_sandbox_dir({ # Create the environment condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) # Check if the environment exists condathis::env_exists("fastqc-env") #> [1] TRUE # Check for a non-existent environment condathis::env_exists("non-existent-env") #> [1] FALSE }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ # Create the environment condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) # Check if the environment exists condathis::env_exists("fastqc-env") #> [1] TRUE # Check for a non-existent environment condathis::env_exists("non-existent-env") #> [1] FALSE }) ## End(Not run)
Retrieve path to where environment should be created. Note: It retrieves the Path even if the environment is not created yet.
get_env_dir(env_name = "condathis-env")
get_env_dir(env_name = "condathis-env")
env_name |
Character. Name of the Conda environment where the packages are going to be installed. Defaults to 'condathis-env'. |
A character string indicating the path where environments will be created.
condathis::with_sandbox_dir({ # Get the default environment directory condathis::get_env_dir() #> "/path/to/condathis/envs/condathis-env" # Get the directory for a specific environment condathis::get_env_dir("my-env") #> "/path/to/condathis/envs/my-env" })
condathis::with_sandbox_dir({ # Get the default environment directory condathis::get_env_dir() #> "/path/to/condathis/envs/condathis-env" # Get the directory for a specific environment condathis::get_env_dir("my-env") #> "/path/to/condathis/envs/my-env" })
condathis
Data DirectoryRetrieves the installation directory for the condathis
package, creating it if it does not exist.
This function ensures that the package data directory complies with the freedesktop's XDG Base Directory Specification.
The base path can be controlled by the XDG_DATA_HOME
environment variable.
Additionally, on Windows, %LOCALAPPDATA%
is also accepted as the base installation directory.
get_install_dir()
get_install_dir()
If the directory does not exist, it will be created. On macOS,
special handling is applied to avoid spaces in the path,
as micromamba run
fails if there are spaces in the path (e.g., in ~/Library/Application Support/condathis
).
Therefore, Unix-style paths are used on macOS.
A character string representing the normalized, real path to the condathis
data directory.
condathis::with_sandbox_dir({ condathis::get_install_dir() #> /home/username/.local/share/condathis })
condathis::with_sandbox_dir({ condathis::get_install_dir() #> /home/username/.local/share/condathis })
This function retrieves the operating system (OS) name and the CPU architecture
of the current system. The output combines the OS and CPU architecture into
a single string in the format "<OS>-<Architecture>"
.
get_sys_arch()
get_sys_arch()
A character string indicating the operating system and CPU architecture,
e.g., "Darwin-x86_64"
or "Linux-aarch64"
.
# Retrieve the system architecture condathis::get_sys_arch() #> [1] "Darwin-x86_64"
# Retrieve the system architecture condathis::get_sys_arch() #> [1] "Darwin-x86_64"
condathis
Controlled PathDownloads and installs the Micromamba binaries in the path managed by the condathis
package.
Micromamba is a lightweight implementation of the Conda package manager and provides an efficient way
to create and manage conda environments.
install_micromamba( micromamba_version = "2.0.4-0", timeout_limit = 3600, download_method = "auto", force = FALSE )
install_micromamba( micromamba_version = "2.0.4-0", timeout_limit = 3600, download_method = "auto", force = FALSE )
micromamba_version |
Character string specifying the version of Micromamba to download.
Defaults to |
timeout_limit |
Numeric value specifying the timeout limit for downloading the Micromamba
binaries, in seconds. Defaults to |
download_method |
Character string passed to the |
force |
Logical. If set to TRUE, the download and installation of the
Micromamba binaries will be forced, even if they already exist in the
system or |
This function checks if Micromamba is already installed in the condathis
controlled path.
If not, it downloads the specified version from the official GitHub releases and installs it.
On Windows, it ensures the binary is downloaded correctly by setting the download mode to "wb"
.
If the download fails, appropriate error messages are displayed.
Invisibly returns the path to the installed Micromamba binary.
## Not run: condathis::with_sandbox_dir({ # Install the default version of Micromamba condathis::install_micromamba() # Install a specific version of Micromamba condathis::install_micromamba(micromamba_version = "2.0.2-2") # Force reinstallation of Micromamba condathis::install_micromamba(force = TRUE) }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ # Install the default version of Micromamba condathis::install_micromamba() # Install a specific version of Micromamba condathis::install_micromamba(micromamba_version = "2.0.2-2") # Force reinstallation of Micromamba condathis::install_micromamba(force = TRUE) }) ## End(Not run)
Install Packages in a Existing Conda Environment
install_packages( packages, env_name = "condathis-env", channels = c("bioconda", "conda-forge"), additional_channels = NULL, verbose = "silent" )
install_packages( packages, env_name = "condathis-env", channels = c("bioconda", "conda-forge"), additional_channels = NULL, verbose = "silent" )
packages |
Character vector with the names of the packages and version strings if necessary. |
env_name |
Name of the Conda environment where the packages are going to be installed. Defaults to 'condathis-env'. |
channels |
Character vector. Names of the channels to be included. By default 'c("bioconda", "conda-forge")' are used for solving dependencies. |
additional_channels |
Character. Additional Channels to be added to the default ones. |
verbose |
Character string specifying the verbosity level of the function's output. Acceptable values are:
|
An object of class list
representing the result of the command
execution. Contains information about the standard output, standard error,
and exit status of the command.
## Not run: condathis::with_sandbox_dir({ condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) # Install the package `python` in the `fastqc-env` environment. # It is not recommended to install multiple packages in the same environment, # # as it defeats the purpose of isolation provided by separate environments. condathis::install_packages(packages = "python", env_name = "fastqc-env") }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) # Install the package `python` in the `fastqc-env` environment. # It is not recommended to install multiple packages in the same environment, # # as it defeats the purpose of isolation provided by separate environments. condathis::install_packages(packages = "python", env_name = "fastqc-env") }) ## End(Not run)
This function retrieves a list of Conda environments installed in the Condathis environment directory. The returned value excludes any environments unrelated to Condathis, such as the base Conda environment itself.
list_envs(verbose = "silent")
list_envs(verbose = "silent")
verbose |
A character string indicating the verbosity level for the command.
Defaults to |
A character vector containing the names of installed Conda environments. If the command fails, the function returns the process exit status as a numeric value.
## Not run: condathis::with_sandbox_dir({ # Create environments condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) condathis::create_env( packages = "python", env_name = "python-env" ) # List environments condathis::list_envs() #> [1] "fastqc-env" "python-env" }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ # Create environments condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) condathis::create_env( packages = "python", env_name = "python-env" ) # List environments condathis::list_envs() #> [1] "fastqc-env" "python-env" }) ## End(Not run)
This function retrieves a list of all packages installed in the specified Conda environment. The result is returned as a tibble with detailed information about each package, including its name, version, and source details.
list_packages(env_name = "condathis-env", verbose = "silent")
list_packages(env_name = "condathis-env", verbose = "silent")
env_name |
Character. The name of the Conda environment where the tool will be run. Defaults to |
verbose |
Character string specifying the verbosity level of the function's output. Acceptable values are:
|
A tibble containing all the packages installed in the specified environment, with the following columns:
The base URL of the package source.
The build number of the package.
The build string describing the package build details.
The channel from which the package was installed.
The distribution name of the package.
The name of the package.
The platform for which the package is built.
The version of the package.
## Not run: condathis::with_sandbox_dir({ # Creates a Conda environment with the CLI `fastqc` condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) # Lists the packages in env `fastqc-env` dat <- condathis::list_packages("fastqc-env") dim(dat) #> [1] 34 8 }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ # Creates a Conda environment with the CLI `fastqc` condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) # Lists the packages in env `fastqc-env` dat <- condathis::list_packages("fastqc-env") dim(dat) #> [1] 34 8 }) ## End(Not run)
micromamba
ExecutableThis function returns the file path to the micromamba
executable managed
by the condathis
package. The path is determined based on the system's
operating system and architecture.
micromamba_bin_path()
micromamba_bin_path()
A character string representing the full path to the micromamba
executable.
The path differs depending on the operating system:
<install_dir>/micromamba/Library/bin/micromamba.exe
<install_dir>/micromamba/bin/micromamba
condathis::with_sandbox_dir({ # Retrieve the path to where micromamba executable is searched micromamba_path <- condathis::micromamba_bin_path() print(micromamba_path) })
condathis::with_sandbox_dir({ # Retrieve the path to where micromamba executable is searched micromamba_path <- condathis::micromamba_bin_path() print(micromamba_path) })
This function processes the result of a run()
call by parsing
the specified output stream (stdout
or stderr
) into individual, trimmed lines.
parse_output(res, stream = c("stdout", "stderr"))
parse_output(res, stream = c("stdout", "stderr"))
res |
A list containing the result of |
stream |
A character string specifying the data stream to parse. Must be either
|
A character vector where each element is a trimmed line from the specified stream.
# Example result object from condathis::run() res <- list( stdout = "line1\nline2\nline3\n", stderr = "error1\nerror2\n" ) # Parse the standard output parse_output(res, stream = "stdout") # Parse the standard error parse_output(res, stream = "stderr")
# Example result object from condathis::run() res <- list( stdout = "line1\nline2\nline3\n", stderr = "error1\nerror2\n" ) # Parse the standard output parse_output(res, stream = "stdout") # Parse the standard error parse_output(res, stream = "stderr")
Remove a Conda environment previously created by create_env()
.
remove_env(env_name = "condathis-env", verbose = "silent")
remove_env(env_name = "condathis-env", verbose = "silent")
env_name |
Character. Name of the Conda environment where the packages are going to be installed. Defaults to 'condathis-env'. |
verbose |
Character string specifying the verbosity level of the function's output. Acceptable values are:
|
An object of class list
representing the result of the command
execution. Contains information about the standard output, standard error,
and exit status of the command.
## Not run: condathis::with_sandbox_dir({ condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) condathis::remove_env(env_name = "fastqc-env") }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ condathis::create_env( packages = "fastqc", env_name = "fastqc-env" ) condathis::remove_env(env_name = "fastqc-env") }) ## End(Not run)
This function allows the execution of command-line tools within a specified Conda environment.
It runs the provided command in the designated Conda environment using the Micromamba binaries managed by the condathis
package.
run( cmd, ..., env_name = "condathis-env", method = c("native", "auto"), verbose = c("silent", "cmd", "output", "full", FALSE, TRUE), error = c("cancel", "continue"), stdout = "|", stderr = "|" )
run( cmd, ..., env_name = "condathis-env", method = c("native", "auto"), verbose = c("silent", "cmd", "output", "full", FALSE, TRUE), error = c("cancel", "continue"), stdout = "|", stderr = "|" )
cmd |
Character. The main command to be executed in the Conda environment. |
... |
Additional arguments to be passed to the command. These arguments will be passed directly to the command executed in the Conda environment. File paths should not contain special characters or spaces. |
env_name |
Character. The name of the Conda environment where the tool will be run. Defaults to |
method |
Character string. The method to use for running the command. Options are |
verbose |
Character string specifying the verbosity level of the function's output. Acceptable values are:
|
error |
Character string. How to handle errors. Options are |
stdout |
Default: "|" keep stdout to the R object
returned by |
stderr |
Default: "|" keep stderr to the R object
returned by |
The run()
function provides a flexible way to execute command-line tools within Conda environments.
This is particularly useful for reproducible research and ensuring that specific versions of tools are used.
An object of class list
representing the result of the command execution.
Contains information about the standard output, standard error, and exit status of the command.
install_micromamba
, create_env
## Not run: condathis::with_sandbox_dir({ ## Create env create_env("samtools", env_name = "samtools-env") ## Run a command in a specific Conda environment samtools_res <- run("samtools", "view", fs::path_package("condathis", "extdata", "example.bam"), env_name = "samtools-env" ) parse_output(samtools_res)[1] #> [1] "SOLEXA-1GA-1_6_FC20ET7:6:92:473:531\t0\tchr1\t10156..." }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ ## Create env create_env("samtools", env_name = "samtools-env") ## Run a command in a specific Conda environment samtools_res <- run("samtools", "view", fs::path_package("condathis", "extdata", "example.bam"), env_name = "samtools-env" ) parse_output(samtools_res)[1] #> [1] "SOLEXA-1GA-1_6_FC20ET7:6:92:473:531\t0\tchr1\t10156..." }) ## End(Not run)
Executes a binary command from a specified Conda environment without
activating the environment or using its environment variables.
This function temporarily clears Conda and Mamba-related environment
variables to prevent interference, ensuring that the command runs in a clean
environment.
Usually this is not what the user wants as this mode of execution does not
load environment variables and scripts defined in the environment
activate.d
, check run()
for the stable function to use.
run_bin( cmd, ..., env_name = "condathis-env", verbose = "silent", error = c("cancel", "continue"), stdout = "|", stderr = "|" )
run_bin( cmd, ..., env_name = "condathis-env", verbose = "silent", error = c("cancel", "continue"), stdout = "|", stderr = "|" )
cmd |
Character. The main command to be executed in the Conda environment. |
... |
Additional arguments to be passed to the command. These arguments will be passed directly to the command executed in the Conda environment. File paths should not contain special characters or spaces. |
env_name |
Character. The name of the Conda environment where the tool will be run. Defaults to |
verbose |
Character string specifying the verbosity level of the function's output. Acceptable values are:
|
error |
Character string. How to handle errors. Options are |
stdout |
Default: "|" keep stdout to the R object
returned by |
stderr |
Default: "|" keep stderr to the R object
returned by |
An object of class list
representing the result of the command
execution. Contains information about the standard output, standard error,
and exit status of the command.
## Not run: condathis::with_sandbox_dir({ # Example assumes that 'my-env' exists and contains 'python' # Run 'python' with a script in 'my-env' environment condathis::run_bin( "python", "-c", "import sys; print(sys.version)", env_name = "my-env", verbose = "output" ) # Run 'ls' command with additional arguments condathis::run_bin("ls", "-la", env_name = "my-env") }) ## End(Not run)
## Not run: condathis::with_sandbox_dir({ # Example assumes that 'my-env' exists and contains 'python' # Run 'python' with a script in 'my-env' environment condathis::run_bin( "python", "-c", "import sys; print(sys.version)", env_name = "my-env", verbose = "output" ) # Run 'ls' command with additional arguments condathis::run_bin("ls", "-la", env_name = "my-env") }) ## End(Not run)
Runs user-defined code inside a temporary directory, setting up a temporary
working environment. This function is intended for use in examples and tests
and ensures that no data is written to the user's file space.
Environment variables such as HOME
, APPDATA
, R_USER_DATA_DIR
,
XDG_DATA_HOME
, LOCALAPPDATA
, and USERPROFILE
are redirected to
temporary directories.
with_sandbox_dir(code, .local_envir = base::parent.frame())
with_sandbox_dir(code, .local_envir = base::parent.frame())
code |
expression An expression containing the user-defined code to be executed in the temporary environment. |
.local_envir |
environment The environment to use for scoping. |
This function is not designed for direct use by package users. It is primarily used to create an isolated environment during examples and tests. The temporary directories are created automatically and cleaned up after execution.
Returns NULL
invisibly.
condathis::with_sandbox_dir(print(fs::path_home())) condathis::with_sandbox_dir(print(tools::R_user_dir("condathis")))
condathis::with_sandbox_dir(print(fs::path_home())) condathis::with_sandbox_dir(print(tools::R_user_dir("condathis")))