• AlberdiLab logo
  • Marcos et al. 2025
  • 1 Introduction
    • 1.1 Prepare the R environment
      • 1.1.1 Environment
      • 1.1.2 Libraries
  • 2 Metagenomic data preparation
    • 2.1 Load data
      • 2.1.1 Chicken sample metadata
      • 2.1.2 Taxonomy of metagenome-assembled genomes
      • 2.1.3 Genome statistics
      • 2.1.4 Phylogenetic tree
      • 2.1.5 Raw gene annotations
      • 2.1.6 DRAM functional annotations
      • 2.1.7 Metagenomic read counts
    • 2.2 Distilling Genome Inferred Functional Traits (GIFTs)
      • 2.2.1 Distilling DRAM annotations
      • 2.2.2 Aggregate GIFTs
    • 2.3 Create working objects
      • 2.3.1 Transform reads into genome counts
      • 2.3.2 Calculate relative abundances
      • 2.3.3 Prepare a phyloseq object
    • 2.4 Prepare color scheme
    • 2.5 Wrap working objects
  • 3 Metatranscriptomic data preparation
    • 3.1 Load data
      • 3.1.1 Metatranscriptomic gene expression counts
      • 3.1.2 Distillation
      • 3.1.3 Combine metatranscriptomic GIFT expression counts
      • 3.1.4 Metadata of chickens sequenced for MT
    • 3.2 Save working objects
  • 4 Catalogue richness
    • 4.1 Load MG data
    • 4.2 Functional ordination
      • 4.2.1 Plot genome length and average MCI gradients
      • 4.2.2 Plot SCFA and vitamin biosynthesis gradients
    • 4.3 Compare MCI and genome length between taxonomic groups
      • 4.3.1 Average MCI by taxonomic phylum and order
      • 4.3.2 Genome length by taxonomic phylum and order
  • 5 Alpha diversity
    • 5.1 Load MG data
    • 5.2 Hill numbers - Alpha diversity
      • 5.2.1 Alpha diversity by sampling day
    • 5.3 Temporal development
      • 5.3.1 Neutral
      • 5.3.2 Phylogenetic
      • 5.3.3 Functional KEGG
  • 6 Bacterial community composition
    • 6.1 Load MG data
    • 6.2 Hill numbers - beta diversity
      • 6.2.1 Compare individuals from same trial and pen between sampling days
      • 6.2.2 Plot difference between sampling points for beta diversity
    • 6.3 Effect of design in microbiome composition
      • 6.3.1 Permanova values for different metrics of beta diversity
      • 6.3.2 Community composition
    • 6.4 Community composition development
      • 6.4.1 Distance-based RDA
  • 7 Bacterial temporal trends
    • 7.1 Load MG data
    • 7.2 Functional ordination - Temporal PCoA with PCA loadings
    • 7.3 Compositional barplots
      • 7.3.1 Compositional barplot at order level
  • 8 Temporal HMSC setup
    • 8.1 Load MG data
      • 8.1.1 Define directories
      • 8.1.2 Generate input objects
    • 8.2 Prepate data for HMSC
      • 8.2.1 Define tables
    • 8.3 Define formula
      • 8.3.1 Define HMSC model
      • 8.3.2 Define MCMC characteristics
    • 8.4 Fit model
    • 8.5 Evaluate convergence
  • 9 HMSC analysis
    • 9.1 Load model
    • 9.2 Cross-validation
      • 9.2.1 Model fit using 2-fold CV: samples randomly assigned to folds
      • 9.2.2 Model fit using 2-fold CV: each fold contains the samples from one trial
    • 9.3 Functional structure
    • 9.4 Bacterial temporal trends table
    • 9.5 Correlate bacterial MCI and response to time
    • 9.6 Variance partitioning
    • 9.7 Phylogenetic correlogram
      • 9.7.1 Calcultate phylogenetic distance
      • 9.7.2 Phylogenetic signal
  • 10 Metabolic capacity index
    • 10.1 Load MG data
    • 10.2 GIFTs per MAG
      • 10.2.1 Plot GIFTs of all bacteria by element
    • 10.3 GIFTs per community
      • 10.3.1 Calculate community GIFTs
      • 10.3.2 Plot community MCI
  • 11 Associate microbial community metrics with chicken body weight
    • 11.1 Load MG and MT data
    • 11.2 Associate alpha diversity metrics with chicken BW
      • 11.2.1 Neutral
    • 11.3 Phylogenetic
    • 11.4 Functional KEGG
    • 11.5 Linear regressions
      • 11.5.1 Neutral
      • 11.5.2 Phylogenetic
      • 11.5.3 Functional KEGG
    • 11.6 Associate community MCI with diversity
    • 11.7 Associate community MCI with chicken BW
    • 11.8 Associate community weighted genome size with chicken BW
  • 12 Bacteria vs Chicken BW HMSC setup
    • 12.1 Load MG and MT data
      • 12.1.1 Define directories
      • 12.1.2 Generate input objects
    • 12.2 Prepate data for HMSC
      • 12.2.1 Define tables
      • 12.2.2 Define formulas
      • 12.2.3 Define HMSC model
      • 12.2.4 Define MCMC
    • 12.3 Fit model
  • 13 Bacteria vs Chicken BW HMSC analysis
    • 13.1 Load model
      • 13.1.1 Define directories
      • 13.1.2 Load model
    • 13.2 Evaluate model
    • 13.3 Examine species responses
    • 13.4 Examine variance partition
    • 13.5 Create a data table
      • 13.5.1 Associate bacteria response with chicken BW
    • 13.6 Create table with HMSC results
  • 14 Comparing metabolic capacity of positively and negatively associated bacteria
    • 14.1 Load MG data
    • 14.2 Taxonomy of positively and negatively associated MAGs
      • 14.2.1 Positive group
      • 14.2.2 Negative group
    • 14.3 Compute MCIs of BW positive and negative associated communities
      • 14.3.1 MCI comparisons of biosynthesis pathways
      • 14.3.2 The significant ones
      • 14.3.3 All
      • 14.3.4 All
    • 14.4 Heatmap of MCI at element level
  • 15 Comparing activity of positively and negatively associated bacteria
    • 15.1 Load data
    • 15.2 Expression profiles of positively and negatively associated bacteria
      • 15.2.1 Element-based expression table of day 35 individuals
      • 15.2.2 Normalised expression table
      • 15.2.3 Filter MAGs associated with chicken body weight
      • 15.2.4 Tidy data
      • 15.2.5 Plot
    • 15.3 Taxonomy of reduced MAGs
    • 15.4 Genomic characteristics of genome reduced MAGs
      • 15.4.1 Genome length of genome reduced MAGss
      • 15.4.2 Expression profiles of genome reduced MAGs
      • 15.4.3 Activity heatmap of genome reduced bacteria
  • 16 Testing completeness correction before calculating distilling GIFTs
    • 16.1 Load MG data
    • 16.2 Define function for correction
    • 16.3 Calculate corrected GIFTs
    • 16.4 Save files so you don’t spend time re-generating them
    • 16.5 compare MCI values without and with correction
    • 16.6 Spearman correlation
    • 16.7 Comparing average MCI with genome completeness
    • 16.8 Reduced genomes
    • 16.9 Spearman correlation
    • 16.10 Illustrating correlation between average MCI and completeness
    • 16.11 Testing differences at community-weighted MCI
  • www.alberdilab.dk
  • www.earthhologenome.org
  • www.holo-omics.science

