## `dbcan` - Carbohydrate active enzymes


```{r dbcan, echo = F, message = F, warning = F, error = T}
# Show data table and make a heatmap for substrates to highlight interpretation


#setwd("~/Desktop/test_dbcan") # DEBUG
df_dbcan_raw = read_tsv(glob_list$dbcan, id = "file", na = "-") %>% #, comment = "#", col_names = c("name", "value")) %>%
    
    #identity()

    # Extract sample name
    mutate(sample = str_extract(file, "/samples/(.+)/dbcan/dbcan-sub.hmm.out", group = 1)) %>% 
    relocate(sample) %>% 
    select(-file) %>% 
    
    # Extract coverage (for some reason, the coverage is outputted twice..)
    rename(coverage_raw = Coverage) %>% 
    mutate(Coverage = str_extract(coverage_raw, "^\\d+\\.\\d+") %>% as.numeric()) %>% 
    select(-coverage_raw)



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


df_dbcan %>% 
    select(-index)  %>% 
    custom_dt("dbcan")


```


*Table `r tableno()`: Overview of dbcan cazyme results. Called using [run_dbcan](https://github.com/linnabrown/run_dbcan).*


```{r dbcan-plot-data, echo=FALSE, message=F, warning=F, error = T}
# Let's just focus on the substrates. Adding all those GT and GHs to the table doesn't help easy interpretation.

df_dbcan_plot_data = df_dbcan  %>%
    
    
    
    select(index, sample, `Subfam Composition`, `Subfam EC`, Substrate, Coverage) %>% 
    filter(Coverage >= 0.90)  %>% 
    filter(!is.na(Substrate)) %>% 
    
    rowwise() %>% 
    mutate(
        substrate_split = str_split(Substrate, ", "), 
        substrate_unique = substrate_split %>% unique() %>% sort() %>%  list(),
    ) %>%     #view()
    
    ungroup() %>% 
    unnest(substrate_unique) %>% 
    
    mutate(substrate_final = str_remove(substrate_unique, "and ")) %>% # Some issues with fancy ", and" in the dbcan output.
    
    count(index, sample, substrate_final) 
    
    
n_dbcan_samples = df_dbcan_plot_data$sample %>% unique() %>% length()
n_dbcan_substrates = df_dbcan_plot_data$substrate_final %>% unique() %>% length()

```


```{r dbcan-plot-show, echo=FALSE, message=F, warning=F, error = T, fig.height = max(2.9, (n_dbcan_samples*0.2)+2.3), fig.width = max(5, (n_dbcan_substrates*0.2)+2.8)}
df_dbcan_plot_data %>% 
    ggplot(aes(substrate_final, reorder(sample, desc(index)), fill = n)) + 
    geom_tile() +
    scale_fill_viridis_c(direction = -1) + 
    theme_classic() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) + 
    labs(
        x = "substrate", 
        y = "sample"
    )

```



*Fig. `r figno()`: Overview of carbohydrate active enzymes (Cazymes). The count (color) signifies the number of cazymes in each sample, that can degrade each of the listed substrates. *

---