sorted subcols_axis script to generate correct axis cols for both PS and lig plots
This commit is contained in:
parent
482eeadb9a
commit
0e3f9e584b
9 changed files with 117 additions and 81 deletions
147
scripts/plotting/subcols_axis_PS.R
Normal file → Executable file
147
scripts/plotting/subcols_axis_PS.R
Normal file → Executable file
|
@ -1,7 +1,6 @@
|
|||
#########################################################
|
||||
# TASK: Adding colours to positions labels according to
|
||||
# active site residues. This is so these can be seen promptly
|
||||
# when visualising the barplot.
|
||||
# TASK: Adding colours to dfs so they can be used for plotting
|
||||
# add cols to each of the my_df* dfs
|
||||
#########################################################
|
||||
#=======================================================================
|
||||
getwd()
|
||||
|
@ -15,46 +14,45 @@ source("plotting_data.R")
|
|||
# my_df_u_lig
|
||||
# dup_muts
|
||||
|
||||
cat(paste0("Directories imported:"
|
||||
, "\ndatadir:", datadir
|
||||
, "\nindir:", indir
|
||||
, "\noutdir:", outdir
|
||||
, "\nplotdir:", plotdir))
|
||||
|
||||
cat(paste0("Variables imported:"
|
||||
, "\ndrug:", drug
|
||||
, "\ngene:", gene
|
||||
, "\ngene_match:", gene_match
|
||||
, "\nLength of upos:", length(upos)
|
||||
, "\nAngstrom symbol:", angstroms_symbol))
|
||||
|
||||
# clear excess variable
|
||||
rm(upos, dup_muts, my_df_u, my_df_u_lig)
|
||||
|
||||
# This is because we want to assign the colours to my_df
|
||||
# and then resubset accordingly for our plots to avoid multiple merges
|
||||
|
||||
#=======================================================================
|
||||
###########################
|
||||
# Read file: struct params
|
||||
###########################
|
||||
cat("Reading struct params including mcsm:", in_filename_params)
|
||||
# df to use: my_df
|
||||
# NOTE: my_df contains duplicate muts but its ok as you are only adding
|
||||
# colours to positions
|
||||
|
||||
my_df = read.csv(infile_params
|
||||
#, stringsAsFactors = F
|
||||
, header = T)
|
||||
# sanity checks: ensure my_df is ordered by position: it should be
|
||||
my_df$position; my_df$mutationinformation
|
||||
|
||||
cat("Input dimensions:", dim(my_df))
|
||||
my_df_o = my_df[order(my_df$position),]
|
||||
my_df_o$position; my_df_o$mutationinformation
|
||||
|
||||
# clear variables
|
||||
rm(in_filename_params, infile_params)
|
||||
head(my_df_o$position) == head(my_df$position)
|
||||
head(my_df_o$mutationinformation) == head(my_df$mutationinformation)
|
||||
tail(my_df_o$position) == tail(my_df$position)
|
||||
tail(my_df_o$mutationinformation) == tail(my_df$mutationinformation)
|
||||
|
||||
# quick checks
|
||||
colnames(my_df)
|
||||
str(my_df)
|
||||
my_df = my_df_o
|
||||
|
||||
# check for duplicate mutations
|
||||
if ( length(unique(my_df$mutationinformation)) != length(my_df$mutationinformation)){
|
||||
cat(paste0("CAUTION:", " Duplicate mutations identified"
|
||||
, "\nExtracting these..."))
|
||||
dup_muts = my_df[duplicated(my_df$mutationinformation),]
|
||||
dup_muts_nu = length(unique(dup_muts$mutationinformation))
|
||||
cat(paste0("\nDim of duplicate mutation df:", nrow(dup_muts)
|
||||
, "\nNo. of unique duplicate mutations:", dup_muts_nu
|
||||
, "\n\nExtracting df with unique mutations only"))
|
||||
my_df_u = my_df[!duplicated(my_df$mutationinformation),]
|
||||
}else{
|
||||
cat(paste0("No duplicate mutations detected"))
|
||||
my_df_u = my_df
|
||||
}
|
||||
|
||||
upos = unique(my_df_u$position)
|
||||
cat("Dim of clean df:"); cat(dim(my_df_u))
|
||||
cat("\nNo. of unique mutational positions:"); cat(length(upos))
|
||||
#=======================================================================
|
||||
# create a new df with unique position numbers and cols
|
||||
position = unique(my_df$position) #130
|
||||
position = unique(my_df$position)
|
||||
position_cols = as.data.frame(position)
|
||||
|
||||
head(position_cols) ; tail(position_cols)
|
||||
|
@ -143,6 +141,7 @@ mut_pos_cols = merge(position_cols, aa_cols_ref
|
|||
, all.x = TRUE)
|
||||
|
||||
head(mut_pos_cols)
|
||||
|
||||
# replace NA"s
|
||||
# :column "lab_bg" with "white"
|
||||
# : column "lab_fg" with "black"
|
||||
|
@ -165,39 +164,69 @@ head(df0$position); tail(df0$position)
|
|||
head(df1$position); tail(df1$position)
|
||||
|
||||
# should now have 3 extra columns
|
||||
my_df = merge(df0, df1
|
||||
my_df_cols = merge(df0, df1
|
||||
, by = "position"
|
||||
, all.x = TRUE)
|
||||
|
||||
# sanity check
|
||||
my_df[my_df$position == "49",]
|
||||
my_df[my_df$position == "13",]
|
||||
my_df_cols[my_df_cols$position == "49",]
|
||||
my_df_cols[my_df_cols$position == "13",]
|
||||
|
||||
rm(df0, df1)
|
||||
#===========
|
||||
# Merge 3: Merge mut_pos_cols with mcsm df_u
|
||||
# Now combined the positions with aa colours with
|
||||
# the mcsm_data
|
||||
#===========
|
||||
# dfs to merge
|
||||
df0 = my_df_u # my_df_u
|
||||
df1 = mut_pos_cols
|
||||
###########################
|
||||
# extract unique mutation entries
|
||||
###########################
|
||||
|
||||
# check the column on which merge will be performed
|
||||
head(df0$position); tail(df0$position)
|
||||
head(df1$position); tail(df1$position)
|
||||
# check for duplicate mutations
|
||||
if ( length(unique(my_df_cols$mutationinformation)) != length(my_df_cols$mutationinformation)){
|
||||
cat(paste0("\nCAUTION:", " Duplicate mutations identified"
|
||||
, "\nExtracting these..."))
|
||||
dup_muts_cols = my_df_cols[duplicated(my_df_cols$mutationinformation),]
|
||||
dup_muts_cols_nu = length(unique(dup_muts_cols$mutationinformation))
|
||||
cat(paste0("\nDim of duplicate mutation df:", nrow(dup_muts_cols)
|
||||
, "\nNo. of unique duplicate mutations:", dup_muts_cols_nu
|
||||
, "\n\nExtracting df with unique mutations only"))
|
||||
my_df_u_cols = my_df_cols[!duplicated(my_df_cols$mutationinformation),]
|
||||
}else{
|
||||
cat(paste0("\nNo duplicate mutations detected"))
|
||||
my_df_u_cols = my_df_cols
|
||||
}
|
||||
|
||||
upos = unique(my_df_u_cols$position)
|
||||
cat("\nDim of clean df:"); cat(dim(my_df_u_cols))
|
||||
cat("\nNo. of unique mutational positions:"); cat(length(upos), "\n")
|
||||
|
||||
# should now have 3 extra columns
|
||||
my_df_u = merge(df0, df1
|
||||
, by = "position"
|
||||
, all.x = TRUE)
|
||||
|
||||
# sanity check
|
||||
my_df[my_df$position == "49",]
|
||||
my_df[my_df$position == "13",]
|
||||
my_df_u_cols[my_df_u_cols$position == "49",]
|
||||
my_df_u_cols[my_df_u_cols$position == "13",]
|
||||
my_df_u_cols[my_df_u_cols$position == "103",]
|
||||
|
||||
###########################
|
||||
# extract mutations <10Angstroms
|
||||
###########################
|
||||
table(my_df_u_cols$ligand_distance<10)
|
||||
|
||||
my_df_u_cols_lig = my_df_u_cols[my_df_u_cols$ligand_distance <10,]
|
||||
angstroms_symbol = "\u212b"
|
||||
cat(paste0("There are ", nrow(my_df_u_cols_lig), " sites lying within 10", angstroms_symbol, " of the ligand"))
|
||||
|
||||
#=================
|
||||
# very important!
|
||||
#=================
|
||||
#my_axis_colours = mut_pos_cols$lab_fg # doesn't work if positions numbers are subsetted as in ligand
|
||||
# need the equivalent of the mut_pos_cols for ligand
|
||||
|
||||
# get position numbers for ligand
|
||||
lig_pos = my_df_u_cols_lig$position
|
||||
|
||||
# subset mut_pos_cols for ligand positions
|
||||
mut_pos_cols_lig = mut_pos_cols[mut_pos_cols$position %in% lig_pos,]
|
||||
#my_axis_colours = mut_pos_cols_lig$lab_fg
|
||||
|
||||
#====================================================================
|
||||
# clear variables
|
||||
rm(aa_cols_ref
|
||||
, my_df
|
||||
, df0
|
||||
, df1
|
||||
, position_cols
|
||||
|
@ -206,3 +235,7 @@ rm(aa_cols_ref
|
|||
, lab_fg
|
||||
, position)
|
||||
|
||||
#######################################################################
|
||||
# end of script
|
||||
#######################################################################
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue