## `eggnog` - Functional annotation through orthology assignment 

[Eggnog-mapper ](https://github.com/eggnogdb/eggnog-mapper/) annotates genomes using a database of pre-computed groups of orthologs to transfer functional information.

```{r eggnog, echo = F, message = F, warning = F, error = T}

#setwd("~/comparem2/")
eggnog_annotations = read_tsv(glob_list$eggnog, comment = "##", na = "-", id = "file") %>% 
    mutate(
        sample = basename(file) %>% str_remove("\\.emapper\\.annotations$")
    ) %>% 
    relocate(sample) %>% 
    select(-file, query = "#query")

eggnog_count = eggnog_annotations %>%
    select(sample, query, GOs, EC, KEGG_ko, CAZy, BiGG_Reaction, PFAMs) %>% 
    pivot_longer(-sample) %>% 
    drop_na(value) %>% 
    group_by(sample) %>% 
    count(name) %>% 
    pivot_wider(names_from = "name", values_from = "n") %>% 
    relocate(sample, query)


metadata_translate %>%
    select(sample) %>%
    left_join(eggnog_count, by = "sample") %>%
    custom_dt("eggnog")


```

*Table `r tableno()`: Count of Eggnogg-mapper hits. Column "query" shows the total number of genes in the genome. The other columns show the number of hits in different categories.*



```{r eggnog-plot, echo = F, message = F, warning = F, error = T, fig.height = max(1.8, N*0.2), fig.width = 10}

cog_categories = tribble(
    ~"letter", ~"description",
    "A", "RNA processing and modification",
    "B", "Chromatin structure and dynamics",
    "C", "Energy production and conversion",
    "D", "Cell cycle control, cell division, chromosome partitioning",
    "E", "Amino acid transport and metabolism",
    "F", "Nucleotide transport and metabolism",
    "G", "Carbohydrate transport and metabolism",
    "H", "Coenzyme transport and metabolism",
    "I", "Lipid transport and metabolism",
    "J", "Translation, ribosomal structure and biogenesis",
    "K", "Transcription",
    "L", "Replication, recombination and repair",
    "M", "Cell wall/membrane/envelope biogenesis",
    "N", "Cell motility",
    "O", "Posttranslational modification, protein turnover, chaperones",
    "P", "Inorganic ion transport and metabolism",
    "Q", "Secondary metabolites biosynthesis, transport and catabolism",
    "R", "General function prediction only",
    "S", "Function unknown",
    "T", "Signal transduction mechanisms",
    "U", "Intracellular trafficking, secretion, and vesicular transport",
    "V", "Defense mechanisms",
    "W", "Extracellular structures",
    "X", "Mobilome: prophages, transposons",
    "Y", "Nuclear structure",
    "Z", "Cytoskeleton"
)


eggnog_annotations_plot_df = eggnog_annotations %>% 
    select(sample, COG_category) %>% #head(40) %>% 
    mutate(COG_category = coalesce(COG_category, "?")) %>% # NAs are actually just unknown/uncategorized genes.
    mutate(
        weight = 1/str_length(COG_category),
        COG_category_splitted = strsplit(COG_category, "")
        ) %>% 
    unnest(COG_category_splitted) %>% 
    
    group_by(sample, COG_category_splitted) %>% 
    summarize(count = sum(weight))


metadata_translate %>%
    select(index, sample) %>%
    left_join(eggnog_annotations_plot_df, by = "sample") %>% 

    ggplot(aes(COG_category_splitted, reorder(sample, desc(index)), fill = count)) + 
    scale_fill_viridis_b(trans = "log", direction = -1) + 
    geom_tile() +
    theme_classic() + 
    labs(
        x = "COG category", 
        y = "sample"
    )


```

*Fig. `r figno()`: Counts of genes from [COG categories](https://www.ncbi.nlm.nih.gov/research/cog/). Column "?" denotes genes not allocated to any COG category. COG categories are abbreviated as follows: A RNA processing and modification, B Chromatin structure and dynamics, C Energy production and conversion, D Cell cycle control, cell division, chromosome partitioning, E Amino acid transport and metabolism, F Nucleotide transport and metabolism, G Carbohydrate transport and metabolism, H Coenzyme transport and metabolism, I Lipid transport and metabolism, J Translation, ribosomal structure and biogenesis, K Transcription, L Replication, recombination and repair, M Cell wall/membrane/envelope biogenesis, N Cell motility, O Posttranslational modification, protein turnover, chaperones, P Inorganic ion transport and metabolism, Q Secondary metabolites biosynthesis, transport and catabolism, R General function prediction only, S Function unknown, T Signal transduction mechanisms, U Intracellular trafficking, secretion, and vesicular transport, V Defense mechanisms, W Extracellular structures, X Mobilome: prophages, transposons, Y Nuclear structure, Z Cytoskeleton.*


---