3 Tumors

3.1 TN1

## Constructing UMAP embedding.
## Loading required package: SingleCellExperiment
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  32 0.02909091
##      c10  24 0.02181818
##      c11  45 0.04090909
##      c12  19 0.01727273
##      c13  79 0.07181818
##      c14  23 0.02090909
##      c15  51 0.04636364
##      c16  23 0.02090909
##      c17 149 0.13545455
##       c2  56 0.05090909
##       c3 134 0.12181818
##       c4  66 0.06000000
##       c5  40 0.03636364
##       c6  39 0.03545455
##       c7  74 0.06727273
##       c8 228 0.20727273
##       c9  18 0.01636364
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns
## Warning: The input is a data frame, convert it to the matrix.

## Warning: Setting row names on a tibble is deprecated.
## 'select()' returned 1:1 mapping between keys and columns
## Warning: The input is a data frame, convert it to the matrix.

3.2 TN2

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  23 0.02246094
##      c10 147 0.14355469
##      c11  31 0.03027344
##      c12 140 0.13671875
##      c13  43 0.04199219
##      c14  34 0.03320312
##      c15 156 0.15234375
##       c2  22 0.02148438
##       c3  55 0.05371094
##       c4  51 0.04980469
##       c5  44 0.04296875
##       c6  44 0.04296875
##       c7  28 0.02734375
##       c8 170 0.16601562
##       c9  36 0.03515625
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns
## Warning: The input is a data frame, convert it to the matrix.

## Warning: Setting row names on a tibble is deprecated.
## 'select()' returned 1:1 mapping between keys and columns
## Warning: The input is a data frame, convert it to the matrix.

3.3 TN3

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  67 0.06085377
##       c2  71 0.06448683
##       c3  63 0.05722071
##       c4 450 0.40871935
##       c5  33 0.02997275
##       c6  32 0.02906449
##       c7  77 0.06993642
##       c8 106 0.09627611
##       c9 202 0.18346957
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns
## Warning: The input is a data frame, convert it to the matrix.

## Warning: Setting row names on a tibble is deprecated.
## 'select()' returned 1:1 mapping between keys and columns
## Warning: The input is a data frame, convert it to the matrix.

3.4 TN4

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1 255 0.19510329
##      c10  29 0.02218822
##      c11  42 0.03213466
##      c12  46 0.03519510
##      c13  32 0.02448355
##      c14  45 0.03442999
##      c15 220 0.16832441
##      c16  29 0.02218822
##      c17  70 0.05355777
##      c18  37 0.02830910
##      c19  27 0.02065800
##       c2  31 0.02371844
##      c20  36 0.02754399
##      c21  66 0.05049732
##      c22  26 0.01989288
##       c3  31 0.02371844
##       c4  56 0.04284621
##       c5  29 0.02218822
##       c6  66 0.05049732
##       c7  54 0.04131599
##       c8  48 0.03672533
##       c9  32 0.02448355
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns

## 'select()' returned 1:1 mapping between keys and columns

3.5 TN5

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  52 0.04200323
##       c2  53 0.04281099
##       c3 573 0.46284330
##       c4  27 0.02180937
##       c5 389 0.31421648
##       c6  26 0.02100162
##       c7 118 0.09531502
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns

## 'select()' returned 1:1 mapping between keys and columns

3.6 TN6

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  31 0.02249637
##      c10 211 0.15312046
##      c11  31 0.02249637
##      c12  82 0.05950653
##      c13 111 0.08055152
##      c14  37 0.02685051
##      c15  37 0.02685051
##      c16 197 0.14296081
##       c2  25 0.01814224
##       c3  37 0.02685051
##       c4  33 0.02394775
##       c5  42 0.03047896
##       c6  51 0.03701016
##       c7 393 0.28519594
##       c8  18 0.01306241
##       c9  42 0.03047896
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns

## 'select()' returned 1:1 mapping between keys and columns

3.7 TN7

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  17 0.01220388
##      c10  59 0.04235463
##      c11 110 0.07896626
##      c12  63 0.04522613
##      c13 152 0.10911701
##      c14 365 0.26202441
##      c15  30 0.02153625
##      c16  37 0.02656138
##      c17  40 0.02871500
##      c18  53 0.03804738
##       c2 114 0.08183776
##       c3  51 0.03661163
##       c4  45 0.03230438
##       c5  40 0.02871500
##       c6  55 0.03948313
##       c7  56 0.04020101
##       c8  80 0.05743001
##       c9  26 0.01866475
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns

