Chapter 7 Functional differences

load("resources/data.Rdata")
# Aggregate bundle-level GIFTs into the compound level
GIFTs_elements <- to.elements(genome_gifts, GIFT_db)
GIFTs_elements_filtered <- GIFTs_elements[rownames(GIFTs_elements) %in% genome_counts$genome, ]
GIFTs_elements_filtered <- as.data.frame(GIFTs_elements_filtered) %>%
  select_if(~ !is.numeric(.) || sum(.) != 0)

elements <- GIFTs_elements_filtered %>%
  as.data.frame()

# Aggregate element-level GIFTs into the function level
GIFTs_functions <- to.functions(GIFTs_elements_filtered, GIFT_db)
functions <- GIFTs_functions %>%
  as.data.frame()

# Aggregate function-level GIFTs into overall Biosynthesis, Degradation and Structural GIFTs
GIFTs_domains <- to.domains(GIFTs_functions, GIFT_db)
domains <- GIFTs_domains %>%
  as.data.frame()

# Get community-weighed average GIFTs per sample
GIFTs_elements_community <- to.community(GIFTs_elements_filtered, genome_counts_filt %>% column_to_rownames(., "genome") %>% tss(), GIFT_db)
GIFTs_functions_community <- to.community(GIFTs_functions, genome_counts_filt %>% column_to_rownames(., "genome") %>% tss(), GIFT_db)
GIFTs_domains_community <- to.community(GIFTs_domains, genome_counts_filt %>% column_to_rownames(., "genome") %>% tss(), GIFT_db)

7.1 Function level

GIFTs_functions_community %>%
    as.data.frame() %>%
    rownames_to_column(var="sample") %>%
    pivot_longer(!sample,names_to="trait",values_to="gift") %>%
    left_join(sample_metadata, by = join_by(sample == sample)) %>%
    ggplot(aes(x=trait,y=sample,fill=gift)) +
        geom_tile(colour="white", size=0.2)+
        scale_fill_gradientn(colours=rev(c("#d53e4f", "#f46d43", "#fdae61", "#fee08b", "#e6f598", "#abdda4", "#ddf1da")))+
        facet_grid(type ~ ., scales="free",space="free")

7.1.1 GIFT test

GIFTs_functions_community_tt <- GIFTs_functions_community %>%
  as.data.frame() %>%
  rownames_to_column("sample") %>%
  separate(sample, into = c("individual", "type"), sep = "\\.") %>%
  pivot_longer(-c(individual, type), names_to = "trait", values_to = "value") %>%
  group_by(trait) %>%
  mutate(model_result = list(lmerTest::lmer(value ~ type + (1 | individual)))) %>%
  ungroup() %>%
  select(trait,model_result) %>%
  unique() %>%
  mutate(estimate = map_dbl(model_result, ~broom.mixed::tidy(.) %>% filter(term == "typefeces") %>% pull(estimate))) %>%

  mutate(p_value = map_dbl(model_result, ~broom.mixed::tidy(.) %>% filter(term == "typefeces") %>% pull(p.value))) %>%
  mutate(p_value_adj = p.adjust(p_value, method = "bonferroni")) %>%
  left_join(GIFT_db %>% select(Code_function,Function) %>% unique(),by=join_by(trait==Code_function)) %>%
  rename(id=trait,trait=Function) %>%
  select(id,trait, estimate, p_value_adj)

GIFTs_functions_community_tt %>% tt() |> 
      style_tt(
        i = which(GIFTs_functions_community_tt$estimate < 0 & GIFTs_functions_community_tt$p_value_adj < 0.05),
        background = "#E5D5B1") |> 
      style_tt(
        i = which(GIFTs_functions_community_tt$estimate > 0 & GIFTs_functions_community_tt$p_value_adj < 0.05),
        background = "#B7BCCE")
tinytable_cctyep691l70f1qcnm2y
id trait estimate p_value_adj
B01 Nucleic acid biosynthesis -0.159859172 3.056969e-02
B02 Amino acid biosynthesis -0.110393771 7.176260e-01
B03 Amino acid derivative biosynthesis -0.005675742 1.000000e+00
B04 SCFA biosynthesis -0.101802836 9.451665e-02
B06 Organic anion biosynthesis -0.142419002 8.690010e-07
B07 Vitamin biosynthesis -0.245911630 1.417697e-03
B08 Aromatic compound biosynthesis -0.075353117 1.000000e+00
B09 Metallophore biosynthesis 0.024252618 9.856097e-04
B10 Antibiotic biosynthesis 0.016871838 9.042655e-04
D01 Lipid degradation 0.024068845 1.000000e+00
D02 Polysaccharide degradation 0.225103688 8.086379e-03
D03 Sugar degradation 0.160719202 1.837959e-02
D05 Amino acid degradation -0.061015780 9.915551e-02
D06 Nitrogen compound degradation 0.069239014 6.663246e-04
D07 Alcohol degradation -0.054220812 1.000000e+00
D08 Xenobiotic degradation 0.014284632 3.645558e-02
D09 Antibiotic degradation -0.051783327 1.000000e+00
S01 Cellular structure -0.059312854 1.000000e+00
S02 Appendages -0.047454351 1.000000e+00
S03 Spore -0.041956134 1.000000e+00

