LSHTM_analysis/mcsm_analysis/pyrazinamide/scripts/plotting/subcols_axis_LIG.R

208 lines
5.4 KiB
R

getwd()
setwd("~/git/LSHTM_analysis/mcsm_analysis/pyrazinamide/scripts/plotting")
getwd()
############################################################
# 1: Installing and loading required packages and functions
############################################################
#source("../Header_TT.R")
#source("../barplot_colour_function.R")
#library(tidyverse)
###########################
#2: Read file: normalised file, output of step 4 mcsm pipeline
###########################
#my_df <- read.csv("../../Data/mcsm_complex1_normalised.csv"
# , row.names = 1
# , stringsAsFactors = F
# , header = T)
# call script combining_df
source("../combining_two_df_lig.R")
#---------------------- PAY ATTENTION
# the above changes the working dir
# from Plotting to Scripts"
#---------------------- PAY ATTENTION
#==========================
# This will return:
# df with NA for pyrazinamide:
#merged_df2
#merged_df2_comp
# df without NA for pyrazinamide:
#merged_df3
#merged_df3_comp
#==========================
###########################
# Data to choose:
# We will be using the small dfs
# to generate the coloured axis
###########################
# 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)
str(my_df)
my_df$Position
c1 = my_df[my_df$Mutationinformation == "A134V",]
# order my_df by Position
my_df_o = my_df[order(my_df$Position),]
head(my_df_o$Position); tail(my_df_o$Position)
c2 = my_df_o[my_df_o$Mutationinformation == "A134V",]
# sanity check
if (sum(table(c1 == c2)) == ncol(my_df)){
print ("Sanity check passsd")
}else{
print ("Error!: Please debug your code")
}
rm(my_df, c1, c2)
# create a new df with unique position numbers and cols
Position = unique(my_df_o$Position)
Position_cols = as.data.frame(Position)
head(Position_cols) ; tail(Position_cols)
# specify active site residues and bg colour
Position = c(49, 51, 57, 71
, 8, 96, 138
, 13, 68
, 103, 137
, 133, 134) #13
lab_bg = rep(c("purple"
, "yellow"
, "cornflowerblue"
, "blue"
, "green"), times = c(4, 3, 2, 2, 2)
)
# second bg colour for active site residues
#lab_bg2 = rep(c("white"
# , "green" , "white", "green"
# , "white"
# , "white"
# , "white"), times = c(4
# , 1, 1, 1
# , 2
# , 2
# , 2)
#)
#%%%%%%%%%
# revised: leave the second box coloured as the first one incase there is no second colour
#%%%%%%%%%
lab_bg2 = rep(c("purple"
, "green", "yellow", "green"
, "cornflowerblue"
, "blue"
, "green"), times = c(4
, 1, 1, 1
, 2
, 2
, 2))
# fg colour for labels for active site residues
lab_fg = rep(c("white"
, "black"
, "black"
, "white"
, "black"), times = c(4, 3, 2, 2, 2))
#%%%%%%%%%
# revised: make the purple ones black
# fg colour for labels for active site residues
#%%%%%%%%%
#lab_fg = rep(c("black"
# , "black"
# , "black"
# , "white"
# , "black"), times = c(4, 3, 2, 2, 2))
# combined df with active sites, bg and fg colours
aa_cols_ref = data.frame(Position
, lab_bg
, lab_bg2
, lab_fg
, stringsAsFactors = F) #13, 4
str(Position_cols); class(Position_cols)
str(aa_cols_ref); class(aa_cols_ref)
# since Position is int and numeric in the two dfs resp,
# converting numeric to int for consistency
aa_cols_ref$Position = as.integer(aa_cols_ref$Position)
class(aa_cols_ref$Position)
#===========
# Merge 1: merging Positions df (Position_cols) and
# active site cols (aa_cols_ref)
# linking column: "Position"
# This is so you can have colours defined for all 130 positions
#===========
head(Position_cols$Position); head(aa_cols_ref$Position)
mut_pos_cols = merge(Position_cols, aa_cols_ref
, by = "Position"
, all.x = TRUE)
head(mut_pos_cols)
# replace NA's
# :column "lab_bg" with "white"
# : column "lab_fg" with "black"
mut_pos_cols$lab_bg[is.na(mut_pos_cols$lab_bg)] <- "white"
mut_pos_cols$lab_bg2[is.na(mut_pos_cols$lab_bg2)] <- "white"
mut_pos_cols$lab_fg[is.na(mut_pos_cols$lab_fg)] <- "black"
head(mut_pos_cols)
#===========
# Merge 2: Merge mut_pos_cols with mcsm df
# Now combined the 130 positions with aa colours with
# the mcsm_data
#===========
# dfs to merge
df0 = my_df_o
df1 = mut_pos_cols
# check the column on which merge will be performed
head(df0$Position); tail(df0$Position)
head(df1$Position); tail(df1$Position)
# should now have 3 extra columns
my_df = merge(df0, df1
, by = "Position"
, all.x = TRUE)
# sanity check
my_df[my_df$Position == "49",]
my_df[my_df$Position == "13",]
my_df$Position
# clear variables
rm(aa_cols_ref
, df0
, df1
, my_df_o
, Position_cols
, lab_bg
, lab_bg2
, lab_fg
, Position
)