## 'select()' returned 1:1 mapping between keys and columns

3.8 TN8

## Constructing UMAP embedding.
## Building SNN graph.
## Running hdbscan.
##  cluster   n    percent
##       c1  38 0.03104575
##      c10  29 0.02369281
##      c11  52 0.04248366
##      c12  45 0.03676471
##      c13  43 0.03513072
##      c14  44 0.03594771
##      c15 263 0.21486928
##       c2  54 0.04411765
##       c3  96 0.07843137
##       c4  55 0.04493464
##       c5  88 0.07189542
##       c6  22 0.01797386
##       c7  35 0.02859477
##       c8 325 0.26552288
##       c9  35 0.02859477
## Done.
## Joining, by = "cells"

## 'select()' returned 1:1 mapping between keys and columns

## 'select()' returned 1:1 mapping between keys and columns

3.10 Shannon diversity

shan <- function(data, indices) {
  data_ind <- data[indices]
  prop <- janitor::tabyl(data_ind) %>% pull(percent) 
  div <- -sum(prop*log(prop))
  return(div)
}

TN1_proportions <- janitor::tabyl(TN1_clustering$subclones) %>% pull(percent)
TN2_proportions <- janitor::tabyl(TN2_clustering$subclones) %>% pull(percent)
TN3_proportions <- janitor::tabyl(TN3_clustering$subclones) %>% pull(percent)
TN4_proportions <- janitor::tabyl(TN4_clustering$subclones) %>% pull(percent)
TN5_proportions <- janitor::tabyl(TN5_clustering$subclones) %>% pull(percent)
TN6_proportions <- janitor::tabyl(TN6_clustering$subclones) %>% pull(percent)
TN7_proportions <- janitor::tabyl(TN7_clustering$subclones) %>% pull(percent)
TN8_proportions <- janitor::tabyl(TN8_clustering$subclones) %>% pull(percent)

TN1_diver <- -sum(TN1_proportions*log(TN1_proportions))
TN2_diver <- -sum(TN2_proportions*log(TN2_proportions))
TN3_diver <- -sum(TN3_proportions*log(TN3_proportions))
TN4_diver <- -sum(TN4_proportions*log(TN4_proportions))
TN5_diver <- -sum(TN5_proportions*log(TN5_proportions))
TN6_diver <- -sum(TN6_proportions*log(TN6_proportions))
TN7_diver <- -sum(TN7_proportions*log(TN7_proportions))
TN8_diver <- -sum(TN8_proportions*log(TN8_proportions))

boot_TN1 <- boot::boot(TN1_clustering$subclones, statistic = shan,  R = 3000)
boot_TN1_ci <- boot::boot.ci(boot_TN1)

boot_TN2 <- boot::boot(TN2_clustering$subclones, statistic = shan,  R = 3000)
boot_TN2_ci <- boot::boot.ci(boot_TN2)

boot_TN3 <- boot::boot(TN3_clustering$subclones, statistic = shan,  R = 3000)
boot_TN3_ci <- boot::boot.ci(boot_TN3)

boot_TN4 <- boot::boot(TN4_clustering$subclones, statistic = shan,  R = 3000)
boot_TN4_ci <- boot::boot.ci(boot_TN4)

boot_TN5 <- boot::boot(TN5_clustering$subclones, statistic = shan,  R = 3000)
boot_TN5_ci <- boot::boot.ci(boot_TN5)

boot_TN6 <- boot::boot(TN6_clustering$subclones, statistic = shan,  R = 3000)
boot_TN6_ci <- boot::boot.ci(boot_TN6)

boot_TN7 <- boot::boot(TN7_clustering$subclones, statistic = shan,  R = 3000)
boot_TN7_ci <- boot::boot.ci(boot_TN7)

boot_TN8 <- boot::boot(TN8_clustering$subclones, statistic = shan,  R = 3000)
boot_TN8_ci <- boot::boot.ci(boot_TN8)


