


## Pan and Core genome

`rule panaroo` [Panaroo](https://gtonkinhill.github.io/panaroo/#/) the pan genome pipeline computes the number of orthologous genes in a number of core/pan spectrum partitions.

The core genome denotes the genes which are conserved between all samples (intersection), whereas the pan genome is the union of all genes across all samples.

```{r panaroo-table, echo=FALSE, message=F, warning=F, error=T}

#   setwd("~/comparem2/tests/strachan_campylo") # DEBUG
#setwd("~/evo/comparem2/tests/MAGs") #DEBUG



# Should exist in the case that the glob_list$panaroo = ...gene_presence_absence.Rtab file exists.
panaroo_summary_file = paste0(output_directory, "/panaroo/summary_statistics.txt")




read_tsv(panaroo_summary_file, col_names = c("partition", "definition", "count")) %>% 

    custom_dt("panaroo", dom = "t", ordering = F, width = 500)



```

*Table `r tableno()`: Distribution of genes in different core/pan spectrum partitions.*

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

#setwd("~/evo/comparem2/tests/MAGs") # DEBUG

panaroo_long = read_tsv(glob_list$panaroo) %>% 
    mutate(shared_by = rowSums(across(where(is.numeric)))) %>%
    pivot_longer(c(everything(), -Gene, -shared_by), names_to = "sample", values_to = "present") %>% 
    filter(present >= 1) 


panaroo_plot_df = metadata_translate %>% 
    select(index, sample) %>% 
    left_join(panaroo_long, by = "sample") %>% 
    
    arrange(desc(shared_by), desc(index)) %>% 
    mutate(gene_order = 1:n()) %>% 
    mutate(gene_num = Gene %>% factor() %>% as.integer()) %>% 
    
    # Create exponential bins
    #mutate(what = 2^(shared_by %>% log2() %>% round())) %>% # Wait, isn't it easier to just color in a logarithmic fashion?
    identity()



# panaroo_plot_df = read_tsv(glob_list$panaroo) %>% 
#     mutate(shared_by = rowSums(across(where(is.numeric)))) %>%
#     pivot_longer(c(everything(), -Gene, -shared_by), names_to = "sample", values_to = "present") %>% 
#     filter(present >= 1)  %>%
# 
#     # Two sorting options:
#     arrange(desc(shared_by), desc(sample)) %>% 
#     #arrange(sample, shared_by) %>% 
# 
#     mutate(gene_order = 1:n()) %>% 
#     mutate(gene_num = Gene %>% factor() %>% as.integer()) %>% 
#     identity()





panaroo_plot_df %>%
    #ggplot(aes(reorder(Gene, gene_order), reorder(sample, desc(index)), fill = factor(shared_by))) +
    ggplot(aes(reorder(Gene, gene_order), reorder(sample, desc(index)), 
        fill =  shared_by
    )) +
    
    geom_tile() +

    theme_classic() +
    theme(
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
    ) +
    scale_fill_binned(
        trans = "log",
        #breaks = 2^(0:11), # Getting error: (## Error in check.length("fill"): 'gpar' element 'fill' must not be length 0) when there is no shared genes and using this breaks difinition. Must disable it.
        type = "viridis",
        #direction = -1
    ) +
    labs(
        x = "gene",
        y = "sample",
        #fill = "number of\nsamples", 
        fill = "shared by\nsamples", 
        
    )

```

*Fig. `r figno()`: Genes shared between samples. Each vertical line represents a gene, and all lines have the same width regardless of the size of the gene. The genes are colored by the number of samples sharing them.*


---



