various usability tweaks to LogoPlotMSA and position_annotation

This commit is contained in:
Tanushree Tunstall 2022-09-05 16:05:45 +01:00
parent f949592dd8
commit 1241ad0b22
4 changed files with 197 additions and 130 deletions

View file

@ -124,7 +124,7 @@ install.packages("data.table")
if (!require("PerformanceAnalytics")){
install.packages("PerformanceAnalytics", dependencies = T)
library(PerformaceAnalytics)
library(PerformanceAnalytics)
}
if (!require ("GGally")){

View file

@ -134,7 +134,7 @@ if(!require("data.table")) {
if (!require("PerformanceAnalytics")){
install.packages("PerformanceAnalytics", dependencies = T)
library(PerformaceAnalytics)
library(PerformanceAnalytics)
}
if (!require ("GGally")){

View file

@ -40,35 +40,40 @@
###########################################
#LogoPlotMSA <- function(msaSeq_mut # chr vector
# , msaSeq_wt # chr vector
LogoPlotMSA <- function(unified_msa
, logo_type = c("EDLogo") #"bits_pfm", "probability_pfm", "bits_raw", "probability_raw")
, EDScore_type = c("log") # see if this relevant, or source function should have it!
, bg_prob = NULL
, my_logo_col = "chemistry"
, plot_positions
, y_breaks
, x_lab_mut = "nsSNP position"
, y_lab_mut
, x_ats = 13 # text size
, x_tangle = 90 # text angle
, x_axis_offset = 0 # dist b/w y-axis and plot start
, x_axis_offset_filtered = 0
, y_axis_offset = 0
, y_axis_increment = 1
, y_ats = 13
, y_tangle = 0
, x_tts = 13 # title size
, y_tts = 13
, leg_pos = "top" # can be top, left, right and bottom or c(0.8, 0.9)
, leg_dir = "horizontal" #can be vertical or horizontal
, leg_ts = 16 # leg text size
, leg_tts = 16 # leg title size
LogoPlotMSA <- function(# unified_msa # <- not needed any more because we have 'target' now
target = 'embb'
, logo_type = c("EDLogo") #"bits_pfm", "probability_pfm", "bits_raw", "probability_raw")
, EDScore_type = c("log") # see if this relevant, or source function should have it!
, bg_prob = NULL
, my_logo_col = "chemistry"
, plot_positions
, y_breaks
, x_lab_mut = ""
, y_lab_mut
, x_ats = 10 # text size
, x_tangle = 90 # text angle
, x_axis_offset = 0 # dist b/w y-axis and plot start
, x_axis_offset_filtered = 0
, y_axis_offset = 0
, y_axis_increment = 1
, y_ats = 10
, y_tangle = 0
, x_tts = 10 # title size
, y_tts = 10
, leg_pos = "top" # can be top, left, right and bottom or c(0.8, 0.9)
, leg_dir = "horizontal" #can be vertical or horizontal
, leg_ts = 14 # leg text size
, leg_tts = 14 # leg title size
, ...
)
{
# FIXME: Hack!
# msaSeq_mut=unified_msa[[1]]
# msaSeq_wt=unified_msa[[2]]
unified_msa = get(paste0(target, "_unified_msa"))
msaSeq_mut=unified_msa[['msa_seq']]
msaSeq_wt=unified_msa[['wt_seq']]
@ -79,6 +84,51 @@ LogoPlotMSA <- function(unified_msa
names(data_ed)
#"pfm_mutM" "pfm_mut_scaledM" "combED_mutM" "pfm_wtM" "pfm_wt_scaledM" "combED_wtM"
#merged_df3 for current target (unfortunatly i can't think of an easy way to get this from unified_msa)
contig_df=data.frame(position=1:max(nchar(unified_msa$wt_seq)))
plot_df = get(paste0(target, "_merged_df3"))
# generate the tile columns
#plot_df=cbind(embb_merged_df3)
plot_df$col_aa = ifelse(plot_df[["position"]]%in%active_aa_pos,
"transparent", "transparent")
plot_df$bg_all = plot_df$col_aa
plot_df$bg_all = ifelse(plot_df[["position"]]%in%aa_pos_drug,
"drug", plot_df$bg_all)
plot_df$col_bg1 = plot_df$bg_all
plot_df$col_bg1 = ifelse(plot_df[["position"]]%in%aa_pos_lig1,
"lig1", plot_df$col_bg1)
plot_df$col_bg2 = plot_df$col_bg1
plot_df$col_bg2 = ifelse(plot_df[["position"]]%in%aa_pos_lig2,
"lig2", plot_df$col_bg2)
plot_df$col_bg3 = plot_df$col_bg2
plot_df$col_bg3 = ifelse(plot_df[["position"]]%in%aa_pos_lig3
, "lig3", plot_df$col_bg3)
plot_df = generate_distance_colour_map(plot_df, debug=FALSE)
# copy only the tile columns to the contiguous DF
contig_df$ligand_distance = plot_df$ligand_distance[match(contig_df$position, plot_df$position)]
contig_df_map = generate_distance_colour_map(contig_df, debug=TRUE)
contig_df$ligD_colours = contig_df_map$ligD_colours[match(contig_df$position, contig_df_map$position)]
#contig_df$ligD_colours = plot_df$ligD_colours[match(contig_df$position, plot_df$position)]
contig_df$bg_all = plot_df$bg_all[match(contig_df$position, plot_df$position)]
contig_df$col_bg1 = plot_df$col_bg1[match(contig_df$position, plot_df$position)]
contig_df$col_bg2 = plot_df$col_bg2[match(contig_df$position, plot_df$position)]
contig_df$col_bg3 = plot_df$col_bg3[match(contig_df$position, plot_df$position)]
contig_df=replace_na(
contig_df,
list(
ligD_colours='transparent',
bg_all = 'transparent',
col_bg1 = 'transparent',
col_bg2 = 'transparent',
col_bg3 = 'transparent'
)
)
if (logo_type == "EDLogo"){
msa_method = "custom"
y_label = "Enrichment Score"
@ -416,13 +466,13 @@ LogoPlotMSA <- function(unified_msa
) +
scale_y_discrete(expand = c(0,0)) +
ylab("") + xlab("Wild-type position")
ylab("") + xlab("")
if (missing(plot_positions)){
# No y-axis needed
ed_wt_logo_P = p1# +
}else{
} else {
ed_wt_logo_P = p1 +
scale_x_discrete(expand = c(0, 0),
@ -430,13 +480,27 @@ LogoPlotMSA <- function(unified_msa
#labels = i_extract,
limits = factor(i_extract)
)
#plot_df=plot_df[plot_df$position %in% plot_positions,]
contig_df=contig_df[contig_df$position %in% plot_positions,]
anno_bar = position_annotation(
contig_df,
aa_pos_drug=aa_pos_drug,
active_aa_pos=active_aa_pos,
aa_pos_lig1=aa_pos_lig1,
aa_pos_lig2=aa_pos_lig2,
aa_pos_lig3=aa_pos_lig3,
generate_colours = FALSE
)
}
cowplot::plot_grid(ed_mut_logo_P
, ed_wt_logo_P
, nrow = 2
, anno_bar
, ncol = 1
, align = "v"
#, axis='lr'
, rel_heights = c(3/4, 1/4))
, rel_heights = c(3/4, 1/4,1/10))
}
#LogoPlotMSA(unified_msa)

View file

@ -4,6 +4,7 @@
position_annotation=function(plot_df,
bg="transparent",
reorder_position = FALSE, # enable to reorder according to plot_df$pos_count
generate_colours = TRUE, #set FALSE if you want to generate all the colour columns elsewhere
aa_pos_drug=1:100,
active_aa_pos=1:100,
aa_pos_lig1=1:100,
@ -32,36 +33,38 @@ position_annotation=function(plot_df,
#plot_df = generate_distance_colour_map(plot_df, debug=TRUE)
# plot_df$col_aa = ifelse(plot_df[["position"]]%in%active_aa_pos,
# "brown", "transparent")
plot_df$col_aa = ifelse(plot_df[["position"]]%in%active_aa_pos,
"transparent", "transparent")
plot_df$bg_all = plot_df$col_aa
plot_df$bg_all = ifelse(plot_df[["position"]]%in%aa_pos_drug,
"drug", plot_df$bg_all)
if (generate_colours){
plot_df$col_aa = ifelse(plot_df[["position"]]%in%active_aa_pos,
"transparent", "transparent")
plot_df$col_bg1 = plot_df$bg_all
plot_df$col_bg1 = ifelse(plot_df[["position"]]%in%aa_pos_lig1,
"lig1", plot_df$col_bg1)
plot_df$bg_all = plot_df$col_aa
plot_df$bg_all = ifelse(plot_df[["position"]]%in%aa_pos_drug,
"drug", plot_df$bg_all)
plot_df$col_bg2 = plot_df$col_bg1
plot_df$col_bg2 = ifelse(plot_df[["position"]]%in%aa_pos_lig2,
"lig2", plot_df$col_bg2)
plot_df$col_bg1 = plot_df$bg_all
plot_df$col_bg1 = ifelse(plot_df[["position"]]%in%aa_pos_lig1,
"lig1", plot_df$col_bg1)
plot_df$col_bg2 = plot_df$col_bg1
plot_df$col_bg2 = ifelse(plot_df[["position"]]%in%aa_pos_lig2,
"lig2", plot_df$col_bg2)
plot_df$col_bg3 = plot_df$col_bg2
plot_df$col_bg3 = ifelse(plot_df[["position"]]%in%aa_pos_lig3
, "lig3", plot_df$col_bg3)
# the call to generate_distance_colour_map should probably be
# wherever the outer DF is built, and not here.
plot_df = generate_distance_colour_map(plot_df, debug=TRUE)
plot_df$col_bg3 = plot_df$col_bg2
plot_df$col_bg3 = ifelse(plot_df[["position"]]%in%aa_pos_lig3
, "lig3", plot_df$col_bg3)
# the call to generate_distance_colour_map should probably be
# wherever the outer DF is built, and not here.
plot_df = generate_distance_colour_map(plot_df, debug=TRUE)
}
heat_bar = ggplot(plot_df) + # THIS STUPID FUCKING FACTOR THING
# scale_x_discrete("Position", labels=factor(plot_df$position)) +
theme_nothing() +
theme(#axis.text.x = element_text(angle = 90, size = 6),
title = element_blank()
title = element_blank()
) + # enable for alignment debug
labs(x = NULL, y = NULL) +
@ -77,87 +80,87 @@ position_annotation=function(plot_df,
}
#end of distance-heat-bar
#NULL,
if(reorder_position) {
pos_tiles = ggplot(plot_df) +
#scale_x_discrete("Position", labels=factor(plot_df$position)) +
scale_color_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
scale_fill_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
theme_nothing() +
theme(plot.background = element_rect(fill = bg, colour=NA),
#plot.margin = margin(t=0,b=0),
panel.background = element_rect(fill = bg, colour=NA),
legend.position = "none"
) +
labs(x = NULL, y= NULL) +
if(reorder_position) {
pos_tiles = ggplot(plot_df) +
#scale_x_discrete("Position", labels=factor(plot_df$position)) +
scale_color_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
scale_fill_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
theme_nothing() +
theme(plot.background = element_rect(fill = bg, colour=NA),
#plot.margin = margin(t=0,b=0),
panel.background = element_rect(fill = bg, colour=NA),
legend.position = "none"
) +
labs(x = NULL, y= NULL) +
geom_tile(aes(y = 1,x=reorder(position,-pos_count), fill = bg_all, colour = bg_all)
) +
geom_tile(aes(y = 2, x=reorder(position,-pos_count), fill = col_bg1, colour = col_bg1)
) +
geom_tile(aes(y = 3, x=reorder(position,-pos_count), fill = col_bg2, colour = col_bg2)
) +
geom_tile(aes(y = 4, x=reorder(position,-pos_count), fill = col_bg3, colour = col_bg3)
)
geom_tile(aes(y = 2, x=reorder(position,-pos_count), fill = col_bg1, colour = col_bg1)
) +
geom_tile(aes(y = 3, x=reorder(position,-pos_count), fill = col_bg2, colour = col_bg2)
) +
geom_tile(aes(y = 4, x=reorder(position,-pos_count), fill = col_bg3, colour = col_bg3)
)
} else {
pos_tiles = ggplot(plot_df) +
#scale_x_discrete("Position", labels=factor(plot_df$position)) +
scale_color_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
scale_fill_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
theme_nothing() +
theme(plot.background = element_rect(fill = bg, colour=NA),
#plot.margin = margin(t=0,b=0),
panel.background = element_rect(fill = bg, colour=NA),
legend.position = "none"
) +
labs(x = NULL, y= NULL) +
} else {
pos_tiles = ggplot(plot_df) +
#scale_x_discrete("Position", labels=factor(plot_df$position)) +
scale_color_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
scale_fill_manual(values = c(
"brown"="brown",
"drug"=drug_colour,
"transparent"="transparent",
"lig1"=lig1_colour,
"lig2"=lig2_colour,
"lig3"=lig3_colour
),
#expand=c(0,0)
) +
theme_nothing() +
theme(plot.background = element_rect(fill = bg, colour=NA),
#plot.margin = margin(t=0,b=0),
panel.background = element_rect(fill = bg, colour=NA),
legend.position = "none"
) +
labs(x = NULL, y= NULL) +
geom_tile(aes(y = 1, x=factor(position), fill = bg_all, colour = bg_all)
) +
geom_tile(aes(y = 2, x=factor(position), fill = col_bg1, colour = col_bg1)
) +
geom_tile(aes(y = 3, x=factor(position), fill = col_bg2, colour = col_bg2)
) +
geom_tile(aes(y = 4, x=factor(position), fill = col_bg3, colour = col_bg3)
)
}
# tile thingies end
geom_tile(aes(y = 2, x=factor(position), fill = col_bg1, colour = col_bg1)
) +
geom_tile(aes(y = 3, x=factor(position), fill = col_bg2, colour = col_bg2)
) +
geom_tile(aes(y = 4, x=factor(position), fill = col_bg3, colour = col_bg3)
)
}
# tile thingies end
heat_legend=get_legend(heat_bar)
heat_legend=get_legend(heat_bar)
out_plot=cowplot::plot_grid(
heat_bar,
NULL,