Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EvoArt committed Sep 8, 2021
1 parent a275354 commit 2d3f340
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 17 deletions.
25 changes: 13 additions & 12 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ jobs:
- 'nightly'
os:
- ubuntu-latest
- macOS-latest
arch:
- x64
- x86
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-r@v1
- run: echo "LD_LIBRARY_PATH=$(R RHOME)/lib" >> $GITHUB_ENV
- run: sudo R -e "install.packages('vegan', repos='http://cran.rstudio.com/')"
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
Expand All @@ -33,8 +37,8 @@ jobs:
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
Expand All @@ -44,21 +48,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
- uses: r-lib/actions/setup-r@v1
- run: echo "LD_LIBRARY_PATH=$(R RHOME)/lib" >> $GITHUB_ENV
- run: sudo R -e "install.packages('vegan', repos='http://cran.rstudio.com/')"
- uses: julia-actions/setup-julia@latest
with:
version: '1'
- run: |
julia --project=docs -e '
using Pkg
Pkg.develop(PackageSpec(path=pwd()))
Pkg.instantiate()'
- run: |
julia --project=docs -e '
using Documenter: DocMeta, doctest
using PERMANOVA
DocMeta.setdocmeta!(PERMANOVA, :DocTestSetup, :(using PERMANOVA); recursive=true)
doctest(PERMANOVA)'
- run: julia --project=docs docs/make.jl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
15 changes: 14 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ uuid = "f33fedc7-88ab-426d-b3f4-502f30fbdde3"
authors = ["Arthur Newbury"]
version = "0.1.0"

[deps]
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsModels = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
TexTables = "ebf5ac4f-3ec1-555f-9ac9-3d72ed88c471"

[compat]
julia = "1.6"

[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
RCall = "6f49c342-dc21-5d91-9882-a32aef131414"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"

[targets]
test = ["Test"]
test = ["Test", "DataFrames", "RCall", "BenchmarkTools"]
22 changes: 22 additions & 0 deletions src/Hydra.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Hydra

using Random, LinearAlgebra, Distances, NamedArrays, StatsModels,Statistics,TexTables,LoopVectorization
include("hydra2.jl")
include("output.jl")

export hydra2,
permute,
Euclidean,
SqEuclidean,
PeriodicEuclidean,
Cityblock,
TotalVariation,
Chebyshev,
Minkowski,
Jaccard,
BrayCurtis,
RogersTanimoto,
@formula


end
19 changes: 18 additions & 1 deletion src/PERMANOVA.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
module PERMANOVA

# Write your package code here.
using Random, LinearAlgebra, Distances, StatsModels,Statistics,TexTables,LoopVectorization
include("perm2.jl")
include("output.jl")

export permanova,
permute,
Euclidean,
SqEuclidean,
PeriodicEuclidean,
Cityblock,
TotalVariation,
Chebyshev,
Minkowski,
Jaccard,
BrayCurtis,
RogersTanimoto,
@formula


end
24 changes: 24 additions & 0 deletions src/output.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

function get_output(coefs,Df,sq,r2,f_terms,Gres,Tot,p,n)
n_term = length(coefs)
DF = RegCol("Df")
R2 = RegCol("")
sumsq = RegCol("SumOfSqs")
F = RegCol("F")
P = RegCol("P")

for i in 1:n_term
setcoef!(R2, coefs[i]=>r2[i])
setcoef!(DF, coefs[i]=>Df[i])
setcoef!(sumsq, coefs[i]=>sq[i])
setcoef!(F, coefs[i]=>f_terms[i])
setcoef!(P, coefs[i]=>p[i])
end
setcoef!(R2, "Residual"=>1-sum(r2))
setcoef!(DF, "Residual"=>n-1-sum(Df))
setcoef!(sumsq, "Residual"=>Gres)
setcoef!(R2, "Total"=>1)
setcoef!(DF, "Total"=>n-1)
setcoef!(sumsq, "Total"=>Tot)
return hcat(DF,sumsq,R2,F,P)
end
80 changes: 80 additions & 0 deletions src/perm1.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#The key to the non-parametric method describedhere
#is that the sum of squared distances between points and
#their centroid is equal to (and can be calculated directly from)
#the sum of squared interpoint distancesdivided by the number of points
# https://onlinelibrary.wiley.com/doi/epdf/10.1111/j.1442-9993.2001.01070.pp.x

SS(D,N) = sum(D .^2)/N

function F(D,W,N,n,a)
SST = SS(D,N)
SSW = SS(W,n)
SSA = SST - SSW
return (SSA/(a-1))/(SSW/(N-a))
end

function F(SST ::Real,W,N,n,a)

SSW = SS(W,n)
SSA = SST - SSW
return (SSA/(a-1))/(SSW/(N-a))
end

function getinds(group,N)
W =Vector{CartesianIndex}(undef,0)
notW = Vector{CartesianIndex}(undef,0)
for i in 1:(N-1)
for j in i+1:N
if i !==1j
if group[i] == group[j]
push!(W,(CartesianIndex(i,j)))
else push!(notW,(CartesianIndex(i,j)))
end
end
end
end
return W, notW
end

function perm1(D,group, n_perm = 1000)
N = length(group)
a = length(unique(group))
n = N/a
Winds ,notWinds =getinds(group,N)

@inbounds W = D[Winds]
@inbounds notW= D[notWinds]

Dvec = vcat(W,notW)

SST = SS(Dvec,N)

f = F(SST,W,N,n,a)
Fs = Vector{Float64}(undef,n_perm)
inds = 1:length(W)

@inbounds for i in 1:n_perm
shuffle!(Dvec)
Fs[i] = F(SST,view(Dvec,inds),N,n,a)
end
P = sum(Fs .>= f)/n_perm

g = unique(group)
if a>2
ppairs = NamedArray(zeros(a,a),( g,g ), ("group","group"))
tpairs = NamedArray(zeros(a,a),( g,g ), ("group","group"))
Threads.@threads for i in 1:a-1
for j in i+1:a
boolmask = (group .== g[i]) .| (group .== g[j])
d = D[boolmask,boolmask]
fstat,pstat =permutest(d,group[boolmask], n_perm)
ppairs[j,i] = pstat
tpairs[j,i] = sqrt(fstat)
end
end
return (F = f,P = P, fpairs = tpairs,ppairs = ppairs)
else
return (F = f,P = P)
end

end
Loading

0 comments on commit 2d3f340

Please sign in to comment.