Skip to content

Commit

Permalink
Add function to join community profiles (#58)
Browse files Browse the repository at this point in the history
* add function to join community profiles

* add news, bump minor version

* add tests
  • Loading branch information
kescobo authored Jul 15, 2021
1 parent c1a997e commit dc9b7fc
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News for Microbiome.jl

## Unreleased

- Add `commjoin` function to merge the contents of multiple `CommunityProfile`s ([#58](https://github.com/BioJulia/Microbiome.jl/pull/58))

## v0.7.0 Major overhaul

Lots of breaking changes in this one, including
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ keywords = ["microbiology", "microbiome", "biology"]
license = "MIT"
desc = "Functions and types for working with microbial community data"
authors = ["@kescobo <[email protected]>"]
version = "0.7.1"
version = "0.7.2"

[deps]
AxisIndices = "f52c9ee2-1b1c-4fd8-8546-6350938c7f11"
Expand Down
1 change: 1 addition & 0 deletions docs/src/profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ samples
features
samplenames
featurenames
commjoin
relativeabundance
relativeabundance!
present
Expand Down
4 changes: 3 additions & 1 deletion src/Microbiome.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export CommunityProfile,
clades,
profiletype,
featuretotals,
sampletotals
sampletotals,
commjoin

# Abundances
export present,
Expand Down Expand Up @@ -73,5 +74,6 @@ include("ecobase.jl")
include("samples_features.jl")
include("profiles.jl")
include("diversity.jl")
include("comm_joins.jl")

end # module Microbiome
73 changes: 73 additions & 0 deletions src/comm_joins.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
commjoin(c1::CommunityProfile, comms::CommunityProfile...)
Join multiple `CommunityProfile`s, creating a new `CommunityProfile`.
For now, sample names cannot overlap in any of the input profiles.
```jldoctest
julia> mss = [MicrobiomeSample("sample\$i") for i in 1:15];
julia> txs = [Taxon("taxon\$i") for i in 1:20];
julia> cm1 = CommunityProfile(spzeros(10,5), txs[1:10], mss[1:5])
CommunityProfile{Float64, Taxon, MicrobiomeSample} with 10 things in 5 places
Thing names:
taxon1, taxon2, taxon3...taxon9, taxon10
Place names:
sample1, sample2, sample3, sample4, sample5
julia> cm2 = CommunityProfile(spzeros(10,5), txs[6:15], mss[6:10])
CommunityProfile{Float64, Taxon, MicrobiomeSample} with 10 things in 5 places
Thing names:
taxon6, taxon7, taxon8...taxon14, taxon15
Place names:
sample6, sample7, sample8, sample9, sample10
julia> cm3 = CommunityProfile(spzeros(10,5), txs[11:20], mss[11:15])
CommunityProfile{Float64, Taxon, MicrobiomeSample} with 10 things in 5 places
Thing names:
taxon11, taxon12, taxon13...taxon19, taxon20
Place names:
sample11, sample12, sample13, sample14, sample15
julia> commjoin(cm1, cm2, cm3)
CommunityProfile{Float64, Taxon, MicrobiomeSample} with 20 things in 15 places
Thing names:
taxon1, taxon2, taxon3...taxon19, taxon20
Place names:
sample1, sample2, sample3...sample14, sample15
```
"""
function commjoin(c1::CommunityProfile, comms::CommunityProfile...)
length(intersect(samplenames(c1), samplenames.(comms)...)) == 0 || error("Duplicate sample names detected: $(intersect(samplenames(c1), samplenames.(comms)...))")

all_samples = vcat(samples(c1), samples.(comms)...)
sample_dict = dictionary(zip(name.(all_samples), eachindex(all_samples)))
all_features = unique(vcat(features(c1), features.(comms)...))
feature_dict = dictionary(zip(name.(all_features), eachindex(all_features)))

mat = spzeros(length(all_features), length(all_samples))
for comm in (c1, comms...)
for sample in samplenames(comm)
for feature in featurenames(comm)
mat[feature_dict[feature], sample_dict[sample]] = comm[feature, sample]
end
end
end

return CommunityProfile(mat, all_features, all_samples)
end
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ end
end

@test_throws ArgumentError relativeabundance!(comm, kind=:invalid)

@test_throws ErrorException commjoin(comm, comm)
let c3 = commjoin(comm[:,1:2], comm[:, 3:4], comm[:, 5])
@test abundances(c3) == abundances(comm)
@test samples(c3) == samples(comm)
@test features(c3) == features(comm)
end
end

@testset "Indexing and Tables integration" begin
Expand Down

0 comments on commit dc9b7fc

Please sign in to comment.