7.1.2 GIFT test visualisation

GIFTs_functions_community %>%
  as.data.frame() %>%
  rownames_to_column("sample") %>%
  separate(sample, into = c("individual", "type"), sep = "\\.") %>%
  pivot_longer(-c(individual, type), names_to = "trait", values_to = "value") %>%
  mutate(trait = case_when(
      trait %in% GIFT_db$Code_function ~ GIFT_db$Function[match(trait, GIFT_db$Code_function)],
      TRUE ~ trait
    )) %>%
  mutate(trait=factor(trait,levels=unique(GIFT_db$Function))) %>%
  ggplot(aes(x=value, y=type, group=type, fill=type, color=type)) +
    geom_boxplot() +
    scale_color_manual(name="Sample type",
          breaks=c("cloaca","feces"),
          labels=c("Cloaca","Faeces"),
          values=c("#e5bd5b", "#6b7398")) +
      scale_fill_manual(name="Sample type",
          breaks=c("cloaca","feces"),
          labels=c("Cloaca","Faeces"),
          values=c("#e5bd5b50", "#6b739850")) +
    facet_grid(trait ~ ., space="free", scales="free") +
              theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
              strip.text.y = element_text(angle = 0)) + 
        labs(y="Traits",x="Metabolic capacity index")

7.2 Element level

GIFTs_elements_community %>%
    as.data.frame() %>%
    rownames_to_column(var="sample") %>%
    pivot_longer(!sample,names_to="trait",values_to="gift") %>%
    left_join(sample_metadata, by = join_by(sample == sample)) %>%
    mutate(functionid = substr(trait, 1, 3)) %>%
    mutate(trait = case_when(
      trait %in% GIFT_db$Code_element ~ GIFT_db$Element[match(trait, GIFT_db$Code_element)],
      TRUE ~ trait
    )) %>%
    mutate(functionid = case_when(
      functionid %in% GIFT_db$Code_function ~ GIFT_db$Function[match(functionid, GIFT_db$Code_function)],
      TRUE ~ functionid
    )) %>%
    mutate(trait=factor(trait,levels=unique(GIFT_db$Element))) %>%
    mutate(functionid=factor(functionid,levels=unique(GIFT_db$Function))) %>%
    ggplot(aes(x=sample,y=trait,fill=gift)) +
        geom_tile(colour="white", linewidth=0.2)+
        scale_fill_gradientn(colours=rev(c("#d53e4f", "#f46d43", "#fdae61", "#fee08b", "#e6f598", "#abdda4", "#ddf1da")))+
        facet_grid(functionid ~ type, scales="free",space="free") +
        theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
              strip.text.y = element_text(angle = 0)) + 
        labs(y="Traits",x="Samples",fill="GIFT")

7.2.1 GIFT test

GIFTs_elements_community_tt <- GIFTs_elements_community %>%
  as.data.frame() %>%
  rownames_to_column("sample") %>%
  separate(sample, into = c("individual", "type"), sep = "\\.") %>%
  pivot_longer(-c(individual, type), names_to = "trait", values_to = "value") %>%
  group_by(trait) %>%
  mutate(model_result = list(lmerTest::lmer(value ~ type + (1 | individual)))) %>%
  ungroup() %>%
  select(trait,model_result) %>%
  unique() %>%
  mutate(estimate = map_dbl(model_result, ~broom.mixed::tidy(.) %>% filter(term == "typefeces") %>% pull(estimate))) %>%
  mutate(p_value = map_dbl(model_result, ~broom.mixed::tidy(.) %>% filter(term == "typefeces") %>% pull(p.value))) %>%
  mutate(p_value_adj = p.adjust(p_value, method = "bonferroni")) %>%
  left_join(GIFT_db %>% select(Code_element,Element) %>% unique(),by=join_by(trait==Code_element)) %>%
  rename(id=trait,trait=Element) %>%
  select(id,trait, estimate, p_value_adj)