AlberdiLab | Marcos et al. 2026

Chapter 7 Bacterial temporal trends

7.1 Load MG data

load("data/data_mg.Rdata")

7.2 Functional ordination - Temporal PCoA with PCA loadings

The resulting plot corresponds to Figure 2c.

gift_pcoa <- 
  gifts_elements %>% 
  as.data.frame() %>%
  vegdist(method="euclidean") %>%
  pcoa()

gift_pcoa_rel_eigen <- gift_pcoa$values$Relative_eig[1:10]

# Get genome positions
gift_pcoa_vectors <- 
  gift_pcoa$vectors %>% #extract vectors
  as.data.frame() %>% 
  select(Axis.1,Axis.2) # keep the first 2 axes


total %>%
  rownames_to_column("genome") %>% 
  pivot_longer(-genome, names_to = "animal_code", values_to = "count") %>%
  left_join(chicken_metadata, by = join_by(animal_code == animal_code)) %>% 
  filter(!is.na(sampling_time)) %>% 
  group_by(genome, trial, sampling_time) %>% 
  summarise(count = mean(count)) %>% 
  left_join(gift_pcoa_vectors %>% rownames_to_column(var = "genome"), by = "genome") %>%
  left_join(genome_taxonomy %>% select(genome, phylum, order), by = "genome") %>%
  ggplot() +
      theme_minimal() +
      #genome positions
      geom_point(aes(x = Axis.1, y = Axis.2, color = order, size = count), 
                 alpha = 0.8, shape = 16) +
      # colors
      scale_color_manual(values = order_colors) +    
      # scale_color_manual(values=phylum_colors) +
      scale_size_continuous(range = c(1,10)) +
      # scale adjustments
      scale_x_continuous(name = paste0("PCoA1 (",round(gift_pcoa_rel_eigen[1]*100, digits = 2), " %)")) +
      scale_y_continuous(name = paste0("PCoA2 (",round(gift_pcoa_rel_eigen[2]*100, digits = 2), " %)")) +
      facet_grid(trial ~ sampling_time) +
      theme(legend.position = "none",
            axis.line = element_line(color = "grey30"),
            panel.border = element_rect(color = "grey80", fill = NA),
            axis.ticks = element_line(color = "grey30"),
            panel.grid = element_line(color = "grey80"),
            strip.background = element_rect(fill = "grey80", color = NA))

7.3 Compositional barplots

7.3.1 Compositional barplot at order level

physeq_comp_rare_order <-
  aggregate_rare(
    phylo_seq,
    level = "order",
    detection = 0.01 / 100,
    prevalence = 50 / 100,
    include.lowest = TRUE)
  
phy_order_df <- 
  psmelt(physeq_comp_rare_order) %>%
  select(OTU, Sample, trial_age, Abundance) %>%
  rename(order = 'OTU') %>%
  separate_wider_delim(trial_age, "_", names = c("trial", "sampling_time"))
# separating by sampling time and trial
phy_order_df %>%
  mutate(order = factor(order, 
                        levels = c('Veillonellales', 'Acidaminococcales',
                                   'Selenomonadales', 'UBA4068', 'UBA1212',
                                   'Monoglobales', 'Peptostreptococcales',
                                   'Monoglobales_A', 'Clostridiales',
                                   'Lachnospirales', 'Christensenellales',
                                   'TANB77', 'Oscillospirales', 'RFN20', 
                                   'Bacillales','Acholeplasmatales', 
                                   'Erysipelotrichales', 'ML615J-28', 
                                   'Lactobacillales','RF39','Deferribacterales',
                                   'Actinomycetales', 'Coriobacteriales',
                                   'Gastranaerophilales','Flavobacteriales',
                                   'Bacteroidales', 'Rs-D84', 'Burkholderiales',
                                   'RF32','Enterobacterales', 
                                   'Desulfovibrionales', 'Verrucomicrobiales',
                                   'Opitutales','Victivallales', 
                                   'Methanomassiliicoccales', 'Synergistales',
                                   'Campylobacterales','Saccharimonadales',
                                   'Methanobacteriales', 'Methanomicrobiales'))) %>%
  mutate(sampling_time = str_replace(sampling_time,
                                     pattern = "7",
                                     replacement = "Day 7"),
         sampling_time = str_replace(sampling_time,
                                     pattern = "21",
                                     replacement = "Day 21"),
         sampling_time = str_replace(sampling_time,
                                     pattern = "35",
                                     replacement = "Day 35")) %>%
  mutate(sampling_time = factor(sampling_time,
                                levels = c("Day 7", "Day 21", "Day 35"))) %>%
  ggplot(aes(x = Abundance, y = Sample,
             fill = order, group = order)) +
  geom_bar(stat = "identity", position = "stack", width = 1,
         color ="darkgrey", size = 0.005) +
  facet_nested(sampling_time * trial ~., scales = "free_y") +
  scale_color_manual('Order', values = order_colors) +
  scale_fill_manual('Order', values = order_colors) +
  ylab('Relative abundance') +
  theme(axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.spacing.x = unit(0.1, "cm"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 8),
        legend.title = element_text(size = 10),
        strip.text = element_text(size = 8),
        legend.position = "right")  +
  guides(fill = guide_legend(ncol = 1))

rm(list = ls())