diff --git a/scripts/functions/consurfP.R b/scripts/functions/consurfP.R index 35d4a08..d806e0b 100644 --- a/scripts/functions/consurfP.R +++ b/scripts/functions/consurfP.R @@ -13,99 +13,100 @@ # input args #========================================================== wideP_consurf <- function(plotdf - , 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" - ){ + , 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 - } - + }else{ + point_colours = point_colours + } + ############################### # custom 1: x-axis text colour ############################## - + if (xtext_colour_aa){ - positionF <- levels(as.factor(plotdf[[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])) + positionF <- levels(as.factor(plotdf[[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 } @@ -127,7 +128,7 @@ wideP_consurf <- function(plotdf #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 #-------------------------------------- @@ -170,9 +171,9 @@ wideP_consurf <- function(plotdf , ligD = plotdf[[lig_dist_colname]] , ligDR = plotdf$lig_distR , ligD_cols = plotdf$ligD_colours)) - } else{ - plotdf = plotdf - } + } else{ + plotdf = plotdf + } ############################################### # Custom 3: x-axis geom tiles ~ active sites @@ -182,7 +183,7 @@ wideP_consurf <- function(plotdf # Build Data with colours # ~ on active sites #========================== - + if(A_xvar_aa) { 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) # , y = yvar_colname # , colour = y_colour_factor) - + g = ggplot(plotdf, aes_string(x = sprintf("factor(%s)", xvar_colname) - , y = yvar_colname - , colour = sprintf("factor(%s)", yvar_colourN_colname) - )) + , y = yvar_colname + , colour = sprintf("factor(%s)", yvar_colourN_colname) + )) # g = ggplot(plotdf, aes_string(x = "position") # , y = "consurf_score" # , colour = "consurf_colour_rev") @@ -273,22 +274,22 @@ wideP_consurf <- function(plotdf , ylim = c(ymin, ymax) , clip = "off") + geom_point(size = p_size) + - scale_colour_manual(values = point_colours) - # , labels = leg_labels - # , name = leg_title1) + scale_colour_manual(values = point_colours) + # , labels = leg_labels + # , name = leg_title1) } - + 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 - + , ymax = eval(parse(text = upper_EB_colname)) + )) + }else{ + + g0 = g0 + } - + #--------------------- # add axis formatting #--------------------- @@ -298,22 +299,22 @@ wideP_consurf <- function(plotdf , 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))+ + , 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 @@ -364,39 +365,39 @@ wideP_consurf <- function(plotdf g2 = g1 + geom_tile(aes(, tpos0 , width = tW0 , height = tH0) - , fill = plotdf$ligD_colours - , colour = plotdf$ligD_colours - , linetype = "blank") - + , fill = plotdf$ligD_colours + , colour = plotdf$ligD_colours + , linetype = "blank") + #cat("Nrows of plot df", length(plotdf$ligD_colours)) out = g2 - # - # #------------------ - # # Extract legend2 - # #------------------ - # labels = seq(lig_min, lig_max, len = 5); labels - # labelsD = round(labels, digits = 0); labelsD - # - # g2_leg = g1 + - # geom_tile(aes(fill = .data[[lig_dist_colname]]) - # , colour = "white") + - # scale_fill_gradient2(midpoint = lig_mean - # , low = "green" - # , mid = "yellow" - # , high = "red" - # , breaks = labels - # #, n.breaks = 11 - # #, minor_breaks = c(2, 4, 6, 8, 10) - # , limits = c(lig_min, lig_max) - # , labels = labelsD - # , name = leg_title2) - # - # legend2 = get_legend(g2_leg) - # - # }else{ - # out = g1 - # } - ###################################################### + # + # #------------------ + # # Extract legend2 + # #------------------ + # labels = seq(lig_min, lig_max, len = 5); labels + # labelsD = round(labels, digits = 0); labelsD + # + # g2_leg = g1 + + # geom_tile(aes(fill = .data[[lig_dist_colname]]) + # , colour = "white") + + # scale_fill_gradient2(midpoint = lig_mean + # , low = "green" + # , mid = "yellow" + # , high = "red" + # , breaks = labels + # #, n.breaks = 11 + # #, minor_breaks = c(2, 4, 6, 8, 10) + # , limits = c(lig_min, lig_max) + # , labels = labelsD + # , name = leg_title2) + # + # legend2 = get_legend(g2_leg) + # + # }else{ + # out = g1 + # } + ###################################################### #------------------ # Extract legend2 #------------------ @@ -404,7 +405,7 @@ wideP_consurf <- function(plotdf labelsD = round(labels, digits = 0); labelsD g2_leg = 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 @@ -423,7 +424,7 @@ wideP_consurf <- function(plotdf }else{ out = g1 } - + ##################################################### # #============================================ # # x-axis: geom_tiles ~ ligand distance @@ -445,7 +446,7 @@ wideP_consurf <- function(plotdf # out = g1 # } # -############################################################################################ + ############################################################################################ #============================================== # x-axis: geom_tiles ~ active sites and others #============================================== @@ -454,48 +455,53 @@ wideP_consurf <- function(plotdf #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))) { - 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 = plotdf[[aa_colour_colname3]] - , colour = plotdf[[aa_colour_colname3]] - , linetype = "solid") + - geom_tile(aes(, tpos2 - , width = tW - , height = tH ) - , fill = plotdf[[aa_colour_colname2]] - , colour = plotdf[[aa_colour_colname2]] - , linetype = "solid")+ - - geom_tile(aes(, tpos1 - , width = tW - , height = tH) - , fill = plotdf[[aa_colour_colname1]] - , colour = plotdf[[aa_colour_colname1]] - , linetype = "solid") - - cat("\nDone Plot with 3 ligands") - } + #--------------------- + # 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 = plotdf[[aa_colour_colname3]] + , colour = plotdf[[aa_colour_colname3]] + , linetype = "solid") + + geom_tile(aes(, tpos2 + , width = tW + , height = tH ) + , fill = plotdf[[aa_colour_colname2]] + , colour = plotdf[[aa_colour_colname2]] + , linetype = "solid")+ + + geom_tile(aes(, tpos1 + , width = tW + , height = tH) + , fill = plotdf[[aa_colour_colname1]] + , colour = plotdf[[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 @@ -509,8 +515,9 @@ wideP_consurf <- function(plotdf , fill = plotdf[[aa_colour_colname1]] , colour = plotdf[[aa_colour_colname1]] , linetype = "solid") - + if (debug){ cat("\nDone Plot with 2 ligands") + } } #--------------------- @@ -519,11 +526,12 @@ wideP_consurf <- function(plotdf 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 @@ -546,20 +554,20 @@ wideP_consurf <- function(plotdf 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 = plotdf[[aa_colour_colname]] - , colour = plotdf[[aa_colour_colname]] - , linetype = "solid") - + , width = tW + , height = tH) + , fill = plotdf[[aa_colour_colname]] + , colour = plotdf[[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") @@ -567,23 +575,23 @@ wideP_consurf <- function(plotdf #============================================== if (A_xvar_lig){ legs = cowplot::plot_grid(legend1 - , legend2 - , ncol = 1 - , align = "hv" - , rel_heights = c(2/4,3/4)) + , legend2 + , 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) + , 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) + , legend1 + , ncol = 2 + , align = "hv" + , rel_widths = c(9/10, 0.5/10) ) } #============================================== @@ -609,9 +617,9 @@ wideP_consurf <- function(plotdf # ) # } #============================================== -return(out2) -#return(out2) - + return(out2) + #return(out2) + } ############################################################# diff --git a/scripts/functions/logoP_snp.R b/scripts/functions/logoP_snp.R index db5c9c8..984aaad 100644 --- a/scripts/functions/logoP_snp.R +++ b/scripts/functions/logoP_snp.R @@ -117,14 +117,16 @@ LogoPlotSnps <- function(plot_df if (is.matrix(tab_mt)){ cat("\nPASS: Mutant matrix successfully created..." - , "\nRownames of mutant matrix:", rownames(tab_mt) - , "\nColnames of mutant matrix:", colnames(tab_mt)) + #, "\nRownames of mutant matrix:", rownames(tab_mt) + #, "\nColnames of mutant matrix:", colnames(tab_mt) + ) } else{ tab_mt = as.matrix(tab_mt, rownames = T) if (is.matrix(tab_mt)){ cat("\nCreating mutant matrix..." - , "\nRownames of mutant matrix:", rownames(tab_mt) - , "\nColnames of mutant matrix:", colnames(tab_mt)) + #, "\nRownames of mutant matrix:", rownames(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 mutant matrix:", dim(tab_mt) , "\n" - , "\nRownames of mutant matrix:", rownames(tab_wt) - , "\nColnames of mutant matrix:", colnames(tab_wt)) + #, "\nRownames of mutant matrix:", rownames(tab_wt) + #, "\nColnames of mutant matrix:", colnames(tab_wt) + ) } ######################################