GIFTs_elements_community_tt %>% tt() |> 
      style_tt(
        i = which(GIFTs_elements_community_tt$estimate < 0 & GIFTs_elements_community_tt$p_value_adj < 0.05),
        background = "#E5D5B1") |> 
      style_tt(
        i = which(GIFTs_elements_community_tt$estimate > 0 & GIFTs_elements_community_tt$p_value_adj < 0.05),
        background = "#B7BCCE")
tinytable_i7c1y42k3kfxj8huzyzk
id trait estimate p_value_adj
B0101 Inosinic acid (IMP) -0.151236188 9.039137e-04
B0102 Uridylic acid (UMP) -0.094812312 1.000000e+00
B0103 UDP/UTP -0.078774552 1.273680e-04
B0104 CDP/CTP -0.291124228 1.894078e-01
B0105 ADP/ATP -0.217775882 3.148082e-02
B0106 GDP/GTP -0.125431870 1.000000e+00
B0204 Serine -0.407615768 7.121696e-04
B0205 Threonine -0.104078962 1.000000e+00
B0206 Cysteine -0.057144737 1.000000e+00
B0207 Methionine -0.080974853 1.000000e+00
B0208 Valine -0.104574494 1.000000e+00
B0209 Isoleucine -0.132603069 1.000000e+00
B0210 Leucine -0.300307892 7.159663e-03
B0211 Lysine -0.188989369 7.751700e-04
B0212 Arginine -0.120559972 1.000000e+00
B0213 Proline 0.030641621 1.000000e+00
B0214 Glutamate -0.121294193 1.000000e+00
B0215 Histidine -0.158359034 1.000000e+00
B0216 Tryptophan -0.222953618 1.000000e+00
B0217 Phenylalanine 0.104654741 5.429839e-03
B0218 Tyrosine -0.070415175 1.000000e+00
B0219 GABA 0.010264623 2.957984e-04
B0220 Beta-alanine 0.061908085 1.000000e+00
B0221 Ornithine -0.075999324 1.000000e+00
B0302 Betaine 0.007429289 6.107924e-02
B0303 Ectoine -0.084897201 1.000000e+00
B0307 Spermidine 0.001525887 1.000000e+00
B0309 Putrescine 0.026195706 4.093011e-04
B0310 Tryptamine 0.008262580 7.302535e-01
B0401 Acetate -0.105801039 1.000000e+00
B0402 Butyrate -0.106374700 1.000000e+00
B0403 Propionate -0.090489652 5.228183e-02
B0601 Succinate -0.236011819 1.219833e-02
B0602 Fumarate -0.185345084 1.412915e-03
B0603 Citrate -0.144668012 4.297163e-02
B0604 L-lactate 0.269612255 1.000000e+00
B0605 D-lactate 0.162110506 1.000000e+00
B0701 Thiamine (B1) -0.467508286 2.093102e-05
B0702 Riboflavin (B2) -0.094914710 1.000000e+00
B0703 Niacin (B3) -0.144515836 1.442833e-05
B0704 Pantothenate (B5) -0.132006523 1.000000e+00
B0705 Pyridoxal-P (B6) -0.136554096 1.000000e+00
B0706 Biotin (B7) -0.505291636 6.783663e-05
B0707 Tetrahydrofolate (B9) -0.183686244 5.399698e-02
B0708 Cobalamin (B12) -0.351214616 5.121565e-09
B0709 Tocopherol/tocotorienol (E) 0.051315283 1.768034e-03
B0710 Phylloquinone (K1) 0.009625337 1.000000e+00
B0711 Menaquinone (K2) -0.094429175 3.872932e-02
B0712 Ubiquinone (Q10) -0.041293838 3.152997e-07
B0801 Salicylate 0.004992759 1.000000e+00
B0802 Gallate -0.056848209 1.000000e+00
B0803 Chorismate -0.204713195 5.034798e-01
B0804 Dipicolinate -0.179083253 6.017894e-01
B0805 Indole-3-acetate 0.003238801 1.000000e+00
B0901 Staphyloferrin 0.033797012 1.045425e-02
B0902 Aerobactin 0.004016512 1.000000e+00
B0903 Staphylopine 0.026547258 1.276233e-02
B1004 Bacilysin -0.019592755 1.882566e-01
B1012 Fosfomycin 0.041748103 1.506260e-03
B1014 Kanosamine 0.003291927 1.000000e+00
B1028 Pyocyanin 0.036245914 1.933005e-04
D0101 Triglyceride 0.067359825 2.628573e-03
D0102 Fatty acid 0.001114093 1.000000e+00
D0103 Oleate 0.028341734 1.000000e+00
D0104 Dicarboxylic acids -0.043831252 1.000000e+00
D0201 Cellulose 0.139526853 1.172528e-03
D0202 Xyloglucan 0.194107364 1.135739e-01
D0203 Starch 0.217328224 1.000000e+00
D0204 Chitin 0.056939483 1.000000e+00
D0205 Pectin 0.166966707 2.387147e-02
D0206 Alpha galactan 0.383968623 1.000000e+00
D0207 Beta-galactan 0.347673216 9.566923e-01
D0208 Mixed-Linkage glucans 0.182788325 5.967716e-01
D0209 Xylans 0.268879339 1.047317e-02
D0210 Beta-mannan 0.217084318 5.479793e-03
D0211 Alpha-mannan 0.121559022 1.696668e-07
D0212 Arabinan 0.358023402 4.330492e-05
D0213 Mucin 0.271503069 2.839188e-01
D0301 Lactose 0.022120411 2.001974e-01
D0302 Sucrose 0.194515642 1.471257e-03
D0303 D-Apiose 0.001448446 1.000000e+00
D0304 D-Arabinose 0.143547948 1.000000e+00
D0305 D-Mannose -0.092031194 1.471735e-02
D0306 D-Xylose 0.246454359 1.097668e-04
D0307 L-Fucose 0.196975665 9.266313e-03
D0308 L-Rhamnose 0.213829078 1.000000e+00
D0309 Galactose 0.113574961 1.000000e+00
D0310 NeuAc 0.255990352 1.000000e+00
D0501 Serine -0.247973804 3.812504e-03
D0502 Threonine -0.210114768 1.000000e+00
D0503 Cysteine 0.012739997 3.429585e-04
D0504 Methionine 0.002898338 1.000000e+00
D0505 Valine -0.126063939 1.000000e+00
D0506 Isoleucine -0.067181509 5.267199e-03
D0507 Leucine -0.011458718 1.000000e+00
D0508 Lysine 0.032538573 8.166576e-05
D0509 Arginine 0.009706886 1.000000e+00
D0510 Proline 0.024264477 1.000000e+00
D0511 Glutamate -0.101293585 1.000000e+00
D0512 Histidine -0.125010734 1.000000e+00
D0513 Tryptophan -0.304229107 1.000000e+00
D0516 Beta-alanine -0.232368879 1.000000e+00
D0517 Ornithine 0.009656190 3.304008e-01
D0518 GABA 0.024186489 1.000000e+00
D0601 Nitrate -0.046019486 1.000000e+00
D0602 Urea 0.165501629 1.335954e-04
D0603 Urate 0.005473487 1.439794e-01
D0604 GlcNAc 0.458453390 1.254991e-02
D0606 Allantoin 0.056328281 2.184103e-01
D0607 Creatinine 0.035964568 1.431127e-02
D0609 L-carnitine 0.140790141 1.643897e-04
D0610 Methylamine -0.089224824 1.000000e+00
D0611 Phenylethylamine 0.004795965 1.000000e+00
D0612 Hypotaurine 0.014267712 1.000000e+00
D0613 Taurine 0.096443326 1.000000e+00
D0701 2,3-Butanediol 0.003790326 1.000000e+00
D0702 Ethanol -0.240044383 1.000000e+00
D0704 Glycerol 0.083494880 1.000000e+00
D0705 Propylene glycol -0.062393794 1.000000e+00
D0706 Ethylene glycol -0.255591415 1.760625e-01
D0708 Phytol -0.081903996 1.065901e-04
D0801 Toluene 0.001551262 1.000000e+00
D0802 Xylene 0.001551262 1.000000e+00
D0805 Benzoate 0.001897626 1.000000e+00
D0807 Catechol 0.023097664 1.000000e+00
D0815 Phthalate 0.004042691 1.000000e+00
D0816 Phenylacetate 0.038329639 1.000000e+00
D0817 Trans-cinnamate 0.045829620 2.579984e-05
D0901 Penicillin -0.182460165 1.000000e+00
D0902 Carbapenem 0.037048290 1.279223e-02
D0903 Cephalosporin 0.007544015 6.387796e-03
D0904 Oxacillin 0.004385875 1.000000e+00
D0905 Streptogramin -0.271876668 1.000000e+00
D0907 Tetracycline 0.452642711 2.261808e-04
D0908 Macrolide -0.160017386 1.000000e+00
D0910 Chloramphenicol -0.093165710 1.000000e+00
D0911 Lincosamide -0.260150907 1.000000e+00
S0101 Peptidoglycan -0.180843448 4.554284e-08
S0103 Teichoic acid -0.124792154 1.000000e+00
S0104 Lipoteichoic acid 0.061887680 1.000000e+00
S0105 Lipopolysaccharide 0.017904784 1.000000e+00
S0201 Flagellum -0.198532641 1.000000e+00
S0202 Pilus 0.053264508 1.561234e-04
S0301 Spore -0.041956134 1.000000e+00