added logo_plot function and test to check it
This commit is contained in:
parent
344a74a9e1
commit
426a5cb0b5
2 changed files with 200 additions and 0 deletions
173
scripts/functions/logo_plots_func.R
Normal file
173
scripts/functions/logo_plots_func.R
Normal file
|
@ -0,0 +1,173 @@
|
||||||
|
#logo plots
|
||||||
|
|
||||||
|
# create functions
|
||||||
|
|
||||||
|
# one with OR
|
||||||
|
# --> select/drop down option to remove empty positions
|
||||||
|
# --> easy peasy, just select the merged_df3_comp
|
||||||
|
# --> select/drop down option for colour
|
||||||
|
# --> if clustalx and taylor, set variable to black bg + white font
|
||||||
|
# --> if chemistry and hydrophobicity, then grey bg + black font
|
||||||
|
|
||||||
|
# --> select/drop down option for log scale
|
||||||
|
# --> should include WT
|
||||||
|
|
||||||
|
# one for multiple muts
|
||||||
|
# --> select/drop down option to filter count of nsSNPs
|
||||||
|
# --> select/drop down option for colour
|
||||||
|
# --> should include WT
|
||||||
|
|
||||||
|
# Data used
|
||||||
|
#wide_df_or # or logo plot
|
||||||
|
#wide_df_or_mult # > 1 sites
|
||||||
|
#wide_df_logor_m #make it as a scale option. REDUNDANT
|
||||||
|
#tab_mt # mutant logo plot
|
||||||
|
#tab_wt # wt logo plot
|
||||||
|
|
||||||
|
# Make it hover over position and then get the corresponding data table!
|
||||||
|
#%%======================================================================
|
||||||
|
|
||||||
|
#==================
|
||||||
|
# logo data: OR
|
||||||
|
#==================
|
||||||
|
# NOTE: my_logo_col
|
||||||
|
# Can be one of these: 'chemistry', 'hydrophobicity', 'clustalx', 'taylor'
|
||||||
|
# if 'chemistry' or 'hydrophobicity' --> then bg is grey with black font
|
||||||
|
# if 'clustalx'or 'taylor' --> then bg is black with white font
|
||||||
|
#, theme_bgc =
|
||||||
|
#, xfont_bgc =
|
||||||
|
#, yfont_bgc =
|
||||||
|
|
||||||
|
# ADD legend for hydrophobicity
|
||||||
|
# ADD option to remove empty positions
|
||||||
|
|
||||||
|
LogoPlotCustomH <- function(plotdf
|
||||||
|
, x_axis_colname = "position"
|
||||||
|
, y_axis_colname = "or_mychisq"
|
||||||
|
, symbol_colname = "mutant_type"
|
||||||
|
, y_axis_log = T
|
||||||
|
, log_value = log10
|
||||||
|
, my_logo_col = "chemistry"
|
||||||
|
, x_lab = "Position"
|
||||||
|
, y_lab = "Odds Ratio"
|
||||||
|
, x_ats = 12 # text size
|
||||||
|
, x_tangle = 90 # text angle
|
||||||
|
#, theme_bgc
|
||||||
|
#, xfont_bgc
|
||||||
|
#, yfont_bgc
|
||||||
|
, y_ats = 22
|
||||||
|
, y_tangle = 0
|
||||||
|
, x_tts = 25 # title size
|
||||||
|
, y_tts = 20
|
||||||
|
, leg_pos = "none"
|
||||||
|
#, leg_pos = c(0.8, 0.9)
|
||||||
|
, leg_ts = 15 # leg text size
|
||||||
|
, leg_tts = 16 # leg title size
|
||||||
|
)
|
||||||
|
|
||||||
|
{
|
||||||
|
#################################
|
||||||
|
# Data processing for logo plot
|
||||||
|
#################################
|
||||||
|
|
||||||
|
logo_df = plotdf
|
||||||
|
|
||||||
|
if (y_axis_log){
|
||||||
|
log_colname = paste0("log_", y_axis_colname)
|
||||||
|
logo_df[log_colname] = log_value(logo_df[y_axis_colname])
|
||||||
|
}
|
||||||
|
|
||||||
|
logo_data_plot2 = logo_df[, c(x_axis_colname, symbol_colname, y_axis_colname, log_colname)]
|
||||||
|
|
||||||
|
#==================
|
||||||
|
# logo data: OR
|
||||||
|
#==================
|
||||||
|
logo_data_plot2_or = logo_df[, c(x_axis_colname, symbol_colname, y_axis_colname)]
|
||||||
|
|
||||||
|
wide_df2_or = as.matrix(logo_data_plot2_or %>% spread(x_axis_colname, y_axis_colname, fill = 0.0))
|
||||||
|
class(wide_df2_or)
|
||||||
|
|
||||||
|
rownames(wide_df2_or) = wide_df2_or[,1]
|
||||||
|
dim(wide_df2_or)
|
||||||
|
|
||||||
|
wide_df2_or = wide_df2_or[,-1]
|
||||||
|
str(wide_df2_or)
|
||||||
|
|
||||||
|
colnames(wide_df2_or)
|
||||||
|
position_or = as.numeric(colnames(wide_df2_or))
|
||||||
|
|
||||||
|
#==================
|
||||||
|
# logo data: logOR
|
||||||
|
#==================
|
||||||
|
logo_data_plot2_logor = logo_df[, c(x_axis_colname, symbol_colname, log_colname)]
|
||||||
|
|
||||||
|
wide_df2_logor = as.matrix(logo_data_plot2_logor %>% spread(x_axis_colname, log_colname, fill = 0.0))
|
||||||
|
class(wide_df2_logor)
|
||||||
|
|
||||||
|
rownames(wide_df2_logor) = wide_df2_logor[,1]
|
||||||
|
dim(wide_df2_logor)
|
||||||
|
|
||||||
|
#wide_df2_logor = subset(wide_df_logor, select = -c(1) )
|
||||||
|
wide_df2_logor = wide_df2_logor[,-1]
|
||||||
|
str(wide_df2_logor)
|
||||||
|
|
||||||
|
colnames(wide_df2_logor)
|
||||||
|
position_logor = as.numeric(colnames(wide_df2_logor))
|
||||||
|
|
||||||
|
|
||||||
|
######################################
|
||||||
|
# Generating plots with given y_axis
|
||||||
|
#####################################
|
||||||
|
#if (my_logo_col == 'clustalx || taylor'){
|
||||||
|
|
||||||
|
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 = "black"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
LogoPlot = ggseqlogo(wide_df2_or
|
||||||
|
, 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
|
||||||
|
, hjust = 1
|
||||||
|
, vjust = 0
|
||||||
|
, colour = yfont_bgc)
|
||||||
|
, axis.title.y = element_text(size = x_tts)
|
||||||
|
, axis.title.x = element_text(size = y_tts)
|
||||||
|
#, legend.position = "bottom") +
|
||||||
|
, legend.position = leg_pos
|
||||||
|
, plot.background = element_rect(fill = theme_bgc))+
|
||||||
|
#, legend.text = element_text(size = leg_ts)
|
||||||
|
#, legend.title = element_text(size = leg_tts))+
|
||||||
|
scale_x_discrete(x_lab
|
||||||
|
#, breaks
|
||||||
|
, labels = position_or
|
||||||
|
, limits = factor(1:length(position_or))) +
|
||||||
|
ylab(y_lab)
|
||||||
|
|
||||||
|
#print(logo_or)
|
||||||
|
return(LogoPlot)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
27
scripts/functions/tests/test_logo_plots_func.R
Normal file
27
scripts/functions/tests/test_logo_plots_func.R
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
#source("~/git/LSHTM_analysis/config/gid.R")
|
||||||
|
#source("~/git/LSHTM_analysis/scripts/plotting/get_plotting_dfs.R")
|
||||||
|
|
||||||
|
|
||||||
|
LogoPlotCustomH (plotdf = merged_df3
|
||||||
|
, x_axis_colname = "position"
|
||||||
|
, y_axis_colname = "or_mychisq"
|
||||||
|
, symbol_colname = "mutant_type"
|
||||||
|
, y_axis_log = T
|
||||||
|
, log_value = log10
|
||||||
|
, my_logo_col = 'hydrophobicity'
|
||||||
|
#, theme_bgc
|
||||||
|
# , xfont_bgc
|
||||||
|
# , yfont_bgc
|
||||||
|
, 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 = 25 # title size
|
||||||
|
, y_tts = 20
|
||||||
|
, leg_pos = "none"
|
||||||
|
, leg_ts = 15 # leg text size
|
||||||
|
, leg_tts = 16 # leg title size
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue