39
How to create and manage conda environments for reproducible bioinformatics analysis
I keep running into dependency conflicts when installing bioinformatics tools. Different tools require different Python versions and library versions. How should I use conda environments to isolate these dependencies, and how do I share my environment with collaborators so they can reproduce my analysis?
6 views
1 Answer
33
✓
✓ Accepted Answer
Conda environments are the standard solution for this. Use **mamba** (drop-in conda replacement) — it's 10× faster for solving environments.
```bash
# Install mamba into base
conda install -n base -c conda-forge mamba
# Create a new environment with specific Python version
mamba create -n rnaseq_env python=3.11
conda activate rnaseq_env
# Install tools (always specify channels: conda-forge > bioconda > defaults)
mamba install -c conda-forge -c bioconda
star=2.7.11
samtools=1.19
trimmomatic=0.39
fastqc
multiqc
subread # featureCounts
# Install R packages in the same environment
mamba install -c conda-forge -c bioconda bioconductor-deseq2 r-ggplot2
```
**Export environment for reproducibility:**
```bash
# Export full spec (exact versions + build strings) — 100% reproducible
conda env export > environment_full.yml
# Export cross-platform spec (no build strings) — more portable
conda env export --no-builds > environment.yml
# Export only manually installed packages
conda env export --from-history > environment_minimal.yml
```
**Share and recreate from yml:**
```bash
mamba env create -f environment.yml
# or
mamba env create -n new_env -f environment.yml
```
**Best practices:**
- One environment per project
- Always include versions in your yml: `star=2.7.11` not just `star`
- Use `conda-forge` before `bioconda` in channel priority
- For Docker-based reproducibility, use Snakemake + conda + `--use-conda` flag
- Add `environment.yml` to your git repository
```bash
# Snakemake auto-installs per-rule environments:
# rule star_align:
# conda: 'envs/star.yaml'
# ...
snakemake --use-conda --cores 8
```