Chapter 7 Diversity

load("data/metagenomics/data.Rdata")

7.1 Alpha diversity

# Calculate Hill numbers
richness <- genome_counts_filt %>%
  column_to_rownames(var = "genome") %>%
  dplyr::select(where(~ !all(. == 0))) %>%
  hilldiv(., q = 0) %>%
  t() %>%
  as.data.frame() %>%
  dplyr::rename(richness = 1) %>%
  rownames_to_column(var = "sample")

neutral <- genome_counts_filt %>%
  column_to_rownames(var = "genome") %>%
  dplyr::select(where(~ !all(. == 0))) %>%
  hilldiv(., q = 1) %>%
  t() %>%
  as.data.frame() %>%
  dplyr::rename(neutral = 1) %>%
  rownames_to_column(var = "sample")

phylogenetic <- genome_counts_filt %>%
  column_to_rownames(var = "genome") %>%
  dplyr::select(where(~ !all(. == 0))) %>%
  hilldiv(., q = 1, tree = genome_tree) %>%
  t() %>%
  as.data.frame() %>%
  dplyr::rename(phylogenetic = 1) %>%
  rownames_to_column(var = "sample")

# Aggregate basal GIFT into elements
dist <- genome_gifts %>%
  to.elements(., GIFT_db) %>%
  traits2dist(., method = "gower")

#functional <- genome_counts_filt %>%
#  filter(genome %in% labels(dist)[[1]]) %>%
#  column_to_rownames(var = "genome") %>%
#  dplyr::select(where(~ !all(. == 0))) %>%
#  hilldiv(., q = 1, dist = dist) %>%
#  t() %>%
#  as.data.frame() %>%
#  dplyr::rename(functional = 1) %>%
#  rownames_to_column(var = "sample") %>%
#  mutate(functional = if_else(is.nan(functional), 1, functional))

# Merge all metrics
alpha_div_metagenomics <- richness %>%
  full_join(neutral, by = join_by(sample == sample)) %>%
  full_join(phylogenetic, by = join_by(sample == sample)) #%>%
  #full_join(functional, by = join_by(sample == sample))
alpha_div_metagenomics %>%
  pivot_longer(-sample, names_to = "metric", values_to = "value") %>%
  left_join(., sample_metadata, by = join_by(sample == sample)) %>%
  mutate(metric=factor(metric,levels=c("richness","neutral","phylogenetic","functional"))) %>%
      ggplot(aes(y = value, x = bat_species, group=bat_species, color=bat_species, fill=bat_species)) +
      geom_boxplot(outlier.shape = NA) +
      geom_jitter(alpha=0.5) +
      scale_color_manual(name="Species",
          breaks=c("Pipistrellus kuhlii","Eptesicus bottae","Hypsugo ariel"),
          labels=c("Pipistrellus kuhlii","Eptesicus bottae","Hypsugo ariel"),
          values=c("#e5bd5b", "#6b7398","#e2815a")) +
      scale_fill_manual(name="Species",
          breaks=c("Pipistrellus kuhlii","Eptesicus bottae","Hypsugo ariel"),
          labels=c("Pipistrellus kuhlii","Eptesicus bottae","Hypsugo ariel"),
          values=c("#e5bd5b50", "#6b739850","#e2815a50")) +
      facet_wrap(. ~ metric, scales = "free", ncol=4) +
      coord_cartesian(xlim = c(1, NA)) +
      theme_classic() +
      theme(
        strip.background = element_blank(),
        panel.grid.minor.x = element_line(size = .1, color = "grey"),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_blank())

7.2 Beta diversity

beta_q0n <- genome_counts %>%
  select(where(~!all(. == 0))) %>% # remove empty samples
  column_to_rownames(., "genome") %>%
  hillpair(., q = 0)

beta_q1n <- genome_counts %>%
  select(where(~!all(. == 0))) %>% # remove empty samples
  column_to_rownames(., "genome") %>%
  hillpair(., q = 1)

beta_q1p <- genome_counts %>%
  select(where(~!all(. == 0))) %>% # remove empty samples
  column_to_rownames(., "genome") %>%
  hillpair(., q = 1, tree = genome_tree)

