diff --git a/scripts/functions/logoP_msa.R b/scripts/functions/logoP_msa.R index 48d2c0a..4b5d2f3 100644 --- a/scripts/functions/logoP_msa.R +++ b/scripts/functions/logoP_msa.R @@ -459,3 +459,4 @@ LogoPlotMSA <- function(unified_msa return(LogoED_comb) } +#LogoPlotMSA(unified_msa) diff --git a/scripts/functions/wideP_consurf.R b/scripts/functions/wideP_consurf.R index 692d4dd..28fc2de 100644 --- a/scripts/functions/wideP_consurf.R +++ b/scripts/functions/wideP_consurf.R @@ -12,81 +12,79 @@ # wideP(): # input args #========================================================== -wideP_consurf <- function(plot_df - , 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 = "white" - , panel_col_fill = "white" - - # 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 +wideP_consurf2 <- function(plot_df + , 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 = "white" + , panel_col_fill = "white" + + # 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 + , annotate_ligand_distance = 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 + , annotate_active_sites = F + , aa_pos_drug = aa_pos_drug + , drug_aa_colour = "purple" + , 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)){ @@ -119,7 +117,7 @@ wideP_consurf <- function(plot_df # Build data with colours # ~ ligand distance #========================= - if (A_xvar_lig){ + if (annotate_ligand_distance){ cat("\nAnnotating x-axis ~", lig_dist_colname, "requested...") #------------------------------------- @@ -159,7 +157,7 @@ wideP_consurf <- function(plot_df ligDcolKey <- data.frame(ligD_colours = lig_cols , lig_distR = ligD_valsR); ligDcolKey - names(ligDcolKey) + #names(ligDcolKey) cat("\nSuccessful: Mapping b/w", lig_dist_colname, "and colours") #------------------------------------- @@ -168,11 +166,9 @@ wideP_consurf <- function(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)) - } else{ - plot_df = plot_df + , ligD = plot_df[[lig_dist_colname]] + , ligDR = plot_df$lig_distR + , ligD_cols = plot_df$ligD_colours)) } ############################################### @@ -184,7 +180,7 @@ wideP_consurf <- function(plot_df # ~ on active sites #========================== - if(A_xvar_aa) { + if(annotate_active_sites) { cat("\nAnnotation for xvar requested. Building colours for annotation...") aa_colour_colname = "bg_all" @@ -196,9 +192,9 @@ wideP_consurf <- function(plot_df # column colour 0: Active site + drug binding sites #-------------------------------------------------- plot_df[[aa_colour_colname]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_drug - , drug_aa_colour - , ifelse(plot_df[[xvar_colname]]%in%active_aa_pos - , active_aa_colour, default_gt_clr )) + , drug_aa_colour + , ifelse(plot_df[[xvar_colname]]%in%active_aa_pos + , active_aa_colour, default_gt_clr )) plot_df[[aa_colour_colname]] cat("\nColumn created 'bg_all':", length(plot_df[[aa_colour_colname]])) @@ -208,20 +204,20 @@ wideP_consurf <- function(plot_df cat("\nAssigning colours to drug binding and ligand-1 binding residues") plot_df[[aa_colour_colname1]] = plot_df[[aa_colour_colname]] plot_df[[aa_colour_colname1]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_lig1 - , aa_colour_lig1, plot_df[[aa_colour_colname]]) + , aa_colour_lig1, plot_df[[aa_colour_colname]]) #------------------------------------------------ # column colour 2: Ligand 2 #------------------------------------------------ plot_df[[aa_colour_colname2]] = plot_df[[aa_colour_colname1]] plot_df[[aa_colour_colname2]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_lig2 - , aa_colour_lig2, plot_df[[aa_colour_colname1]]) + , aa_colour_lig2, plot_df[[aa_colour_colname1]]) #------------------------------------------------ # column colour 3: Ligand 3 #------------------------------------------------ plot_df[[aa_colour_colname3]] = plot_df[[aa_colour_colname2]] plot_df[[aa_colour_colname3]] = ifelse(plot_df[[xvar_colname]]%in%aa_pos_lig3 - , aa_colour_lig3, plot_df[[aa_colour_colname2]]) + , aa_colour_lig3, plot_df[[aa_colour_colname2]]) } ################### @@ -237,12 +233,13 @@ wideP_consurf <- function(plot_df ymax = max(plot_df[[yvar_colname]]); ymax g = ggplot(plot_df, 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) )) #"if SPECIAL do SPECIAL THING, otherwise do NORMAL THING" if (plot_type == "bar"){ + cat('\ndoing bar plot') g0 = g + geom_bar(stat = "identity") } @@ -256,6 +253,7 @@ wideP_consurf <- function(plot_df } if (plot_error_bars){ + cat('\nplotting error bars') g0 = g0 + geom_errorbar(aes(ymin = eval(parse(text = lower_EB_colname)) , ymax = eval(parse(text = upper_EB_colname)) @@ -291,7 +289,7 @@ wideP_consurf <- function(plot_df , b = b_margin , l = l_margin , unit = unit_margin))+ - guides(colour = guide_legend(title = "ConsurfXXXX")) + + #guides(colour = guide_legend(title = "ConsurfXXXX")) + labs(title = ptitle , x = xlab @@ -302,7 +300,7 @@ wideP_consurf <- function(plot_df #------------------ # yayy g1_leg = ggplot(plot_df, aes_string(x = sprintf("factor(%s)" - , xvar_colname) )) + , xvar_colname) )) g1_leg = g1_leg + geom_bar(); g1_leg g1_leg = g1_leg + geom_bar(aes_string(fill = sprintf("factor(%s)" , yvar_colourN_colname))) @@ -320,7 +318,7 @@ wideP_consurf <- function(plot_df #------- # plot #------- - if(A_xvar_lig){ # 0 is a magic number that does my sensible default + if(annotate_ligand_distance){ # 0 is a magic number that does my sensible default if (tpos0 == 0){ tpos0 = ymin-0.5 } @@ -339,8 +337,6 @@ wideP_consurf <- function(plot_df , "\nNo. of colours:", n_colours) g2 = g1 + geom_tile(aes(, tpos0 -# g2 = g1 + geom_tile(aes(, tpos0 - , width = tW0 , height = tH0) , fill = plot_df$ligD_colours @@ -349,14 +345,19 @@ wideP_consurf <- function(plot_df #cat("Nrows of plot df", length(plot_df$ligD_colours)) out = g2 - + cat('\nOut is g2') }else{ + cat('\nOut is g1') out = g1 } #============================================== # x-axis: geom_tiles ~ active sites and others #============================================== - if(A_xvar_aa){ + if(annotate_active_sites){ + cat('\nDrug Positions: ',aa_pos_drug) + cat ('\nLig 1 Positions:', aa_pos_lig1) + cat ('\nLig 2 Positions:', aa_pos_lig2) + cat ('\nLig 2 Positions:', aa_pos_lig3) #tpos = 0 #tW = 1 #tH = 0.2 @@ -366,67 +367,70 @@ wideP_consurf <- function(plot_df #--------------------- 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 - out = out + geom_tile(aes(,2 - , width = tW - , height = tH ) + !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 = tW0 + , height = tH0 ) , fill = plot_df[[aa_colour_colname3]] , colour = plot_df[[aa_colour_colname3]] , linetype = "solid") + geom_tile(aes(, tpos2 - , width = tW - , height = tH ) + , width = tW0 + , height = tH0 ) , fill = plot_df[[aa_colour_colname2]] , colour = plot_df[[aa_colour_colname2]] , linetype = "solid")+ geom_tile(aes(, tpos1 - , width = tW - , height = tH) + , width = tW0 + , height = tH0) , fill = plot_df[[aa_colour_colname1]] , colour = plot_df[[aa_colour_colname1]] , linetype = "solid") - if (debug){ - cat("\nDone Plot with 3 ligands") - } - } + #if (debug){ + cat("\nDone Plot with 3 ligands") + #} + } else {cat("\n3 Lig Plot condition FAIL")} #--------------------- # 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") - } + !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 - , height = tH) + , width = tW0 + , height = tH0) , fill = plot_df[[aa_colour_colname2]] , colour = plot_df[[aa_colour_colname2]] , linetype = "solid")+ geom_tile(aes(, tpos1 - , width = tW - , height = tH) + , width = tW0 + , height = tH0) , fill = plot_df[[aa_colour_colname1]] , colour = plot_df[[aa_colour_colname1]] , linetype = "solid") - if (debug){ - cat("\nDone Plot with 2 ligands") - } - } + #if (debug){ + cat("\nDone Plot with 2 ligands") + #} + } else {cat("\n2 Lig Plot condition FAIL")} #--------------------- # Add2plot: 1 ligand @@ -434,53 +438,54 @@ wideP_consurf <- function(plot_df 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") - } + #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 - , height = tH) + , width = tW0 + , height = tH0) , fill = plot_df[[aa_colour_colname1]] , colour = plot_df[[aa_colour_colname1]] , linetype = "solid") cat("\nDone Plot with 1 ligand") - } + } else {cat("\n3 Lig Plot condition FAIL")} #----------------------------------- # Add2plot:NO ligands # No Ligs: Just drug and active site - # DEFAULT: A_xvar_aa == TRUE + # DEFAULT: annotate_active_sites == TRUE #---------------------------------- 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 and drug binding sites") - cat("\nCreating column colours, column name:", aa_colour_colname) - cat("\nDoing Plot with 0 ligands: active and drug site only") - } + #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) + , width = tW0 + , height = tH0) , fill = plot_df[[aa_colour_colname]] , colour = plot_df[[aa_colour_colname]] , linetype = "solid") - if (debug){ - cat("\nDone Plot with: Active and Drug sites") - } + #if (debug){ + cat("\nDone Plot with: Active and Drug sites") } - }else{ + #} + } else { cat("\nNo annotation for additional ligands on xvar requested") } - if (A_xvar_lig){ + if (annotate_ligand_distance){ + cat('\nOutput: Plot + distance heat-bar + other stuff') legs = cowplot::plot_grid(legend1 , generate_distance_legend(plot_df, yvar_colname = yvar_colname) , ncol = 1 @@ -505,13 +510,13 @@ wideP_consurf <- function(plot_df #return(out2) } - - -# wideP_consurf( -# merged_df3, -# A_xvar_aa = T, -# active_aa_pos = active_aa_pos, -# aa_pos_lig1 = aa_pos_lig1, -# aa_pos_lig2 = aa_pos_lig2, -# aa_pos_lig3 = aa_pos_lig3, +# +# wideP_consurf2( +# merged_df3, +# annotate_active_sites = TRUE, +# active_aa_pos = active_aa_pos, +# aa_pos_drug = aa_pos_drug, +# aa_pos_lig1 = aa_pos_lig1, +# aa_pos_lig2 = aa_pos_lig2, +# aa_pos_lig3 = aa_pos_lig3, # debug=T) \ No newline at end of file