## Cazymes from dbCAN

`rule dbcan`

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


#setwd("~/assemblycomparator2/tests/student");  # DEBUG
df_dbcan_raw = read_tsv(glob_list$dbcan, id = "file", na = "-") %>% #, comment = "#", col_names = c("name", "value")) %>%
    
    # Extract sample name
    mutate(sample = str_extract(file, "results_ac2/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}


# Now let's make a parsed version that collapeses per subfamily (without underscore id) and collapses redundant substrates. This table can be summarized and counted per sample with coverage above a threshold (i.e. 90%).


df_dbcan_plot_data = df_dbcan %>% 

    select(index, sample, substrate = Substrate, coverage = Coverage, dbcan_subfam = `dbCAN subfam`) %>% 
    #drop_na(substrate) %>% # Will I get too many then?
    
    # Some cazymes have substrate "xylan, xylan" and "xylan, xylan, xylan" which doesn't make any sense. Let's collapse unique substrates per cazyme.
    
    rowwise() %>% 
    mutate(
        substrate_split = str_split(substrate, ", "), 
        substrate_unique = substrate_split %>% unique() %>% sort() %>%  list(),
        substrate_unique_paste = paste(substrate_unique, collapse = ", ")
    ) %>%
    
    mutate(dbcan_subfam_short = str_extract(dbcan_subfam, "^[^_]+")) %>% 
    
    # Put unique substrates and subfam together
    mutate(
        pretty = paste0(substrate_unique_paste, " (", dbcan_subfam_short, ")") 
    ) 
    


df_dbcan_plot_data_summarized = df_dbcan_plot_data %>% 

    
    group_by(index, sample, pretty) %>% 
    filter(coverage >= .9) %>% 
    summarize(count = n())

n_dbcan_samples = df_dbcan_plot_data$sample %>% unique() %>% length()
n_dbcan_substrates = df_dbcan_plot_data$pretty %>% unique() %>% length()




```




```{r dbcan-plot-show, echo=FALSE, message=F, warning=F, error = T, fig.height = max(1.8, (n_dbcan_samples*0.2)+1.3), fig.width = max(1.8, (n_dbcan_substrates*0.2)+2.3)}

df_dbcan_plot_data_summarized %>% 
    ggplot(aes(pretty, reorder(sample, desc(index)), fill = count)) + 
    geom_tile() +
    scale_fill_viridis_c() + 
    theme_classic() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) + 
    labs(
        x = "substrate (dbCAN subfamily)", 
        y = "sample"
    )

# ggsave("dbcan.png", height = 10, width = 16) # DEBUG

```

*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. *

---