div_table <- tibble(
  sample = c("TN1",
             "TN2",
             "TN3",
             "TN4",
             "TN5",
             "TN6",
             "TN7",
             "TN8"),
  shannon_index = c(
    TN1_diver,
    TN2_diver,
    TN3_diver,
    TN4_diver,
    TN5_diver,
    TN6_diver,
    TN7_diver,
    TN8_diver
  ),
  lci = c(
    boot_TN1_ci$normal[2],
    boot_TN2_ci$normal[2],
    boot_TN3_ci$normal[2],
    boot_TN4_ci$normal[2],
    boot_TN5_ci$normal[2],
    boot_TN6_ci$normal[2],
    boot_TN7_ci$normal[2],
    boot_TN8_ci$normal[2]
  ),
  uci = c(
    boot_TN1_ci$normal[3],
    boot_TN2_ci$normal[3],
    boot_TN3_ci$normal[3],
    boot_TN4_ci$normal[3],
    boot_TN5_ci$normal[3],
    boot_TN6_ci$normal[3],
    boot_TN7_ci$normal[3],
    boot_TN8_ci$normal[3]
  )
)

# error bar = 95% confidence interval
p_tumors_diver <- div_table %>%
  ggplot() +
  geom_errorbar(aes(
    x = fct_reorder(sample, shannon_index),
    ymin = lci,
    ymax = uci
  ),
  width = .1,
  size = 2) +
  geom_point(aes(
    x = fct_reorder(sample, shannon_index),
    y = shannon_index,
    color = sample
  ), size = 4) +
  theme_classic() +
  my_theme +
  theme(legend.position = "none",
        axis.text.x = element_text(
          angle = 90,
          vjust = 0.5,
          hjust = 1
        )) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 12)) +
  scale_color_manual(values = c(rcartocolor::carto_pal(8, "Safe"))) +
  ylab("shannon diversity index") +
  xlab("")

p_tumors_diver

3.11 Gains/Losses plot

TN1_alt_perc <-
  gain_loss_percentage(consensus = TN1_consensus,
                       ploidy_VAL = TN1_ploidy,
                       sample = "TN1")

TN2_alt_perc <-
  gain_loss_percentage(consensus = TN2_consensus,
                       ploidy = TN2_ploidy,
                       sample = "TN2")

TN3_alt_perc <-
  gain_loss_percentage(consensus = TN3_consensus,
                       ploidy = TN3_ploidy,
                       sample = "TN3")

TN4_alt_perc <-
  gain_loss_percentage(consensus = TN4_consensus,
                       ploidy = TN4_ploidy,
                       sample = "TN4")

TN5_alt_perc <-
  gain_loss_percentage(consensus = TN5_consensus,
                       ploidy = TN5_ploidy,
                       sample = "TN5")

TN6_alt_perc <-
  gain_loss_percentage(consensus = TN6_consensus,
                       ploidy = TN6_ploidy,
                       sample = "TN6")

TN7_alt_perc <-
  gain_loss_percentage(consensus = TN7_consensus,
                       ploidy = TN7_ploidy,
                       sample = "TN7")

TN8_alt_perc <-
  gain_loss_percentage(consensus = TN8_consensus,
                       ploidy = TN8_ploidy,
                       sample = "TN8")


all_tumors_gainloss_percentage <- 
  bind_rows(TN2_alt_perc,
            TN3_alt_perc,
            TN4_alt_perc,
            TN1_alt_perc,
            TN5_alt_perc,
            TN6_alt_perc,
            TN7_alt_perc,
            TN8_alt_perc
            ) %>% 
  dplyr::rename(percent = n)

p_gain_loss <- ggplot() +
  geom_col(data = all_tumors_gainloss_percentage, 
           aes(x = fct_relevel(clone, rev(unique(gtools::mixedsort(all_tumors_gainloss_percentage$clone)))),
               y = percent,
               fill = class
           ),
           color = "black") +
  facet_wrap(vars(sample), ncol = 4, scales = "free_y") +
  scale_fill_manual(values = c("ground_state" = "white",
                               "gain" = "firebrick3",
                               "loss" = "steelblue"),
                    label = c("loss",
                              "neutral",
                              "gain"),
                    breaks = c("loss",
                               "ground_state",
                               "gain")) +
  theme_cowplot() +
    theme(strip.background = element_rect(fill = "white"),
        legend.title = element_blank(),
        legend.position = "top",
        axis.text = element_text(size = 10)) +
  coord_flip() +
  xlab("") 
  # ggtitle("Percentage of gain_loss per subclone")

p_gain_loss

3.12 Genomic classes plots

3.12.1 Counts