fix many plot functions to stop them using the "g=ggplot()" pattern,
which annoyingly throws away lots of useful data that RShiny needs for clickable plots. Also split the "flame bar" for ligand distance out into separate functions in generate_distance_colour_map.R. This can now be easily incorporated into any "wide" graph showing all positions.
This commit is contained in:
parent
e498d46f8b
commit
bdbc97c40a
8 changed files with 1323 additions and 725 deletions
|
@ -2,6 +2,7 @@
|
||||||
# 1b: Define function: coloured barplot by subgroup
|
# 1b: Define function: coloured barplot by subgroup
|
||||||
# LINK: https://stackoverflow.com/questions/49818271/stacked-barplot-with-colour-gradients-for-each-bar
|
# LINK: https://stackoverflow.com/questions/49818271/stacked-barplot-with-colour-gradients-for-each-bar
|
||||||
#########################################################
|
#########################################################
|
||||||
|
source("~/git/LSHTM_analysis/scripts/functions/generate_distance_colour_map.R")
|
||||||
|
|
||||||
ColourPalleteMulti = function(df, group, subgroup){
|
ColourPalleteMulti = function(df, group, subgroup){
|
||||||
|
|
||||||
|
@ -35,6 +36,7 @@ ColourPalleteMulti = function(df, group, subgroup){
|
||||||
|
|
||||||
bp_stability_hmap <- function(plotdf = merged_df3
|
bp_stability_hmap <- function(plotdf = merged_df3
|
||||||
, xvar_colname = "position"
|
, xvar_colname = "position"
|
||||||
|
, yvar_colname = 'duet_scaled' #FIXME: temp, remove
|
||||||
#, bar_col_colname = "group"
|
#, bar_col_colname = "group"
|
||||||
, stability_colname = "duet_scaled"
|
, stability_colname = "duet_scaled"
|
||||||
, stability_outcome_colname = "duet_outcome"
|
, stability_outcome_colname = "duet_outcome"
|
||||||
|
@ -46,8 +48,27 @@ bp_stability_hmap <- function(plotdf = merged_df3
|
||||||
, my_pts = 20 # plot-title size
|
, my_pts = 20 # plot-title size
|
||||||
, my_xlab = "Position"
|
, my_xlab = "Position"
|
||||||
, my_ylab = "No. of nsSNPs"
|
, my_ylab = "No. of nsSNPs"
|
||||||
|
|
||||||
|
# Custom 2: x-axis: geom tiles ~ lig distance
|
||||||
|
#, A_xvar_lig = T
|
||||||
|
, lig_dist_colname = LigDist_colname # from globals
|
||||||
|
, tpos0 = 0 # 0 is a magic number that does my sensible default
|
||||||
|
, tW0 = 1
|
||||||
|
, tH0 = 0.2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
################################################
|
||||||
|
# Custom 2: x-axis geom tiles ~ lig distance
|
||||||
|
################################################
|
||||||
|
|
||||||
|
#=========================
|
||||||
|
# Build data with colours
|
||||||
|
# ~ ligand distance
|
||||||
|
#=========================
|
||||||
|
plotdf = generate_distance_colour_map(plotdf, yvar_colname = stability_colname, debug=TRUE)
|
||||||
|
|
||||||
# order the df by position and ensure it is a factor
|
# order the df by position and ensure it is a factor
|
||||||
plotdf = plotdf[order(plotdf[[xvar_colname]]), ]
|
plotdf = plotdf[order(plotdf[[xvar_colname]]), ]
|
||||||
|
@ -74,17 +95,15 @@ bp_stability_hmap <- function(plotdf = merged_df3
|
||||||
|
|
||||||
cat("\nNo. of sub colours generated:", length(subcols_bp_hmap))
|
cat("\nNo. of sub colours generated:", length(subcols_bp_hmap))
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------
|
#-------------------------------
|
||||||
# Generate the subcols barplot
|
# Generate the subcols barplot
|
||||||
#-------------------------------
|
#-------------------------------
|
||||||
|
cowplot::plot_grid(
|
||||||
#g = ggplot(plotdf, aes(x = factor(position, ordered = T)))
|
ggplot(plotdf, aes_string(x = xvar_colname
|
||||||
g = ggplot(plotdf, aes_string(x = xvar_colname
|
|
||||||
# , ordered = T)
|
# , ordered = T)
|
||||||
))
|
)) +
|
||||||
|
geom_bar(aes(fill = group)
|
||||||
|
|
||||||
OutWidePlot = g + geom_bar(aes(fill = group)
|
|
||||||
, colour = "grey") +
|
, colour = "grey") +
|
||||||
|
|
||||||
scale_fill_manual( values = subcols_bp_hmap
|
scale_fill_manual( values = subcols_bp_hmap
|
||||||
|
@ -102,10 +121,19 @@ bp_stability_hmap <- function(plotdf = merged_df3
|
||||||
, axis.title.y = element_text(size = my_yaxts )
|
, axis.title.y = element_text(size = my_yaxts )
|
||||||
, plot.title = element_text(size = my_pts
|
, plot.title = element_text(size = my_pts
|
||||||
, hjust = 0.5)) +
|
, hjust = 0.5)) +
|
||||||
|
geom_tile(aes(, tpos0 # heat-mapped distance tiles along the bot
|
||||||
|
, width = tW0
|
||||||
|
, height = tH0)
|
||||||
|
, fill = plotdf$ligD_colours
|
||||||
|
, colour = plotdf$ligD_colours
|
||||||
|
, linetype = "blank") +
|
||||||
|
|
||||||
labs(title = p_title
|
labs(title = p_title
|
||||||
, x = my_xlab
|
, x = my_xlab
|
||||||
, y = my_ylab)
|
, y = my_ylab),
|
||||||
|
generate_distance_legend(plotdf, yvar_colname = stability_colname),
|
||||||
return(OutWidePlot)
|
ncol = 2,
|
||||||
|
#align = "hv",
|
||||||
|
rel_widths = c(9/10, 0.4/10)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,29 +239,11 @@ wideP_consurf <- function(plotdf
|
||||||
my_xlim = length(unique(plotdf[[xvar_colname]])); my_xlim
|
my_xlim = length(unique(plotdf[[xvar_colname]])); my_xlim
|
||||||
ymin = min(plotdf[[yvar_colname]]); ymin
|
ymin = min(plotdf[[yvar_colname]]); ymin
|
||||||
ymax = max(plotdf[[yvar_colname]]); ymax
|
ymax = max(plotdf[[yvar_colname]]); ymax
|
||||||
# DEBUG
|
|
||||||
# x_factor = as.factor(plotdf[[xvar_colname]])
|
|
||||||
# y_colour_factor = as.factor(plotdf[[yvar_colourN_colname]])
|
|
||||||
|
|
||||||
# cat('XXXX THIS:', str(x_factor))
|
|
||||||
#
|
|
||||||
# g = ggplot(plotdf, aes_string(x = x_factor)
|
|
||||||
# , y = yvar_colname
|
|
||||||
# , colour = y_colour_factor)
|
|
||||||
|
|
||||||
g = ggplot(plotdf, aes_string(x = sprintf("factor(%s)", xvar_colname)
|
g = ggplot(plotdf, aes_string(x = sprintf("factor(%s)", xvar_colname)
|
||||||
, y = yvar_colname
|
, y = yvar_colname
|
||||||
, colour = sprintf("factor(%s)", yvar_colourN_colname)
|
, colour = sprintf("factor(%s)", yvar_colourN_colname)
|
||||||
))
|
))
|
||||||
# g = ggplot(plotdf, aes_string(x = "position")
|
|
||||||
# , y = "consurf_score"
|
|
||||||
# , colour = "consurf_colour_rev")
|
|
||||||
# g = ggplot(plotdf, aes_string(x = "position")
|
|
||||||
# , y = "consurf_score"
|
|
||||||
# , colour = "consurf_colour_rev")
|
|
||||||
|
|
||||||
# DEBUG
|
|
||||||
#g0=g
|
|
||||||
|
|
||||||
"if SPECIAL do SPECIAL THING, otherwise do NORMAL THING"
|
"if SPECIAL do SPECIAL THING, otherwise do NORMAL THING"
|
||||||
if (plot_type == "bar"){
|
if (plot_type == "bar"){
|
||||||
|
@ -275,8 +257,6 @@ wideP_consurf <- function(plotdf
|
||||||
, clip = "off") +
|
, clip = "off") +
|
||||||
geom_point(size = p_size) +
|
geom_point(size = p_size) +
|
||||||
scale_colour_manual(values = point_colours)
|
scale_colour_manual(values = point_colours)
|
||||||
# , labels = leg_labels
|
|
||||||
# , name = leg_title1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (plot_error_bars){
|
if (plot_error_bars){
|
||||||
|
@ -424,29 +404,6 @@ wideP_consurf <- function(plotdf
|
||||||
}else{
|
}else{
|
||||||
out = g1
|
out = g1
|
||||||
}
|
}
|
||||||
|
|
||||||
#####################################################
|
|
||||||
# #============================================
|
|
||||||
# # x-axis: geom_tiles ~ ligand distance
|
|
||||||
# #============================================
|
|
||||||
# if(A_xvar_lig){
|
|
||||||
# cat("\nColouring x-axis aa based on", lig_dist_colname
|
|
||||||
# , "\nNo. of colours:", n_colours)
|
|
||||||
#
|
|
||||||
# g2 = g1 + geom_tile(aes(, tpos0
|
|
||||||
# , width = tW0
|
|
||||||
# , height = tH0)
|
|
||||||
# , fill = plotdf$ligD_colours
|
|
||||||
# , colour = plotdf$ligD_colours
|
|
||||||
# , linetype = "blank")
|
|
||||||
#
|
|
||||||
# #cat("Nrows of plot df", length(plotdf$ligD_colours))
|
|
||||||
# out = g2
|
|
||||||
# }else{
|
|
||||||
# out = g1
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
############################################################################################
|
|
||||||
#==============================================
|
#==============================================
|
||||||
# x-axis: geom_tiles ~ active sites and others
|
# x-axis: geom_tiles ~ active sites and others
|
||||||
#==============================================
|
#==============================================
|
||||||
|
|
121
scripts/functions/generate_distance_colour_map.R
Normal file
121
scripts/functions/generate_distance_colour_map.R
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
# takes a dataframe and returns the same dataframe with two extra columns for colours and position
|
||||||
|
generate_distance_colour_map = function(plotdf,
|
||||||
|
xvar_colname = "position",
|
||||||
|
yvar_colname = 'duet_scaled',
|
||||||
|
lig_dist_colname = "ligand_distance",
|
||||||
|
lig_dist_colours = c("green", "yellow", "orange", "red"),
|
||||||
|
#tpos0 = 0,
|
||||||
|
#tpos1 = 0,
|
||||||
|
#tpos2 = 0,
|
||||||
|
#tpos3 = 0,
|
||||||
|
debug = FALSE
|
||||||
|
)
|
||||||
|
{
|
||||||
|
#-------------------
|
||||||
|
# x and y axis
|
||||||
|
# range, scale, etc
|
||||||
|
#-------------------
|
||||||
|
my_xlim = length(unique(plotdf[[yvar_colname]])); my_xlim
|
||||||
|
ymin = min(plotdf[[yvar_colname]]); ymin
|
||||||
|
ymax = max(plotdf[[yvar_colname]]); ymax
|
||||||
|
|
||||||
|
#if (tpos0 == 0){
|
||||||
|
# tpos0 = ymin-0.5
|
||||||
|
#}
|
||||||
|
#if (tpos1 == 0){
|
||||||
|
# tpos1 = ymin-0.65
|
||||||
|
#}
|
||||||
|
#if (tpos2 == 0){
|
||||||
|
# tpos2 = ymin-0.75
|
||||||
|
#}
|
||||||
|
#if (tpos3 == 0){
|
||||||
|
# tpos3 = ymin-0.85
|
||||||
|
#}
|
||||||
|
if (debug) {
|
||||||
|
cat("\nAnnotating x-axis ~", lig_dist_colname, "requested...")
|
||||||
|
}
|
||||||
|
#-------------------------------------
|
||||||
|
# round column values: to colour by
|
||||||
|
#--------------------------------------
|
||||||
|
#plotdf = plotdf[order(plotdf[[lig_dist_colname]]),]
|
||||||
|
plotdf['lig_distR'] = round(plotdf[[lig_dist_colname]], digits = 0)
|
||||||
|
#head(plotdf['lig_distR'])
|
||||||
|
|
||||||
|
#-------------------------------------
|
||||||
|
# ligand distance range, min, max, etc
|
||||||
|
#--------------------------------------
|
||||||
|
lig_min = min(round(plotdf[[lig_dist_colname]]), na.rm = T); lig_min
|
||||||
|
lig_max = max(round(plotdf[[lig_dist_colname]]), na.rm = T); lig_max
|
||||||
|
lig_mean = round(mean(round(plotdf[[lig_dist_colname]]), na.rm = T)); lig_mean
|
||||||
|
|
||||||
|
#-------------------------------------
|
||||||
|
# Create mapping colour key
|
||||||
|
#--------------------------------------
|
||||||
|
# sorting removes NA, so that n_colours == length(ligD_valsR)
|
||||||
|
n_colours = length(sort(unique(round(plotdf[[lig_dist_colname]], digits = 0)))); n_colours
|
||||||
|
|
||||||
|
lig_cols = colorRampPalette(lig_dist_colours)(n_colours); lig_cols
|
||||||
|
ligD_valsR = sort(unique(round(plotdf[[lig_dist_colname]], digits = 0))); ligD_valsR
|
||||||
|
length(ligD_valsR)
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
if (n_colours == length(ligD_valsR)) {
|
||||||
|
cat("\nStarting: mapping b/w"
|
||||||
|
, lig_dist_colname
|
||||||
|
, "and colours")
|
||||||
|
}else{
|
||||||
|
cat("\nCannot start mapping b/w", lig_dist_colname, "and colours..."
|
||||||
|
, "\nLength mismatch:"
|
||||||
|
, "No. of colours: ", n_colours
|
||||||
|
, "\nValues to map:", length(ligD_valsR))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ligDcolKey <- data.frame(ligD_colours = lig_cols
|
||||||
|
, lig_distR = ligD_valsR); ligDcolKey
|
||||||
|
names(ligDcolKey)
|
||||||
|
if (debug) {
|
||||||
|
cat("\nSuccessful: Mapping b/w", lig_dist_colname, "and colours")
|
||||||
|
}
|
||||||
|
#-------------------------------------
|
||||||
|
# merge colour key with plotdf
|
||||||
|
#--------------------------------------
|
||||||
|
plotdf = merge(plotdf, ligDcolKey, by = 'lig_distR')
|
||||||
|
|
||||||
|
plotdf_check = as.data.frame(cbind(position = plotdf[[xvar_colname]]
|
||||||
|
, ligD = plotdf[[lig_dist_colname]]
|
||||||
|
, ligDR = plotdf$lig_distR
|
||||||
|
, ligD_cols = plotdf$ligD_colours))
|
||||||
|
return(plotdf)
|
||||||
|
}
|
||||||
|
|
||||||
|
generate_distance_legend = function(plotdf,
|
||||||
|
yvar_colname,
|
||||||
|
xvar_colname = 'position',
|
||||||
|
lig_dist_colname = "ligand_distance",
|
||||||
|
legend_title = "Ligand\nDistance"
|
||||||
|
)
|
||||||
|
{
|
||||||
|
# build legend for ligand distance "heat bar"
|
||||||
|
lig_min = min(round(plotdf[[lig_dist_colname]]), na.rm = T); lig_min
|
||||||
|
lig_max = max(round(plotdf[[lig_dist_colname]]), na.rm = T); lig_max
|
||||||
|
lig_mean = round(mean(round(plotdf[[lig_dist_colname]]), na.rm = T)); lig_mean
|
||||||
|
|
||||||
|
labels = seq(lig_min, lig_max, len = 5); labels
|
||||||
|
labelsD = round(labels, digits = 0); labelsD
|
||||||
|
|
||||||
|
get_legend(ggplot(plotdf, aes_string(x = sprintf("factor(%s)", xvar_colname)
|
||||||
|
, y = yvar_colname)) +
|
||||||
|
|
||||||
|
geom_tile(aes(fill = .data[[lig_dist_colname]])
|
||||||
|
, colour = "white") +
|
||||||
|
scale_fill_gradient2(midpoint = lig_mean
|
||||||
|
, low = "green"
|
||||||
|
, mid = "yellow"
|
||||||
|
, high = "red"
|
||||||
|
, breaks = labels
|
||||||
|
, limits = c(lig_min, lig_max)
|
||||||
|
, labels = labelsD
|
||||||
|
, name = legend_title)
|
||||||
|
)
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ lineage_distP <- function(plotdf
|
||||||
, all_lineages = F
|
, all_lineages = F
|
||||||
, use_lineages = c("L1", "L2", "L3", "L4")
|
, use_lineages = c("L1", "L2", "L3", "L4")
|
||||||
, with_facet = F
|
, with_facet = F
|
||||||
, facet_wrap_var = ""
|
, facet_wrap_var = "" # FIXME: document what this is for
|
||||||
, fill_categ = "mutation_info_labels"
|
, fill_categ = "mutation_info_labels"
|
||||||
, fill_categ_cols = c("#E69F00", "#999999")
|
, fill_categ_cols = c("#E69F00", "#999999")
|
||||||
, my_ats = 15 # axis text size
|
, my_ats = 15 # axis text size
|
||||||
|
@ -35,7 +35,7 @@ if(!all_lineages){
|
||||||
plotdf = plotdf[plotdf[[y_axis]]%in%use_lineages,]
|
plotdf = plotdf[plotdf[[y_axis]]%in%use_lineages,]
|
||||||
}
|
}
|
||||||
|
|
||||||
LinDistP = ggplot(plotdf, aes_string(x = x_axis
|
ggplot(plotdf, aes_string(x = x_axis
|
||||||
, y = y_axis))+
|
, y = y_axis))+
|
||||||
|
|
||||||
geom_density_ridges(aes_string(fill = fill_categ)
|
geom_density_ridges(aes_string(fill = fill_categ)
|
||||||
|
@ -57,19 +57,17 @@ LinDistP = ggplot(plotdf, aes_string(x = x_axis
|
||||||
, legend.title = element_text(size = my_leg_title)
|
, legend.title = element_text(size = my_leg_title)
|
||||||
, legend.position = c(0.8, 0.9)) +
|
, legend.position = c(0.8, 0.9)) +
|
||||||
labs(x = x_lab
|
labs(x = x_lab
|
||||||
, fill = leg_label)
|
, fill = leg_label) +
|
||||||
|
|
||||||
|
# FIXME: This didn't work BEFORE i fixed the ggplot() assignment thing!!!
|
||||||
if (with_facet){
|
if (with_facet){
|
||||||
|
|
||||||
# used reformulate or make as formula
|
# used reformulate or make as formula
|
||||||
#fwv = reformulate(facet_wrap_var)
|
#fwv = reformulate(facet_wrap_var)
|
||||||
fwv = as.formula(paste0("~", facet_wrap_var))
|
fwv = as.formula(paste0("~", facet_wrap_var))
|
||||||
|
|
||||||
LinDistP = LinDistP +
|
|
||||||
facet_wrap(fwv) +
|
facet_wrap(fwv) +
|
||||||
theme(legend.position = leg_pos_wf
|
theme(legend.position = leg_pos_wf
|
||||||
, legend.direction = leg_dir_wf)
|
, legend.direction = leg_dir_wf)
|
||||||
}
|
}
|
||||||
|
|
||||||
return(LinDistP)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,12 +59,16 @@ LogoPlotCustomH <- function(plot_df
|
||||||
, leg_dir = "horizontal" #can be vertical or horizontal
|
, leg_dir = "horizontal" #can be vertical or horizontal
|
||||||
, leg_ts = 15 # leg text size
|
, leg_ts = 15 # leg text size
|
||||||
, leg_tts = 16 # leg title size
|
, leg_tts = 16 # leg title size
|
||||||
|
, tpos0 = 0 # 0 is a magic number that does my sensible default
|
||||||
|
, tW0 = 1
|
||||||
|
, tH0 = 0.3
|
||||||
)
|
)
|
||||||
|
|
||||||
{
|
{
|
||||||
#################################
|
#################################
|
||||||
# Data processing for logo plot
|
# Data processing for logo plot
|
||||||
#################################
|
#################################
|
||||||
|
plot_df = generate_distance_colour_map(plot_df, yvar_colname = y_axis_colname, debug=TRUE)
|
||||||
|
|
||||||
if (rm_empty_y){
|
if (rm_empty_y){
|
||||||
plot_df = plot_df[!is.na(plot_df[y_axis_colname]),]
|
plot_df = plot_df[!is.na(plot_df[y_axis_colname]),]
|
||||||
|
@ -142,7 +146,16 @@ LogoPlotCustomH <- function(plot_df
|
||||||
ytt_col = "black"
|
ytt_col = "black"
|
||||||
}
|
}
|
||||||
|
|
||||||
p0 = ggseqlogo(logo_dfP_wf
|
if (y_axis_log){
|
||||||
|
|
||||||
|
if (grepl("Log", y_lab)){
|
||||||
|
y_lab = y_lab
|
||||||
|
|
||||||
|
}else{
|
||||||
|
y_lab = paste("Log", y_lab)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ggseqlogo(logo_dfP_wf
|
||||||
, method = "custom"
|
, method = "custom"
|
||||||
, col_scheme = my_logo_col
|
, col_scheme = my_logo_col
|
||||||
, seq_type = "aa") +
|
, seq_type = "aa") +
|
||||||
|
@ -172,26 +185,20 @@ LogoPlotCustomH <- function(plot_df
|
||||||
scale_x_discrete(x_lab
|
scale_x_discrete(x_lab
|
||||||
#, breaks
|
#, breaks
|
||||||
, labels = position_or
|
, labels = position_or
|
||||||
, limits = factor(1:length(position_or)))
|
, limits = factor(1:length(position_or))) +
|
||||||
|
|
||||||
LogoPlot = p0 + scale_y_continuous(y_lab
|
scale_y_continuous(y_lab
|
||||||
, breaks = seq(0, (y_lim), by = y_axis_increment)
|
, breaks = seq(0, (y_lim), by = y_axis_increment)
|
||||||
#, labels = seq(0, (y_lim), by = y_axis_increment)
|
#, labels = seq(0, (y_lim), by = y_axis_increment)
|
||||||
, limits = c(0, y_lim))
|
, limits = c(0, y_lim)) +
|
||||||
if (y_axis_log){
|
ylab(y_lab) +
|
||||||
|
geom_tile(aes(plot_df$position, tpos0 # heat-mapped distance tiles along the bot
|
||||||
if (grepl("Log", y_lab)){
|
, width = tW0
|
||||||
y_lab = y_lab
|
, height = tH0)
|
||||||
|
, fill = plot_df$ligD_colours
|
||||||
}else{
|
, colour = plot_df$ligD_colours
|
||||||
y_lab = paste("Log", y_lab)
|
, linetype = "blank")
|
||||||
}
|
#LogoPlot = p0 + ylab(y_lab)
|
||||||
|
#return(LogoPlot)
|
||||||
LogoPlot = p0 + ylab(y_lab)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return(LogoPlot)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,11 @@ LogoPlotSnps <- function(plot_df
|
||||||
, leg_dir = "horizontal" #can be vertical or horizontal
|
, leg_dir = "horizontal" #can be vertical or horizontal
|
||||||
, leg_ts = 20 # leg text size
|
, leg_ts = 20 # leg text size
|
||||||
, leg_tts = 16 # leg title size
|
, leg_tts = 16 # leg title size
|
||||||
|
, tpos0 = 0 # 0 is a magic number that does my sensible default
|
||||||
|
, tW0 = 1
|
||||||
|
, tH0 = 0.2
|
||||||
|
, debug=FALSE
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -58,11 +63,15 @@ LogoPlotSnps <- function(plot_df
|
||||||
############################################
|
############################################
|
||||||
# Data processing for logo plot for nsSNPS
|
# Data processing for logo plot for nsSNPS
|
||||||
############################################
|
############################################
|
||||||
setDT(plot_df)[, mut_pos_occurrence := .N, by = .(eval(parse(text=x_axis_colname)))]
|
|
||||||
|
|
||||||
|
# Generate "ligand distance" colour map
|
||||||
|
plot_df = generate_distance_colour_map(plot_df, debug=TRUE)
|
||||||
|
|
||||||
|
setDT(plot_df)[, mut_pos_occurrence := .N, by = .(eval(parse(text=x_axis_colname)))]
|
||||||
|
if (debug) {
|
||||||
table(plot_df[[x_axis_colname]])
|
table(plot_df[[x_axis_colname]])
|
||||||
table(plot_df$mut_pos_occurrence)
|
table(plot_df$mut_pos_occurrence)
|
||||||
|
}
|
||||||
max_mut = max(table(plot_df[[x_axis_colname]]))
|
max_mut = max(table(plot_df[[x_axis_colname]]))
|
||||||
|
|
||||||
# Subset Data as specified by user
|
# Subset Data as specified by user
|
||||||
|
@ -73,13 +82,14 @@ LogoPlotSnps <- function(plot_df
|
||||||
my_data_snp = plot_df
|
my_data_snp = plot_df
|
||||||
u = unique(my_data_snp[[x_axis_colname]])
|
u = unique(my_data_snp[[x_axis_colname]])
|
||||||
max_mult_mut = max(table(my_data_snp[[x_axis_colname]]))
|
max_mult_mut = max(table(my_data_snp[[x_axis_colname]]))
|
||||||
|
if (debug) {
|
||||||
cat("\nNo filtering requested:"
|
cat("\nNo filtering requested:"
|
||||||
, "\nTotal no. of nsSNPs:", sum(table(plot_df$mut_pos_occurrence))
|
, "\nTotal no. of nsSNPs:", sum(table(plot_df$mut_pos_occurrence))
|
||||||
, "\nTotal no. of nsSNPs omitted:", sum(table(plot_df$mut_pos_occurrence)[omit_snp_count])
|
, "\nTotal no. of nsSNPs omitted:", sum(table(plot_df$mut_pos_occurrence)[omit_snp_count])
|
||||||
, "\nDim of data:", dim(my_data_snp)
|
, "\nDim of data:", dim(my_data_snp)
|
||||||
, "\nNo. of positions:", length(u)
|
, "\nNo. of positions:", length(u)
|
||||||
, "\nMax no. of muts at any position:", max_mult_mut)
|
, "\nMax no. of muts at any position:", max_mult_mut)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
my_data_snp = subset(plot_df, !(mut_pos_occurrence%in%omit_snp_count) )
|
my_data_snp = subset(plot_df, !(mut_pos_occurrence%in%omit_snp_count) )
|
||||||
|
@ -88,7 +98,7 @@ LogoPlotSnps <- function(plot_df
|
||||||
got_rows = sum(table(my_data_snp$mut_pos_occurrence))
|
got_rows = sum(table(my_data_snp$mut_pos_occurrence))
|
||||||
u = unique(my_data_snp[[x_axis_colname]])
|
u = unique(my_data_snp[[x_axis_colname]])
|
||||||
max_mult_mut = max(table(my_data_snp[[x_axis_colname]]))
|
max_mult_mut = max(table(my_data_snp[[x_axis_colname]]))
|
||||||
|
if (debug) {
|
||||||
if (got_rows == exp_nrows) {
|
if (got_rows == exp_nrows) {
|
||||||
cat("\nPass: Position with the stated nsSNP frequency filtered:", omit_snp_count
|
cat("\nPass: Position with the stated nsSNP frequency filtered:", omit_snp_count
|
||||||
, "\nTotal no. of nsSNPs:", sum(table(plot_df$mut_pos_occurrence))
|
, "\nTotal no. of nsSNPs:", sum(table(plot_df$mut_pos_occurrence))
|
||||||
|
@ -103,6 +113,7 @@ LogoPlotSnps <- function(plot_df
|
||||||
, "\nGot:", got_rows )
|
, "\nGot:", got_rows )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#--------------------------------------
|
#--------------------------------------
|
||||||
# matrix for mutant type
|
# matrix for mutant type
|
||||||
|
@ -116,19 +127,23 @@ LogoPlotSnps <- function(plot_df
|
||||||
tab_mt = unclass(tab_mt)
|
tab_mt = unclass(tab_mt)
|
||||||
|
|
||||||
if (is.matrix(tab_mt)){
|
if (is.matrix(tab_mt)){
|
||||||
|
if (debug) {
|
||||||
cat("\nPASS: Mutant matrix successfully created..."
|
cat("\nPASS: Mutant matrix successfully created..."
|
||||||
#, "\nRownames of mutant matrix:", rownames(tab_mt)
|
#, "\nRownames of mutant matrix:", rownames(tab_mt)
|
||||||
#, "\nColnames of mutant matrix:", colnames(tab_mt)
|
#, "\nColnames of mutant matrix:", colnames(tab_mt)
|
||||||
)
|
)
|
||||||
|
}
|
||||||
} else{
|
} else{
|
||||||
tab_mt = as.matrix(tab_mt, rownames = T)
|
tab_mt = as.matrix(tab_mt, rownames = T)
|
||||||
if (is.matrix(tab_mt)){
|
if (is.matrix(tab_mt)){
|
||||||
|
if (debug) {
|
||||||
cat("\nCreating mutant matrix..."
|
cat("\nCreating mutant matrix..."
|
||||||
#, "\nRownames of mutant matrix:", rownames(tab_mt)
|
#, "\nRownames of mutant matrix:", rownames(tab_mt)
|
||||||
#, "\nColnames of mutant matrix:", colnames(tab_mt)
|
#, "\nColnames of mutant matrix:", colnames(tab_mt)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#-------------------------------------
|
#-------------------------------------
|
||||||
# matrix for wild type
|
# matrix for wild type
|
||||||
|
@ -146,7 +161,7 @@ LogoPlotSnps <- function(plot_df
|
||||||
wt
|
wt
|
||||||
|
|
||||||
tab_wt = table(wt[[symbol_wt_colname]], wt[[x_axis_colname]]); tab_wt # should all be 1
|
tab_wt = table(wt[[symbol_wt_colname]], wt[[x_axis_colname]]); tab_wt # should all be 1
|
||||||
|
if (debug) {
|
||||||
if ( identical(colnames(tab_mt), colnames(tab_wt) ) && identical(ncol(tab_mt), ncol(tab_wt)) ){
|
if ( identical(colnames(tab_mt), colnames(tab_wt) ) && identical(ncol(tab_mt), ncol(tab_wt)) ){
|
||||||
|
|
||||||
cat("\nPASS: Wild type matrix successfully created"
|
cat("\nPASS: Wild type matrix successfully created"
|
||||||
|
@ -157,6 +172,7 @@ LogoPlotSnps <- function(plot_df
|
||||||
#, "\nColnames of mutant matrix:", colnames(tab_wt)
|
#, "\nColnames of mutant matrix:", colnames(tab_wt)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
######################################
|
######################################
|
||||||
# Generating plots for muts and wt
|
# Generating plots for muts and wt
|
||||||
|
@ -188,11 +204,11 @@ LogoPlotSnps <- function(plot_df
|
||||||
#####################################
|
#####################################
|
||||||
# Generating logo plots for nsSNPs
|
# Generating logo plots for nsSNPs
|
||||||
#####################################
|
#####################################
|
||||||
|
cowplot::plot_grid(
|
||||||
#-------------------
|
#-------------------
|
||||||
# Mutant logo plot
|
# Mutant logo plot
|
||||||
#-------------------
|
#-------------------
|
||||||
p0 = ggseqlogo(tab_mt
|
ggseqlogo(tab_mt
|
||||||
, method = 'custom'
|
, method = 'custom'
|
||||||
, col_scheme = my_logo_col
|
, col_scheme = my_logo_col
|
||||||
, seq_type = 'aa') +
|
, seq_type = 'aa') +
|
||||||
|
@ -204,18 +220,21 @@ LogoPlotSnps <- function(plot_df
|
||||||
scale_x_continuous(breaks = 1:ncol(tab_mt)
|
scale_x_continuous(breaks = 1:ncol(tab_mt)
|
||||||
, expand = c(0.01,0)
|
, expand = c(0.01,0)
|
||||||
, labels = colnames(tab_mt))+
|
, labels = colnames(tab_mt))+
|
||||||
|
|
||||||
scale_y_continuous(breaks = 0:(max_mult_mut-1)
|
scale_y_continuous(breaks = 0:(max_mult_mut-1)
|
||||||
, labels = c(1:max_mult_mut)
|
, labels = c(1:max_mult_mut)
|
||||||
, limits = c(0, max_mult_mut)) +
|
, limits = c(0, max_mult_mut)) +
|
||||||
#xlab(x_lab) +
|
# FIXME: currently broken, possibly due to ggseqlogo() not working in the
|
||||||
ylab(y_lab)
|
# way standard ggplot2() does
|
||||||
|
geom_tile(aes(plot_df$position, tpos0 # heat-mapped distance tiles along the bottom.
|
||||||
cat('\nDone: p0')
|
, width = tW0
|
||||||
|
, height = tH0)
|
||||||
# further customisation
|
, fill = plot_df$ligD_colours
|
||||||
mut_logo_p = p0 + theme(legend.position = leg_pos
|
, colour = plot_df$ligD_colours
|
||||||
|
, linetype = "blank") +
|
||||||
|
ylab(y_lab) +
|
||||||
|
theme(legend.position = leg_pos
|
||||||
, legend.direction = leg_dir
|
, legend.direction = leg_dir
|
||||||
#, legend.title = element_blank()
|
|
||||||
, legend.title = element_text(size = leg_tts
|
, legend.title = element_text(size = leg_tts
|
||||||
, colour = ytt_col)
|
, colour = ytt_col)
|
||||||
, legend.text = element_text(size = leg_ts)
|
, legend.text = element_text(size = leg_ts)
|
||||||
|
@ -225,7 +244,6 @@ LogoPlotSnps <- function(plot_df
|
||||||
, hjust = 1
|
, hjust = 1
|
||||||
, vjust = 0.4
|
, vjust = 0.4
|
||||||
, colour = xfont_bgc)
|
, colour = xfont_bgc)
|
||||||
#, axis.text.y = element_blank()
|
|
||||||
, axis.text.y = element_text(size = y_ats
|
, axis.text.y = element_text(size = y_ats
|
||||||
, angle = y_tangle
|
, angle = y_tangle
|
||||||
, hjust = 1
|
, hjust = 1
|
||||||
|
@ -236,16 +254,9 @@ LogoPlotSnps <- function(plot_df
|
||||||
, axis.title.y = element_text(size = y_tts
|
, axis.title.y = element_text(size = y_tts
|
||||||
, colour = ytt_col)
|
, colour = ytt_col)
|
||||||
|
|
||||||
, plot.background = element_rect(fill = theme_bgc))
|
, plot.background = element_rect(fill = theme_bgc)
|
||||||
|
)
|
||||||
cat('\nDone: mut_logo_p')
|
,ggseqlogo(tab_wt
|
||||||
#return(mut_logo_p)
|
|
||||||
LogoPlotL[['mut_logoP']] <- mut_logo_p
|
|
||||||
|
|
||||||
#------------------
|
|
||||||
# Wild logo plot
|
|
||||||
#------------------
|
|
||||||
p1 = ggseqlogo(tab_wt
|
|
||||||
, method = 'custom'
|
, method = 'custom'
|
||||||
, col_scheme = my_logo_col
|
, col_scheme = my_logo_col
|
||||||
, seq_type = 'aa') +
|
, seq_type = 'aa') +
|
||||||
|
@ -259,12 +270,7 @@ LogoPlotSnps <- function(plot_df
|
||||||
, expand = c(0.01,0)
|
, expand = c(0.01,0)
|
||||||
, labels = colnames(tab_wt))+
|
, labels = colnames(tab_wt))+
|
||||||
|
|
||||||
xlab(x_lab)
|
xlab(x_lab) +
|
||||||
|
|
||||||
cat('\nDone: p1')
|
|
||||||
|
|
||||||
# further customisation
|
|
||||||
wt_logo_p = p1 +
|
|
||||||
|
|
||||||
theme(legend.position = "none"
|
theme(legend.position = "none"
|
||||||
, legend.direction = leg_dir
|
, legend.direction = leg_dir
|
||||||
|
@ -272,44 +278,22 @@ LogoPlotSnps <- function(plot_df
|
||||||
, legend.title = element_text(size = y_tts
|
, legend.title = element_text(size = y_tts
|
||||||
, colour = ytt_col)
|
, colour = ytt_col)
|
||||||
, legend.text = element_text(size = leg_ts)
|
, legend.text = element_text(size = leg_ts)
|
||||||
|
|
||||||
, axis.text.x = element_text(size = x_ats
|
, axis.text.x = element_text(size = x_ats
|
||||||
, angle = x_tangle
|
, angle = x_tangle
|
||||||
, hjust = 1
|
, hjust = 1
|
||||||
, vjust = 0.4
|
, vjust = 0.4
|
||||||
, colour = xfont_bgc)
|
, colour = xfont_bgc)
|
||||||
, axis.text.y = element_blank()
|
, axis.text.y = element_blank()
|
||||||
|
|
||||||
, axis.title.x = element_text(size = x_tts
|
, axis.title.x = element_text(size = x_tts
|
||||||
, colour = xtt_col)
|
, colour = xtt_col)
|
||||||
, axis.title.y = element_text(size = y_tts
|
, axis.title.y = element_text(size = y_tts
|
||||||
, colour = ytt_col)
|
, colour = ytt_col)
|
||||||
|
, plot.background = element_rect(fill = theme_bgc)
|
||||||
, plot.background = element_rect(fill = theme_bgc))
|
)
|
||||||
|
|
||||||
cat('\nDone: wt_logo_p')
|
|
||||||
#return(wt_logo_p)
|
|
||||||
LogoPlotL[['wt_logoP']] <- wt_logo_p
|
|
||||||
|
|
||||||
#=========================================
|
|
||||||
# Output
|
|
||||||
# Combined plot: logo_mutliple_muts.svg
|
|
||||||
#=========================================
|
|
||||||
#suppressMessages( require(cowplot) )
|
|
||||||
#plot_grid(p1, p3, ncol = 1, align = 'v')
|
|
||||||
cat('\nDone: mut_logo_p + wt_logo_p')
|
|
||||||
|
|
||||||
# colour scheme: https://rdrr.io/cran/ggseqlogo/src/R/col_schemes.r
|
|
||||||
#cat("\nOutput plot:", LogoSNPs_comb, "\n")
|
|
||||||
#svg(LogoSNPs_combined, width = 32, height = 10)
|
|
||||||
LogoPlotL[['wt_logoP']] <- wt_logo_p
|
|
||||||
|
|
||||||
LogoSNPs_comb = cowplot::plot_grid(LogoPlotL[['mut_logoP']]
|
|
||||||
, LogoPlotL[['wt_logoP']]
|
|
||||||
, nrow = 2
|
, nrow = 2
|
||||||
, align = "v"
|
, align = "v"
|
||||||
, rel_heights = c(3/4, 1/4))
|
, rel_heights = c(3/4, 1/4))
|
||||||
|
#------------------
|
||||||
return(LogoSNPs_comb)
|
# Wild logo plot
|
||||||
|
#------------------
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,8 +105,8 @@ site_snp_count_bp <- function (plotdf
|
||||||
# not sure if to use with sort or directly
|
# not sure if to use with sort or directly
|
||||||
my_x = sort(unique(snpsBYpos_df$snpsBYpos))
|
my_x = sort(unique(snpsBYpos_df$snpsBYpos))
|
||||||
|
|
||||||
g = ggplot(snpsBYpos_df, aes(x = snpsBYpos))
|
ggplot(snpsBYpos_df, aes(x = snpsBYpos)) +
|
||||||
OutPlot_pos_count = g + geom_bar(aes (alpha = 0.5)
|
geom_bar(aes (alpha = 0.5)
|
||||||
, show.legend = FALSE) +
|
, show.legend = FALSE) +
|
||||||
scale_x_continuous(breaks = unique(snpsBYpos_df$snpsBYpos)) +
|
scale_x_continuous(breaks = unique(snpsBYpos_df$snpsBYpos)) +
|
||||||
geom_label(stat = "count", aes(label = ..count..)
|
geom_label(stat = "count", aes(label = ..count..)
|
||||||
|
@ -127,13 +127,10 @@ site_snp_count_bp <- function (plotdf
|
||||||
, plot.subtitle = element_text(size = subtitle_size
|
, plot.subtitle = element_text(size = subtitle_size
|
||||||
, hjust = 0.5
|
, hjust = 0.5
|
||||||
, colour = subtitle_colour)) +
|
, colour = subtitle_colour)) +
|
||||||
|
|
||||||
labs(title = bp_plot_title
|
labs(title = bp_plot_title
|
||||||
, subtitle = subtitle_text
|
, subtitle = subtitle_text
|
||||||
, x = xaxis_title
|
, x = xaxis_title
|
||||||
, y = yaxis_title)
|
, y = yaxis_title)
|
||||||
|
|
||||||
return(OutPlot_pos_count)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
506
scripts/functions/wideP_consurf.R
Normal file
506
scripts/functions/wideP_consurf.R
Normal file
|
@ -0,0 +1,506 @@
|
||||||
|
#!/usr/bin/env Rscript
|
||||||
|
|
||||||
|
#########################################################
|
||||||
|
# TASK: function for wide plot
|
||||||
|
#with consurf score and error bars
|
||||||
|
#position numbers coloured by
|
||||||
|
# - ligand distance
|
||||||
|
# - active site residues
|
||||||
|
#########################################################
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
# wideP():
|
||||||
|
# input args
|
||||||
|
#==========================================================
|
||||||
|
wideP_consurf <- function(plot_df
|
||||||
|
, xvar_colname = "position"
|
||||||
|
, yvar_colname = "consurf_score"
|
||||||
|
, yvar_colourN_colname = "consurf_colour_rev" # num from 0-1
|
||||||
|
, plot_error_bars = T
|
||||||
|
, upper_EB_colname = "consurf_ci_upper"
|
||||||
|
, lower_EB_colname = "consurf_ci_lower"
|
||||||
|
|
||||||
|
, plot_type = "point" # default is point
|
||||||
|
, point_colours
|
||||||
|
, p_size = 2
|
||||||
|
, leg_title1 = ""
|
||||||
|
, leg_labels = c("0": "Insufficient Data"
|
||||||
|
, "1": "Variable"
|
||||||
|
, "2", "3", "4", "5", "6", "7", "8"
|
||||||
|
, "9": "Conserved")
|
||||||
|
, panel_col = "black"
|
||||||
|
, panel_col_fill = "black"
|
||||||
|
|
||||||
|
# axes title and label sizes
|
||||||
|
, x_axls = 12 # x-axis label size
|
||||||
|
, y_axls = 15 # y-axis label size
|
||||||
|
, x_axts = 12 # x-axis text size
|
||||||
|
, y_axts = 12 # y-axis text size
|
||||||
|
, default_xtc = "black" # x-axis text colour
|
||||||
|
, ptitle = ""
|
||||||
|
, xlab = ""
|
||||||
|
, ylab = ""
|
||||||
|
, pts = 20
|
||||||
|
|
||||||
|
# plot margins
|
||||||
|
, t_margin = 0.5
|
||||||
|
, r_margin = 0.5
|
||||||
|
, b_margin = 1
|
||||||
|
, l_margin = 1
|
||||||
|
, unit_margin = "cm"
|
||||||
|
|
||||||
|
# Custom 1: x-axis: text colour
|
||||||
|
, xtext_colour_aa = F
|
||||||
|
, xtext_colour_aa1 = active_aa_pos
|
||||||
|
, xtext_colour_aa2 = aa_pos_drug
|
||||||
|
, xtext_colours = c("purple", "brown", "black")
|
||||||
|
|
||||||
|
# Custom 2: x-axis: geom tiles ~ lig distance
|
||||||
|
, A_xvar_lig = T
|
||||||
|
, leg_title2 = "Ligand Distance"
|
||||||
|
, lig_dist_colname = LigDist_colname # from globals
|
||||||
|
, lig_dist_colours = c("green", "yellow", "orange", "red")
|
||||||
|
, tpos0 = 0 # 0 is a magic number that does my sensible default
|
||||||
|
, tW0 = 1
|
||||||
|
, tH0 = 0.3
|
||||||
|
|
||||||
|
# Custom 3: x-axis: geom tiles ~ active sites and ligand
|
||||||
|
, A_xvar_aa = F
|
||||||
|
, aa_pos_drug = NULL
|
||||||
|
, drug_aa_colour = "purple"
|
||||||
|
, tW = 1
|
||||||
|
, tH = 0.2
|
||||||
|
, active_aa_pos = NULL
|
||||||
|
, active_aa_colour = "brown"
|
||||||
|
|
||||||
|
, aa_pos_lig1 = NULL
|
||||||
|
, aa_colour_lig1 = "blue"
|
||||||
|
, tpos1 = 0
|
||||||
|
|
||||||
|
, aa_pos_lig2 = NULL
|
||||||
|
, aa_colour_lig2 = "cyan"
|
||||||
|
, tpos2 = 0
|
||||||
|
|
||||||
|
, aa_pos_lig3 = NULL
|
||||||
|
, aa_colour_lig3 = "cornflowerblue"
|
||||||
|
, tpos3 = 0
|
||||||
|
|
||||||
|
, default_gt_clr = "white"
|
||||||
|
, debug=FALSE
|
||||||
|
){
|
||||||
|
|
||||||
|
if(missing(point_colours)){
|
||||||
|
temp_cols = colorRampPalette(c("seagreen", "sienna3"))(30)
|
||||||
|
point_colours = temp_cols
|
||||||
|
}else{
|
||||||
|
point_colours = point_colours
|
||||||
|
}
|
||||||
|
|
||||||
|
###############################
|
||||||
|
# custom 1: x-axis text colour
|
||||||
|
##############################
|
||||||
|
|
||||||
|
if (xtext_colour_aa){
|
||||||
|
positionF <- levels(as.factor(plot_df[[xvar_colname]]))
|
||||||
|
length(positionF)
|
||||||
|
aa_pos_colours = ifelse(positionF%in%xtext_colour_aa1, xtext_colours[1]
|
||||||
|
, ifelse(positionF%in%xtext_colour_aa2
|
||||||
|
, xtext_colours[2]
|
||||||
|
, xtext_colours[3]))
|
||||||
|
}else{
|
||||||
|
aa_pos_colours = default_xtc
|
||||||
|
}
|
||||||
|
|
||||||
|
################################################
|
||||||
|
# Custom 2: x-axis geom tiles ~ lig distance
|
||||||
|
################################################
|
||||||
|
|
||||||
|
#=========================
|
||||||
|
# Build data with colours
|
||||||
|
# ~ ligand distance
|
||||||
|
#=========================
|
||||||
|
if (A_xvar_lig){
|
||||||
|
cat("\nAnnotating x-axis ~", lig_dist_colname, "requested...")
|
||||||
|
|
||||||
|
#-------------------------------------
|
||||||
|
# round column values: to colour by
|
||||||
|
#--------------------------------------
|
||||||
|
#plot_df = plot_df[order(plot_df[[lig_dist_colname]]),]
|
||||||
|
plot_df['lig_distR'] = round(plot_df[[lig_dist_colname]], digits = 0)
|
||||||
|
head(plot_df['lig_distR'])
|
||||||
|
|
||||||
|
#-------------------------------------
|
||||||
|
# ligand distance range, min, max, etc
|
||||||
|
#--------------------------------------
|
||||||
|
lig_min = min(round(plot_df[[lig_dist_colname]]), na.rm = T); lig_min
|
||||||
|
lig_max = max(round(plot_df[[lig_dist_colname]]), na.rm = T); lig_max
|
||||||
|
lig_mean = round(mean(round(plot_df[[lig_dist_colname]]), na.rm = T)); lig_mean
|
||||||
|
|
||||||
|
#-------------------------------------
|
||||||
|
# Create mapping colour key
|
||||||
|
#--------------------------------------
|
||||||
|
# sorting removes NA, so that n_colours == length(ligD_valsR)
|
||||||
|
n_colours = length(sort(unique(round(plot_df[[lig_dist_colname]], digits = 0)))); n_colours
|
||||||
|
|
||||||
|
lig_cols = colorRampPalette(lig_dist_colours)(n_colours); lig_cols
|
||||||
|
ligD_valsR = sort(unique(round(plot_df[[lig_dist_colname]], digits = 0))); ligD_valsR
|
||||||
|
length(ligD_valsR)
|
||||||
|
|
||||||
|
if (n_colours == length(ligD_valsR)) {
|
||||||
|
cat("\nStarting: mapping b/w"
|
||||||
|
, lig_dist_colname
|
||||||
|
, "and colours")
|
||||||
|
}else{
|
||||||
|
cat("\nCannot start mapping b/w", lig_dist_colname, "and colours..."
|
||||||
|
, "\nLength mismatch:"
|
||||||
|
, "No. of colours: ", n_colours
|
||||||
|
, "\nValues to map:", length(ligD_valsR))
|
||||||
|
}
|
||||||
|
|
||||||
|
ligDcolKey <- data.frame(ligD_colours = lig_cols
|
||||||
|
, lig_distR = ligD_valsR); ligDcolKey
|
||||||
|
names(ligDcolKey)
|
||||||
|
cat("\nSuccessful: Mapping b/w", lig_dist_colname, "and colours")
|
||||||
|
|
||||||
|
#-------------------------------------
|
||||||
|
# merge colour key with plot_df
|
||||||
|
#--------------------------------------
|
||||||
|
plot_df = merge(plot_df, ligDcolKey, by = 'lig_distR')
|
||||||
|
|
||||||
|
plot_df_check = as.data.frame(cbind(position = plot_df[[xvar_colname]]
|
||||||
|
, ligD = plot_df[[lig_dist_colname]]
|
||||||
|
, ligDR = plot_df$lig_distR
|
||||||
|
, ligD_cols = plot_df$ligD_colours))
|
||||||
|
} else{
|
||||||
|
plot_df = plot_df
|
||||||
|
}
|
||||||
|
|
||||||
|
###############################################
|
||||||
|
# Custom 3: x-axis geom tiles ~ active sites
|
||||||
|
################################################
|
||||||
|
|
||||||
|
#==========================
|
||||||
|
# Build Data with colours
|
||||||
|
# ~ on active sites
|
||||||
|
#==========================
|
||||||
|
|
||||||
|
if(A_xvar_aa) {
|
||||||
|
cat("\nAnnotation for xvar requested. Building colours for annotation...")
|
||||||
|
|
||||||
|
aa_colour_colname = "bg_all"
|
||||||
|
aa_colour_colname1 = "col_bg1"
|
||||||
|
aa_colour_colname2 = "col_bg2"
|
||||||
|
aa_colour_colname3 = "col_bg3"
|
||||||
|
|
||||||
|
#--------------------------------------------------
|
||||||
|
# column colour 0: Active site + drug binding sites
|
||||||
|
#--------------------------------------------------
|
||||||
|
plot_df[[aa_colour_colname]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_drug
|
||||||
|
, drug_aa_colour
|
||||||
|
, ifelse(plot_df[[xvar_colname]]%in%active_aa_pos
|
||||||
|
, active_aa_colour, default_gt_clr ))
|
||||||
|
plot_df[[aa_colour_colname]]
|
||||||
|
cat("\nColumn created 'bg_all':", length(plot_df[[aa_colour_colname]]))
|
||||||
|
|
||||||
|
#------------------------------------------------
|
||||||
|
# column colour 1: Ligand 1 + drug binding sites
|
||||||
|
#------------------------------------------------
|
||||||
|
cat("\nAssigning colours to drug binding and ligand-1 binding residues")
|
||||||
|
plot_df[[aa_colour_colname1]] = plot_df[[aa_colour_colname]]
|
||||||
|
plot_df[[aa_colour_colname1]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_lig1
|
||||||
|
, aa_colour_lig1, plot_df[[aa_colour_colname]])
|
||||||
|
#------------------------------------------------
|
||||||
|
# column colour 2: Ligand 2
|
||||||
|
#------------------------------------------------
|
||||||
|
plot_df[[aa_colour_colname2]] = plot_df[[aa_colour_colname1]]
|
||||||
|
plot_df[[aa_colour_colname2]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_lig2
|
||||||
|
, aa_colour_lig2, plot_df[[aa_colour_colname1]])
|
||||||
|
|
||||||
|
#------------------------------------------------
|
||||||
|
# column colour 3: Ligand 3
|
||||||
|
#------------------------------------------------
|
||||||
|
plot_df[[aa_colour_colname3]] = plot_df[[aa_colour_colname2]]
|
||||||
|
plot_df[[aa_colour_colname3]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_lig3
|
||||||
|
, aa_colour_lig3, plot_df[[aa_colour_colname2]])
|
||||||
|
|
||||||
|
}
|
||||||
|
###################
|
||||||
|
# start plot
|
||||||
|
###################
|
||||||
|
|
||||||
|
#-------------------
|
||||||
|
# x and y axis
|
||||||
|
# range, scale, etc
|
||||||
|
#-------------------
|
||||||
|
my_xlim = length(unique(plot_df[[xvar_colname]])); my_xlim
|
||||||
|
ymin = min(plot_df[[yvar_colname]]); ymin
|
||||||
|
ymax = max(plot_df[[yvar_colname]]); ymax
|
||||||
|
|
||||||
|
g = ggplot(plot_df, aes_string(x = sprintf("factor(%s)", xvar_colname)
|
||||||
|
, y = yvar_colname
|
||||||
|
, colour = sprintf("factor(%s)", yvar_colourN_colname)
|
||||||
|
))
|
||||||
|
|
||||||
|
"if SPECIAL do SPECIAL THING, otherwise do NORMAL THING"
|
||||||
|
if (plot_type == "bar"){
|
||||||
|
g0 = g +
|
||||||
|
geom_bar(stat = "identity")
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
g0 = g +
|
||||||
|
coord_cartesian(xlim = c(1, my_xlim)
|
||||||
|
, ylim = c(ymin, ymax)
|
||||||
|
, clip = "off") +
|
||||||
|
geom_point(size = p_size) +
|
||||||
|
scale_colour_manual(values = point_colours)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plot_error_bars){
|
||||||
|
g0 = g0 +
|
||||||
|
geom_errorbar(aes(ymin = eval(parse(text = lower_EB_colname))
|
||||||
|
, ymax = eval(parse(text = upper_EB_colname))
|
||||||
|
))
|
||||||
|
}else{
|
||||||
|
|
||||||
|
g0 = g0
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#---------------------
|
||||||
|
# add axis formatting
|
||||||
|
#---------------------
|
||||||
|
g1 = g0 + theme( axis.text.x = element_text(size = x_axts
|
||||||
|
, angle = 90
|
||||||
|
, hjust = 1
|
||||||
|
, vjust = 0.4
|
||||||
|
, face = "bold"
|
||||||
|
, colour = aa_pos_colours)
|
||||||
|
, axis.text.y = element_text(size = y_axts
|
||||||
|
, angle = 0
|
||||||
|
, hjust = 1
|
||||||
|
, vjust = 0)
|
||||||
|
, axis.title.x = element_text(size = x_axls)
|
||||||
|
, axis.title.y = element_text(size = y_axls )
|
||||||
|
, panel.background = element_rect(fill = panel_col_fill, color = panel_col)
|
||||||
|
, panel.grid.major = element_line(color = "black")
|
||||||
|
, panel.grid.minor = element_line(color = "black")
|
||||||
|
, plot.title = element_text(size = pts
|
||||||
|
, hjust = 0.5)
|
||||||
|
, plot.margin = margin(t = t_margin
|
||||||
|
, r = r_margin
|
||||||
|
, b = b_margin
|
||||||
|
, l = l_margin
|
||||||
|
, unit = unit_margin))+
|
||||||
|
guides(colour = guide_legend(title = "ConsurfXXXX")) +
|
||||||
|
|
||||||
|
labs(title = ptitle
|
||||||
|
, x = xlab
|
||||||
|
, y = ylab)
|
||||||
|
|
||||||
|
#------------------
|
||||||
|
#Extract legend1
|
||||||
|
#------------------
|
||||||
|
# yayy
|
||||||
|
g1_leg = ggplot(plot_df, aes_string(x = sprintf("factor(%s)"
|
||||||
|
, xvar_colname) ))
|
||||||
|
g1_leg = g1_leg + geom_bar(); g1_leg
|
||||||
|
g1_leg = g1_leg + geom_bar(aes_string(fill = sprintf("factor(%s)"
|
||||||
|
, yvar_colourN_colname)))
|
||||||
|
|
||||||
|
g1_leg = g1_leg + scale_fill_manual(values = consurf_palette2 , name = leg_title1)
|
||||||
|
g1_leg
|
||||||
|
|
||||||
|
legend1 = get_legend(g1_leg)
|
||||||
|
|
||||||
|
|
||||||
|
#####################################################
|
||||||
|
#============================================
|
||||||
|
# x-axis: geom_tiles ~ ligand distance
|
||||||
|
#============================================
|
||||||
|
#-------
|
||||||
|
# plot
|
||||||
|
#-------
|
||||||
|
if(A_xvar_lig){ # 0 is a magic number that does my sensible default
|
||||||
|
if (tpos0 == 0){
|
||||||
|
tpos0 = ymin-0.5
|
||||||
|
}
|
||||||
|
if (tpos1 == 0){
|
||||||
|
tpos1 = ymin-0.65
|
||||||
|
}
|
||||||
|
if (tpos2 == 0){
|
||||||
|
tpos2 = ymin-0.75
|
||||||
|
}
|
||||||
|
if (tpos3 == 0){
|
||||||
|
tpos3 = ymin-0.85
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
cat("\nColouring x-axis aa based on", lig_dist_colname
|
||||||
|
, "\nNo. of colours:", n_colours)
|
||||||
|
|
||||||
|
g2 = g1 + geom_tile(aes(, 2
|
||||||
|
# g2 = g1 + geom_tile(aes(, tpos0
|
||||||
|
|
||||||
|
, width = tW0
|
||||||
|
, height = tH0)
|
||||||
|
, fill = plot_df$ligD_colours
|
||||||
|
, colour = plot_df$ligD_colours
|
||||||
|
, linetype = "blank")
|
||||||
|
|
||||||
|
#cat("Nrows of plot df", length(plot_df$ligD_colours))
|
||||||
|
out = g2
|
||||||
|
|
||||||
|
}else{
|
||||||
|
out = g1
|
||||||
|
}
|
||||||
|
#==============================================
|
||||||
|
# x-axis: geom_tiles ~ active sites and others
|
||||||
|
#==============================================
|
||||||
|
if(A_xvar_aa){
|
||||||
|
#tpos = 0
|
||||||
|
#tW = 1
|
||||||
|
#tH = 0.2
|
||||||
|
|
||||||
|
#---------------------
|
||||||
|
# Add2plot: 3 ligands
|
||||||
|
#---------------------
|
||||||
|
if (all(!is.null(active_aa_pos) &&
|
||||||
|
!is.null(aa_pos_drug) &&
|
||||||
|
!is.null(aa_pos_lig1) && !is.null(aa_pos_lig2) && !is.null(aa_pos_lig3))) {
|
||||||
|
if (debug){
|
||||||
|
cat("\n\nAnnotating xvar with active, drug binding, and Lig 1&2&3 sites")
|
||||||
|
cat("\nCreating column colours, column name:", aa_colour_colname3)
|
||||||
|
|
||||||
|
cat("\nDoing Plot with 3 ligands")
|
||||||
|
}
|
||||||
|
out = out + geom_tile(aes(,tpos3
|
||||||
|
, width = tW
|
||||||
|
, height = tH )
|
||||||
|
, fill = plot_df[[aa_colour_colname3]]
|
||||||
|
, colour = plot_df[[aa_colour_colname3]]
|
||||||
|
, linetype = "solid") +
|
||||||
|
geom_tile(aes(, tpos2
|
||||||
|
, width = tW
|
||||||
|
, height = tH )
|
||||||
|
, fill = plot_df[[aa_colour_colname2]]
|
||||||
|
, colour = plot_df[[aa_colour_colname2]]
|
||||||
|
, linetype = "solid")+
|
||||||
|
|
||||||
|
geom_tile(aes(, tpos1
|
||||||
|
, width = tW
|
||||||
|
, height = tH)
|
||||||
|
, fill = plot_df[[aa_colour_colname1]]
|
||||||
|
, colour = plot_df[[aa_colour_colname1]]
|
||||||
|
, linetype = "solid")
|
||||||
|
if (debug){
|
||||||
|
cat("\nDone Plot with 3 ligands")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#---------------------
|
||||||
|
# Add2plot: 2 ligands
|
||||||
|
#---------------------
|
||||||
|
|
||||||
|
if (all(!is.null(active_aa_pos) &&
|
||||||
|
!is.null(aa_pos_drug) &&
|
||||||
|
!is.null(aa_pos_lig1) && !is.null(aa_pos_lig2) && is.null(aa_pos_lig3))) {
|
||||||
|
if (debug){
|
||||||
|
cat("\n\nAnnotating xvar with active, drug binding, and Lig 1&2 sites")
|
||||||
|
cat("\nCreating column colours, column name:", aa_colour_colname2)
|
||||||
|
|
||||||
|
cat("\nDoing Plot with 2 ligands")
|
||||||
|
}
|
||||||
|
out = out +
|
||||||
|
geom_tile(aes(, tpos2
|
||||||
|
, width = tW
|
||||||
|
, height = tH)
|
||||||
|
, fill = plot_df[[aa_colour_colname2]]
|
||||||
|
, colour = plot_df[[aa_colour_colname2]]
|
||||||
|
, linetype = "solid")+
|
||||||
|
geom_tile(aes(, tpos1
|
||||||
|
, width = tW
|
||||||
|
, height = tH)
|
||||||
|
, fill = plot_df[[aa_colour_colname1]]
|
||||||
|
, colour = plot_df[[aa_colour_colname1]]
|
||||||
|
, linetype = "solid")
|
||||||
|
if (debug){
|
||||||
|
cat("\nDone Plot with 2 ligands")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#---------------------
|
||||||
|
# Add2plot: 1 ligand
|
||||||
|
#---------------------
|
||||||
|
if (all(!is.null(active_aa_pos) &&
|
||||||
|
!is.null(aa_pos_drug) &&
|
||||||
|
!is.null(aa_pos_lig1) && is.null(aa_pos_lig2) && is.null(aa_pos_lig3))) {
|
||||||
|
if (debug){
|
||||||
|
cat("\n\nAnnotating xvar with active, drug binding, and Lig 1 sites")
|
||||||
|
cat("\nCreating column colours, column name:", aa_colour_colname1)
|
||||||
|
|
||||||
|
cat("\nDoing Plot with 1 ligands")
|
||||||
|
}
|
||||||
|
out = out +
|
||||||
|
geom_tile(aes(, tpos1
|
||||||
|
, width = tW
|
||||||
|
, height = tH)
|
||||||
|
, fill = plot_df[[aa_colour_colname1]]
|
||||||
|
, colour = plot_df[[aa_colour_colname1]]
|
||||||
|
, linetype = "solid")
|
||||||
|
|
||||||
|
cat("\nDone Plot with 1 ligand")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#-----------------------------------
|
||||||
|
# Add2plot:NO ligands
|
||||||
|
# No Ligs: Just drug and active site
|
||||||
|
# DEFAULT: A_xvar_aa == TRUE
|
||||||
|
#----------------------------------
|
||||||
|
if (all(!is.null(active_aa_pos) &&
|
||||||
|
!is.null(aa_pos_drug) &&
|
||||||
|
is.null(aa_pos_lig1) &&
|
||||||
|
is.null(aa_pos_lig2) &&
|
||||||
|
is.null(aa_pos_lig3))) {
|
||||||
|
if (debug){
|
||||||
|
cat("\n\nAnnotating xvar with active and drug binding sites")
|
||||||
|
cat("\nCreating column colours, column name:", aa_colour_colname)
|
||||||
|
cat("\nDoing Plot with 0 ligands: active and drug site only")
|
||||||
|
}
|
||||||
|
out = out + geom_tile(aes(, tpos3
|
||||||
|
, width = tW
|
||||||
|
, height = tH)
|
||||||
|
, fill = plot_df[[aa_colour_colname]]
|
||||||
|
, colour = plot_df[[aa_colour_colname]]
|
||||||
|
, linetype = "solid")
|
||||||
|
if (debug){
|
||||||
|
cat("\nDone Plot with: Active and Drug sites")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
cat("\nNo annotation for additional ligands on xvar requested")
|
||||||
|
}
|
||||||
|
if (A_xvar_lig){
|
||||||
|
legs = cowplot::plot_grid(legend1
|
||||||
|
, generate_distance_legend(plot_df, yvar_colname = yvar_colname)
|
||||||
|
, ncol = 1
|
||||||
|
, align = "hv"
|
||||||
|
, rel_heights = c(2/4,3/4))
|
||||||
|
|
||||||
|
out2 = cowplot::plot_grid( out + theme(legend.position = "none")
|
||||||
|
, legs
|
||||||
|
, ncol = 2
|
||||||
|
, align = "hv"
|
||||||
|
, rel_widths = c(9/10, 0.4/10)
|
||||||
|
)
|
||||||
|
}else{
|
||||||
|
out2 = cowplot::plot_grid( out + theme(legend.position = "none")
|
||||||
|
, legend1
|
||||||
|
, ncol = 2
|
||||||
|
, align = "hv"
|
||||||
|
, rel_widths = c(9/10, 0.5/10)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return(out2)
|
||||||
|
#return(out2)
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue