Skip to content

Commit

Permalink
remove repetitions, format code, fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
elena-buscaroli committed Jun 30, 2024
1 parent 1554f30 commit 507f436
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions R/plot_clustering_QC.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

# plots heatmap of scores
plot_cls_score_heatmap <- function(x, type, exposure_thr=0.05) {
plot_cls_score_heatmap = function(x, type, exposure_thr=0.05) {

df = get_clusters_score(x, type, exposure_thr) %>% subset(significance == T)
scores = get_clusters_score(x, type, exposure_thr) %>% subset(significance==T)

# df2 = significant_signatures(x, types, threshold)

Expand All @@ -19,84 +18,85 @@ plot_cls_score_heatmap <- function(x, type, exposure_thr=0.05) {
# )
# }

p = ggplot(data=df, aes(x=cluster, y=signature, fill=round(score, 3), label=round(score, 3))) +
p = ggplot(data=scores,
aes(x=cluster, y=signature, fill=round(score, 3), label=round(score, 3))) +
geom_tile(color="white") +
scale_fill_gradient(low="grey", high="dodgerblue1") +
geom_text(color="black", size=3) + # Add text annotations
# scale_fill_gradient(low="white", high="steelblue") + # Choose your desired color gradient
labs(
title="Significant signatures in clusters",
subtitle = "(based on clustering scores)",
title="Significant signatures in clusters",
subtitle = "(based on clustering scores)",
x="Clusters",
y="Signatures",
fill = "Score") +
y="Signatures",
fill = "Score") +
theme_minimal() +
theme(
# remove the vertical grid lines
panel.grid.major.x=element_blank(),
# explicitly set the horizontal lines (or they will disappear too)
panel.grid.major.y=element_line( linewidth=.1, color="black" ),
#legend.position="none"
) +
) +
theme_bw()

return(p)
}

#===============================================================================

# input : basilica object
# output: ggplot (samples frequency in clusters)
plot_cluster_freq = function(x) {
df = get_cluster_assignments(x) # dataframe
p=ggplot(df, aes(x=clusters)) +
geom_bar(stat="count", fill="purple") +
labs(title="Distribution of samples in clusters", x="Clusters", y="Number of Samples") +
theme_bw()
return(p)
}
## SAME AS plot_mixture_weights(x, empirical=TRUE)
# # input : basilica object
# # output: ggplot (samples frequency in clusters)
# plot_cluster_freq = function(x) {
# df = get_cluster_assignments(x) # dataframe
# ggplot(df, aes(x=clusters)) +
# geom_bar(stat="count", fill="purple") +
# labs(title="Distribution of samples in clusters", x="Clusters", y="Number of Samples") +
# theme_bw()
# }

#===============================================================================

plot_cluster_scores = function(x, type, cluster_label, final_score=TRUE) {

plot_cluster_scores <- function(x, context_type, cluster_label, final_score=TRUE) {

if ( !(cluster_label %in% get_cluster_labels(x)) ) {
warning("invalid cluster label!")
}

scores <- get_clusters_score(x, types = context_type, exposure_thr = 0.05, quantile_thr = 0.9)
df = scores %>% subset(type == context_type & cluster == cluster_label)

if (final_score) {
if (!(cluster_label %in% get_cluster_labels(x))) warning("invalid cluster label!")

scores = get_clusters_score(x, types=type, exposure_thr=0.05, quantile_thr=0.9) %>%
subset(type==type & cluster==cluster_label)

if (final_score==TRUE) {
# FINAL SCORE
p = ggplot(df, aes(x=signature, y=score, color=cluster, group=cluster)) +
p = ggplot(scores, aes(x=signature, y=score, color=cluster, group=cluster)) +
geom_line() +
geom_point() +
geom_hline(yintercept=df$score_quantile, linetype="dashed", color="black") +
labs(title=paste0("Signatures Score (Final) in Cluster: ", b), x="Mutational Signature", y="Score") +
geom_hline(aes(yintercept=score_quantile), linetype="dashed", color="black") +
labs(title=paste0("Signatures scores (final) in cluster ", cluster_label),
x="Signature", y="Score") +
theme(axis.text.x=element_text(angle=90)) +
theme_bw() + guides(color = guide_legend(title = "Clustering Scores"))
} else if (!final_score) {
theme_bw() +
guides(color=guide_legend(title="Cluster"))

return(p)

} else if (final_score==FALSE) {
# PARTIAL SCORES
df_long = tidyr::pivot_longer(df[c(1,2,3,4,5)],
cols=c(varRatio, activeRatio, mutRatio),
names_to="score_title", values_to="value")
p <- ggplot(subset(df_long, cluster == cluster_label),
aes(x=signature, y=value, color=score_title, group=score_title)) +
scores_long = scores %>%
dplyr::select(signature, cluster, varRatio, activeRatio, mutRatio) %>%
tidyr::pivot_longer(cols=c(varRatio, activeRatio, mutRatio),
names_to="score_title", values_to="value")
p = ggplot(subset(scores_long, cluster==cluster_label),
aes(x=signature, y=value, color=score_title, group=score_title)) +
geom_line() +
geom_point() +
labs(
title=paste0("Signatures Score (Ingredients) in Cluster: ", cluster_label),
x="Mutational Signature",
y="Value"
) +
theme_bw()
} else {warning("invalid final_score!")}

return(p)
}
labs(title=paste0("Signatures scores (partial) in cluster ", cluster_label),
x="Signature", y="Score") +
theme_bw() +
guides(color=guide_legend(title="Score name"))

return(p)

} else {cli::cli_abort("Argument `final_score` can be either {TRUE} or {FALSE}")}

}



Expand Down

0 comments on commit 507f436

Please sign in to comment.