beta_q1f <- genome_counts %>%
  select(where(~!all(. == 0))) %>% # remove empty samples
  filter(genome %in% labels(dist)[[1]]) %>%
  column_to_rownames(., "genome") %>%
  hillpair(., q = 1, dist = dist)

7.2.1 Richness (q0n)

tinytable_ougfb7rd4kv08x9avrbk
Homogeneity of variances
Df Sum Sq Mean Sq F N.Perm Pr(>F)
2 0.002096317 0.0010481586 1.566315 999 0.227
48 0.032121016 0.0006691878 NA NA NA
tinytable_uwvr7scpju4kof0c6m16
Permanova
term df SumOfSqs R2 statistic p.value
bat_species 2 0.007009903 0.08603871 2.259318 0.013
Residual 48 0.074463917 0.91396129 NA NA
Total 50 0.081473820 1.00000000 NA NA
beta_q0n$C %>%
  vegan::metaMDS(., trymax = 500, k = 2, trace = 0) %>%
  vegan::scores() %>%
  as_tibble(., rownames = "sample") %>%
  dplyr::left_join(sample_metadata, by = join_by(sample == sample)) %>%
  group_by(bat_species) %>%
  mutate(x_cen = mean(NMDS1, na.rm = TRUE)) %>%
  mutate(y_cen = mean(NMDS2, na.rm = TRUE)) %>%
  ungroup() %>%
  ggplot(aes(x = NMDS1, y = NMDS2, color = bat_species)) +
    scale_color_manual(values = c("#e5bd5b", "#6b7398","#e2815a")) +
    scale_shape_manual(values = 1:10) +
    geom_point(size = 4) +
    #   stat_ellipse(aes(color = beta_q1n_nmds$Groups))+
    geom_segment(aes(x = x_cen, y = y_cen, xend = NMDS1, yend = NMDS2), alpha = 0.9) +
    theme_classic() +
    theme(
      axis.text.x = element_text(size = 12),
      axis.text.y = element_text(size = 12),
      axis.title = element_text(size = 20, face = "bold"),
      axis.text = element_text(face = "bold", size = 18),
      panel.background = element_blank(),
      axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"),
      legend.text = element_text(size = 16),
      legend.title = element_text(size = 18),
      legend.position = "right", legend.box = "vertical"
    )

7.2.2 Neutral (q1n)

tinytable_25b1kapr6xepae0vs0jq
Homogeneity of variances
Df Sum Sq Mean Sq F N.Perm Pr(>F)
2 0.2043596 0.10217979 4.995482 999 0.009
48 0.9818131 0.02045444 NA NA NA
tinytable_zoyvjzxz46g63nsmf93a
Permanova
term df SumOfSqs R2 statistic p.value
bat_species 2 1.702184 0.0979211 2.605211 0.001
Residual 48 15.681038 0.9020789 NA NA
Total 50 17.383222 1.0000000 NA NA
beta_q1n$C %>%
  vegan::metaMDS(., trymax = 500, k = 2, trace = 0) %>%
  vegan::scores() %>%
  as_tibble(., rownames = "sample") %>%
  dplyr::left_join(sample_metadata, by = join_by(sample == sample)) %>%
  group_by(bat_species) %>%
  mutate(x_cen = mean(NMDS1, na.rm = TRUE)) %>%
  mutate(y_cen = mean(NMDS2, na.rm = TRUE)) %>%
  ungroup() %>%
  ggplot(aes(x = NMDS1, y = NMDS2, color = bat_species)) +
    scale_color_manual(values = c("#e5bd5b", "#6b7398","#e2815a")) +
    scale_shape_manual(values = 1:10) +
    geom_point(size = 4) +
    #   stat_ellipse(aes(color = beta_q1n_nmds$Groups))+
    geom_segment(aes(x = x_cen, y = y_cen, xend = NMDS1, yend = NMDS2), alpha = 0.9) +
    theme_classic() +
    theme(
      axis.text.x = element_text(size = 12),
      axis.text.y = element_text(size = 12),
      axis.title = element_text(size = 20, face = "bold"),
      axis.text = element_text(face = "bold", size = 18),
      panel.background = element_blank(),
      axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"),
      legend.text = element_text(size = 16),
      legend.title = element_text(size = 18),
      legend.position = "right", legend.box = "vertical"
    )

