consurf plot: add debug option. logoP SNP: distance flame tiles

This commit is contained in:
Tanushree Tunstall 2022-08-03 10:48:25 +01:00
parent f2709f3992
commit e498d46f8b
2 changed files with 230 additions and 219 deletions

View file

@ -13,99 +13,100 @@
# input args # input args
#========================================================== #==========================================================
wideP_consurf <- function(plotdf wideP_consurf <- function(plotdf
, xvar_colname = "position" , xvar_colname = "position"
, yvar_colname = "consurf_score" , yvar_colname = "consurf_score"
, yvar_colourN_colname = "consurf_colour_rev" # num from 0-1 , yvar_colourN_colname = "consurf_colour_rev" # num from 0-1
, plot_error_bars = T , plot_error_bars = T
, upper_EB_colname = "consurf_ci_upper" , upper_EB_colname = "consurf_ci_upper"
, lower_EB_colname = "consurf_ci_lower" , lower_EB_colname = "consurf_ci_lower"
, plot_type = "point" # default is point , plot_type = "point" # default is point
, point_colours , point_colours
, p_size = 2 , p_size = 2
, leg_title1 = "" , leg_title1 = ""
, leg_labels = c("0": "Insufficient Data" , leg_labels = c("0": "Insufficient Data"
, "1": "Variable" , "1": "Variable"
, "2", "3", "4", "5", "6", "7", "8" , "2", "3", "4", "5", "6", "7", "8"
, "9": "Conserved") , "9": "Conserved")
, panel_col = "black" , panel_col = "black"
, panel_col_fill = "black" , panel_col_fill = "black"
# axes title and label sizes # axes title and label sizes
, x_axls = 12 # x-axis label size , x_axls = 12 # x-axis label size
, y_axls = 15 # y-axis label size , y_axls = 15 # y-axis label size
, x_axts = 12 # x-axis text size , x_axts = 12 # x-axis text size
, y_axts = 12 # y-axis text size , y_axts = 12 # y-axis text size
, default_xtc = "black" # x-axis text colour , default_xtc = "black" # x-axis text colour
, ptitle = "" , ptitle = ""
, xlab = "" , xlab = ""
, ylab = "" , ylab = ""
, pts = 20 , pts = 20
# plot margins # plot margins
, t_margin = 0.5 , t_margin = 0.5
, r_margin = 0.5 , r_margin = 0.5
, b_margin = 1 , b_margin = 1
, l_margin = 1 , l_margin = 1
, unit_margin = "cm" , unit_margin = "cm"
# Custom 1: x-axis: text colour # Custom 1: x-axis: text colour
, xtext_colour_aa = F , xtext_colour_aa = F
, xtext_colour_aa1 = active_aa_pos , xtext_colour_aa1 = active_aa_pos
, xtext_colour_aa2 = aa_pos_drug , xtext_colour_aa2 = aa_pos_drug
, xtext_colours = c("purple", "brown", "black") , xtext_colours = c("purple", "brown", "black")
# Custom 2: x-axis: geom tiles ~ lig distance # Custom 2: x-axis: geom tiles ~ lig distance
, A_xvar_lig = T , A_xvar_lig = T
, leg_title2 = "Ligand Distance" , leg_title2 = "Ligand Distance"
, lig_dist_colname = LigDist_colname # from globals , lig_dist_colname = LigDist_colname # from globals
, lig_dist_colours = c("green", "yellow", "orange", "red") , lig_dist_colours = c("green", "yellow", "orange", "red")
, tpos0 = 0 # 0 is a magic number that does my sensible default , tpos0 = 0 # 0 is a magic number that does my sensible default
, tW0 = 1 , tW0 = 1
, tH0 = 0.3 , tH0 = 0.3
# Custom 3: x-axis: geom tiles ~ active sites and ligand # Custom 3: x-axis: geom tiles ~ active sites and ligand
, A_xvar_aa = F , A_xvar_aa = F
, aa_pos_drug = NULL , aa_pos_drug = NULL
, drug_aa_colour = "purple" , drug_aa_colour = "purple"
, tW = 1 , tW = 1
, tH = 0.2 , tH = 0.2
, active_aa_pos = NULL , active_aa_pos = NULL
, active_aa_colour = "brown" , active_aa_colour = "brown"
, aa_pos_lig1 = NULL , aa_pos_lig1 = NULL
, aa_colour_lig1 = "blue" , aa_colour_lig1 = "blue"
, tpos1 = 0 , tpos1 = 0
, aa_pos_lig2 = NULL , aa_pos_lig2 = NULL
, aa_colour_lig2 = "cyan" , aa_colour_lig2 = "cyan"
, tpos2 = 0 , tpos2 = 0
, aa_pos_lig3 = NULL , aa_pos_lig3 = NULL
, aa_colour_lig3 = "cornflowerblue" , aa_colour_lig3 = "cornflowerblue"
, tpos3 = 0 , tpos3 = 0
, default_gt_clr = "white" , default_gt_clr = "white"
){ , debug=FALSE
){
if(missing(point_colours)){ if(missing(point_colours)){
temp_cols = colorRampPalette(c("seagreen", "sienna3"))(30) temp_cols = colorRampPalette(c("seagreen", "sienna3"))(30)
point_colours = temp_cols point_colours = temp_cols
}else{ }else{
point_colours = point_colours point_colours = point_colours
} }
############################### ###############################
# custom 1: x-axis text colour # custom 1: x-axis text colour
############################## ##############################
if (xtext_colour_aa){ if (xtext_colour_aa){
positionF <- levels(as.factor(plotdf[[xvar_colname]])) positionF <- levels(as.factor(plotdf[[xvar_colname]]))
length(positionF) length(positionF)
aa_pos_colours = ifelse(positionF%in%xtext_colour_aa1, xtext_colours[1] aa_pos_colours = ifelse(positionF%in%xtext_colour_aa1, xtext_colours[1]
, ifelse(positionF%in%xtext_colour_aa2 , ifelse(positionF%in%xtext_colour_aa2
, xtext_colours[2] , xtext_colours[2]
, xtext_colours[3])) , xtext_colours[3]))
}else{ }else{
aa_pos_colours = default_xtc aa_pos_colours = default_xtc
} }
@ -127,7 +128,7 @@ wideP_consurf <- function(plotdf
#plotdf = plotdf[order(plotdf[[lig_dist_colname]]),] #plotdf = plotdf[order(plotdf[[lig_dist_colname]]),]
plotdf['lig_distR'] = round(plotdf[[lig_dist_colname]], digits = 0) plotdf['lig_distR'] = round(plotdf[[lig_dist_colname]], digits = 0)
head(plotdf['lig_distR']) head(plotdf['lig_distR'])
#------------------------------------- #-------------------------------------
# ligand distance range, min, max, etc # ligand distance range, min, max, etc
#-------------------------------------- #--------------------------------------
@ -170,9 +171,9 @@ wideP_consurf <- function(plotdf
, ligD = plotdf[[lig_dist_colname]] , ligD = plotdf[[lig_dist_colname]]
, ligDR = plotdf$lig_distR , ligDR = plotdf$lig_distR
, ligD_cols = plotdf$ligD_colours)) , ligD_cols = plotdf$ligD_colours))
} else{ } else{
plotdf = plotdf plotdf = plotdf
} }
############################################### ###############################################
# Custom 3: x-axis geom tiles ~ active sites # Custom 3: x-axis geom tiles ~ active sites
@ -182,7 +183,7 @@ wideP_consurf <- function(plotdf
# Build Data with colours # Build Data with colours
# ~ on active sites # ~ on active sites
#========================== #==========================
if(A_xvar_aa) { if(A_xvar_aa) {
cat("\nAnnotation for xvar requested. Building colours for annotation...") cat("\nAnnotation for xvar requested. Building colours for annotation...")
@ -247,11 +248,11 @@ wideP_consurf <- function(plotdf
# g = ggplot(plotdf, aes_string(x = x_factor) # g = ggplot(plotdf, aes_string(x = x_factor)
# , y = yvar_colname # , y = yvar_colname
# , colour = y_colour_factor) # , 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") # g = ggplot(plotdf, aes_string(x = "position")
# , y = "consurf_score" # , y = "consurf_score"
# , colour = "consurf_colour_rev") # , colour = "consurf_colour_rev")
@ -273,22 +274,22 @@ wideP_consurf <- function(plotdf
, ylim = c(ymin, ymax) , ylim = c(ymin, ymax)
, 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 # , labels = leg_labels
# , name = leg_title1) # , name = leg_title1)
} }
if (plot_error_bars){ if (plot_error_bars){
g0 = g0 + g0 = g0 +
geom_errorbar(aes(ymin = eval(parse(text = lower_EB_colname)) geom_errorbar(aes(ymin = eval(parse(text = lower_EB_colname))
, ymax = eval(parse(text = upper_EB_colname)) , ymax = eval(parse(text = upper_EB_colname))
)) ))
}else{ }else{
g0 = g0 g0 = g0
} }
#--------------------- #---------------------
# add axis formatting # add axis formatting
#--------------------- #---------------------
@ -298,22 +299,22 @@ wideP_consurf <- function(plotdf
, vjust = 0.4 , vjust = 0.4
, face = "bold" , face = "bold"
, colour = aa_pos_colours) , colour = aa_pos_colours)
, axis.text.y = element_text(size = y_axts , axis.text.y = element_text(size = y_axts
, angle = 0 , angle = 0
, hjust = 1 , hjust = 1
, vjust = 0) , vjust = 0)
, axis.title.x = element_text(size = x_axls) , axis.title.x = element_text(size = x_axls)
, axis.title.y = element_text(size = y_axls ) , axis.title.y = element_text(size = y_axls )
, panel.background = element_rect(fill = panel_col_fill, color = panel_col) , panel.background = element_rect(fill = panel_col_fill, color = panel_col)
, panel.grid.major = element_line(color = "black") , panel.grid.major = element_line(color = "black")
, panel.grid.minor = element_line(color = "black") , panel.grid.minor = element_line(color = "black")
, plot.title = element_text(size = pts , plot.title = element_text(size = pts
, hjust = 0.5) , hjust = 0.5)
, plot.margin = margin(t = t_margin , plot.margin = margin(t = t_margin
, r = r_margin , r = r_margin
, b = b_margin , b = b_margin
, l = l_margin , l = l_margin
, unit = unit_margin))+ , unit = unit_margin))+
guides(colour = guide_legend(title = "ConsurfXXXX")) + guides(colour = guide_legend(title = "ConsurfXXXX")) +
labs(title = ptitle labs(title = ptitle
@ -364,39 +365,39 @@ wideP_consurf <- function(plotdf
g2 = g1 + geom_tile(aes(, tpos0 g2 = g1 + geom_tile(aes(, tpos0
, width = tW0 , width = tW0
, height = tH0) , height = tH0)
, fill = plotdf$ligD_colours , fill = plotdf$ligD_colours
, colour = plotdf$ligD_colours , colour = plotdf$ligD_colours
, linetype = "blank") , linetype = "blank")
#cat("Nrows of plot df", length(plotdf$ligD_colours)) #cat("Nrows of plot df", length(plotdf$ligD_colours))
out = g2 out = g2
# #
# #------------------ # #------------------
# # Extract legend2 # # Extract legend2
# #------------------ # #------------------
# labels = seq(lig_min, lig_max, len = 5); labels # labels = seq(lig_min, lig_max, len = 5); labels
# labelsD = round(labels, digits = 0); labelsD # labelsD = round(labels, digits = 0); labelsD
# #
# g2_leg = g1 + # g2_leg = g1 +
# geom_tile(aes(fill = .data[[lig_dist_colname]]) # geom_tile(aes(fill = .data[[lig_dist_colname]])
# , colour = "white") + # , colour = "white") +
# scale_fill_gradient2(midpoint = lig_mean # scale_fill_gradient2(midpoint = lig_mean
# , low = "green" # , low = "green"
# , mid = "yellow" # , mid = "yellow"
# , high = "red" # , high = "red"
# , breaks = labels # , breaks = labels
# #, n.breaks = 11 # #, n.breaks = 11
# #, minor_breaks = c(2, 4, 6, 8, 10) # #, minor_breaks = c(2, 4, 6, 8, 10)
# , limits = c(lig_min, lig_max) # , limits = c(lig_min, lig_max)
# , labels = labelsD # , labels = labelsD
# , name = leg_title2) # , name = leg_title2)
# #
# legend2 = get_legend(g2_leg) # legend2 = get_legend(g2_leg)
# #
# }else{ # }else{
# out = g1 # out = g1
# } # }
###################################################### ######################################################
#------------------ #------------------
# Extract legend2 # Extract legend2
#------------------ #------------------
@ -404,7 +405,7 @@ wideP_consurf <- function(plotdf
labelsD = round(labels, digits = 0); labelsD labelsD = round(labels, digits = 0); labelsD
g2_leg = ggplot(plotdf, aes_string(x = sprintf("factor(%s)", xvar_colname) g2_leg = ggplot(plotdf, aes_string(x = sprintf("factor(%s)", xvar_colname)
, y = yvar_colname) , y = yvar_colname)
) + ) +
geom_tile(aes(fill = .data[[lig_dist_colname]]) geom_tile(aes(fill = .data[[lig_dist_colname]])
, colour = "white") + , colour = "white") +
scale_fill_gradient2(midpoint = lig_mean scale_fill_gradient2(midpoint = lig_mean
@ -423,7 +424,7 @@ wideP_consurf <- function(plotdf
}else{ }else{
out = g1 out = g1
} }
##################################################### #####################################################
# #============================================ # #============================================
# # x-axis: geom_tiles ~ ligand distance # # x-axis: geom_tiles ~ ligand distance
@ -445,7 +446,7 @@ wideP_consurf <- function(plotdf
# out = g1 # out = g1
# } # }
# #
############################################################################################ ############################################################################################
#============================================== #==============================================
# x-axis: geom_tiles ~ active sites and others # x-axis: geom_tiles ~ active sites and others
#============================================== #==============================================
@ -454,48 +455,53 @@ wideP_consurf <- function(plotdf
#tW = 1 #tW = 1
#tH = 0.2 #tH = 0.2
#--------------------- #---------------------
# Add2plot: 3 ligands # Add2plot: 3 ligands
#--------------------- #---------------------
if (all(!is.null(active_aa_pos) && if (all(!is.null(active_aa_pos) &&
!is.null(aa_pos_drug) && !is.null(aa_pos_drug) &&
!is.null(aa_pos_lig1) && !is.null(aa_pos_lig2) && !is.null(aa_pos_lig3))) { !is.null(aa_pos_lig1) && !is.null(aa_pos_lig2) && !is.null(aa_pos_lig3))) {
cat("\n\nAnnotating xvar with active, drug binding, and Lig 1&2&3 sites") if (debug){
cat("\nCreating column colours, column name:", aa_colour_colname3) 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 cat("\nDoing Plot with 3 ligands")
, width = tW }
, height = tH ) out = out + geom_tile(aes(,tpos3
, fill = plotdf[[aa_colour_colname3]] , width = tW
, colour = plotdf[[aa_colour_colname3]] , height = tH )
, linetype = "solid") + , fill = plotdf[[aa_colour_colname3]]
geom_tile(aes(, tpos2 , colour = plotdf[[aa_colour_colname3]]
, width = tW , linetype = "solid") +
, height = tH ) geom_tile(aes(, tpos2
, fill = plotdf[[aa_colour_colname2]] , width = tW
, colour = plotdf[[aa_colour_colname2]] , height = tH )
, linetype = "solid")+ , fill = plotdf[[aa_colour_colname2]]
, colour = plotdf[[aa_colour_colname2]]
geom_tile(aes(, tpos1 , linetype = "solid")+
, width = tW
, height = tH) geom_tile(aes(, tpos1
, fill = plotdf[[aa_colour_colname1]] , width = tW
, colour = plotdf[[aa_colour_colname1]] , height = tH)
, linetype = "solid") , fill = plotdf[[aa_colour_colname1]]
, colour = plotdf[[aa_colour_colname1]]
cat("\nDone Plot with 3 ligands") , linetype = "solid")
} if (debug){
cat("\nDone Plot with 3 ligands")
}
}
#--------------------- #---------------------
# Add2plot: 2 ligands # Add2plot: 2 ligands
#--------------------- #---------------------
if (all(!is.null(active_aa_pos) && if (all(!is.null(active_aa_pos) &&
!is.null(aa_pos_drug) && !is.null(aa_pos_drug) &&
!is.null(aa_pos_lig1) && !is.null(aa_pos_lig2) && is.null(aa_pos_lig3))) { !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("\n\nAnnotating xvar with active, drug binding, and Lig 1&2 sites")
cat("\nCreating column colours, column name:", aa_colour_colname2) cat("\nCreating column colours, column name:", aa_colour_colname2)
cat("\nDoing Plot with 2 ligands") cat("\nDoing Plot with 2 ligands")
}
out = out + out = out +
geom_tile(aes(, tpos2 geom_tile(aes(, tpos2
, width = tW , width = tW
@ -509,8 +515,9 @@ wideP_consurf <- function(plotdf
, fill = plotdf[[aa_colour_colname1]] , fill = plotdf[[aa_colour_colname1]]
, colour = plotdf[[aa_colour_colname1]] , colour = plotdf[[aa_colour_colname1]]
, linetype = "solid") , linetype = "solid")
if (debug){
cat("\nDone Plot with 2 ligands") cat("\nDone Plot with 2 ligands")
}
} }
#--------------------- #---------------------
@ -519,11 +526,12 @@ wideP_consurf <- function(plotdf
if (all(!is.null(active_aa_pos) && if (all(!is.null(active_aa_pos) &&
!is.null(aa_pos_drug) && !is.null(aa_pos_drug) &&
!is.null(aa_pos_lig1) && is.null(aa_pos_lig2) && is.null(aa_pos_lig3))) { !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("\n\nAnnotating xvar with active, drug binding, and Lig 1 sites")
cat("\nCreating column colours, column name:", aa_colour_colname1) cat("\nCreating column colours, column name:", aa_colour_colname1)
cat("\nDoing Plot with 1 ligands") cat("\nDoing Plot with 1 ligands")
}
out = out + out = out +
geom_tile(aes(, tpos1 geom_tile(aes(, tpos1
, width = tW , width = tW
@ -546,20 +554,20 @@ wideP_consurf <- function(plotdf
is.null(aa_pos_lig1) && is.null(aa_pos_lig1) &&
is.null(aa_pos_lig2) && is.null(aa_pos_lig2) &&
is.null(aa_pos_lig3))) { is.null(aa_pos_lig3))) {
if (debug){
cat("\n\nAnnotating xvar with active and drug binding sites") cat("\n\nAnnotating xvar with active and drug binding sites")
cat("\nCreating column colours, column name:", aa_colour_colname) cat("\nCreating column colours, column name:", aa_colour_colname)
cat("\nDoing Plot with 0 ligands: active and drug site only") cat("\nDoing Plot with 0 ligands: active and drug site only")
}
out = out + geom_tile(aes(, tpos3 out = out + geom_tile(aes(, tpos3
, width = tW , width = tW
, height = tH) , height = tH)
, fill = plotdf[[aa_colour_colname]] , fill = plotdf[[aa_colour_colname]]
, colour = plotdf[[aa_colour_colname]] , colour = plotdf[[aa_colour_colname]]
, linetype = "solid") , linetype = "solid")
if (debug){
cat("\nDone Plot with: Active and Drug sites") cat("\nDone Plot with: Active and Drug sites")
}
} }
}else{ }else{
cat("\nNo annotation for additional ligands on xvar requested") cat("\nNo annotation for additional ligands on xvar requested")
@ -567,23 +575,23 @@ wideP_consurf <- function(plotdf
#============================================== #==============================================
if (A_xvar_lig){ if (A_xvar_lig){
legs = cowplot::plot_grid(legend1 legs = cowplot::plot_grid(legend1
, legend2 , legend2
, ncol = 1 , ncol = 1
, align = "hv" , align = "hv"
, rel_heights = c(2/4,3/4)) , rel_heights = c(2/4,3/4))
out2 = cowplot::plot_grid( out + theme(legend.position = "none") out2 = cowplot::plot_grid( out + theme(legend.position = "none")
, legs , legs
, ncol = 2 , ncol = 2
, align = "hv" , align = "hv"
, rel_widths = c(9/10, 0.4/10) , rel_widths = c(9/10, 0.4/10)
) )
}else{ }else{
out2 = cowplot::plot_grid( out + theme(legend.position = "none") out2 = cowplot::plot_grid( out + theme(legend.position = "none")
, legend1 , legend1
, ncol = 2 , ncol = 2
, align = "hv" , align = "hv"
, rel_widths = c(9/10, 0.5/10) , rel_widths = c(9/10, 0.5/10)
) )
} }
#============================================== #==============================================
@ -609,9 +617,9 @@ wideP_consurf <- function(plotdf
# ) # )
# } # }
#============================================== #==============================================
return(out2) return(out2)
#return(out2) #return(out2)
} }
############################################################# #############################################################

View file

@ -117,14 +117,16 @@ LogoPlotSnps <- function(plot_df
if (is.matrix(tab_mt)){ if (is.matrix(tab_mt)){
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)){
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)
)
} }
} }
@ -151,8 +153,9 @@ LogoPlotSnps <- function(plot_df
, "\nDim of wt matrix:", dim(tab_wt) , "\nDim of wt matrix:", dim(tab_wt)
, "\nDim of mutant matrix:", dim(tab_mt) , "\nDim of mutant matrix:", dim(tab_mt)
, "\n" , "\n"
, "\nRownames of mutant matrix:", rownames(tab_wt) #, "\nRownames of mutant matrix:", rownames(tab_wt)
, "\nColnames of mutant matrix:", colnames(tab_wt)) #, "\nColnames of mutant matrix:", colnames(tab_wt)
)
} }
###################################### ######################################