Chapter 2 Data preparation

2.1 Load data

Load the original data files outputted by the bioinformatic pipeline.

2.1.1 Sample metadata

sample_metadata <- read_csv("data/sample_metadata.csv") %>% 
  mutate(location=factor(location,levels=c("Aruba","Brazil","CaboVerde","Spain","Denmark","Malaysia")),
         origin=factor(origin,levels=c("Domestic","Feral")))

2.1.2 Genome metadata

genome_metadata <- read_csv("data/genome_metadata.csv") %>%
    mutate(phylum = case_when(
        phylum == "Actinobacteriota" ~ "Actinomycetota",
        phylum == "Firmicutes" ~ "Bacillota",
        phylum == "Firmicutes_A" ~ "Bacillota_A",
        phylum == "Firmicutes_C" ~ "Bacillota_C",
        phylum == "Cyanobacteria" ~ "Cyanobacteriota",
        phylum == "Proteobacteria" ~ "Pseudomonadota",
        TRUE ~ phylum))

2.1.3 Read counts

read_counts <- read_tsv("data/read_counts.tsv") %>%
    rename(genome=1) %>%
    select(c("genome",sample_metadata$sample)) %>%
    arrange(match(genome,genome_metadata$genome))

2.1.4 Genome coverage

genome_coverage <- read_tsv("data/genome_coverage.tsv") %>%
    rename(genome=1) %>%
    select(c("genome",sample_metadata$sample))%>%
    arrange(match(genome,genome_metadata$genome))

2.1.5 Genome tree

genome_tree <- read_tree("data/genome_tree.tre")
genome_tree$tip.label <- str_replace_all(genome_tree$tip.label,"'", "") #remove single quotes in MAG names
genome_tree <- keep.tip(genome_tree, tip=genome_metadata$genome) # keep only MAG tips

2.1.6 Genome annotations

genome_annotations <- read_tsv("data/genome_annotations.tsv.xz") %>%
    rename(gene=1, genome=2, contig=3)

2.2 Create working objects

Transform the original data files into working objects for downstream analyses.

2.2.1 Filter reads by coverage

min_coverage=0.3
read_counts_filt <- genome_coverage %>%
  mutate(across(where(is.numeric), ~ ifelse(. > min_coverage, 1, 0))) %>%
  mutate(across(-1, ~ . * read_counts[[cur_column()]])) 

2.2.2 Transform reads into genome counts

readlength=150
genome_counts <- read_counts %>%
  mutate(across(where(is.numeric), ~ . / (genome_metadata$length / readlength) ))
readlength=150
genome_counts_filt <- read_counts_filt %>%
  mutate(across(where(is.numeric), ~ . / (genome_metadata$length / readlength) ))

2.2.3 Distill annotations into GIFTs

genome_gifts <- distill(genome_annotations,GIFT_db,genomecol=2,annotcol=c(9,10,19), verbosity=F)

2.3 Prepare color scheme

AlberdiLab projects use unified color schemes developed for the Earth Hologenome Initiative, to facilitate figure interpretation.

phylum_colors <- read_tsv("https://raw.githubusercontent.com/earthhologenome/EHI_taxonomy_colour/main/ehi_phylum_colors.tsv") %>%
    mutate(phylum=str_remove_all(phylum, "p__")) %>%
    right_join(genome_metadata, by=join_by(phylum == phylum)) %>%
    arrange(match(genome, genome_tree$tip.label)) %>%
    dplyr::select(phylum, colors) %>% 
    unique() %>%
    arrange(phylum) %>%
    pull(colors, name=phylum)

location_colors <- c('#3D5C61','#41B6C0','#90C8C5','#E5D388','#BFA366','#6E5244')

origin_colors <- c("#bd70ae","#949293")

2.4 Wrap working objects

All working objects are wrapped into a single Rdata object to facilitate downstream usage.

save(sample_metadata, 
     genome_metadata, 
     read_counts, 
     genome_counts, 
     genome_counts_filt, 
     genome_tree,
     genome_gifts, 
     phylum_colors,
     location_colors,
     origin_colors,
#     physeq_genome,
#     physeq_genome_clr,
     file = "data/data.Rdata")