Skip to content

Commit

Permalink
Metadata for CommunityProfiles (#66)
Browse files Browse the repository at this point in the history
* slow but functional metadata export

* add tests

* make return value a row table
  • Loading branch information
kescobo authored Aug 9, 2021
1 parent 8511606 commit ce1b94f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Microbiome.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export CommunityProfile,
profiletype,
featuretotals,
sampletotals,
commjoin
commjoin,
metadata

# Abundances
export present,
Expand Down
21 changes: 21 additions & 0 deletions src/profiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,24 @@ function prevalence_filter(comm::AbstractAbundanceTable; minabundance=0.0, minpr
comm = comm[vec(prevalence(comm, minabundance) .>= minprevalence), :]
return renorm ? relativeabundance(comm) : comm
end


## Metadata

"""
metadata(cp::CommunityProfile)
Returns iterator of `NamedTuple` per sample, where keys are `:sample`
and each metadata key found in `cp`.
Samples without given metadata are filled with `missing`.
Returned values can be passed to any Tables.rowtable - compliant type,
eg `DataFrame`.
"""
function metadata(cp::CommunityProfile)
ss = samples(cp)
cols = unique(reduce(hcat, collect.(keys.(metadata.(samples(cp))))))
return Tables.rowtable(merge((; sample=name(s)),
NamedTuple(c => get(s, c, missing) for c in cols)
) for s in ss)
end
1 change: 1 addition & 0 deletions src/samples_features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ end

Base.keys(as::AbstractSample) = keys(metadata(as))
Base.haskey(as::AbstractSample, key::Symbol) = in(key, keys(as))
Base.get(as::AbstractSample, key::Symbol, default) = get(metadata(as), key, default)

"""
MicrobiomeSample(name::String, metadata::Dictionary{Symbol, T}) <: AbstractSample
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ end
@test size(prevalence_filter(filtertest, minabundance=2, minprevalence=0.4)) == (2,3)
@test all(<=(1.0), abundances(prevalence_filter(filtertest, renorm=true)))
@test all(x-> isapprox(x, 1.0, atol=1e-8), sum(abundances(prevalence_filter(filtertest, renorm=true)), dims=1))

s1 = MicrobiomeSample("sample1", Dictionary(Dict(:age=> 37, :name=>"kevin", :something=>1.0)))
s2 = MicrobiomeSample("sample2", Dictionary(Dict(:age=> 37, :name=>"kevin", :something_else=>2.0)))

md1, md2 = metadata(CommunityProfile(sparse([1 1; 2 2; 3 3]), [Taxon(string(i)) for i in 1:3], [s1, s2]))

@test all(row-> row[:age] == 37, [md1, md2])
@test all(row-> row[:name] == "kevin", [md1, md2])
@test md1[:something] == 1.0
@test ismissing(md2[:something])
@test md2[:something_else] == 2.0
@test ismissing(md1[:something_else])

end

@testset "Indexing and Tables integration" begin
Expand Down

4 comments on commit ce1b94f

@kescobo
Copy link
Member Author

@kescobo kescobo commented on ce1b94f Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Action not recognized: release

@kescobo
Copy link
Member Author

@kescobo kescobo commented on ce1b94f Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/42511

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.2 -m "<description of version>" ce1b94f7ef183fceea09ed588ebe7906989b2838
git push origin v0.7.2

Please sign in to comment.