Skip to content

Commit

Permalink
modify the the plot_cluster_scores function to return ggplot object
Browse files Browse the repository at this point in the history
  • Loading branch information
Azad Sadr committed Jun 28, 2024
1 parent f34aba7 commit 3f2c311
Showing 1 changed file with 35 additions and 92 deletions.
127 changes: 35 additions & 92 deletions R/plot_clustering_QC.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,103 +58,46 @@ plot_cluster_freq = function(x) {

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

# plot the final score for a cluster and single type
# a : scores data-frame
# b : context type (single value)
# c : cluster name
plot_scores_aux1_1 = function(a, b, c) {

# filter on context type and cluster name
df = a %>% subset(type == b & cluster == c)

# find the quantile value
# val = df %>% dplyr::pull(score) %>% quantile(probs = c(0.9))
# df1 =df[c(1,2,6)]

p = ggplot(df, 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="Value") +
theme(axis.text.x=element_text(angle=90)) +
theme_bw() + guides(color = guide_legend(title = "Clustering Scores"))

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) {
# FINAL SCORE
p = ggplot(df, 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") +
theme(axis.text.x=element_text(angle=90)) +
theme_bw() + guides(color = guide_legend(title = "Clustering Scores"))
} else if (!final_score) {
# 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)) +
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)
}


# plot the final score for a cluster and all context types
# a : scores data-frame
# b : context types (multiple)
# c : cluster name
plot_scores_aux1 = function(a, b, c) {

plot_list = lapply(b, function(tid) plot_scores_aux1_1(a, tid, c))

p = patchwork::wrap_plots(
plot_list,
nrow=length(plot_list),
ncol=1,
guides="collect",
tag_level="keep",
design="AAAA\nBBBB"
) & theme(axis.text.x=element_text(angle=90))

return(p)
}


# plot the scores ingredients for single cluster
# a : scores data-frame
# b : single context type
# c : cluster name
plot_scores_aux2_1 = function(a, b, c) {

df = a %>% subset(type == b & cluster == c)

df_long = tidyr::pivot_longer(df[c(1,2,3,4,5)],
cols=c(varRatio, activeRatio, mutRatio),
names_to="name", values_to="value")
p = ggplot(subset(df_long, cluster == c),
aes(x=signature, y=value, color=name, group=name)) +
geom_line() +
geom_point() +
labs(title=paste0("Signatures Score (Ingredients) in Cluster: ", b), x="Mutational Signature", y="Value") +
theme_bw()
return(p)
}


plot_scores_aux2 = function(a, b, c) {

plot_list = lapply(b, function(tid) plot_scores_aux2_1(a, tid, c))

p = patchwork::wrap_plots(
plot_list,
nrow=length(plot_list),
ncol=1,
guides="collect",
tag_level="keep",
design="AAAA\nBBBB",
axes="collect"
)

return(p)
}


plot_cluster_scores = function(x, clusters=get_cluster_labels(x),
types=get_types(x), min_exposure=0.05,
quantile_thr=0.9) {

a = get_clusters_score(x, types, min_exposure, quantile_thr)

lapply(clusters, function(cluster_name) {
p1 = plot_scores_aux1(a=a, b=types, c=cluster_name)
p2 = plot_scores_aux2(a=a, b=types, c=cluster_name)
p3 = plot_exposures(x, types, clusters=cluster_name, min_exposure=min_exposure)

return( (p1 | p2) / p3 )
}) %>% setNames(clusters)
}

0 comments on commit 3f2c311

Please sign in to comment.