Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CondaPkg.jl to manage deps #100

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ build/
test/files/metaphlan/merged*
test/files/humann/joined_roundtrip.tsv
Manifest.toml
.CondaPkg
7 changes: 7 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
channels = ["biobakery", "conda-forge", "bioconda"]

[deps]
metaphlan = ""
humann = ""
tbb = "=2020.2"
kneaddata = ""
7 changes: 3 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ keywords = ["microbiology", "microbiome", "biology", "metagenomics"]
license = "MIT"
desc = "Convenience functions for working with the bioBakery"
authors = ["kescobo <kevbonham@gmail.com>", "annelle-abatoni <aa1@wellesley.edu>", "anikaluo <al8@wellesley.edu>", "Vanja Klepac-Ceraj <vklepacc@wellesley.edu>"]
version = "0.6"
version = "0.6.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Microbiome = "3bd8f0ae-a0f2-5238-a5af-e1b399a4940c"
ReTest = "e0db7c4e-2690-44b9-bad6-7687da720f89"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Expand All @@ -17,9 +17,8 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
CSV = "0.8, 0.9, 0.10"
Conda = "1.5"
Microbiome = "0.9"
Reexport = "0.2, 1"
ReTest = "0.3"
Reexport = "0.2, 1"
Tables = "1.2.1"
julia = "1.6"
58 changes: 20 additions & 38 deletions docs/src/gettingstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ CurrentModule = BiobakeryUtils
This is a package for the [julia programming language](http://julialang.org),
designed for working with the [bioBakery](https://github.com/biobakery/biobakery) family of tools
for metagenomic analysis of microbial communities.
Currently, we support [`MetaPhlAn`](https://github.com/biobakery/MetaPhlAn) and [`HUMAnN`](https://github.com/biobakery/HUMAnN).
Currently, we support:

- [`KneadData`](https://github.com/biobakery/kneaddata)
- [`MetaPhlAn`](https://github.com/biobakery/MetaPhlAn)
- [`HUMAnN`](https://github.com/biobakery/HUMAnN).

Read on to learn how to install the package and use it
to begin using it to uncover insights about your microbial community data!
Expand Down Expand Up @@ -93,49 +97,27 @@ or `julia --project=<path to project>` if you're in a different working director

Some functions provided by this package (eg [`humann_regroup`](@ref) and [`humann_rename`](@ref)),
require the appropriate `bioBakery` tools to be installed and accessible from the julia `shell` environment.
The easiest way to do this is to use `Conda.jl`,
The easiest way to do this is to use `CondaPkg.jl`,
though other installation methods are possible as well.
If you have not previously installed these tools,
they will be installed automatically the first time you attempt to call them.
If you have, read on to find out how to use your own `conda` environment.

### Using a previous installation

If you have a previous installation of `metaphlan` and/or `humann`,
you can tell julia to use them by modifying the `$PATH` environment variable.

Environment variables in julia are stored in a `Dict` called `ENV`.
For example, the `$PATH` variable in Unix tells the shell where to look
for executable programs, and is available in julia using `ENV["PATH"]`

```@repl conda
ENV["PATH"]
```

If you launch julia from the shell,
this variable is automatically populated with the same `$PATH`,
so if you can access `humann` or `metaphlan` from your shell,
then launch julia, you should be all set
(eg, if you've installed them with miniconda, and you do `conda activate envname`,
then launch julia from the same shell, they should already be available).

If not, you need to identify where `humann` or `metaphlan` executables are located,
then add that location to `ENV["PATH"]` (delimeted by `:`).
For example, if the `humann` executable is found at `/home/kevin/.local/bin`,
you would run:
If you have a previous `conda` installation of `kneaddata`, `metaphlan`, `humann`,
you can tell julia to use them by activating your `conda` environment
prior to starting julia.

```@repl conda
ENV["PATH"] = ENV["PATH"] * ":" * "/home/kevin/.local/bin"
```

If you don't know where your installation is located,
from the terminal, you can use the `which` command:

```sh
$ which humann
/home/kevin/.local/bin/humann
```
Using non-conda installation is not currently supported,
though you can probably hack it together.
If you need this functionality, [open an issue](https://github.com/EcoJulia/BiobakeryUtils.jl/issues/new/choose).

### [Using Conda.jl](@id using-conda)
### [Using CondaPkg.jl](@id using-conda)

If you don't have a previous installation, you can use [`Conda.jl`](https://github.com/JuliaPy/Conda.jl) to install the necessary tools.
If you don't have a previous installation,
and you want to manage the dependencies directly,
you can use [`CondaPkg.jl`](https://github.com/JuliaPy/Conda.jl) to install the necessary tools.

This can be done automatically for you using [`BiobakeryUtils.install_deps()`](@ref).

Expand Down Expand Up @@ -254,7 +236,7 @@ for next steps.
So, you followed all the steps above, and you're still having problems?
There are a couple of common things that can go wrong.

### Cannot find `{program}`
### Can not find `{program}`

If you get an error that looks like this:

Expand Down
2 changes: 1 addition & 1 deletion src/BiobakeryUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ using Reexport
using CSV
using Tables
using SparseArrays
using Conda
using ReTest
using CondaPkg

include("utils.jl")
include("metaphlan.jl")
Expand Down
24 changes: 15 additions & 9 deletions src/humann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function humann(inputfile, output; kwargs...)
add_cli_kwargs!(c, kwargs; optunderscores=false)

@info "Running command: $(Cmd(c))"
return run(Cmd(c))
return CondaPkg.withenv() do; run(Cmd(c)) end
end

function _gf_parse(gf)
Expand Down Expand Up @@ -139,9 +139,12 @@ function humann_rename(comm::CommunityProfile; kind::String="ec")
ss = samples(comm)

CSV.write(in_path, comm; delim='\t')
run(```
humann_rename_table -i $in_path -n $kind -o $out_path
```)
CondaPkg.withenv() do
run(```
humann_rename_table -i $in_path -n $kind -o $out_path
```
)
end

return humann_profiles(out_path; samples=ss)
end
Expand All @@ -163,9 +166,12 @@ function humann_renorm(comm::CommunityProfile; units="cpm")
ss = samples(comm)

CSV.write(in_path, comm; delim='\t')
run(```
humann_renorm_table -i $in_path --units $units -o $out_path
```)
CondaPkg.withenv() do
run(```
humann_renorm_table -i $in_path --units $units -o $out_path
```
)
end

return humann_profiles(out_path; samples=ss)
end
Expand All @@ -187,7 +193,7 @@ function humann_join(in_path, out_path; file_name=nothing, search_subdirectories
search_subdirectories && push!(cmd, "--search-subdirectories")
verbose && push!(cmd, " --verbose")

run(Cmd(cmd))
CondaPkg.withenv() do; run(Cmd(cmd)) end
end

"""
Expand Down Expand Up @@ -272,5 +278,5 @@ function humann_barplot(comm::CommunityProfile, outpath; kwargs...)
"--last-metadata", string(last(keys(first(metadata(comm)))))]

add_cli_kwargs!(cmd, kwargs; optunderscores=false)
run(Cmd(cmd))
return CondaPkg.withenv() do; run(Cmd(cmd)) end
end
6 changes: 3 additions & 3 deletions src/kneaddata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function kneaddata(inputs, output; kwargs...)
add_cli_kwargs!(c, kwargs; optunderscores=false, skip=[:reference_db])

@info "Running command: $(Cmd(c))"
return run(Cmd(c))
return CondaPkg.withenv() do; run(Cmd(c)) end
end

"""
Expand All @@ -81,7 +81,7 @@ function kneaddata_database(db, kind, path)
c = ["kneaddata_database", "--download", db, kind, path]

@info "Running command: $(Cmd(c))"
return run(Cmd(c))
return CondaPkg.withenv() do; run(Cmd(c)) end
end

"""
Expand All @@ -98,6 +98,6 @@ function kneaddata_read_count_table(input, output)
c = ["kneaddata_read_count_table", "--input", input, "--output", output]

@info "Running command: $(Cmd(c))"
return run(Cmd(c))
return CondaPkg.withenv() do; run(Cmd(c)) end
end

4 changes: 2 additions & 2 deletions src/metaphlan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function metaphlan(inputfile, output; kwargs...)

deleteat!(cmd, findall(==(""), cmd))
@info "Running command: $(Cmd(cmd))"
return run(Cmd(cmd))
return CondaPkg.withenv() do; run(Cmd(cmd)) end
end

"""
Expand All @@ -64,7 +64,7 @@ function metaphlan_merge(paths, output; kwargs...)
end
end
append!(cmd, paths)
run(Cmd(cmd))
CondaPkg.withenv() do; run(Cmd(cmd)) end
end


Expand Down
70 changes: 37 additions & 33 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
"""
install_deps([env]; [force=false])
# """
# install_deps([env]; [force=false])

Uses Conda.jl to install HUMAnN and MetaPhlAn.
In order to use the commandline tools,
you must have the conda environment bin directory in `ENV["PATH"]`.
See "[Using Conda](@ref using-conda)" for more information.
"""
function install_deps(env=:BiobakeryUtils; force=false)
if isdir(Conda.bin_dir(env)) && !force
@warn """
You already seem to have an environment, '$env'.
If you've already installed the bioBakery packages, try
# Uses Conda.jl to install HUMAnN and MetaPhlAn.
# In order to use the commandline tools,
# you must have the conda environment bin directory in `ENV["PATH"]`.
# See "[Using Conda](@ref using-conda)" for more information.
# """
# function install_deps(env=:BiobakeryUtils; force=false)
# if isdir(Conda.bin_dir(env)) && !force
# @warn """
# You already seem to have an environment, '$env'.
# If you've already installed the bioBakery packages, try

`ENV["PATH"] = ENV["PATH"] * ":" * "$(Conda.bin_dir(env))"`
# `ENV["PATH"] = ENV["PATH"] * ":" * "$(Conda.bin_dir(env))"`

Use `force=true` to install anyway"""
return nothing
end
# Use `force=true` to install anyway"""
# return nothing
# end

Conda.add_channel("bioconda", env)
Conda.add_channel("conda-forge", env)
Conda.add_channel("biobakery", env)
# Conda.add_channel("bioconda", env)
# Conda.add_channel("conda-forge", env)
# Conda.add_channel("biobakery", env)

Conda.add("humann", env)
Conda.add("tbb=2020.2", env) # https://www.biostars.org/p/494922/
Conda.add("kneaddata", env)
# Conda.add("humann", env)
# Conda.add("tbb=2020.2", env) # https://www.biostars.org/p/494922/
# Conda.add("kneaddata", env)

@warn """
Don't forget to add $(Conda.bin_dir(env)) to your PATH!
# @warn """
# Don't forget to add $(Conda.bin_dir(env)) to your PATH!

This can be done in a julia session with:
# This can be done in a julia session with:

`ENV["PATH"] = ENV["PATH"] * ":" * "$(Conda.bin_dir(env))"`,
or you can set it in your shell environment.
"""
return nothing
end
# `ENV["PATH"] = ENV["PATH"] * ":" * "$(Conda.bin_dir(env))"`,
# or you can set it in your shell environment.
# """
# return nothing
# end

function add_cli_kwargs!(cmd, kwargs; optunderscores=true, skip = [])
for (key,val) in pairs(kwargs)
Expand All @@ -53,12 +53,16 @@ function add_cli_kwargs!(cmd, kwargs; optunderscores=true, skip = [])
end

function check_for_install(tool)
try run(pipeline(`which $tool`, stdout=devnull))
try
CondaPkg.withenv() do
run(pipeline(`which $tool`, stdout=devnull))
end
return nothing
catch e
@error """
Can not find `$tool`! If you think it should be installed,
try running:
Can not find `$tool`! This should have been installed automatically,
if you


```
ENV["PATH"] = ENV["PATH"] * ":" * Conda.bin_dir(env)
Expand Down
20 changes: 10 additions & 10 deletions test/BiobakeryUtilsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module BiobakeryUtilsTests
using Random
using ReTest
using BiobakeryUtils
using BiobakeryUtils.Conda
# using BiobakeryUtils.Conda
using SparseArrays
using DelimitedFiles
using CSV

isdir(Conda.bin_dir(:BiobakeryUtils)) || BiobakeryUtils.install_deps()
ENV["PATH"] = ENV["PATH"] * ":" * Conda.bin_dir(:BiobakeryUtils)
# isdir(Conda.bin_dir(:BiobakeryUtils)) || BiobakeryUtils.install_deps()
# ENV["PATH"] = ENV["PATH"] * ":" * Conda.bin_dir(:BiobakeryUtils)

@testset "CLI" begin
@testset "Utilities" begin
Expand All @@ -23,8 +23,8 @@ ENV["PATH"] = ENV["PATH"] * ":" * Conda.bin_dir(:BiobakeryUtils)
end

@testset "Metaphlan" begin
@test BiobakeryUtils.check_for_install("metaphlan") |> isnothing
@test BiobakeryUtils.check_for_install("merge_metaphlan_tables.py") |> isnothing
# @test BiobakeryUtils.check_for_install("metaphlan") |> isnothing
# @test BiobakeryUtils.check_for_install("merge_metaphlan_tables.py") |> isnothing

@test metaphlan("", ""; help=true).exitcode == 0

Expand All @@ -33,11 +33,11 @@ ENV["PATH"] = ENV["PATH"] * ":" * Conda.bin_dir(:BiobakeryUtils)
end

@testset "Humann" begin
@test BiobakeryUtils.check_for_install("humann") |> isnothing
@test BiobakeryUtils.check_for_install("humann_rename_table") |> isnothing
@test BiobakeryUtils.check_for_install("humann_renorm_table") |> isnothing
@test BiobakeryUtils.check_for_install("humann_join_tables") |> isnothing
@test BiobakeryUtils.check_for_install("humann") |> isnothing
# @test BiobakeryUtils.check_for_install("humann") |> isnothing
# @test BiobakeryUtils.check_for_install("humann_rename_table") |> isnothing
# @test BiobakeryUtils.check_for_install("humann_renorm_table") |> isnothing
# @test BiobakeryUtils.check_for_install("humann_join_tables") |> isnothing
# @test BiobakeryUtils.check_for_install("humann") |> isnothing
@test humann("", ""; help=true).exitcode == 0

end
Expand Down