diff --git a/scripts/functions/generate_distance_colour_map.R b/scripts/functions/generate_distance_colour_map.R index bf5974a..8110676 100644 --- a/scripts/functions/generate_distance_colour_map.R +++ b/scripts/functions/generate_distance_colour_map.R @@ -1,64 +1,29 @@ # takes a dataframe and returns the same dataframe with two extra columns for colours and position -generate_distance_colour_map = function(plotdf, +generate_distance_colour_map = function(plot_df, 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 + debug = TRUE ) { - #------------------- - # 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 + plot_df['lig_distR'] = round(plot_df[[lig_dist_colname]], digits = 0) + + 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(plotdf[[lig_dist_colname]], digits = 0)))); n_colours + 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(plotdf[[lig_dist_colname]], digits = 0))); ligD_valsR - length(ligD_valsR) + ligD_valsR = sort(unique(round(plot_df[[lig_dist_colname]], digits = 0))); ligD_valsR if (debug) { + length(ligD_valsR) if (n_colours == length(ligD_valsR)) { cat("\nStarting: mapping b/w" , lig_dist_colname @@ -73,49 +38,49 @@ generate_distance_colour_map = function(plotdf, ligDcolKey <- data.frame(ligD_colours = lig_cols , lig_distR = ligD_valsR); ligDcolKey - names(ligDcolKey) if (debug) { + names(ligDcolKey) cat("\nSuccessful: Mapping b/w", lig_dist_colname, "and colours") } #------------------------------------- - # merge colour key with plotdf + # merge colour key with plot_df #-------------------------------------- - plotdf = merge(plotdf, ligDcolKey, by = 'lig_distR') + plot_df = merge(plot_df, 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) + # 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) } -generate_distance_legend = function(plotdf, +generate_distance_legend = function(plot_df, 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 + 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 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) - ) + get_legend( + ggplot(plot_df, aes_string(x = sprintf("factor(%s)", xvar_colname), y=0)) + + + 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) + ) } diff --git a/scripts/functions/lf_bp2.R b/scripts/functions/lf_bp2.R index 3a49621..630f8b8 100644 --- a/scripts/functions/lf_bp2.R +++ b/scripts/functions/lf_bp2.R @@ -54,6 +54,7 @@ lf_bp2 <- function(lf_df = lf_duet }else{ bp_width = bp_width } + my_comparisonsL <- list( stat_grp_comp ) ggplot(lf_df, aes_string(x = x_grp, y = y_var)) + @@ -93,39 +94,11 @@ lf_bp2 <- function(lf_df = lf_duet , x = "" , y = "") + - if (add_stats){ - my_comparisonsL <- list( stat_grp_comp ) - + stat_compare_means(comparisons = my_comparisonsL , method = stat_method , paired = my_paired - , label = stat_label[2]) - - - if (make_boxplot){ - geom_boxplot(fill = "white" - , outlier.colour = NA - #, position = position_dodge(width = 0.9) - , width = bp_width) + - - geom_quasirandom(#priority = "density" - #, shape = 21 - size = dot_size - , alpha = dot_transparency - , show.legend = FALSE - , cex = 0.8 - , aes( - colour = factor( - eval( - parse( - text = colour_categ - ) - ) - ) - ) - ) - - } else { + , label = stat_label[2]) + geom_quasirandom( size = dot_size , alpha = dot_transparency @@ -142,8 +115,6 @@ lf_bp2 <- function(lf_df = lf_duet ) ) ) - } - } } #lf_bp2(lf_consurf) \ No newline at end of file diff --git a/scripts/functions/logoP_or.R b/scripts/functions/logoP_or.R index d3a3498..278b48b 100644 --- a/scripts/functions/logoP_or.R +++ b/scripts/functions/logoP_or.R @@ -46,7 +46,7 @@ LogoPlotCustomH <- function(plot_df , rm_empty_y = F , y_axis_log = F , log_value = log10 - , y_axis_increment = 5 + , y_axis_increment = 50 , x_lab = "Position" , y_lab = "Odds Ratio" , x_ats = 12 # text size @@ -68,16 +68,24 @@ LogoPlotCustomH <- function(plot_df ################################# # Data processing for logo plot ################################# - plot_df = generate_distance_colour_map(plot_df, yvar_colname = y_axis_colname, debug=TRUE) + 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") - }else{ - plot_df = plot_df } - y_max = max(plot_df['or_mychisq'], na.rm = T) + 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) @@ -155,50 +163,90 @@ LogoPlotCustomH <- function(plot_df y_lab = paste("Log", y_lab) } } - ggseqlogo(logo_dfP_wf - , method = "custom" - , col_scheme = my_logo_col - , seq_type = "aa") + - #ylab("my custom height") + - theme(axis.text.x = element_text(size = x_ats - , angle = x_tangle - , hjust = 1 - , vjust = 0.4 - , colour = xfont_bgc) - , axis.text.y = element_text(size = y_ats - , angle = y_tangle + 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.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 + # , hjust = 1 + # , vjust = 0.4 + # , colour = xfont_bgc + # , margin = margin(b=0) + # ) + , axis.text.y = element_text(size = y_ats + , angle = y_tangle + , hjust = 1 + , vjust = 0 + , 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_rect(fill = theme_bgc) + , plot.margin = margin(b=0) + , panel.grid=element_blank() + , panel.background = element_rect(fill = theme_bgc) + + )+ + + 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_text(size = x_ats + , angle = x_tangle , hjust = 1 - , vjust = 0 - , colour = yfont_bgc) - , axis.title.x = element_text(size = x_tts - , colour = xtt_col) - , 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_rect(fill = theme_bgc))+ - - 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)) + - ylab(y_lab) + - geom_tile(aes(plot_df$position, tpos0 # heat-mapped distance tiles along the bot - , width = tW0 - , height = tH0) - , fill = plot_df$ligD_colours - , colour = plot_df$ligD_colours - , linetype = "blank") - #LogoPlot = p0 + ylab(y_lab) - #return(LogoPlot) - -} + , vjust = 0.4 + , colour = xfont_bgc) + , axis.text.y = element_blank() + , axis.ticks.y = element_blank() + , axis.title.x = element_text(size = x_tts + , colour = xtt_col) + , 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_rect(fill = theme_bgc) + , plot.margin = margin(t=0) + , panel.grid=element_blank() + , panel.background = element_rect(fill = theme_bgc) + ) + + 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), + ncol=1, align='v', rel_heights = c(8/10,1/10) + ) + } +#LogoPlotCustomH(merged_df3) \ No newline at end of file diff --git a/scripts/functions/logoP_snp.R b/scripts/functions/logoP_snp.R index d48df52..6222b70 100644 --- a/scripts/functions/logoP_snp.R +++ b/scripts/functions/logoP_snp.R @@ -33,7 +33,7 @@ LogoPlotSnps <- function(plot_df , x_axis_colname = "position" , symbol_mut_colname = "mutant_type" - , symbol_wt_colname = "mutant_type" + , symbol_wt_colname = "wild_type" , omit_snp_count = c(0) # can be 1, 2, etc. , my_logo_col = "chemistry" , x_lab = "Position" @@ -66,11 +66,18 @@ LogoPlotSnps <- function(plot_df # Generate "ligand distance" colour map 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) + # 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$mut_pos_occurrence) + table(plot_df[[x_axis_colname]]) + table(plot_df$mut_pos_occurrence) } max_mut = max(table(plot_df[[x_axis_colname]])) @@ -200,7 +207,7 @@ LogoPlotSnps <- function(plot_df xtt_col = "black" ytt_col = "black" } - + ##################################### # Generating logo plots for nsSNPs ##################################### @@ -212,11 +219,7 @@ LogoPlotSnps <- function(plot_df , method = 'custom' , col_scheme = my_logo_col , seq_type = 'aa') + - - theme(text=element_text(family="FreeSans"))+ - theme(axis.text.x = element_blank()) + - theme_logo()+ - + scale_x_continuous(breaks = 1:ncol(tab_mt) , expand = c(0.01,0) , labels = colnames(tab_mt))+ @@ -224,16 +227,9 @@ LogoPlotSnps <- function(plot_df scale_y_continuous(breaks = 0:(max_mult_mut-1) , labels = c(1:max_mult_mut) , limits = c(0, max_mult_mut)) + - # FIXME: currently broken, possibly due to ggseqlogo() not working in the - # way standard ggplot2() does - geom_tile(aes(plot_df$position, tpos0 # heat-mapped distance tiles along the bottom. - , width = tW0 - , height = tH0) - , fill = plot_df$ligD_colours - , colour = plot_df$ligD_colours - , linetype = "blank") + ylab(y_lab) + - theme(legend.position = leg_pos + theme(text=element_text(family="FreeSans") + , legend.position = leg_pos , legend.direction = leg_dir , legend.title = element_text(size = leg_tts , colour = ytt_col) @@ -260,40 +256,58 @@ LogoPlotSnps <- function(plot_df , method = 'custom' , col_scheme = my_logo_col , seq_type = 'aa') + - - theme(text = element_text(family="FreeSans"))+ - theme(axis.text.x = element_blank() - , axis.text.y = element_blank()) + - theme_logo()+ - scale_x_continuous(breaks = 1:ncol(tab_wt) , expand = c(0.01,0) , labels = colnames(tab_wt))+ - - xlab(x_lab) + - - theme(legend.position = "none" - , legend.direction = leg_dir - #, legend.title = element_blank() - , legend.title = element_text(size = y_tts - , colour = ytt_col) - , legend.text = element_text(size = leg_ts) - , axis.text.x = element_text(size = x_ats - , angle = x_tangle - , hjust = 1 - , vjust = 0.4 - , colour = xfont_bgc) + theme(text = element_text(family="FreeSans") + , legend.position = "none" + , axis.text.x = element_blank() , axis.text.y = element_blank() + , axis.title.x = element_blank() + , axis.title.y = element_blank() + , plot.background = element_rect(fill = theme_bgc) + ) + + labs(x=NULL, y=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_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.title.x = element_text(size = x_tts , colour = xtt_col) , 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_rect(fill = theme_bgc) - ) - , nrow = 2 + , plot.margin = margin(t=0) + , panel.grid=element_blank() + , panel.background = element_rect(fill = theme_bgc) + ) + + 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) + , nrow = 3 , align = "v" - , rel_heights = c(3/4, 1/4)) + , rel_heights = c(7/10, 2/7, 1/7)) #------------------ # Wild logo plot #------------------ } +#LogoPlotSnps(mutable_df3)