Package: conda-deps
PurposeThe goal ofconda_depsis to generate aconda environment fileas a result of
the dependencies found in a repository. At the moment, it only translates Python and R dependencies
but it would be great to have it working for other programming languages as well.conda_depstranslates import statements in Python source code like:import numpy
import scipyinto an environment file:name: testenv

channels:
- conda-forge
- bioconda
- defaults

dependencies:
- python
- numpy
- scipyFor R it translates library imports like:library(reshape2)
library(ggplot2)into:name: testenv

channels:
- conda-forge
- bioconda
- defaults

dependencies:
- r-base
- r-reshape2    
- r-ggplot2WarningPlease note thatconda_depsdoes not check dependencies in a clever way. For example, if your code importsscipyandnumpy, the script will generate an environment with both listed even thoughnumpyisa dependency ofscipyand only the latter would be required. So the expected output ofconda_depsis a direct translation of the dependencies found in your code.Installationconda_depsonly works inPython 3and will only scan properlyPython 3source code.
There should be no restriction in the case of R.conda_depshas been uploaded toconda-forgeso you can install it with:# if you don't have conda available:
curl -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p conda-install
source conda-install/etc/profile.d/conda.sh 
conda update --all --yes

# once conda is available:
conda create --name conda_deps --channel conda-forge conda_deps
conda activate conda_deps
conda_deps --helpUsageThis is how you scan a single Python or R file:conda_deps </path/to/filename>The script can also scan folders:conda_deps </path/to/folder/>In case you want to exclude one or more subfolders, use the--exclude-folderoption one or more times:conda_deps --exclude-folder </path/to/folder/folder1> </path/to/folder>You may also want to scan additonal files of folders:conda_deps </path/to/folder> --include-files my-script.py --include-files </another/folder>How it worksPython source codeThe script usesPython's Abstract Syntax Treesto parse files ending in.py. It looks forimport <module>statements, and discards the modules belonging to the
Python Standard Library (e.g.import os). It assumes that<module>has a corresponding conda package
with the same name (e.g.import numpycorresponds toconda install numpy). However, that is not
always the case and you can provide a proper translation between the module name and its corresponding
conda package (e.g.import yamlwill requireconda install pyyaml) via thepython_deps.jsonfile, which
will be loaded into a dictionary at the beginning of the script. It looks like this:{
    "Bio":"biopython",
    "Cython":"cython",
    "bs4":"beautifulsoup4",
    "bx":"bx-python",
    "lzo":"python-lzo",
    "pyBigWig":"pybigwig",
    "sklearn":"scikit-learn",
    "web":"web.py",
    "weblogolib":"python-weblogo",
    "yaml":"pyyaml"
}The dictionary key is the name inimport <module>and the value is the name of the conda package.Thepython_deps.jsonfile is meant to be useful for generic use. However, it is possible to include
additional json files specific to your project:conda_deps --include-py-json my_project.json </path/to/project/>The translations inmy_project.jsonwill take priority over those inpython_deps.json.If you find that there are missing translations in the general purposepython_deps.jsonfile, please
feel free to open a pull request to add more.R source codeIn the case of R files, it usesgrepto look forlibrary(name)regular expressions in files ending in.R.
The same way we use ajsonfile to detail translations for Python,
we use ther_deps.jsonfile which will be loaded into a dictionary at the beginning of the script. Here is how it looks like:{
    "dplyr":"r-dplyr",
    "edgeR":"bioconductor-edger",
    "flashClust":"r-flashclust",
    "gcrma":"bioconductor-gcrma",
    "ggplot2":"r-ggplot2",
    "gplots":"r-gplots",
    "gridExtra":"r-gridextra",
    "grid":"r-gridbase",
    "gtools":"r-gtools",
    "hpar":"bioconductor-hpar",
    "knitr":"r-knitr",
    "limma":"bioconductor-limma",
    "maSigPro":"bioconductor-masigpro",
}In this case the dictionary key is the name inlibrary(name)and the value is the name of the conda package.If you are missing a translation inr_deps.jsonyou can either open a pull request to add it or include it
in your own json file:conda_deps --include-r-json my_project.json </path/to/project/>Please note that the translations inmy_project.jsonwill take priority over those inr_deps.json.WarningAn important point to bear in mind is that the translations for both Python and R are not comprehensive and are mainly based in the dependencies used in the past. It will be a matter of time to keep adding new dependencies to the json files in charge of the translation. This implies that the environment file produced as output may not be valid straight away and conda will complain about that when creating the environment (i.e. error message:PackagesNotFoundError).Related toolssnakefood: a more comprehensive tool but it works only with Python 2.pipreqs: does a similar job but forrequirements.txtfiles and pip.Referenceshttps://docs.python.org/3/library/ast.html#module-asthttp://bit.ly/2rDf5xuhttp://bit.ly/2r0Uv9thttps://github.com/titusjan/astviewerChangelogv0.0.9:Scan.Rmdand.ipynbfiles as well, therefore the script now depends onnbconvertAble to scan Python imports with multiple modules (e.g.import numpy, matplotlib)Add new R dependencies to the json dictionaryScanrmagicandcythonmagicin.ipynbfiles (#1)v0.0.8:Add new R dependenciesv0.0.7:Improve the test to check whether a module belongs to the Python Standard LibraryAdd new R dependencies to the json dictionaryv0.0.6:Add sanity check for dependency translationImprove regex to translate Bioconductor dependenciesv0.0.5:Addr_deps.jsonto manifest fileRename option--include-py-filesto--include-filesv0.0.4:Add translation of R dependenciesNot uploaded to conda-forge due to missingr_deps.jsonin the manifest filev0.0.3: minor bugfixes.v0.0.2: first working version uploaded to conda-forge.