forked from pwilliams0/Biogeography_and_global_diversity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_PhyloB_Bat_12.R
62 lines (54 loc) · 2.23 KB
/
2_PhyloB_Bat_12.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#==========================================================#
# Calculate phylobetadiversity for bats, cell groups 1 & 2 #
#==========================================================#
library(tidyverse)
library(betapart)
library(picante)
library(ape)
# Split cells into 3 groups, every third cell
cell_nums <- unique(read.csv(file="Data/bat_cell_df.csv",
stringsAsFactors=FALSE)$cell_id)
group_1 <- cell_nums[c(TRUE, FALSE, FALSE)]
group_2 <- cell_nums[c(FALSE, TRUE, FALSE)]
group_3 <- cell_nums[c(FALSE, FALSE, TRUE)]
# Select set of cells to calculate phylobetadiversity
cell_list <- c(group_1, group_2)
# Create community matrix
bat_comm_mat <- read.csv(file="Data/bat_cell_df.csv",
stringsAsFactors=FALSE) %>%
dplyr::filter(cell_id %in% cell_list) %>%
group_by(cell_id, names_phylo) %>%
slice(1) %>% ungroup() %>%
mutate(present = 1) %>%
dplyr::select(cell_id, names_phylo, present) %>%
pivot_wider(names_from = names_phylo,
values_from = present) %>%
replace(.,is.na(.),0) %>%
mutate(cell_id = paste("X",cell_id,sep="")) %>%
column_to_rownames("cell_id") %>%
as.matrix()
bat_comm_mat <- bat_comm_mat[,order(colnames(bat_comm_mat))]
# Load phylogeny, prune for species in community matrix
bat_phylo <- picante::prune.sample(
bat_comm_mat,
ape::read.tree("Data/Raw/Upham_mean_phylogeny_ultrametric.tree"))
print("Done loading data")
# Run phylogenetic beta diversity (Sorenson)
start_time <- Sys.time()
bat_pb <- betapart::phylo.beta.pair(
bat_comm_mat,
bat_phylo,
index.family="sorensen")
end_time <- Sys.time()
print(end_time - start_time)
saveRDS(bat_pb, "Data/bat_pb_12.rds")
# sor is total phylobetdiveristy (turnover + nestedness)
# sim is phylobetdiveristy turnover
# sne is phylobetdiveristy nestedness
write.table(as.matrix(bat_pb$phylo.beta.sim),
file="Data/bat_pb_sim_12.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
#write.table(as.matrix(bat_pb$phylo.beta.sor),
# file="Data/bat_pb_sor_12.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
#write.table(as.matrix(bat_pb$phylo.beta.sne),
# file="Data/bat_pb_sne_12.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
print("Done with bat_12")