## KEGG pathway enrichment analysis

`rule kegg_pathway` For each genome the prokka-prodigal called amino-acid sequences are searched in the Uniref100-KO database. This is the same database that CheckM2 uses. For the results produced for this analysis, the alignment criteria are stricter (>=85% coverage and >=50% identity). Using clusterProfilers "enricher" function, Benjamini-Hochberg adjusted p-values for the pathway enrichment for the called genes is computed.

```{r kegg-pathway, echo = F, message = F, warning = F, error = T}

#setwd("~/asscom2/tests/strachan_campylo") # DEBUG

kegg_pathway_results_raw = read_tsv(glob_list$kegg_pathway)




# Clean up table before presenting
kegg_pathway_results = kegg_pathway_results_raw %>% 
    mutate(
        #geneID = str_replace_all(geneID, "/", " "),
        `-log10(p_adjusted)` = -log10(`p.adjust`) %>% signif(5),
        class = a_class %>% str_remove("^[0-9]+") %>% str_trim(), # I'm removing the number codes in front as they're too long and unnecessary.
        group = b_class %>% str_remove("^[0-9]+") %>% str_trim(),
        hierarchy = paste(class, group, sep = "; ") 
    ) %>% 
    select(sample, class, group, hierarchy, pathway, `-log10(p_adjusted)`, `gene ratio` = GeneRatio, KOs = geneID, count = Count)

N_kegg_groups = kegg_pathway_results$group %>% unique() %>% length()


```



```{r kegg_pathway_figure, echo=FALSE, message=F, warning=F, error = T, fig.width = max(2, (N_kegg_groups*0.2)+2.5), fig.height = max(8, (N*0.2)+4)}


# A figure, that for each class, shows how many pathway are enriched for.
#> kegg_pathway_results %>%
#>     #group_by(class) %>%
#>     count(class, group, sample) %>%  # Let's see what happens if we have both class and group
#>     pivot_wider(names_from = sample, values_from = n) %>% 
#>     identity() %>% 
#>     arrange(class, group) %>% 
#>     view()
#> 
#> 
#> kegg_pathway_results %>%
#>     count(sample, hierarchy) %>% 
#>     #pivot_wider(names_from = hierarchy, values_from = n)
#>     pivot_wider(names_from = sample, values_from = n) %>%
#>     view()

metadata_translate %>% 
    select(index, sample) %>%
    left_join(kegg_pathway_results, by = "sample") %>%
    #group_by(class) %>%
    count(index, sample, hierarchy)  %>% 
    
    ggplot(aes(hierarchy, reorder(sample, desc(index)), fill = n)) + 
    scale_fill_viridis_c(breaks = 2^(0:10), trans = "log") +
    theme_classic() + 
    #scale_y_discrete(position = "right") +
    scale_x_discrete(position = "top") +
    geom_tile() + 
    theme(
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0),
        legend.position = "bottom"
        ) +
    labs(
        x = "KEGG pathway groups",
        y = "sample",
        fill = "significant groups"
    )

```

*Fig. `r figno()`: Summary of the KEGG-ortholog based pathway enrichment analysis results. The KEGG pathway hierarchy consists of a number of pathway-classes that are listed on the vertical axis. n denotes the number of pathways from that class, that are significally enriched in each sample.*


```{r kegg-pathway-table, echo = F, message = F, warning = F, error = T}
metadata_translate %>% 
    select(sample) %>% 
    left_join(kegg_pathway_results %>% select(-class, -group, -count), by = "sample") %>% 
    arrange(sample, hierarchy, pathway) %>% 
    custom_dt("kegg_pathway")


```

*Table `r tableno()`: Results from the KEGG-ortholog based pathway enrichment analysis produced with clusterProfiler::enricher. Only significant results are shown. The KOs can be entered directly into [KEGG mapper search](https://www.genome.jp/kegg/mapper/search.html) by setting mode to "Reference".*


---