LSHTM_analysis/scripts/functions/logoP_or.R

196 lines
6.5 KiB
R

# 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 = 5
, 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
)
{
#################################
# Data processing for logo plot
#################################
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)
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"
}
p0 = 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
, 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)))
LogoPlot = p0 + 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))
if (y_axis_log){
if (grepl("Log", y_lab)){
y_lab = y_lab
}else{
y_lab = paste("Log", y_lab)
}
LogoPlot = p0 + ylab(y_lab)
}
return(LogoPlot)
}