added foldx_scaled and deepddg_scaled values added to combine_df.py and also used that script to merge all the dfs so that merged_df2 and merged_df3 are infact what we need for downstream processing
This commit is contained in:
parent
dda5d1ea93
commit
4ba4ff602e
5 changed files with 354 additions and 977 deletions
|
@ -8,11 +8,11 @@ setwd("~/git/LSHTM_analysis/scripts/plotting")
|
|||
getwd()
|
||||
|
||||
source("Header_TT.R")
|
||||
source("../functions/my_pairs_panel.R") # with lower panel turned off
|
||||
source("../functions/plotting_globals.R")
|
||||
source("../functions/plotting_data.R")
|
||||
source("../functions/combining_dfs_plotting.R")
|
||||
source("../functions/bp_subcolours.R")
|
||||
# source("../functions/my_pairs_panel.R") # with lower panel turned off
|
||||
# source("../functions/plotting_globals.R")
|
||||
# source("../functions/plotting_data.R")
|
||||
# source("../functions/combining_dfs_plotting.R")
|
||||
# source("../functions/bp_subcolours.R")
|
||||
|
||||
#********************
|
||||
# cmd args passed
|
||||
|
@ -41,8 +41,8 @@ import_dirs(drug, gene)
|
|||
#---------------------------
|
||||
if (!exists("infile_params") && exists("gene")){
|
||||
#if (!is.character(infile_params) && exists("gene")){ # when running as cmd
|
||||
#in_filename_params = paste0(tolower(gene), "_all_params.csv") #for pncA
|
||||
in_filename_params = paste0(tolower(gene), "_comb_afor.csv") # part combined for gid
|
||||
in_filename_params = paste0(tolower(gene), "_all_params.csv") #for pncA (and for gid finally) 10/09/21
|
||||
#in_filename_params = paste0(tolower(gene), "_comb_afor.csv") # part combined for gid
|
||||
infile_params = paste0(outdir, "/", in_filename_params)
|
||||
cat("\nInput file for mcsm comb data not specified, assuming filename: ", infile_params, "\n")
|
||||
}
|
||||
|
@ -91,369 +91,139 @@ merged_df3 = all_plot_dfs[[2]]
|
|||
merged_df2_comp = all_plot_dfs[[3]]
|
||||
merged_df3_comp = all_plot_dfs[[4]]
|
||||
#======================================================================
|
||||
# read other files
|
||||
infilename_dynamut = paste0("~/git/Data/", drug, "/output/dynamut_results/", gene
|
||||
, "_complex_dynamut_norm.csv")
|
||||
#TODO: Think! MOVE TO COMBINE or singular file for deepddg
|
||||
|
||||
infilename_dynamut2 = paste0("~/git/Data/", drug, "/output/dynamut_results/dynamut2/", gene
|
||||
, "_complex_dynamut2_norm.csv")
|
||||
#============================
|
||||
# adding deepddg scaled values
|
||||
# scale data b/w -1 and 1
|
||||
#============================
|
||||
n = which(colnames(merged_df3) == "deepddg"); n
|
||||
|
||||
infilename_mcsm_na = paste0("~/git/Data/", drug, "/output/mcsm_na_results/", gene
|
||||
, "_complex_mcsm_na_norm.csv")
|
||||
|
||||
infilename_mcsm_f_snps <- paste0("~/git/Data/", drug, "/output/", gene
|
||||
, "_mcsm_formatted_snps.csv")
|
||||
|
||||
dynamut_df = read.csv(infilename_dynamut)
|
||||
dynamut2_df = read.csv(infilename_dynamut2)
|
||||
mcsm_na_df = read.csv(infilename_mcsm_na)
|
||||
mcsm_f_snps = read.csv(infilename_mcsm_f_snps, header = F)
|
||||
names(mcsm_f_snps) = "mutationinformation"
|
||||
my_min = min(merged_df3[,n]); my_min
|
||||
my_max = max(merged_df3[,n]); my_max
|
||||
|
||||
####################################################################
|
||||
# Data for subcols barplot (~heatmpa)
|
||||
####################################################################
|
||||
# can include: mutation, or_kin, pwald, af_kin
|
||||
cols_to_select = c("mutationinformation", "drtype"
|
||||
, "wild_type"
|
||||
, "position"
|
||||
, "mutant_type"
|
||||
, "chain", "ligand_id", "ligand_distance"
|
||||
, "duet_stability_change", "duet_outcome", "duet_scaled"
|
||||
, "ligand_affinity_change", "ligand_outcome", "affinity_scaled"
|
||||
, "ddg_foldx", "foldx_scaled", "foldx_outcome"
|
||||
, "deepddg", "deepddg_outcome" # comment out as not available for pnca
|
||||
, "asa", "rsa", "rd_values", "kd_values"
|
||||
, "af", "or_mychisq", "pval_fisher"
|
||||
, "or_fisher", "or_logistic", "pval_logistic"
|
||||
, "wt_prop_water", "mut_prop_water", "wt_prop_polarity", "mut_prop_polarity"
|
||||
, "wt_calcprop", "mut_calcprop")
|
||||
merged_df3$deepddg_scaled = ifelse(merged_df3[,n] < 0
|
||||
, merged_df3[,n]/abs(my_min)
|
||||
, merged_df3[,n]/my_max)
|
||||
# sanity check
|
||||
my_min = min(merged_df3$deepddg_scaled); my_min
|
||||
my_max = max(merged_df3$deepddg_scaled); my_max
|
||||
|
||||
#=======================
|
||||
# Data for sub colours
|
||||
# barplot: PS
|
||||
#=======================
|
||||
|
||||
cat("\nNo. of cols to select:", length(cols_to_select))
|
||||
|
||||
subcols_df_ps = merged_df3[, cols_to_select]
|
||||
|
||||
cat("\nNo of unique positions for ps:"
|
||||
, length(unique(subcols_df_ps$position)))
|
||||
|
||||
# add count_pos col that counts the no. of nsSNPS at a position
|
||||
setDT(subcols_df_ps)[, pos_count := .N, by = .(position)]
|
||||
|
||||
# should be a factor
|
||||
if (is.factor(subcols_df_ps$duet_outcome)){
|
||||
cat("\nDuet_outcome is factor")
|
||||
table(subcols_df_ps$duet_outcome)
|
||||
if (my_min == -1 && my_max == 1){
|
||||
cat("\nPASS: DeepDDG successfully scaled b/w -1 and 1"
|
||||
#, "\nProceeding with assigning deep outcome category")
|
||||
, "\n")
|
||||
}else{
|
||||
cat("\nConverting duet_outcome to factor")
|
||||
subcols_df_ps$duet_outcome = as.factor(subcols_df_ps$duet_outcome)
|
||||
table(subcols_df_ps$duet_outcome)
|
||||
cat("\nFAIL: could not scale DeepDDG ddg values"
|
||||
, "Aborting!")
|
||||
}
|
||||
|
||||
# should be -1 and 1
|
||||
min(subcols_df_ps$duet_scaled)
|
||||
max(subcols_df_ps$duet_scaled)
|
||||
|
||||
tapply(subcols_df_ps$duet_scaled, subcols_df_ps$duet_outcome, min)
|
||||
tapply(subcols_df_ps$duet_scaled, subcols_df_ps$duet_outcome, max)
|
||||
####################################################################
|
||||
# Data for combining other dfs
|
||||
####################################################################
|
||||
|
||||
# check unique values in normalised data
|
||||
cat("\nNo. of unique values in duet scaled, no rounding:"
|
||||
, length(unique(subcols_df_ps$duet_scaled)))
|
||||
source("other_dfs_data.R")
|
||||
|
||||
# No rounding
|
||||
my_grp = subcols_df_ps$duet_scaled; length(my_grp)
|
||||
####################################################################
|
||||
# Data for subcols barplot (~heatmap)
|
||||
####################################################################
|
||||
|
||||
# Add rounding is to be used
|
||||
n = 3
|
||||
subcols_df_ps$duet_scaledR = round(subcols_df_ps$duet_scaled, n)
|
||||
|
||||
cat("\nNo. of unique values in duet scaled", n, "places rounding:"
|
||||
, length(unique(subcols_df_ps$duet_scaledR)))
|
||||
|
||||
my_grp_r = subcols_df_ps$duet_scaledR # rounding
|
||||
|
||||
# Add grp cols
|
||||
subcols_df_ps$group <- paste0(subcols_df_ps$duet_outcome, "_", my_grp, sep = "")
|
||||
subcols_df_ps$groupR <- paste0(subcols_df_ps$duet_outcome, "_", my_grp_r, sep = "")
|
||||
|
||||
# Call the function to create the palette based on the group defined above
|
||||
subcols_ps <- ColourPalleteMulti(subcols_df_ps, "duet_outcome", "my_grp")
|
||||
subcolsR_ps <- ColourPalleteMulti(subcols_df_ps, "duet_outcome", "my_grp_r")
|
||||
|
||||
print(paste0("Colour palette generated for my_grp: ", length(subcols_ps), " colours"))
|
||||
print(paste0("Colour palette generated for my_grp_r: ", length(subcolsR_ps), " colours"))
|
||||
source("coloured_bp_data.R")
|
||||
|
||||
####################################################################
|
||||
# Data for logoplots
|
||||
####################################################################
|
||||
#-------------------------
|
||||
# choose df for logoplot
|
||||
#-------------------------
|
||||
logo_data = merged_df3
|
||||
#logo_data = merged_df3_comp
|
||||
|
||||
# quick checks
|
||||
colnames(logo_data)
|
||||
str(logo_data)
|
||||
source("logo_data.R")
|
||||
|
||||
c1 = unique(logo_data$position)
|
||||
nrow(logo_data)
|
||||
cat("No. of rows in my_data:", nrow(logo_data)
|
||||
, "\nDistinct positions corresponding to snps:", length(c1)
|
||||
, "\n===========================================================")
|
||||
#=======================================================================
|
||||
#==================
|
||||
# logo data: OR
|
||||
#==================
|
||||
foo = logo_data[, c("position"
|
||||
, "mutant_type","duet_scaled", "or_mychisq"
|
||||
, "mut_prop_polarity", "mut_prop_water")]
|
||||
s1 = c("\nSuccessfully sourced logo_data.R")
|
||||
cat(s1)
|
||||
|
||||
logo_data$log10or = log10(logo_data$or_mychisq)
|
||||
logo_data_plot = logo_data[, c("position"
|
||||
, "mutant_type", "or_mychisq", "log10or")]
|
||||
|
||||
logo_data_plot_or = logo_data[, c("position", "mutant_type", "or_mychisq")]
|
||||
wide_df_or <- logo_data_plot_or %>% spread(position, or_mychisq, fill = 0.0)
|
||||
|
||||
wide_df_or = as.matrix(wide_df_or)
|
||||
rownames(wide_df_or) = wide_df_or[,1]
|
||||
dim(wide_df_or)
|
||||
wide_df_or = wide_df_or[,-1]
|
||||
str(wide_df_or)
|
||||
|
||||
position_or = as.numeric(colnames(wide_df_or))
|
||||
|
||||
#==================
|
||||
# logo data: logOR
|
||||
#==================
|
||||
logo_data_plot_logor = logo_data[, c("position", "mutant_type", "log10or")]
|
||||
wide_df_logor <- logo_data_plot_logor %>% spread(position, log10or, fill = 0.0)
|
||||
|
||||
wide_df_logor = as.matrix(wide_df_logor)
|
||||
|
||||
rownames(wide_df_logor) = wide_df_logor[,1]
|
||||
wide_df_logor = subset(wide_df_logor, select = -c(1) )
|
||||
colnames(wide_df_logor)
|
||||
wide_df_logor_m = data.matrix(wide_df_logor)
|
||||
|
||||
rownames(wide_df_logor_m)
|
||||
colnames(wide_df_logor_m)
|
||||
|
||||
position_logor = as.numeric(colnames(wide_df_logor_m))
|
||||
|
||||
#===============================
|
||||
# logo data: multiple nsSNPs (>1)
|
||||
#=================================
|
||||
#require(data.table)
|
||||
|
||||
# get freq count of positions so you can subset freq<1
|
||||
setDT(logo_data)[, mut_pos_occurrence := .N, by = .(position)]
|
||||
|
||||
table(logo_data$position)
|
||||
table(logo_data$mut_pos_occurrence)
|
||||
|
||||
max_mut = max(table(logo_data$position))
|
||||
|
||||
# extract freq_pos > 1
|
||||
my_data_snp = logo_data[logo_data$mut_pos_occurrence!=1,]
|
||||
u = unique(my_data_snp$position)
|
||||
max_mult_mut = max(table(my_data_snp$position))
|
||||
|
||||
if (nrow(my_data_snp) == nrow(logo_data) - table(logo_data$mut_pos_occurrence)[[1]] ){
|
||||
|
||||
cat("PASS: positions with multiple muts extracted"
|
||||
, "\nNo. of mutations:", nrow(my_data_snp)
|
||||
, "\nNo. of positions:", length(u)
|
||||
, "\nMax no. of muts at any position", max_mult_mut)
|
||||
}else{
|
||||
cat("FAIL: positions with multiple muts could NOT be extracted"
|
||||
, "\nExpected:",nrow(logo_data) - table(logo_data$mut_pos_occurrence)[[1]]
|
||||
, "\nGot:", nrow(my_data_snp) )
|
||||
}
|
||||
|
||||
cat("\nNo. of sites with only 1 mutations:", table(logo_data$mut_pos_occurrence)[[1]])
|
||||
|
||||
#--------------------------------------
|
||||
# matrix for_mychisq mutant type
|
||||
# frequency of mutant type by position
|
||||
#---------------------------------------
|
||||
table(my_data_snp$mutant_type, my_data_snp$position)
|
||||
tab_mt = table(my_data_snp$mutant_type, my_data_snp$position)
|
||||
class(tab_mt)
|
||||
|
||||
# unclass to convert to matrix
|
||||
tab_mt = unclass(tab_mt)
|
||||
tab_mt = as.matrix(tab_mt, rownames = T)
|
||||
|
||||
# should be TRUE
|
||||
is.matrix(tab_mt)
|
||||
|
||||
rownames(tab_mt) #aa
|
||||
colnames(tab_mt) #pos
|
||||
|
||||
#-------------------------------------
|
||||
# matrix for wild type
|
||||
# frequency of wild type by position
|
||||
#-------------------------------------
|
||||
tab_wt = table(my_data_snp$wild_type, my_data_snp$position); tab_wt
|
||||
tab_wt = unclass(tab_wt)
|
||||
|
||||
# remove wt duplicates
|
||||
wt = my_data_snp[, c("position", "wild_type")]
|
||||
wt = wt[!duplicated(wt),]
|
||||
|
||||
tab_wt = table(wt$wild_type, wt$position); tab_wt # should all be 1
|
||||
|
||||
rownames(tab_wt)
|
||||
rownames(tab_wt)
|
||||
|
||||
identical(colnames(tab_mt), colnames(tab_wt))
|
||||
identical(ncol(tab_mt), ncol(tab_wt))
|
||||
|
||||
#----------------------------------
|
||||
# logo data OR: multiple nsSNPs (>1)
|
||||
#----------------------------------
|
||||
logo_data_or_mult = my_data_snp[, c("position", "mutant_type", "or_mychisq")]
|
||||
#wide_df_or <- logo_data_or %>% spread(position, or_mychisq, fill = 0.0)
|
||||
wide_df_or_mult <- logo_data_or_mult %>% spread(position, or_mychisq, fill = NA)
|
||||
|
||||
wide_df_or_mult = as.matrix(wide_df_or_mult)
|
||||
rownames(wide_df_or_mult) = wide_df_or_mult[,1]
|
||||
wide_df_or_mult = wide_df_or_mult[,-1]
|
||||
str(wide_df_or_mult)
|
||||
|
||||
position_or_mult = as.numeric(colnames(wide_df_or_mult))
|
||||
|
||||
####################################################################
|
||||
# Data for Corrplots
|
||||
####################################################################
|
||||
cat("\n=========================================="
|
||||
, "\nCORR PLOTS data: PS"
|
||||
, "\n===========================================")
|
||||
|
||||
df_ps = merged_df2
|
||||
|
||||
#--------------------
|
||||
# adding log cols : NEW UNCOMMENT
|
||||
#--------------------
|
||||
#df_ps$log10_or_mychisq = log10(df_ps$or_mychisq)
|
||||
#df_ps$neglog_pval_fisher = -log10(df_ps$pval_fisher)
|
||||
|
||||
##df_ps$log10_or_kin = log10(df_ps$or_kin)
|
||||
##df_ps$neglog_pwald_kin = -log10(df_ps$pwald_kin)
|
||||
|
||||
#df_ps$mutation_info_labels = ifelse(df_ps$mutation_info == dr_muts_col, 1, 0)
|
||||
|
||||
#----------------------------
|
||||
# columns for corr plots:PS
|
||||
#----------------------------
|
||||
# subset data to generate pairwise correlations
|
||||
cols_to_select = c("mutationinformation"
|
||||
, "duet_scaled"
|
||||
, "foldx_scaled"
|
||||
#, "mutation_info_labels"
|
||||
, "asa"
|
||||
, "rsa"
|
||||
, "rd_values"
|
||||
, "kd_values"
|
||||
, "log10_or_mychisq"
|
||||
, "neglog_pval_fisher"
|
||||
##, "or_kin"
|
||||
##, "neglog_pwald_kin"
|
||||
, "af"
|
||||
##, "af_kin"
|
||||
, "duet_outcome"
|
||||
, drug)
|
||||
|
||||
corr_data_ps = df_ps[cols_to_select]
|
||||
|
||||
dim(corr_data_ps)
|
||||
|
||||
#--------------------------------------
|
||||
# assign nice colnames (for display)
|
||||
#--------------------------------------
|
||||
my_corr_colnames = c("Mutation"
|
||||
, "DUET"
|
||||
, "FoldX"
|
||||
#, "Mutation class"
|
||||
, "ASA"
|
||||
, "RSA"
|
||||
, "RD"
|
||||
, "KD"
|
||||
, "Log (OR)"
|
||||
, "-Log (P)"
|
||||
##, "Adjusted (OR)"
|
||||
##, "-Log (P wald)"
|
||||
, "MAF"
|
||||
##, "AF_kin"
|
||||
, "duet_outcome"
|
||||
, drug)
|
||||
|
||||
length(my_corr_colnames)
|
||||
|
||||
colnames(corr_data_ps)
|
||||
colnames(corr_data_ps) <- my_corr_colnames
|
||||
colnames(corr_data_ps)
|
||||
|
||||
start = 1
|
||||
end = which(colnames(corr_data_ps) == drug); end # should be the last column
|
||||
offset = 1
|
||||
|
||||
#===========================
|
||||
# Corr data for plots: PS
|
||||
# big_df ps: ~ merged_df2
|
||||
#===========================
|
||||
|
||||
#corr_ps_df2 = corr_data_ps[start:(end-offset)] # without drug
|
||||
corr_ps_df2 = corr_data_ps[start:end]
|
||||
head(corr_ps_df2)
|
||||
|
||||
#===========================
|
||||
# Corr data for plots: PS
|
||||
# short_df ps: ~merged_df3
|
||||
#===========================
|
||||
corr_ps_df3 = corr_ps_df2[!duplicated(corr_ps_df2$Mutation),]
|
||||
|
||||
na_or = sum(is.na(corr_ps_df3$`Log (OR)`))
|
||||
check1 = nrow(corr_ps_df3) - na_or
|
||||
|
||||
##na_adj_or = sum(is.na(corr_ps_df3$`adjusted (OR)`))
|
||||
##check2 = nrow(corr_ps_df3) - na_adj_or
|
||||
|
||||
if (nrow(corr_ps_df3) == nrow(merged_df3) && nrow(merged_df3_comp) == check1) {
|
||||
cat( "\nPASS: No. of rows for corr_ps_df3 match"
|
||||
, "\nPASS: No. of OR values checked: " , check1)
|
||||
} else {
|
||||
cat("\nFAIL: Numbers mismatch:"
|
||||
, "\nExpected nrows: ", nrow(merged_df3)
|
||||
, "\nGot: ", nrow(corr_ps_df3)
|
||||
, "\nExpected OR values: ", nrow(merged_df3_comp)
|
||||
, "\nGot: ", check1)
|
||||
}
|
||||
|
||||
rm(foo)
|
||||
####################################################################
|
||||
# Data for DM OM Plots: Long format dfs
|
||||
####################################################################
|
||||
|
||||
source("other_plots_data.R")
|
||||
#source("other_plots_data.R")
|
||||
|
||||
source("dm_om_data.R")
|
||||
|
||||
s2 = c("\nSuccessfully sourced other_plots_data.R")
|
||||
cat(s2)
|
||||
|
||||
####################################################################
|
||||
# Data for Lineage barplots: WF and LF dfs
|
||||
####################################################################
|
||||
|
||||
source("lineage_bp_data.R")
|
||||
source("lineage_data.R")
|
||||
|
||||
s3 = c("\nSuccessfully sourced lineage_data.R")
|
||||
cat(s3)
|
||||
|
||||
####################################################################
|
||||
# Data for corr plots:
|
||||
####################################################################
|
||||
# make sure the above script works because merged_df2_combined is needed
|
||||
source("corr_data.R")
|
||||
|
||||
s4 = c("\nSuccessfully sourced corr_data.R")
|
||||
cat(s4)
|
||||
|
||||
########################################################################
|
||||
# End of script
|
||||
########################################################################
|
||||
if ( all( length(s1), length(s2), length(s3), length(s4) ) >0 ){
|
||||
cat(
|
||||
"\n##################################################"
|
||||
, "\nSuccessful: get_plotting_dfs.R worked!"
|
||||
, "\n###################################################\n")
|
||||
} else {
|
||||
cat(
|
||||
"\n#################################################"
|
||||
, "\nFAIL: get_plotting_dfs.R didn't complete fully!Please check"
|
||||
, "\n###################################################\n" )
|
||||
}
|
||||
|
||||
########################################################################
|
||||
# clear excess variables
|
||||
rm(c1, c2, c3, c4, check1
|
||||
, curr_count, curr_total
|
||||
, cols_check
|
||||
, cols_to_select
|
||||
, cols_to_select_deepddg
|
||||
, cols_to_select_duet
|
||||
, cols_to_select_dynamut
|
||||
, cols_to_select_dynamut2
|
||||
, cols_to_select_encomddg
|
||||
, cols_to_select_encomdds
|
||||
, cols_to_select_mcsm
|
||||
, cols_to_select_mcsm_na
|
||||
, cols_to_select_sdm
|
||||
, infile_metadata
|
||||
, infile_params
|
||||
#, infilename_dynamut
|
||||
#, infilename_dynamut2
|
||||
#, infilename_mcsm_f_snps
|
||||
#, infilename_mcsm_na
|
||||
)
|
||||
|
||||
cat("\n######################################################\n"
|
||||
, "\nSuccessful: get_plotting_dfs.R worked!"
|
||||
, "\n###################################################\n")
|
||||
rm(pivot_cols
|
||||
, pivot_cols_deepddg
|
||||
, pivot_cols_duet
|
||||
, pivot_cols_dynamut
|
||||
, pivot_cols_dynamut2
|
||||
, pivot_cols_encomddg
|
||||
, pivot_cols_encomdds
|
||||
, pivot_cols_foldx
|
||||
, pivot_cols_mcsm
|
||||
, pivot_cols_mcsm_na
|
||||
, pivot_cols_n
|
||||
, pivot_cols_sdm)
|
||||
|
||||
rm(expected_cols
|
||||
, expected_ncols
|
||||
, expected_rows
|
||||
, expected_rows_lf
|
||||
, fact_cols)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue