create_env(
"bioconda::samtools==1.22.1",
env_name = "samtools-env",
channels = c("conda-forge", "bioconda")
)Running command line (CLI) tools from R can extend your analysis, but it often comes with challenges. How do you ensure that the tool is installed? How do you manage its dependencies without conflicting with other software on your system? How do you make your analysis reproducible for others?
condathis runs any CLI tool in an isolated, reproducible Conda environment, powered by micromamba.
Imagine you want to analyze a BAM file using samtools, a popular bioinformatics tool. You could write an R script that calls samtools using system(), but this approach has some drawbacks:
samtools being installed and available in the system’s PATH. If you share your script with a colleague, they will need to install it manually. They might install a different version, which could lead to different results.samtools and its dependencies might conflict with other tools already installed on the system.condathis Solutioncondathis solves this by creating isolated environments for your tools.
Create a Conda environment that contains samtools with create_env(). Pass the package specs first, here samtools 1.22.1 from the bioconda channel. Name the environment with env_name. channels lists, in priority order, the channels for the other packages: here conda-forge and then bioconda.
bioconda builds samtools, like its other tools with compiled code, for Linux and macOS only, so this example does not run on Windows. On Windows, run R in the Windows Subsystem for Linux (WSL), or use a tool that conda-forge builds for Windows.
create_env(
"bioconda::samtools==1.22.1",
env_name = "samtools-env",
channels = c("conda-forge", "bioconda")
)This command will:
micromamba if no usable copy is found. It is a single, self-contained executable that stays in the condathis data directory and does not interfere with your system.samtools-env.samtools into that environment.Now that we have our environment, we can use run() to execute samtools commands. condathis includes an example BAM file that we can use for this demonstration.
Let’s use samtools view to inspect the header of our example BAM file.
# Get the path to the example BAM file
bam_file <- system.file("extdata", "example.bam", package = "condathis")
# Run samtools view -H on the BAM file
run("samtools", "view", "-H", bam_file, env_name = "samtools-env")run() activates the environment and runs the command in it. The output of the command is printed to the R console. It ends with a line that records this call and the version of samtools that ran it:
@SQ SN:chr1 LN:249250621
@SQ SN:chr2 LN:243199373
...
@PG ID:samtools.2 PN:samtools PP:samtools.1 VN:1.22.1 CL:samtools view -H .../example.bam
To use the output in R instead, pass verbose = "silent" and read the result of run() with parse_output(), which returns one string per line.
Here is a complete, reproducible example.
# Load the package
library(condathis)
# Create an environment with samtools 1.22.1
create_env(
"bioconda::samtools==1.22.1",
env_name = "samtools-env",
channels = c("conda-forge", "bioconda")
)
# Get the path to the example BAM file
bam_file <- system.file("extdata", "example.bam", package = "condathis")
# Run samtools to view the header
run(
"samtools", "view", "-H", bam_file,
env_name = "samtools-env"
)
# Clean up the environment
remove_env(env_name = "samtools-env")condathis?By using condathis, you get:
"bioconda::samtools==1.22.1", and everyone who runs it gets the same tools.install_packages() adds packages to an existing environment, and list_packages() lists them.env_definition() creates an environment from a file, such as an environment YAML file or a lock file: create_env(env_definition("environment.yml"), env_name = "my-env"). A lock file, such as an @EXPLICIT or conda-lock file, installs the exact packages it lists, without a solve.run_pipeline() connects commands with pipes, as | does in a shell, each in its own environment if needed.env_path() lets run() use an environment created outside condathis.list_envs() lists the environments of condathis, remove_env() removes one, and clean_cache() deletes the downloaded package files.get_install_dir() shows the data directory of condathis, which holds micromamba, the environments and the package cache. To remove everything that condathis installed, see the section Removing everything in ?clean_cache.reticulate, how to stream binary data through pipelines, and how to choose a backend.