Skip to content

Instantly share code, notes, and snippets.

@mkweskin
Last active February 16, 2024 18:45
Show Gist options
  • Save mkweskin/ab1f95ec7dd69e723fff96580f42f31a to your computer and use it in GitHub Desktop.
Save mkweskin/ab1f95ec7dd69e723fff96580f42f31a to your computer and use it in GitHub Desktop.
create a conda env with one bioconda package and all its dependencies
#!/bin/sh
# creates a new env for a bioconda package and its dependencies. Then, it display the list
# of newly installed binaries for the focal program.
# The directory where the env is created is: ~/share/apps/bioinformatics/[program]/conda/[version]/
if [ -z $1 ]; then
echo "error: give the name of the bioconda program to be installed when calling this program"
echo " example: $0 augustus"
exit 1
fi
# Set bioconda program name to install here:
program=$1
# Where your envs are located, The new env will be created in here
install_base=~/share/apps/bioinformatics
echo "Searching for $program in bioconda..."
# Look up version using mamba search. This is partly to create the install path which
# includes the version number
mamba search bioconda::$program 2>/dev/null >/tmp/search.$$ || search_error=TRUE
if [ ! -z $search_error ]; then
echo " ERROR: There was an error with finding $program in bioconda."
rm -f /tmp/search.$$
exit 1
fi
version=$(tail -n 1 /tmp/search.$$ | awk '{print $2}')
rm -f /tmp/search.$$
echo " found $version."
env_dir=$install_base/$program/conda/$version
# exit if there's already a directory in the destination
if [ -d $env_dir ]; then
echo "ERROR: the destination directory already exists:"
echo " $env_dir"
exit 1
fi
echo "Creating new env in: $env_dir"
# Install the package into the designated directory
# Note that the version ISN'T specified here. I want mamba to choose the version again.
mamba create -y -q -c bioconda -c conda-forge --override-channels -p $env_dir $program 2>/dev/null || create_error=TRUE
if [ ! -z $create_error ]; then
echo "ERROR: There was an error when creating the environment"
exit 1
fi
# List the programs that were installed for the focal program.
# Other files in the env's bin are not included.
# Check for the expected json file
json="$env_dir/conda-meta/$program-$version-*.json"
if [ ! -f $josn ]; then
echo "ERROR: something went wrong. The file $env_dir/conda-meta/$program-$version-*.json was expected, but now found."
echo " The version that was expected was $version, perhaps a different version was installed?"
exit 1
fi
echo "Executables now available for $program:"
grep -E '^ *.\"bin' $env_dir/conda-meta/$program-$version-*.json | sed -r -e 's/^ *"bin\///' -e 's/\",?$//' | column -c 80
echo "mamba activate $env_dir"
exit 0
@mkweskin
Copy link
Author

$ ./mamba_install.sh angsd
Searching for angsd in bioconda...
  found 0.940.
Creating new env in: /home/blah/share/apps/bioinformatics/angsd/conda/0.940
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
Executables now available for angsd:
NGSadmix	haploToPlink	ngsPSMC		smartCount
angsd		ibs		printIcounts	splitgl
contamination	msHOT2glf	realSFS		supersim
contamination2	msToGlf		scounts		thetaStat
mamba activate /home/blah/share/apps/bioinformatics/angsd/conda/0.940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment