202 lines
5.8 KiB
R
202 lines
5.8 KiB
R
getwd()
|
|
setwd("~/git/LSHTM_analysis/mcsm_analysis/pyrazinamide/scripts/plotting")
|
|
getwd()
|
|
|
|
########################################################################
|
|
# Installing and loading required packages and functions #
|
|
########################################################################
|
|
|
|
source("../Header_TT.R")
|
|
source("../barplot_colour_function.R")
|
|
|
|
########################################################################
|
|
# Read file: call script for combining df for lig #
|
|
########################################################################
|
|
|
|
source("../combining_two_df_lig.R")
|
|
|
|
#---------------------- PAY ATTENTION
|
|
# the above changes the working dir
|
|
#[1] "git/LSHTM_analysis/mcsm_analysis/pyrazinamide/scripts"
|
|
#---------------------- PAY ATTENTION
|
|
|
|
#==========================
|
|
# This will return:
|
|
|
|
# df with NA:
|
|
# merged_df2
|
|
# merged_df3
|
|
|
|
# df without NA:
|
|
# merged_df2_comp
|
|
# merged_df3_comp
|
|
#===========================
|
|
|
|
###########################
|
|
# Data for Lig plots
|
|
# you need merged_df3
|
|
# or
|
|
# merged_df3_comp
|
|
# since these have unique SNPs
|
|
# I prefer to use the merged_df3
|
|
# because using the _comp dataset means
|
|
# we lose some muts and at this level, we should use
|
|
# as much info as available
|
|
###########################
|
|
|
|
# uncomment as necessary
|
|
#<<<<<<<<<<<<<<<<<<<<<<<<<
|
|
# REASSIGNMENT
|
|
my_df = merged_df3
|
|
#my_df = merged_df3_comp
|
|
#<<<<<<<<<<<<<<<<<<<<<<<<<
|
|
|
|
# delete variables not required
|
|
rm(merged_df2, merged_df2_comp, merged_df3, merged_df3_comp)
|
|
|
|
# quick checks
|
|
colnames(my_df)
|
|
str(my_df)
|
|
|
|
# Ensure correct data type in columns to plot: need to be factor
|
|
# sanity check
|
|
is.factor(my_df$Lig_outcome)
|
|
my_df$Lig_outcome = as.factor(my_df$Ligoutcome)
|
|
is.factor(my_df$Lig_outcome)
|
|
#[1] TRUE
|
|
|
|
#############################
|
|
# Extra sanity check:
|
|
# for mcsm_lig ONLY
|
|
# Dis_lig_Ang should be <10
|
|
#############################
|
|
|
|
if (max(my_df$Dis_lig_Ang) < 10){
|
|
print ("Sanity check passed: lig data is <10Ang")
|
|
}else{
|
|
print ("Error: data should be filtered to be within 10Ang")
|
|
}
|
|
########################################################################
|
|
# end of data extraction and cleaning for plots #
|
|
########################################################################
|
|
|
|
#==========================
|
|
# Plot: Barplot with scores (unordered)
|
|
# corresponds to Lig_outcome
|
|
# Stacked Barplot with colours: Lig_outcome @ position coloured by
|
|
# stability scores. This is a barplot where each bar corresponds
|
|
# to a SNP and is coloured by its corresponding Lig stability value.
|
|
# Normalised values (range between -1 and 1 ) to aid visualisation
|
|
# NOTE: since barplot plots discrete values, colour = score, so number of
|
|
# colours will be equal to the no. of unique normalised scores
|
|
# rather than a continuous scale
|
|
# will require generating the colour scale separately.
|
|
#============================
|
|
|
|
#===================
|
|
# Data for plots
|
|
#===================
|
|
|
|
#<<<<<<<<<<<<<<<<<<<<<<<<
|
|
# REASSIGNMENT
|
|
df = my_df
|
|
#<<<<<<<<<<<<<<<<<<<<<<<<<
|
|
|
|
rm(my_df)
|
|
|
|
# sanity checks
|
|
table(df$Lig_outcome)
|
|
|
|
# should be -1 and 1: may not be in this case because you have filtered the data
|
|
# FIXME: normalisation before or after filtering?
|
|
min(df$ratioPredAff) #
|
|
max(df$ratioPredAff) #
|
|
|
|
# sanity checks
|
|
# very important!!!!
|
|
tapply(df$ratioPredAff, df$Lig_outcome, min)
|
|
|
|
tapply(df$ratioPredAff, df$Lig_outcome, max)
|
|
|
|
|
|
#******************
|
|
# generate plot
|
|
#******************
|
|
|
|
# set output dir for plots
|
|
getwd()
|
|
setwd("~/git/Data/pyrazinamide/output/plots")
|
|
getwd()
|
|
|
|
# My colour FUNCTION: based on group and subgroup
|
|
# in my case;
|
|
# df = df
|
|
# group = Lig_outcome
|
|
# subgroup = normalised score i.e ratioPredAff
|
|
|
|
# Prepare data: round off ratioLig scores
|
|
# round off to 3 significant digits:
|
|
# 165 if no rounding is performed: used to generate the originalgraph
|
|
# 156 if rounded to 3 places
|
|
# FIXME: check if reducing precision creates any ML prob
|
|
|
|
# check unique values in normalised data
|
|
u = unique(df$ratioPredAff)
|
|
|
|
# <<<<< -------------------------------------------
|
|
# Run this section if rounding is to be used
|
|
# specify number for rounding
|
|
n = 3
|
|
df$ratioLigR = round(df$ratioPredAff, n)
|
|
u = unique(df$ratioLigR) # 156
|
|
# create an extra column called group which contains the "gp name and score"
|
|
# so colours can be generated for each unique values in this column
|
|
my_grp = df$ratioLigR
|
|
df$group <- paste0(df$Lig_outcome, "_", my_grp, sep = "")
|
|
|
|
# else
|
|
# uncomment the below if rounding is not required
|
|
|
|
#my_grp = df$ratioLig
|
|
#df$group <- paste0(df$Lig_outcome, "_", my_grp, sep = "")
|
|
|
|
# <<<<< -----------------------------------------------
|
|
|
|
# Call the function to create the palette based on the group defined above
|
|
colours <- ColourPalleteMulti(df, "Lig_outcome", "my_grp")
|
|
my_title = "Ligand affinity"
|
|
|
|
# axis label size
|
|
my_xaxls = 13
|
|
my_yaxls = 15
|
|
|
|
# axes text size
|
|
my_xaxts = 15
|
|
my_yaxts = 15
|
|
|
|
# no ordering of x-axis
|
|
g = ggplot(df, aes(factor(Position, ordered = T)))
|
|
g +
|
|
geom_bar(aes(fill = group), colour = "grey") +
|
|
scale_fill_manual( values = colours
|
|
, guide = 'none') +
|
|
theme( axis.text.x = element_text(size = my_xaxls
|
|
, angle = 90
|
|
, hjust = 1
|
|
, vjust = 0.4)
|
|
, axis.text.y = element_text(size = my_yaxls
|
|
, angle = 0
|
|
, hjust = 1
|
|
, vjust = 0)
|
|
, axis.title.x = element_text(size = my_xaxts)
|
|
, axis.title.y = element_text(size = my_yaxts ) ) +
|
|
labs(title = my_title
|
|
, x = "Position"
|
|
, y = "Frequency")
|
|
|
|
# for sanity and good practice
|
|
rm(df)
|
|
#======================= end of plot
|
|
# axis colours labels
|
|
# https://stackoverflow.com/questions/38862303/customize-ggplot2-axis-labels-with-different-colors
|
|
# https://stackoverflow.com/questions/56543485/plot-coloured-boxes-around-axis-label
|