LSHTM_analysis/scripts/functions/position_annotation.R
2023-02-27 20:02:10 +00:00

198 lines
No EOL
6.8 KiB
R

# position_annotation takes a Data Frame (df) and returns a ggplot object.
#
# This plots position tiles for the (up to) three ligands as well as drug
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,
aa_pos_lig2=1:100,
aa_pos_lig3=1:100,
drug_colour='green',
lig1_colour='slategrey',
lig2_colour='navyblue',
lig3_colour='purple',
x_label=NULL
)
{
x_ats = 12
x_tangle = 90
x_tts = 20
y_tts = 23
xtt_col = "black"
ytt_col = "black"
leg_dir = "horizontal"
leg_ts = 15
leg_tts = 16
leg_pos = "none"
# plot_df=plot_df[order(plot_df$ligand_distance),]
#
# plot_df$position = factor(plot_df$position)
#plot_df = generate_distance_colour_map(plot_df, debug=TRUE)
# plot_df$col_aa = ifelse(plot_df[["position"]]%in%active_aa_pos,
# "brown", "transparent")
if (generate_colours){
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)
# 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()
) + # enable for alignment debug
labs(x = NULL, y = NULL) +
# if reorder_position is turned on then we need to reorder 'x'
# according to the pos_count column (creating this column is
# left as a fun exercise to whoever reads this next)
if(reorder_position) {
geom_tile(aes(y=0, x=reorder(position,-pos_count)),
fill=plot_df$ligD_colours)
} else {
geom_tile(aes(y=0, x=factor(position)),
fill=plot_df$ligD_colours)
}
#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", axis.title.x = element_text(size = 8)
) +
labs(x = x_label, 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)
)
} 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", axis.title.x = element_text(size = 8)
) +
labs(x = x_label, 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
heat_legend=get_legend(heat_bar)
out_plot=cowplot::plot_grid(
heat_bar,
NULL,
pos_tiles,
ncol=1,
align='v',
rel_heights = c(1,
-0.1,
2)
)
return(out_plot)
}
# position_annotation(small_df3,
# 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
# )
#
# # proof that you can use this function to pass arbitrary lists of numbers :-)
# position_annotation(merged_df3,
# aa_pos_drug=1:1000,
# active_aa_pos=1:1000,
# aa_pos_lig1=1:1000,
# aa_pos_lig2=1:1000,
# aa_pos_lig3=1:1000,
# drug_colour = "red",
# lig1_colour = "green",
# lig2_colour = "blue",
# lig3_colour = "skyblue"
# )