


## 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") #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("~/CompareM2") #DEBUG
panaroo_raw = read_tsv(glob_list$panaroo)
 

panaroo_long = panaroo_raw %>% 
    mutate(shared_by = rowSums(across(where(is.numeric)))) %>%
    pivot_longer(c(everything(), -Gene, -shared_by), names_to = "sample", values_to = "present", cols_vary = "slowest") %>% 
    filter(present >= 1) %>% # Just to be on the safe side.
    
    group_by(Gene) %>% 
    mutate(samples = paste(sample, collapse = ", ")) %>% # This creates a sortable column that identifies the unique combination of samples, so they can be ordered better.
    
    identity()


panaroo_plot_df = metadata_translate %>% 
    select(index, sample) %>% 
    left_join(panaroo_long, by = "sample") %>% 
    
    #arrange(desc(shared_by), desc(index)) %>%  # previous, before using "samples"
    
    arrange(desc(shared_by), desc(samples)) %>%  # Here is where we define the gene order
    mutate(gene_order = 1:n()) %>% 
    identity()

panaroo_plot_df %>%
    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_continuous(type = "viridis", trans = "identity") +
    labs(
        x = "gene",
        y = "sample",
        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.*


---




