Skip to content

Commit

Permalink
removed saving of posterior due to size issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-calonaci committed Apr 5, 2024
1 parent 6cadb3a commit 18b1e70
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
11 changes: 5 additions & 6 deletions R/classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ classify = function(x,
map = map %>% dplyr::select(label, state, value, entropy) %>% dplyr::rename(posterior = value) %>% dplyr::mutate(id = id)

list(
fit = dplyr::right_join(input(x) %>% dplyr::select(colnames(genomic_data(x, PASS = TRUE)), id), map, by = 'id'),
posterior = posterior
fit = dplyr::right_join(input(x) %>% dplyr::select(colnames(genomic_data(x, PASS = TRUE)), id), map, by = 'id')
# posterior = posterior
)
}

Expand All @@ -105,8 +105,6 @@ classify = function(x,
mc.cores = num_cores)

tests = do.call(rbind, tests) %>% t()

colnames(tests) = ids(x)
} else {

tests = sapply(ids(x), function(id) {
Expand All @@ -116,8 +114,9 @@ classify = function(x,
}

output$classification$fit = tests['fit', ] %>% do.call(rbind, .)
output$classification$posterior = tests['posterior', ]
output$classification$parameters = dplyr::tibble(entropy_cutoff = entropy_cutoff, rho = rho)
output$classification$parameters = dplyr::tibble(entropy_cutoff = entropy_cutoff,
rho = rho,
karyotypes = list(karyotypes))

cli::cli_alert_info('There are: ')
for (state in c('HMD', 'LOH', 'CNLOH', 'AM', 'Tier-2')) {
Expand Down
78 changes: 39 additions & 39 deletions R/plot_classification.R
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
#' Visualize classification results for individual observations.
#'
#' @param x An object of class \code{'INCOMMON'} containing the classification results, as
#' @param x An object of class \code{'INCOMMON'} containing the classification results, as
#' produced by function `classify`.
#' @param sample Sample name from the dataset.
#' @param assembly Whether to assemble plots of mutations from the same sample in
#' a multi-faceted plot.
#' a multi-faceted plot.
#' @return An object or a list of objects of class \code{'ggplot2'}.
#' @export
#' @importFrom dplyr filter mutate rename select %>%
#' @importFrom dplyr filter mutate rename select %>%
plot_classification = function(x, sample, assembly = F){

stopifnot(inherits(x,"INCOMMON"))

x = subset_sample(x, sample = sample)

plots = lapply(ids(x), function(id){

# Get posterior of specific classified mutations in easy-to-plot format
toplot = posterior(x, id = id)
toplot = toplot %>%
dplyr::group_by(NV) %>%
toplot = toplot %>%
dplyr::group_by(NV) %>%
dplyr::reframe(max = (value == max(value)), dplyr::across(dplyr::everything()))

toplot$multiplicity = factor(toplot$multiplicity)
toplot$ploidy = factor(toplot$ploidy)

highlight = toplot %>% dplyr::filter(max)
highlight = lapply(split(highlight, highlight$label), function(s){
highlight = lapply(split(highlight, highlight$label), function(s){
if(nrow(s) >= 20){
stride = as.integer(nrow(s)/20)
s = s[seq(1,nrow(s),stride),]
}
s
}) %>% do.call(rbind, .)

# Get scale factor for overlapped visualisation of posterior and entropy
scaleFactor = max(toplot$value)/max(toplot$entropy)
scaleFactor = ifelse(is.infinite(scaleFactor), 1, scaleFactor)
plot = toplot %>%

plot = toplot %>%
ggplot2::ggplot() +
ggplot2::geom_line(ggplot2::aes(x = NV, y = value),
color = 'white',
size = .5,
ggplot2::geom_line(ggplot2::aes(x = NV, y = value),
color = 'white',
size = .5,
alpha = 0)+
ggplot2::geom_line(
ggplot2::aes(x = NV, y = entropy * scaleFactor),
Expand Down Expand Up @@ -78,8 +78,8 @@ plot_classification = function(x, sample, assembly = F){
size = .5
)+
ggplot2::scale_size_manual(values = c("1"=.3,"2"=.3))+
ggplot2::scale_y_continuous("Classification Probability",
sec.axis = ggplot2::sec_axis(~./scaleFactor, name = "Classification Entropy"),
ggplot2::scale_y_continuous("Classification Probability",
sec.axis = ggplot2::sec_axis(~./scaleFactor, name = "Classification Entropy"),
limits = c(0,max(toplot$value)))+
ggplot2::scale_color_manual(values = ploidy_colors)+
CNAqc:::my_ggplot_theme() +
Expand All @@ -93,37 +93,37 @@ plot_classification = function(x, sample, assembly = F){
title = paste0(info(x, id)$gene, ' (', info(x, id)$state, ')'),
subtitle = paste0(x$sample, ' (Purity = ', purity(x, id), '); ', info(x, id)$chr, '; ', info(x, id)$from, '; ', info(x, id)$ref, '>', info(x, id)$alt),
caption = paste0('Entropy cut-off: ', parameters(x)$entropy_cutoff, '; Overdispersion: ', parameters(x)$rho))

})
if(assembly){
toplot = lapply(ids(x), function(id){posterior(x, id = id) %>% dplyr::mutate(gene = gene(x, id))}) %>%

toplot = lapply(ids(x), function(id){posterior(x, id = id) %>% dplyr::mutate(gene = gene(x, id))}) %>%
do.call(rbind, .)
toplot = toplot %>%
dplyr::group_by(gene, NV) %>%
toplot = toplot %>%
dplyr::group_by(gene, NV) %>%
dplyr::reframe(max = (value == max(value)), dplyr::across(dplyr::everything()))

toplot$multiplicity = factor(toplot$multiplicity)
toplot$ploidy = factor(toplot$ploidy)

highlight = toplot %>% dplyr::filter(max)
highlight = lapply(split(highlight, highlight$label), function(s){
highlight = lapply(split(highlight, highlight$label), function(s){
if(nrow(s) >= 20){
stride = as.integer(nrow(s)/20)
s = s[seq(1,nrow(s),stride),]
}
s
}) %>% do.call(rbind, .)

# Get scale factor for overlapped visualisation of posterior and entropy
scaleFactor = max(toplot$value)/max(toplot$entropy)
scaleFactor = ifelse(is.infinite(scaleFactor), 1, scaleFactor)
plot = toplot %>%

plot = toplot %>%
ggplot2::ggplot() +
ggplot2::geom_line(ggplot2::aes(x = NV, y = value),
color = 'white',
size = .5,
ggplot2::geom_line(ggplot2::aes(x = NV, y = value),
color = 'white',
size = .5,
alpha = 0)+
ggplot2::geom_line(
ggplot2::aes(x = NV, y = entropy * scaleFactor),
Expand All @@ -147,22 +147,22 @@ plot_classification = function(x, sample, assembly = F){
),
size = .5) +
ggplot2::geom_vline(
data = lapply(ids(x), function(id) tibble(id = id, gene = gene(x, id), NV = NV(x, id))) %>% do.call(rbind, .),
data = lapply(ids(x), function(id) dplyr::tibble(id = id, gene = gene(x, id), NV = NV(x, id))) %>% do.call(rbind, .),
ggplot2::aes(xintercept = NV),
color = 'black',
linetype = 'dashed',
size = .5
)+
ggplot2::geom_hline(
data = lapply(ids(x), function(id) tibble(id = id, gene = gene(x, id), entropy = entropy(x, id)*scaleFactor)) %>% do.call(rbind, .),
data = lapply(ids(x), function(id) dplyr::tibble(id = id, gene = gene(x, id), entropy = entropy(x, id)*scaleFactor)) %>% do.call(rbind, .),
ggplot2::aes(yintercept = entropy),
color = 'deeppink4',
linetype = 'dashed',
size = .5
)+
ggplot2::scale_size_manual(values = c("1"=.3,"2"=.3))+
ggplot2::scale_y_continuous("Classification Probability",
sec.axis = ggplot2::sec_axis(~./scaleFactor, name = "Classification Entropy"),
ggplot2::scale_y_continuous("Classification Probability",
sec.axis = ggplot2::sec_axis(~./scaleFactor, name = "Classification Entropy"),
limits = c(0,max(toplot$value)))+
ggplot2::scale_color_manual(values = ploidy_colors)+
ggplot2::facet_wrap(~gene) +
Expand All @@ -175,4 +175,4 @@ plot_classification = function(x, sample, assembly = F){
color = ggplot2::guide_legend(title = 'Ploidy', ncol = 2))
return(plot)
} else plots
}
}
17 changes: 14 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,20 @@ parameters = function(x) {
posterior = function(x, id) {
stopifnot(inherits(x, "INCOMMON"))
stopifnot("classification" %in% names(x))
stopifnot("posterior" %in% names(x$classification))
stopifnot(id %in% names(x$classification$posterior))
x$classification$posterior[[id]]
stopifnot("parameters" %in% names(x$classification))
posterior = compute_posterior(
NV = NV(x, id),
DP = DP(x, id),
gene = gene(x, id),
priors = priors,
tumor_type = tumor_type(x, id),
purity = purity(x, id),
entropy_cutoff = parameters(x)$entropy_cutoff,
rho = parameters(x)$rho,
karyotypes = unlist(parameters(x)$karyotypes)
)

return(posterior)
}

idify = function(x){
Expand Down

0 comments on commit 18b1e70

Please sign in to comment.