From 0cc7a8fcaee16c8aec3f419ab3334f329188eb25 Mon Sep 17 00:00:00 2001 From: Tanushree Tunstall Date: Mon, 15 Aug 2022 16:29:38 +0100 Subject: [PATCH] config: add tile stuff for all targets. Other functions: many rewrites! --- config/alr.R | 2 + config/embb.R | 6 +- config/gid.R | 5 +- config/katg.R | 3 + config/pnca.R | 5 +- config/rpob.R | 3 + scripts/functions/bp_subcolours.R | 49 +++---- .../functions/generate_distance_colour_map.R | 25 ++-- scripts/functions/logoP_or.R | 77 +++-------- scripts/functions/logoP_snp.R | 59 ++++---- scripts/functions/position_annotation.R | 129 +++++++++--------- 11 files changed, 163 insertions(+), 200 deletions(-) diff --git a/config/alr.R b/config/alr.R index 8ef70d8..2e3c04a 100644 --- a/config/alr.R +++ b/config/alr.R @@ -107,4 +107,6 @@ if ( all(c1, c2, c3) ) { aa_pos_lig1 = NULL aa_pos_lig2 = NULL aa_pos_lig3 = NULL +tile_map=data.frame(tile=c("ALR","DSL","CDL","Ca"), + tile_colour=c("green","darkslategrey","navyblue","purple")) diff --git a/config/embb.R b/config/embb.R index 8a68343..965b857 100644 --- a/config/embb.R +++ b/config/embb.R @@ -115,4 +115,8 @@ cat("\n===================================================" aa_pos_lig1 = aa_pos_dsl #slategray aa_pos_lig2 = aa_pos_cdl #navy blue -aa_pos_lig3 = aa_pos_ca #purple \ No newline at end of file +aa_pos_lig3 = aa_pos_ca #purple + +tile_map=data.frame(tile=c("EMB","DSL","CDL","Ca"), + tile_colour=c("green","darkslategrey","navyblue","purple")) + diff --git a/config/gid.R b/config/gid.R index 0f04044..18c6328 100644 --- a/config/gid.R +++ b/config/gid.R @@ -131,4 +131,7 @@ cat("\n===================================================" # var for position customisation for plots aa_pos_lig1 = aa_pos_rna aa_pos_lig2 = aa_pos_sam -aa_pos_lig3 = aa_pos_amp \ No newline at end of file +aa_pos_lig3 = aa_pos_amp +tile_map=data.frame(tile=c("GID","DSL","CDL","Ca"), + tile_colour=c("green","darkslategrey","navyblue","purple")) + diff --git a/config/katg.R b/config/katg.R index f2fade6..5cee244 100644 --- a/config/katg.R +++ b/config/katg.R @@ -106,3 +106,6 @@ cat("\n===================================================" aa_pos_lig1 = aa_pos_hem aa_pos_lig2 = NULL aa_pos_lig3 = NULL +tile_map=data.frame(tile=c("KAT","DSL","CDL","Ca"), + tile_colour=c("green","darkslategrey","navyblue","purple")) + diff --git a/config/pnca.R b/config/pnca.R index 2ac7a36..1af54e2 100644 --- a/config/pnca.R +++ b/config/pnca.R @@ -53,4 +53,7 @@ cat("\n===================================================" # var for position customisation for plots aa_pos_lig1 = aa_pos_fe #aa_pos_lig2 = aa_pos_catalytic -#aa_pos_lig3 = aa_pos_hbond \ No newline at end of file +#aa_pos_lig3 = aa_pos_hbond +tile_map=data.frame(tile=c("PZA","DSL","CDL","Ca"), + tile_colour=c("green","darkslategrey","navyblue","purple")) + diff --git a/config/rpob.R b/config/rpob.R index 14a1bca..b5478ab 100644 --- a/config/rpob.R +++ b/config/rpob.R @@ -59,3 +59,6 @@ cat("\n===================================================" aa_pos_lig1 = NULL aa_pos_lig2 = NULL aa_pos_lig3 = NULL +tile_map=data.frame(tile=c("RPO","DSL","CDL","Ca"), + tile_colour=c("green","darkslategrey","navyblue","purple")) + diff --git a/scripts/functions/bp_subcolours.R b/scripts/functions/bp_subcolours.R index 98f39d5..836fc2c 100755 --- a/scripts/functions/bp_subcolours.R +++ b/scripts/functions/bp_subcolours.R @@ -60,17 +60,9 @@ bp_stability_hmap <- function(plot_df = merged_df3 ) { - ################################################ # Custom 2: x-axis geom tiles ~ lig distance - ################################################ - - #========================= - # Build data with colours - # ~ ligand distance - #========================= - # plot_df = generate_distance_colour_map(plot_df, debug=TRUE) - - # order the df by position and ensure it is a factor + + # order the df by position and ensure it is a factor plot_df = plot_df[order(plot_df[[xvar_colname]]), ] plot_df[[xvar_colname]] = factor(plot_df[[xvar_colname]]) @@ -79,26 +71,30 @@ bp_stability_hmap <- function(plot_df = merged_df3 # stability values isolated to help with generating column called: 'group' my_grp = plot_df[[stability_colname]] - cat( "\nLength of nsSNPs:", length(my_grp) - , "\nLength of unique values for nsSNPs:", length(unique(my_grp)) ) - + # cat( "\nLength of nsSNPs:", length(my_grp) + # , "\nLength of unique values for nsSNPs:", length(unique(my_grp)) ) + # # Add col: 'group' plot_df$group = paste0(plot_df[[stability_outcome_colname]], "_", my_grp, sep = "") # check unique values in normalised data - cat("\nNo. of unique values in", stability_colname, "no rounding:" - , length(unique(plot_df[[stability_colname]]))) + # cat("\nNo. of unique values in", stability_colname, "no rounding:" + # , length(unique(plot_df[[stability_colname]]))) # Call the function to create the palette based on the group defined above #subcols_ps subcols_bp_hmap = ColourPalleteMulti(plot_df, stability_outcome_colname, stability_colname) cat("\nNo. of sub colours generated:", length(subcols_bp_hmap)) + anno_bar=position_annotation(plot_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 the subcols barplot - #------------------------------- cowplot::plot_grid( ggplot(plot_df, aes_string(x = xvar_colname # , ordered = T) @@ -111,7 +107,8 @@ bp_stability_hmap <- function(plot_df = merged_df3 # scale_x_discrete("Position", labels=factor(plot_df$position)) + theme( - axis.text.x = element_text(size = my_xaxls + panel.grid = element_line(color="lightgrey", size=0.125) + , axis.text.x = element_text(size = my_xaxls , angle = 90 , hjust = 1 , vjust = 0.4) @@ -128,25 +125,17 @@ bp_stability_hmap <- function(plot_df = merged_df3 # , panel.grid = element_blank() , panel.background = element_rect(fill = "transparent", colour=NA) ) + - labs(title = p_title , x = my_xlab , y = my_ylab), NULL, - position_annotation(plot_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 - ) - , + anno_bar, #generate_distance_legend(plot_df), ncol = 1, align = "v", - rel_heights = c(10,-0.1,1) + rel_heights = c(6,-0.1,1) #rel_widths = c(9/10, 0.4/10) ) } -#bp_stability_hmap(merged_df3) +# bp_stability_hmap(small_df3) diff --git a/scripts/functions/generate_distance_colour_map.R b/scripts/functions/generate_distance_colour_map.R index e21f476..167de44 100644 --- a/scripts/functions/generate_distance_colour_map.R +++ b/scripts/functions/generate_distance_colour_map.R @@ -1,9 +1,9 @@ # takes a dataframe and returns the same dataframe with two extra columns for colours and position +library('viridis') + generate_distance_colour_map = function(plot_df, xvar_colname = "position", lig_dist_colname = "ligand_distance", - #lig_dist_colours = c("green", "yellow", "orange", "red"), - lig_dist_colours = c("green", "yellow", "magenta"), debug = TRUE ) { @@ -16,12 +16,9 @@ generate_distance_colour_map = function(plot_df, 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 - 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 + n_colours = length(sort(unique(round(plot_df[[lig_dist_colname]], digits = 0)))) + lig_cols = magma(n_colours, direction=-1) + ligD_valsR = sort(unique(round(plot_df[[lig_dist_colname]], digits = 0))) if (debug) { length(ligD_valsR) @@ -43,15 +40,9 @@ generate_distance_colour_map = function(plot_df, 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)) return(plot_df) } @@ -75,9 +66,9 @@ generate_distance_legend = function(plot_df, geom_tile(aes(fill = .data[[lig_dist_colname]]) , colour = "white") + scale_fill_gradient2(midpoint = lig_mean - , low = "green" - , mid = "yellow" - , high = "magenta" + , low = magma(3, direction=-1)[1] + , mid = magma(3, direction=-1)[2] + , high = magma(3, direction=-1)[3] , breaks = labels , limits = c(lig_min, lig_max) , labels = labelsD diff --git a/scripts/functions/logoP_or.R b/scripts/functions/logoP_or.R index d3d75ca..a73db03 100644 --- a/scripts/functions/logoP_or.R +++ b/scripts/functions/logoP_or.R @@ -49,16 +49,16 @@ LogoPlotCustomH <- function(plot_df , y_axis_increment = 50 , x_lab = "Position" , y_lab = "Odds Ratio" - , x_ats = 12 # text size + , x_ats = 6 # text size , x_tangle = 90 # text angle - , y_ats = 22 + , y_ats = 11 , y_tangle = 0 - , x_tts = 20 # title size - , y_tts = 23 + , x_tts = 10 # title size + , y_tts = 11 , leg_pos = "none" # can be top, left, right and bottom or c(0.8, 0.9) , leg_dir = "horizontal" #can be vertical or horizontal - , leg_ts = 15 # leg text size - , leg_tts = 16 # leg title size + , leg_ts = 7 # leg text size + , leg_tts = 8 # leg title size , tpos0 = 0 # 0 is a magic number that does my sensible default , tW0 = 1 , tH0 = 0.3 @@ -68,16 +68,16 @@ LogoPlotCustomH <- function(plot_df ################################# # Data processing for logo plot ################################# - plot_df = generate_distance_colour_map(plot_df, debug=TRUE) - - unique_colour_map = unique(plot_df[,c("position","ligD_colours")]) - unique_colour_map = unique_colour_map[order(unique_colour_map$position), ] - rownames(unique_colour_map) = unique_colour_map$position - - unique_colour_map2 = unique_colour_map - unique_colour_map2$position=as.factor(unique_colour_map2$position) - unique_colour_map2$ligD_colours = as.factor(unique_colour_map2$ligD_colours) + # plot_df = generate_distance_colour_map(plot_df, debug=TRUE) + # unique_colour_map = unique(plot_df[,c("position","ligD_colours")]) + # unique_colour_map = unique_colour_map[order(unique_colour_map$position), ] + # rownames(unique_colour_map) = unique_colour_map$position + # + # unique_colour_map2 = unique_colour_map + # unique_colour_map2$position=as.factor(unique_colour_map2$position) + # unique_colour_map2$ligD_colours = as.factor(unique_colour_map2$ligD_colours) + # if (rm_empty_y){ @@ -147,7 +147,7 @@ LogoPlotCustomH <- function(plot_df cat('\nSelected colour scheme:', my_logo_col , "\nUsing grey theme") - theme_bgc = "grey" + theme_bgc = "white" xfont_bgc = "black" yfont_bgc = "black" xtt_col = "black" @@ -179,7 +179,7 @@ LogoPlotCustomH <- function(plot_df , angle = x_tangle , colour = xfont_bgc , vjust = 0.4 - , margin = margin(t=-8,r=0,b=0,l=0, unit="mm") + , margin = margin(t=0,r=0,b=0,l=0, unit="mm") ) , axis.text.y = element_text(size = y_ats , angle = y_tangle @@ -211,47 +211,6 @@ LogoPlotCustomH <- function(plot_df #, labels = seq(0, (y_lim), by = y_axis_increment) , limits = c(0, y_lim)) + labs(y=y_lab), - NULL, - ggplot(data=unique_colour_map2, aes( - x=factor(position), 0 # heat-mapped distance tiles along the bot - , fill = position - , colour = position - , linetype = "blank" - ) - ) + - geom_tile() + - theme( - axis.text.x = element_blank() - # axis.text.x = element_text(size = x_ats - # , angle = x_tangle - # , hjust = 1 - # , vjust = 0.4 - # , colour = xfont_bgc) - , axis.text.y = element_blank() - , axis.ticks.y = element_blank() - , axis.ticks.x = element_blank() - , axis.title.x = element_blank() - , axis.title.y = element_text(size = y_tts - , colour = ytt_col) - , legend.title = element_text(size = leg_tts - , colour = ytt_col) - , legend.text = element_text(size = leg_ts) - - , legend.position = leg_pos - , legend.direction = leg_dir - #, plot.background = element_blank() - , plot.margin = margin(t=0,b=1) - , panel.grid=element_blank() - , plot.background = element_rect(fill = theme_bgc, colour=NA) - , panel.background = element_rect(fill = "transparent", colour=NA) - #, panel.background = element_rect(fill = theme_bgc) - #, panel.background = element_blank() - ) + - #scale_x_discrete(x_lab, labels=factor(unique_colour_map$position)) + - scale_color_manual(values=unique_colour_map$ligD_colours) + - scale_fill_manual(values=unique_colour_map$ligD_colours) + - labs(y = NULL), - NULL, position_annotation(plot_df, bg = theme_bgc, aa_pos_drug=aa_pos_drug, @@ -261,7 +220,7 @@ LogoPlotCustomH <- function(plot_df aa_pos_lig3=aa_pos_lig3 ), - ncol=1, align='v', rel_heights = c(16,0,1,0,1) + ncol=1, align='v', rel_heights = c(6,1) ) } #LogoPlotCustomH(small_df3) diff --git a/scripts/functions/logoP_snp.R b/scripts/functions/logoP_snp.R index 314c9fd..808c211 100644 --- a/scripts/functions/logoP_snp.R +++ b/scripts/functions/logoP_snp.R @@ -208,25 +208,20 @@ LogoPlotSnps <- function(plot_df xtt_col = "black" ytt_col = "black" } - + position_mt = as.numeric(colnames(tab_mt)) + position_wt = as.numeric(colnames(tab_wt)) ##################################### # Generating logo plots for nsSNPs ##################################### #------------------- # Mutant logo plot #------------------- - logo_top =ggseqlogo(tab_mt - , method = 'custom' - , col_scheme = my_logo_col - , seq_type = 'aa') + - - scale_x_continuous(breaks = 1:ncol(tab_mt) - , expand = c(0.01,0) - , labels = colnames(tab_mt))+ - - scale_y_continuous(breaks = 0:(max_mult_mut-1) - , labels = c(1:max_mult_mut) - , limits = c(0, max_mult_mut)) + + logo_top = ggplot() + + geom_logo(tab_mt + , method = 'custom' + , col_scheme = my_logo_col + , seq_type = 'aa') + + theme_nothing() + ylab(y_lab) + theme(text=element_text(family="FreeSans") , legend.position = leg_pos @@ -239,7 +234,9 @@ LogoPlotSnps <- function(plot_df , angle = x_tangle #, hjust = 1 #, vjust = 0.4 - , colour = xfont_bgc) + , colour = xfont_bgc + #, margin = margin(t = 0.1) + ) , axis.text.y = element_text(size = y_ats , angle = y_tangle , hjust = 1 @@ -249,17 +246,25 @@ LogoPlotSnps <- function(plot_df # , colour = xtt_col) , axis.title.x = element_blank() , axis.title.y = element_text(size = y_tts + , angle = 90 , colour = ytt_col) , plot.background = element_rect(fill = theme_bgc, colour=NA) + ) + + scale_x_discrete("Position" + , labels = position_mt + , limits = factor(1:length(position_mt)) ) - logo_bottom = ggseqlogo(tab_wt - , method = 'custom' - , col_scheme = my_logo_col - , seq_type = 'aa') + - scale_x_continuous(breaks = 1:ncol(tab_wt) - , expand = c(0.01,0) - , labels = as.factor(colnames(tab_wt))) + + + logo_bottom = ggplot() + + geom_logo(tab_wt + , method = 'custom' + , col_scheme = my_logo_col + , seq_type = 'aa') + + theme_nothing() + + scale_x_discrete("Position" + , labels = x_axis_colname + , limits = factor(1:length(x_axis_colname))) + theme(text = element_text(family="FreeSans") , legend.position = "none" , axis.text.x = element_blank() @@ -278,13 +283,13 @@ LogoPlotSnps <- function(plot_df aa_pos_lig2=aa_pos_lig2, aa_pos_lig3=aa_pos_lig3) - aligned=align_plots(logo_top, logo_bottom, anno_bar, align='v', axis='lr') + #aligned=align_plots(logo_top, logo_bottom, anno_bar, align='vh', axis='lr') cowplot::plot_grid( - aligned[[1]], aligned[[2]], aligned[[3]], + logo_top, logo_bottom, anno_bar, + #aligned[[1]], aligned[[2]], aligned[[3]], ncol=1, - #align = "v", - rel_heights = c(7, 1,1), - rel_widths = c(1,1,0.75) + align = "v", + rel_heights = c(7, 1,1) ) # cowplot::plot_grid( @@ -303,4 +308,4 @@ LogoPlotSnps <- function(plot_df #------------------ } -#LogoPlotSnps(mutable_df3) +#LogoPlotSnps(small_df3) diff --git a/scripts/functions/position_annotation.R b/scripts/functions/position_annotation.R index 34f5579..ea69746 100644 --- a/scripts/functions/position_annotation.R +++ b/scripts/functions/position_annotation.R @@ -49,77 +49,78 @@ position_annotation=function(plot_df, plot_df = generate_distance_colour_map(plot_df, debug=TRUE) - cowplot::plot_grid( - ggplot(plot_df, - aes(x=factor(position), # THIS STUPID FUCKING FACTOR THING - ) + heat_bar = ggplot(plot_df, + aes(x=factor(position)) # THIS STUPID FUCKING FACTOR THING + ) + + geom_tile(aes(y=0), + fill=plot_df$ligD_colours) + + # 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) #end of distance-heat-bar + #NULL, + pos_tiles = ggplot(plot_df, + aes(x=factor(position), # THIS STUPID FUCKING FACTOR THING + ) + ) + + # geom_tile(aes(y = 0, fill = col_aa, colour = col_aa) + # ) + + geom_tile(aes(y = 1, fill = bg_all, colour = bg_all) ) + - geom_tile(aes(y=0), - fill=plot_df$ligD_colours) + - #scale_x_discrete("Position", labels=factor(plot_df$position)) + - #theme_nothing() + - theme(plot.background = element_rect(fill = "transparent", colour=NA), - plot.margin = margin(t=0,b=0), - axis.ticks.x = element_blank(), - axis.ticks.y = element_blank(), - axis.text.y = element_blank(), - panel.grid = element_blank(), - panel.background = element_rect(fill = "transparent", colour=NA), - ) + - labs(x = NULL, y = NULL), #end of distance-heat-bar - #NULL, - ggplot(plot_df, - aes(x=factor(position), # THIS STUPID FUCKING FACTOR THING - #reorder(ligand_distance) - ) + geom_tile(aes(y = 2, fill = col_bg1, colour = col_bg1) ) + - # geom_tile(aes(y = 0, fill = col_aa, colour = col_aa) - # ) + - geom_tile(aes(y = 1, fill = bg_all, colour = bg_all) - ) + - geom_tile(aes(y = 2, fill = col_bg1, colour = col_bg1) - ) + - geom_tile(aes(y = 3, fill = col_bg2, colour = col_bg2) - ) + - geom_tile(aes(y = 4, fill = col_bg3, colour = col_bg3) - ) + - - #scale_x_discrete("Position", labels=factor(plot_df$position)) + - scale_color_manual(values = c( - "brown"="brown", - "green"="#00ff00", - "transparent"="transparent", - "slategrey"="#2f4f4f", - "navyblue"="#000080", - "purple"="#a020f0" - ), - expand=c(0,0) - ) + - scale_fill_manual(values = c( - "brown"="brown", - "green"="#00ff00", - "transparent"="transparent", - "slategrey"="#2f4f4f", - "navyblue"="#000080", - "purple"="#a020f0" - ), - expand=c(0,0) - ) + - #scale_x_continuous(expand=c(0,0)) + - #scale_y_continuous(expand=c(0,0)) + - theme_nothing() + - - theme(plot.background = element_rect(fill = bg, colour=NA), - plot.margin = margin(t=0,b=0)) + - labs(x = NULL, y = NULL), + geom_tile(aes(y = 3, fill = col_bg2, colour = col_bg2) + ) + + geom_tile(aes(y = 4, fill = col_bg3, colour = col_bg3) + ) + + + #scale_x_discrete("Position", labels=factor(plot_df$position)) + + scale_color_manual(values = c( + "brown"="brown", + "green"="#00ff00", + "transparent"="transparent", + "slategrey"="#2f4f4f", + "navyblue"="#000080", + "purple"="#a020f0" + ), + #expand=c(0,0) + ) + + scale_fill_manual(values = c( + "brown"="brown", + "green"="#00ff00", + "transparent"="transparent", + "slategrey"="#2f4f4f", + "navyblue"="#000080", + "purple"="#a020f0" + ), + #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) + + 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, - 1) + -0.1, + 2) ) + + return(out_plot) } -# position_annotation(merged_df3, +# position_annotation(small_df3, # aa_pos_drug=aa_pos_drug, # active_aa_pos=active_aa_pos, # aa_pos_lig1=aa_pos_lig1,