Skip to content

Commit acb4017

Browse files
committed
Copy functions from phyloregion
to avoid loading betapart. See futureverse/future#554 Closes #2
1 parent afbd28a commit acb4017

14 files changed

+952
-125
lines changed

DESCRIPTION

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ Imports:
3333
assertthat,
3434
dplyr,
3535
future.apply,
36-
phyloregion,
36+
Matrix,
37+
methods,
38+
phangorn,
3739
progressr,
3840
purrr,
3941
stats,
@@ -51,7 +53,8 @@ Suggests:
5153
stringr,
5254
magrittr,
5355
covr,
54-
picante
56+
picante,
57+
phyloregion
5558
Config/testthat/edition: 3
5659
Depends:
5760
R (>= 3.5.0)

R/calc_biodiv_random.R

+11-11
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ calc_biodiv_random <- function(comm, phy, phy_alt,
115115

116116
# Calculations ----
117117

118-
# Convert comm to sparse matrix format for phyloregions
119-
comm_sparse <- phyloregion::dense2sparse(comm)
118+
# Convert comm to sparse matrix format
119+
comm_sparse <- dense2sparse(comm)
120120

121121
# Generate random community
122122
random_comm <- cpr_rand_comm(comm, null_model = null_model, n_iterations = n_iterations, thin = thin, seed = seed)
123-
random_comm_sparse <- phyloregion::dense2sparse(random_comm)
123+
random_comm_sparse <- dense2sparse(random_comm)
124124

125125
# Calculate statistics for random community
126126
# - set up null vectors first
@@ -132,20 +132,20 @@ calc_biodiv_random <- function(comm, phy, phy_alt,
132132
rpe <- NULL
133133

134134
# - calculate selected metrics
135-
if ("pd" %in% metrics) pd <- phyloregion::PD(random_comm_sparse, phy)
136-
if ("pd_alt" %in% metrics) pd_alt <- phyloregion::PD(random_comm_sparse, phy_alt)
135+
if ("pd" %in% metrics) pd <- PD(random_comm_sparse, phy)
136+
if ("pd_alt" %in% metrics) pd_alt <- PD(random_comm_sparse, phy_alt)
137137
# pd_alt is inferred by rpd
138138
if ("rpd" %in% metrics) {
139-
if (is.null(pd)) pd <- phyloregion::PD(random_comm_sparse, phy)
140-
if (is.null(pd_alt)) pd_alt <- phyloregion::PD(random_comm_sparse, phy_alt)
139+
if (is.null(pd)) pd <- PD(random_comm_sparse, phy)
140+
if (is.null(pd_alt)) pd_alt <- PD(random_comm_sparse, phy_alt)
141141
rpd <- pd / pd_alt
142142
}
143143
# pe_alt is inferred by rpe
144-
if ("pe" %in% metrics) pe <- phyloregion::phylo_endemism(random_comm_sparse, phy, weighted = TRUE)
145-
if ("pe_alt" %in% metrics) pe_alt <- phyloregion::phylo_endemism(random_comm_sparse, phy_alt, weighted = TRUE)
144+
if ("pe" %in% metrics) pe <- phylo_endemism(random_comm_sparse, phy, weighted = TRUE)
145+
if ("pe_alt" %in% metrics) pe_alt <- phylo_endemism(random_comm_sparse, phy_alt, weighted = TRUE)
146146
if ("rpe" %in% metrics) {
147-
if (is.null(pe)) pe <- phyloregion::phylo_endemism(random_comm_sparse, phy, weighted = TRUE)
148-
if (is.null(pe_alt)) pe_alt <- phyloregion::phylo_endemism(random_comm_sparse, phy_alt, weighted = TRUE)
147+
if (is.null(pe)) pe <- phylo_endemism(random_comm_sparse, phy, weighted = TRUE)
148+
if (is.null(pe_alt)) pe_alt <- phylo_endemism(random_comm_sparse, phy_alt, weighted = TRUE)
149149
rpe <- pe / pe_alt
150150
}
151151

R/cpr_rand_test.R

+10-10
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ cpr_rand_test <- function(comm, phy, null_model,
151151
#' convert to integer before numeric comparisons
152152
# - null_model
153153
assertthat::assert_that(
154-
assertthat::is.string(null_model) | inherits(null_model, "commsim"),
155-
msg = "'null_model' must be a string (character vector of length 1) or an object of class 'commsim'"
154+
assertthat::is.string(null_model) | inherits(null_model, "commsim"),
155+
msg = "'null_model' must be a string (character vector of length 1) or an object of class 'commsim'"
156156
)
157157
assertthat::assert_that(assertthat::noNA(null_model))
158158
# - n_reps
@@ -229,7 +229,7 @@ cpr_rand_test <- function(comm, phy, null_model,
229229
comm <- comm_df
230230
}
231231
#' @srrstats {UL1.2} Check for default-looking rownames
232-
# Default rownames not allowed because phyloregion::dense2sparse() will convert them to NULL
232+
# Default rownames not allowed because dense2sparse() will convert them to NULL
233233
assertthat::assert_that(
234234
!identical(rownames(comm), as.character(seq(nrow(comm)))),
235235
msg = "'comm' cannot have default row names (consecutive integers from 1 to the number of rows)"
@@ -364,7 +364,7 @@ cpr_rand_test <- function(comm, phy, null_model,
364364
phy$edge.length <- phy$edge.length / sum(phy$edge.length)
365365

366366
# Make sparse community df
367-
comm_sparse <- phyloregion::dense2sparse(comm)
367+
comm_sparse <- dense2sparse(comm)
368368

369369
# Calculate biodiversity metrics ----
370370

@@ -398,26 +398,26 @@ cpr_rand_test <- function(comm, phy, null_model,
398398

399399
# - calculate selected metrics
400400
if ("pd" %in% metrics) {
401-
pd_obs <- phyloregion::PD(comm_sparse, phy)
401+
pd_obs <- PD(comm_sparse, phy)
402402
ses_pd <- get_ses(random_vals, pd_obs, "pd")
403403
}
404404

405405
if ("rpd" %in% metrics) {
406-
if (!exists("pd_obs")) pd_obs <- phyloregion::PD(comm_sparse, phy)
407-
pd_alt_obs <- phyloregion::PD(comm_sparse, phy_alt)
406+
if (!exists("pd_obs")) pd_obs <- PD(comm_sparse, phy)
407+
pd_alt_obs <- PD(comm_sparse, phy_alt)
408408
ses_pd_alt <- get_ses(random_vals, pd_alt_obs, "pd_alt")
409409
rpd_obs <- pd_obs / pd_alt_obs
410410
ses_rpd <- get_ses(random_vals, rpd_obs, "rpd")
411411
}
412412

413413
if ("pe" %in% metrics) {
414-
pe_obs <- phyloregion::phylo_endemism(comm_sparse, phy, weighted = TRUE)
414+
pe_obs <- phylo_endemism(comm_sparse, phy, weighted = TRUE)
415415
ses_pe <- get_ses(random_vals, pe_obs, "pe")
416416
}
417417

418418
if ("rpe" %in% metrics) {
419-
if (!exists("pe_obs")) pe_obs <- phyloregion::phylo_endemism(comm_sparse, phy, weighted = TRUE)
420-
pe_alt_obs <- phyloregion::phylo_endemism(comm_sparse, phy_alt, weighted = TRUE)
419+
if (!exists("pe_obs")) pe_obs <- phylo_endemism(comm_sparse, phy, weighted = TRUE)
420+
pe_alt_obs <- phylo_endemism(comm_sparse, phy_alt, weighted = TRUE)
421421
ses_pe_alt <- get_ses(random_vals, pe_alt_obs, "pe_alt")
422422
rpe_obs <- pe_obs / pe_alt_obs
423423
ses_rpe <- get_ses(random_vals, rpe_obs, "rpe")

R/get_ses.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#' 1:100,
2929
#' ~ calc_biodiv_random(comm, phy, phy_alt, "independentswap", 1000L, metrics = "pe")
3030
#' )
31-
#' comm_sparse <- phyloregion::dense2sparse(comm)
32-
#' pe_obs <- phyloregion::phylo_endemism(comm_sparse, phy, weighted = TRUE)
31+
#' comm_sparse <- dense2sparse(comm)
32+
#' pe_obs <- phylo_endemism(comm_sparse, phy, weighted = TRUE)
3333
#' get_ses(random_vals, pe_obs, "pe")
3434
#' }
3535
#' @autoglobal

R/utils.R

+58
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,61 @@ match_phylo_comm <- function(phy, comm) {
247247
res$comm <- comm[, res$phy$tip.label]
248248
return(res)
249249
}
250+
251+
# Functions copied from phyloregion v1.0.6 under AGPL-3 ----
252+
253+
# Corresponds to phyloregion::dense2sparse()
254+
dense2sparse <- function(x) {
255+
x <- as.matrix(x)
256+
Matrix::Matrix(x, sparse = TRUE)
257+
}
258+
259+
# Corresponds to phyloregion:::phylo_community()
260+
phylo_community <- function(x, phy) {
261+
el <- numeric(max(phy$edge))
262+
el[phy$edge[, 2]] <- phy$edge.length
263+
x <- x[, phy$tip.label]
264+
anc <- phangorn::Ancestors(phy, seq_along(phy$tip.label))
265+
anc <- mapply(c, seq_along(phy$tip.label), anc, SIMPLIFY = FALSE)
266+
M <- Matrix::sparseMatrix(as.integer(rep(
267+
seq_along(anc),
268+
lengths(anc)
269+
)), as.integer(unlist(anc)), x = 1L)
270+
commphylo <- x %*% M
271+
commphylo@x[commphylo@x > 1e-08] <- 1
272+
list(Matrix = commphylo, edge.length = el)
273+
}
274+
275+
# Corresponds to phyloregion::PD()
276+
PD <- function(x, phy) {
277+
if (!methods::is(x, "sparseMatrix")) {
278+
stop("x needs to be a sparse matrix!")
279+
}
280+
if (length(setdiff(colnames(x), phy$tip.label)) > 0) {
281+
stop("There are species labels in community matrix missing in the tree!")
282+
}
283+
if (length(setdiff(phy$tip.label, colnames(x))) > 0) {
284+
phy <- ape::keep.tip(phy, intersect(phy$tip.label, colnames(x)))
285+
}
286+
x <- x[, intersect(phy$tip.label, colnames(x))]
287+
z <- phylo_community(x, phy)
288+
(z$Matrix %*% z$edge.length)[, 1]
289+
}
290+
291+
# Corresponds to phyloregion::phylo_endemism()
292+
phylo_endemism <- function(x, phy, weighted = TRUE) {
293+
if (length(setdiff(colnames(x), phy$tip.label)) > 0) {
294+
stop("There are species labels in community matrix missing in the tree!")
295+
}
296+
if (length(setdiff(phy$tip.label, colnames(x))) > 0) {
297+
phy <- ape::keep.tip(phy, intersect(phy$tip.label, colnames(x)))
298+
}
299+
comm_phylo <- phylo_community(x, phy)
300+
weights <- comm_phylo$Matrix %*% Matrix::Diagonal(x = 1 / Matrix::colSums(comm_phylo$Matrix))
301+
if (weighted == FALSE) {
302+
weights[weights < 1] <- 0
303+
}
304+
pd <- (weights %*% comm_phylo$edge.length)[, 1]
305+
pd <- pd[row.names(x)]
306+
return(pd)
307+
}

README.Rmd

+5-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ library(canaper)
5353
5454
data(phylocom)
5555
56-
# Example community matrix including 4 "clumped" communities,
56+
# Example community matrix including 4 "clumped" communities,
5757
# one "even" community, and one "random" community
5858
phylocom$comm
5959
@@ -71,7 +71,7 @@ rand_test_results <- cpr_rand_test(phylocom$comm, phylocom$phy, null_model = "sw
7171
`cpr_rand_test` produces **a lot** of columns (nine per metric), so let's just look at a subset of them:
7272

7373
```{r rand-test-res}
74-
rand_test_results[,1:9]
74+
rand_test_results[, 1:9]
7575
```
7676

7777
This is a summary of the columns:
@@ -133,9 +133,10 @@ You can find DOIs for older versions by viewing the "Releases" menu on the right
133133

134134
## Licenses
135135

136-
- Code: [MIT](LICENSE.md)
136+
- Original code: [MIT](LICENSE.md)
137+
- Functions from [phyloregion](https://github.com/darunabas/phyloregion): [AGPL-3](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-agpl-3.txt)
137138
- Example datasets
138-
- `acacia`, `biod_example`: [GNU General Public License v3.0](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-gpl.txt)
139+
- `acacia`, `biod_example`: [GPL-3](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-gpl-3.txt)
139140
- `phylocom`: [BSD-3-Clause](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-bsd3.txt)
140141

141142
## References

README.md

+19-16
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ library(canaper)
4949

5050
data(phylocom)
5151

52-
# Example community matrix including 4 "clumped" communities,
52+
# Example community matrix including 4 "clumped" communities,
5353
# one "even" community, and one "random" community
5454
phylocom$comm
5555
#> sp1 sp10 sp11 sp12 sp13 sp14 sp15 sp17 sp18 sp19 sp2 sp20 sp21 sp22
@@ -91,29 +91,29 @@ value to the alternative value (relative PD, relative PE).
9191
``` r
9292
set.seed(071421)
9393
rand_test_results <- cpr_rand_test(phylocom$comm, phylocom$phy, null_model = "swap")
94-
#> [1] "Dropping tips from the tree because they are not present in the community data:"
95-
#> [1] "sp16" "sp23" "sp27" "sp28" "sp30" "sp31" "sp32"
94+
#> Warning in match_phylo_comm(phy = phy, comm = comm): Dropping tips from the tree because they are not present in the community data:
95+
#> sp16, sp23, sp27, sp28, sp30, sp31, sp32
9696
```
9797

9898
`cpr_rand_test` produces **a lot** of columns (nine per metric), so
9999
let’s just look at a subset of them:
100100

101101
``` r
102-
rand_test_results[,1:9]
102+
rand_test_results[, 1:9]
103103
#> pd_obs pd_rand_mean pd_rand_sd pd_obs_z pd_obs_c_upper
104-
#> clump1 0.3018868 0.4692453 0.03214267 -5.206739 0
105-
#> clump2a 0.3207547 0.4762264 0.03263836 -4.763465 0
106-
#> clump2b 0.3396226 0.4681132 0.03462444 -3.710978 0
107-
#> clump4 0.4150943 0.4667925 0.03180131 -1.625660 3
108-
#> even 0.5660377 0.4660377 0.03501739 2.855724 100
109-
#> random 0.5094340 0.4733962 0.03070539 1.173662 79
104+
#> clump1 0.3018868 0.4675472 0.03623666 -4.571624 0
105+
#> clump2a 0.3207547 0.4684906 0.03116570 -4.740335 0
106+
#> clump2b 0.3396226 0.4684906 0.03150994 -4.089754 0
107+
#> clump4 0.4150943 0.4664151 0.03307178 -1.551799 3
108+
#> even 0.5660377 0.4641509 0.03517108 2.896891 100
109+
#> random 0.5094340 0.4713208 0.03295196 1.156629 80
110110
#> pd_obs_c_lower pd_obs_q pd_obs_p_upper pd_obs_p_lower
111111
#> clump1 100 100 0.00 1.00
112112
#> clump2a 100 100 0.00 1.00
113113
#> clump2b 100 100 0.00 1.00
114-
#> clump4 91 100 0.03 0.91
114+
#> clump4 90 100 0.03 0.90
115115
#> even 0 100 1.00 0.00
116-
#> random 6 100 0.79 0.06
116+
#> random 7 100 0.80 0.07
117117
```
118118

119119
This is a summary of the columns:
@@ -145,7 +145,7 @@ canape_results[, "endem_type", drop = FALSE]
145145
#> clump2a not significant
146146
#> clump2b not significant
147147
#> clump4 not significant
148-
#> even mixed
148+
#> even super
149149
#> random mixed
150150
```
151151

@@ -192,10 +192,13 @@ the right.
192192

193193
## Licenses
194194

195-
- Code: [MIT](LICENSE.md)
195+
- Original code: [MIT](LICENSE.md)
196+
- Functions from
197+
[phyloregion](https://github.com/darunabas/phyloregion):
198+
[AGPL-3](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-agpl-3.txt)
196199
- Example datasets
197-
- `acacia`, `biod_example`: [GNU General Public License
198-
v3.0](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-gpl.txt)
200+
- `acacia`, `biod_example`:
201+
[GPL-3](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-gpl-3.txt)
199202
- `phylocom`:
200203
[BSD-3-Clause](https://github.com/joelnitta/canaper/blob/main/data-raw/LICENSE-bsd3.txt)
201204

codemeta.json

+33-4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@
205205
"url": "https://cran.r-project.org"
206206
},
207207
"sameAs": "https://CRAN.R-project.org/package=picante"
208+
},
209+
{
210+
"@type": "SoftwareApplication",
211+
"identifier": "phyloregion",
212+
"name": "phyloregion",
213+
"provider": {
214+
"@id": "https://cran.r-project.org",
215+
"@type": "Organization",
216+
"name": "Comprehensive R Archive Network (CRAN)",
217+
"url": "https://cran.r-project.org"
218+
},
219+
"sameAs": "https://CRAN.R-project.org/package=phyloregion"
208220
}
209221
],
210222
"softwareRequirements": [
@@ -270,15 +282,32 @@
270282
},
271283
{
272284
"@type": "SoftwareApplication",
273-
"identifier": "phyloregion",
274-
"name": "phyloregion",
285+
"identifier": "Matrix",
286+
"name": "Matrix",
275287
"provider": {
276288
"@id": "https://cran.r-project.org",
277289
"@type": "Organization",
278290
"name": "Comprehensive R Archive Network (CRAN)",
279291
"url": "https://cran.r-project.org"
280292
},
281-
"sameAs": "https://CRAN.R-project.org/package=phyloregion"
293+
"sameAs": "https://CRAN.R-project.org/package=Matrix"
294+
},
295+
{
296+
"@type": "SoftwareApplication",
297+
"identifier": "methods",
298+
"name": "methods"
299+
},
300+
{
301+
"@type": "SoftwareApplication",
302+
"identifier": "phangorn",
303+
"name": "phangorn",
304+
"provider": {
305+
"@id": "https://cran.r-project.org",
306+
"@type": "Organization",
307+
"name": "Comprehensive R Archive Network (CRAN)",
308+
"url": "https://cran.r-project.org"
309+
},
310+
"sameAs": "https://CRAN.R-project.org/package=phangorn"
282311
},
283312
{
284313
"@type": "SoftwareApplication",
@@ -341,7 +370,7 @@
341370
}
342371
],
343372
"releaseNotes": "https://github.com/joelnitta/canaper/blob/master/NEWS.md",
344-
"fileSize": "7891.09KB",
373+
"fileSize": "7891.857KB",
345374
"contIntegration": ["https://github.com/joelnitta/canaper/actions", "https://codecov.io/gh/joelnitta/canaper?branch=main"],
346375
"developmentStatus": "https://www.repostatus.org/#wip",
347376
"keywords": [

0 commit comments

Comments
 (0)