The Herper package is a simple toolset to install and manage Conda
packages and environments from within the R console.
Unfortunately many tools for data analysis are not available in R, but
are present in public repositories like conda. With Herper users can
install, manage, record and run conda tools from the comfort of their R
session.
Furthermore, many R packages require the use of these external
dependencies. Again these dependencies can be installed and managed with
the Conda package repository. For example 169 Bioconductor packages have
external dependencies listed in their System Requirements field (often
with these packages having several requirements) [03 September, 2020].
Herper provides an ad-hoc approach to handling external system
requirements for R packages. For people developing packages with python
conda dependencies we recommend using
basilisk
to internally support these system requirements pre-hoc.
Use the BiocManager package to download and install the package from
our Github repository:
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
BiocManager::install("Herper")
Once installed, load it into your R session:
library(Herper)
Simple install of Conda packages from R console using install_CondaTools.
The install_CondaTools() function allows the user to specify
required Conda software and the desired environment to install into.
Miniconda is installed as part of the process (by default into the
r-reticulate’s default Conda location -
/Users/mattpaul/Library/r-miniconda) and the user’s requested conda
environment built within the same directory (by default
/Users/mattpaul/Library/r-miniconda/envs/USERS_ENVIRONMENT_HERE).
If you already have Miniconda installed or you would like to install to
a custom location, you can specify the path with the pathToMiniConda
parameter. In this example we are installing in a temporary directory,
but most likely you will want to install/use a stable version of
Miniconda.
Specific package versions can be installed using conda formatted
inputs into the tools argument i.e. “salmon==1.3”, “salmon>=1.3” or
“salmon<=1.3”. This can also be used to specifically upgrade or
downgrade existing tools in the chosen environment.
Install R package dependencies with install_CondaSysReqs.
The install_CondaSysReqs checks the System Requirements for the
specified R package, and uses Conda to install this software. Here we
will use a test package contained within Herper. This test package has
two System Requirements:
By default these packages are installed in a new environment, which has
the name name of the R package and its version number. Users can control
the environment name using the env parameter. As with
install_CondaTools(), user can control which version of Miniconda
with the parameter pathToMiniConda, and whether they want to amend an
existing environment with the parameter updateEnv.
Note: install_CondaSysReqs can handle standard System Requirement
formats, but will not work if the package has free form text. In this
case just use install_CondaTools
Using external software with the with_CondaEnv and local_CondaEnv functions.
Once installed within a conda environment, many external software can be
executed directly from the conda environment’s bin directory without
having to perform any additional actions.
pathToSalmon <- file.path(pathToConda$pathToEnvBin,"salmon")
Res <- system2(command=pathToSalmon, args = "-h",stdout = TRUE)
Res
## salmon v1.3.0
##
## Usage: salmon -h|--help or
## salmon -v|--version or
## salmon -c|--cite or
## salmon [--no-version-check] <COMMAND> [-h | options]
##
## Commands:
## index : create a salmon index
## quant : quantify a sample
## alevin : single cell analysis
## swim : perform super-secret operation
## quantmerge : merge multiple quantifications into a single file
Some external software however require additional environmental variable
to be set in order to execute correctly. An example of this would be
Cytoscape which requires the java home directory and java library
paths to be set prior to its execution.
The Herper package uses the withr family
of functions (with_CondaEnv() and local_CondaEnv()) to provide
methods to temporarily alter the system PATH and to add or update
any required environmental variables. This is done without formally
activating your environment or initializing your conda.
The with_CondaEnv allows users to run R code with the required PATH
and environmental variables automatically set. The with_CondaEnv
function simply requires the name of conda environment and the code to
be executed within this environment. Additionally we can also the
pathToMiniconda argument to specify any custom miniconda install
location.
The with_CondaEnv function will update the PATH we can now run the
above salmon command without specifying the full directory path to
salmon.
res <- with_CondaEnv("herper",
system2(command="salmon",args = "-h",stdout = TRUE),
pathToMiniConda=myMiniconda)
res
## salmon v1.3.0
##
## Usage: salmon -h|--help or
## salmon -v|--version or
## salmon -c|--cite or
## salmon [--no-version-check] <COMMAND> [-h | options]
##
## Commands:
## index : create a salmon index
## quant : quantify a sample
## alevin : single cell analysis
## swim : perform super-secret operation
## quantmerge : merge multiple quantifications into a single file
The local_CondaEnv function acts in a similar fashion to the
with_CondaEnv function and allows the user to temporarily update
the required PATH and environmental variable from within a function. The
PATH and environmental variables will be modified only until the current
function ends.
local_CondaEnv is best used within a user-created function,
allowing access to the Conda environment’s PATH and variables from
within the the function itself but resetting all environmental variables
once complete.
## salmon v1.3.0
##
## Usage: salmon -h|--help or
## salmon -v|--version or
## salmon -c|--cite or
## salmon [--no-version-check] <COMMAND> [-h | options]
##
## Commands:
## index : create a salmon index
## quant : quantify a sample
## alevin : single cell analysis
## swim : perform super-secret operation
## quantmerge : merge multiple quantifications into a single file
To further demonstrate this we will use the first command from the
seqCNA
vignette. This step requires samtools. If this is not installed and
available there is an error.
Samtools is listed as a System Requirement for seqCNA, so we can first
use install_CondaSysReqs() to install samtools. In this case we are
installing samtools in the environment: seqCNA_env. We can then run the
seqCNA command using with_CondaEnv specifying that we want to use
our environment containing samtools. seqCNA can then find samtools and
execute successfully.
## Basic information:
## SeqCNAInfo object with 5314 200Kbp-long windows.
## PEM information is not available.
## Paired normal is not available.
## Genome and build unknown (chromosomes chr1 to chr5).
## The profile is not yet normalized and not yet segmented.
Finding Conda packages with conda_search
If the user is unsure of the exact name, or version of a tool available
on conda, they can use the conda_search function.
Specific package versions can be searched for using the conda format
i.e. “salmon==1.3”, “salmon>=1.3” or “salmon<=1.3”. Searches will also
find close matches for incorrect queries. Channels to search in can be
controlled with channels parameter.
Export of Conda environments to YAML files using export_CondaEnv.
The export_CondaEnv function allows the user to export the
environment information to a .yml file. These environment YAML files
contain all essential information about the package, allowing for
reproducibility and easy distribution of Conda system configuration for
collaboration.
The YAML export will contain all packages in the environment by default.
If the user wants to only export the packages that were specifically
installed and not their dependencies they can use the depends
paramter.
Import of Conda environments from YAML files using import_CondaEnv.
The import_CondaEnv function allows the user to create a new conda
environment from a .yml file. These can be previously exported from
export_CondaEnv, conda, renv or manually created.
Users can simply provide a path to the YAML file for import. They can
also specify the environment name, but by default the name will be taken
from the YAML.
What is Herper?
The Herper package is a simple toolset to install and manage Conda packages and environments from within the R console.
Unfortunately many tools for data analysis are not available in R, but are present in public repositories like conda. With Herper users can install, manage, record and run conda tools from the comfort of their R session.
Furthermore, many R packages require the use of these external dependencies. Again these dependencies can be installed and managed with the Conda package repository. For example 169 Bioconductor packages have external dependencies listed in their System Requirements field (often with these packages having several requirements) [03 September, 2020].
Herper provides an ad-hoc approach to handling external system requirements for R packages. For people developing packages with python conda dependencies we recommend using basilisk to internally support these system requirements pre-hoc.
The Herper package was developed by Matt Paul, Doug Barrows and Thomas Carroll at the Rockefeller University Bioinformatics Resources Center with contributions from Kathryn Rozen-Gagnon.
Installation
Use the
BiocManagerpackage to download and install the package from our Github repository:Once installed, load it into your R session:
Simple install of Conda packages from R console using install_CondaTools.
The install_CondaTools() function allows the user to specify required Conda software and the desired environment to install into.
Miniconda is installed as part of the process (by default into the r-reticulate’s default Conda location - /Users/mattpaul/Library/r-miniconda) and the user’s requested conda environment built within the same directory (by default /Users/mattpaul/Library/r-miniconda/envs/USERS_ENVIRONMENT_HERE).
If you already have Miniconda installed or you would like to install to a custom location, you can specify the path with the pathToMiniConda parameter. In this example we are installing in a temporary directory, but most likely you will want to install/use a stable version of Miniconda.
We can add additional tools to our Conda environment by specifying updateEnv = TRUE. A vector of tools can be used to install several at once.
Specific package versions can be installed using conda formatted inputs into the tools argument i.e. “salmon==1.3”, “salmon>=1.3” or “salmon<=1.3”. This can also be used to specifically upgrade or downgrade existing tools in the chosen environment.
Install R package dependencies with install_CondaSysReqs.
The install_CondaSysReqs checks the System Requirements for the specified R package, and uses Conda to install this software. Here we will use a test package contained within Herper. This test package has two System Requirements:
The user can simply supply the name of an installed R package, and install_CondaSysReqs will install the System Requirements through conda.
By default these packages are installed in a new environment, which has the name name of the R package and its version number. Users can control the environment name using the env parameter. As with install_CondaTools(), user can control which version of Miniconda with the parameter pathToMiniConda, and whether they want to amend an existing environment with the parameter updateEnv.
Note: install_CondaSysReqs can handle standard System Requirement formats, but will not work if the package has free form text. In this case just use install_CondaTools
Using external software with the with_CondaEnv and local_CondaEnv functions.
Once installed within a conda environment, many external software can be executed directly from the conda environment’s bin directory without having to perform any additional actions.
Some external software however require additional environmental variable to be set in order to execute correctly. An example of this would be Cytoscape which requires the java home directory and java library paths to be set prior to its execution.
The Herper package uses the withr family of functions (with_CondaEnv() and local_CondaEnv()) to provide methods to temporarily alter the system PATH and to add or update any required environmental variables. This is done without formally activating your environment or initializing your conda.
The with_CondaEnv allows users to run R code with the required PATH and environmental variables automatically set. The with_CondaEnv function simply requires the name of conda environment and the code to be executed within this environment. Additionally we can also the pathToMiniconda argument to specify any custom miniconda install location.
The with_CondaEnv function will update the PATH we can now run the above salmon command without specifying the full directory path to salmon.
The local_CondaEnv function acts in a similar fashion to the with_CondaEnv function and allows the user to temporarily update the required PATH and environmental variable from within a function. The PATH and environmental variables will be modified only until the current function ends.
local_CondaEnv is best used within a user-created function, allowing access to the Conda environment’s PATH and variables from within the the function itself but resetting all environmental variables once complete.
To further demonstrate this we will use the first command from the seqCNA vignette. This step requires samtools. If this is not installed and available there is an error.
Samtools is listed as a System Requirement for seqCNA, so we can first use install_CondaSysReqs() to install samtools. In this case we are installing samtools in the environment: seqCNA_env. We can then run the seqCNA command using with_CondaEnv specifying that we want to use our environment containing samtools. seqCNA can then find samtools and execute successfully.
Finding Conda packages with conda_search
If the user is unsure of the exact name, or version of a tool available on conda, they can use the conda_search function.
Specific package versions can be searched for using the conda format i.e. “salmon==1.3”, “salmon>=1.3” or “salmon<=1.3”. Searches will also find close matches for incorrect queries. Channels to search in can be controlled with channels parameter.
Export of Conda environments to YAML files using export_CondaEnv.
The export_CondaEnv function allows the user to export the environment information to a .yml file. These environment YAML files contain all essential information about the package, allowing for reproducibility and easy distribution of Conda system configuration for collaboration.
The YAML export will contain all packages in the environment by default. If the user wants to only export the packages that were specifically installed and not their dependencies they can use the depends paramter.
Import of Conda environments from YAML files using import_CondaEnv.
The import_CondaEnv function allows the user to create a new conda environment from a .yml file. These can be previously exported from export_CondaEnv, conda, renv or manually created.
Users can simply provide a path to the YAML file for import. They can also specify the environment name, but by default the name will be taken from the YAML.
Checking for existing environments with list_CondaEnv
The list_CondaEnv function allows users to check what environments already exist within the given conda build.
If the User is using multiple builds of conda and wants to check environments across all them, they can include the parameter allCondas = TRUE.
Checking for packages in an environment with list_CondaPkgs
The list_CondaPkgs function allows users to check what packages are installed in a given environment.
Acknowledgements
Thank you to Ji-Dung Luo and Wei Wang for testing/vignette review/critical feedback and Ziwei Liang for their support.
Session Information