# Input: # Data: # plot_df: merged_df3 containing the OR column to use as y-axis or any other relevant column # x_axis_colname = "position" # y_axis_colname = "or_mychisq" # symbol_colname = "mutant_type" # y_axis_log = F # log_value = log10 # if used, y-axis label has "Log" appended to it # my_logo_col = c("chemistry", "hydrophobicity", "clustalx", "taylor") # --> if clustalx and taylor, set variable to black bg + white font # --> if chemistry and hydrophobicity, then grey bg + black font # rm_empty_y = F # option to remove empty positions i.e positions with no assocaited y-val # y_axis_log = F # option to use log scale # FIXME Minor bug: if used with rm_empty_y, sometimes the labels are too small to render(!?) # so positions appear empty despite having y-vals # ...other params # Returns: Logo plot from combined data containing specific y-value such as OR, etc by position. # TODO: SHINY # select/drop down option to remove empty positions # select/drop down option for colour # select/drop down option for log scale # include WT # Make it hover over position and then get the corresponding data table! ########################a########################################################### #================== # logo data: OR #================== LogoPlotCustomH <- function(plot_df , x_axis_colname = "position" , y_axis_colname = "or_mychisq" , symbol_colname = "mutant_type" , my_logo_col = "chemistry" , rm_empty_y = F , y_axis_log = F , log_value = log10 , y_axis_increment = 50 , x_lab = "Position" , y_lab = "Odds Ratio" , x_ats = 12 # text size , x_tangle = 90 # text angle , y_ats = 22 , y_tangle = 0 , x_tts = 20 # title size , y_tts = 23 , 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 , tpos0 = 0 # 0 is a magic number that does my sensible default , tW0 = 1 , tH0 = 0.3 ) { ################################# # 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) if (rm_empty_y){ plot_df = plot_df[!is.na(plot_df[y_axis_colname]),] cat("\nRemoving empty positions...\n") } y_max = max(plot_df[[y_axis_colname]], na.rm = T) cat("\nRemoving y scale incremenet:", y_axis_increment) y_lim = round_any(y_max, y_axis_increment, f = ceiling) #------------------- # logo data: LogOR #------------------- if (y_axis_log){ log_colname = paste0("log_", y_axis_colname) plot_df[log_colname] = log_value(plot_df[y_axis_colname]) logo_df = plot_df[, c(x_axis_colname, symbol_colname, log_colname)] logo_df_plot = logo_df[, c(x_axis_colname, symbol_colname, log_colname)] logo_dfP_wf = as.matrix(logo_df_plot %>% spread(x_axis_colname, log_colname, fill = 0.0)) #!!! For consideration: to add y_axis 'breaks' and 'limits' !!! #y_max = max(plot_df[[log_colname]], na.rm = T) #y_axis_increment = #cat("\nRemoving y scale incremenet:", y_axis_increment) #y_lim = round_any(y_max, y_axis_increment, f = ceiling) } else { #------------------- # logo data: OR #------------------- logo_df = plot_df[, c(x_axis_colname, symbol_colname, y_axis_colname)] logo_df_plot = logo_df[, c(x_axis_colname, symbol_colname, y_axis_colname)] logo_dfP_wf = as.matrix(logo_df_plot %>% spread(x_axis_colname, y_axis_colname, fill = 0.0)) } #class(logo_dfP_wf) rownames(logo_dfP_wf) = logo_dfP_wf[,1] #dim(logo_dfP_wf) logo_dfP_wf = logo_dfP_wf[,-1] #str(logo_dfP_wf) #colnames(logo_dfP_wf) position_or = as.numeric(colnames(logo_dfP_wf)) ###################################### # Generating plots with given y_axis ##################################### if (my_logo_col %in% c('clustalx','taylor')) { cat("\nSelected colour scheme:", my_logo_col , "\nUsing black theme\n") theme_bgc = "black" xfont_bgc = "white" yfont_bgc = "white" xtt_col = "white" ytt_col = "white" } if (my_logo_col %in% c('chemistry', 'hydrophobicity')) { cat('\nSelected colour scheme:', my_logo_col , "\nUsing grey theme") theme_bgc = "grey" xfont_bgc = "black" yfont_bgc = "black" xtt_col = "black" ytt_col = "black" } if (y_axis_log){ if (grepl("Log", y_lab)){ y_lab = y_lab }else{ y_lab = paste("Log", y_lab) } } plot_grid( ggplot() + geom_logo(logo_dfP_wf , method = "custom" , col_scheme = my_logo_col , seq_type = "aa") + #ylab("my custom height") + theme( axis.ticks.x = element_blank() , axis.ticks.length = unit(0, "pt") , axis.title.x = element_blank() # , axis.text.x = element_blank() # turn this off and the below on if you want to visually # verify positions. , axis.text.x = element_text(size = x_ats , angle = x_tangle , colour = xfont_bgc , vjust = 0.4 , margin = margin(t=-8,r=0,b=0,l=0, unit="mm") ) , axis.text.y = element_text(size = y_ats , angle = y_tangle , colour = yfont_bgc) , 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.text = element_blank() , legend.position = leg_pos , legend.direction = leg_dir #, plot.background = element_blank() , plot.margin = margin(b=0) , panel.grid=element_blank() , plot.background = element_rect(fill = theme_bgc, colour=NA) , panel.background = element_rect(fill = "transparent", colour=NA) )+ scale_x_discrete(x_lab #, breaks , labels = position_or , limits = factor(1:length(position_or))) + scale_y_continuous(y_lab , breaks = seq(0, (y_lim), by = y_axis_increment) #, labels = seq(0, (y_lim), by = y_axis_increment) , limits = c(0, y_lim)) + labs(y=y_lab), 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=0) , 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), ncol=1, align='v', rel_heights = c(16,1,-0.1, 1) ) } #LogoPlotCustomH(small_df3)