forked from pwilliams0/Biogeography_and_global_diversity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_PhyloB_Mamm_12.R
66 lines (57 loc) · 2.37 KB
/
2_PhyloB_Mamm_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
63
64
65
66
#=============================================================#
# Calculate phylobetadiversity for mammals, cell groups 1 & 2 #
#=============================================================#
library(tidyverse)
library(betapart)
library(picante)
library(ape)
# Split cells into 4 groups, every fourth cell
cell_nums <- unique(read.csv(file="Data/mamm_cell_df.csv",
stringsAsFactors=FALSE)$cell_id)
group_1 <- cell_nums[c(TRUE, FALSE, FALSE, FALSE)]
group_2 <- cell_nums[c(FALSE, TRUE, FALSE, FALSE)]
group_3 <- cell_nums[c(FALSE, FALSE, TRUE, FALSE)]
group_4 <- cell_nums[c(FALSE, FALSE, FALSE, TRUE)]
# Select set of cells to calculate phylobetadiversity
cell_list <- c(group_1, group_2)
# Create community matrix
mamm_comm_mat <- read.csv(file="Data/mamm_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()
mamm_comm_mat <- mamm_comm_mat[,order(colnames(mamm_comm_mat))]
# Load phylogeny, prune for species in community matrix
mamm_phylo <- picante::prune.sample(
mamm_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()
mamm_pb <- betapart::phylo.beta.pair(
mamm_comm_mat,
mamm_phylo,
index.family="sorensen")
end_time <- Sys.time()
print(end_time - start_time)
saveRDS(mamm_pb, "Data/mamm_pb_12.rds")
# sor is total phylobetdiveristy (turnover + nestedness)
# sim is phylobetdiveristy turnover
# sne is phylobetdiveristy nestedness
write.table(as.matrix(mamm_pb$phylo.beta.sim),
file="Data/mamm_pb_sim_12.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
#write.table(as.matrix(mamm_pb$phylo.beta.sor),
# file="Data/mamm_pb_sor_12.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
#write.table(as.matrix(mamm_pb$phylo.beta.sne),
# file="Data/mamm_pb_sne_12.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
print("Done with mamm_12")
# [Runtime 5 hr 41 min]
# [219 GB memory used]