7.2.3 Phylogenetic (q1p)

tinytable_z72bg2vr4ly2gmwreckz
Homogeneity of variances
Df Sum Sq Mean Sq F N.Perm Pr(>F)
2 0.1390146 0.06950729 3.719514 999 0.03
48 0.8969856 0.01868720 NA NA NA
tinytable_u38hwahf4jvg89qva0l0
Permanova
term df SumOfSqs R2 statistic p.value
bat_species 2 0.6452775 0.08607004 2.260218 0.012
Residual 48 6.8518437 0.91392996 NA NA
Total 50 7.4971212 1.00000000 NA NA
beta_q1p$C %>%
  vegan::metaMDS(., trymax = 500, k = 2, trace = 0) %>%
  vegan::scores() %>%
  as_tibble(., rownames = "sample") %>%
  dplyr::left_join(sample_metadata, by = join_by(sample == sample)) %>%
  group_by(bat_species) %>%
  mutate(x_cen = mean(NMDS1, na.rm = TRUE)) %>%
  mutate(y_cen = mean(NMDS2, na.rm = TRUE)) %>%
  ungroup() %>%
  ggplot(aes(x = NMDS1, y = NMDS2, color = bat_species)) +
    scale_color_manual(values = c("#e5bd5b", "#6b7398","#e2815a")) +
    scale_shape_manual(values = 1:10) +
    geom_point(size = 4) +
    #   stat_ellipse(aes(color = beta_q1n_nmds$Groups))+
    geom_segment(aes(x = x_cen, y = y_cen, xend = NMDS1, yend = NMDS2), alpha = 0.9) +
    theme_classic() +
    theme(
      axis.text.x = element_text(size = 12),
      axis.text.y = element_text(size = 12),
      axis.title = element_text(size = 20, face = "bold"),
      axis.text = element_text(face = "bold", size = 18),
      panel.background = element_blank(),
      axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"),
      legend.text = element_text(size = 16),
      legend.title = element_text(size = 18),
      legend.position = "right", legend.box = "vertical"
    )

7.2.4 Functional (q1f)

tinytable_el5fnotr03z7exf6anyb
Homogeneity of variances
Df Sum Sq Mean Sq F N.Perm Pr(>F)
2 0.02809638 0.01404819 0.266268 999 0.782
48 2.53246042 0.05275959 NA NA NA
tinytable_3wt31ikfd7u4iuku9voq
Permanova
term df SumOfSqs R2 statistic p.value
bat_species 2 1.062947 0.1351829 3.751532 0.015
Residual 48 6.800086 0.8648171 NA NA
Total 50 7.863034 1.0000000 NA NA
beta_q1f$C %>%
  vegan::metaMDS(., trymax = 500, k = 2, trace = 0) %>%
  vegan::scores() %>%
  as_tibble(., rownames = "sample") %>%
  dplyr::left_join(sample_metadata, by = join_by(sample == sample)) %>%
  group_by(bat_species) %>%
  mutate(x_cen = mean(NMDS1, na.rm = TRUE)) %>%
  mutate(y_cen = mean(NMDS2, na.rm = TRUE)) %>%
  ungroup() %>%
  ggplot(aes(x = NMDS1, y = NMDS2, color = bat_species)) +
    scale_color_manual(values = c("#e5bd5b", "#6b7398","#e2815a")) +
    scale_shape_manual(values = 1:10) +
    geom_point(size = 4) +
    #   stat_ellipse(aes(color = beta_q1n_nmds$Groups))+
    geom_segment(aes(x = x_cen, y = y_cen, xend = NMDS1, yend = NMDS2), alpha = 0.9) +
    theme_classic() +
    theme(
      axis.text.x = element_text(size = 12),
      axis.text.y = element_text(size = 12),
      axis.title = element_text(size = 20, face = "bold"),
      axis.text = element_text(face = "bold", size = 18),
      panel.background = element_blank(),
      axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"),
      legend.text = element_text(size = 16),
      legend.title = element_text(size = 18),
      legend.position = "right", legend.box = "vertical"
    )