212 lines
8.4 KiB
R
212 lines
8.4 KiB
R
#source("~/git/LSHTM_analysis/config/pnca.R")
|
|
#source("~/git/LSHTM_analysis/config/alr.R")
|
|
#source("~/git/LSHTM_analysis/config/gid.R")
|
|
#source("~/git/LSHTM_analysis/config/embb.R")
|
|
#source("~/git/LSHTM_analysis/config/katg.R")
|
|
#source("~/git/LSHTM_analysis/config/rpob.R")
|
|
|
|
source("/home/tanu/git/LSHTM_analysis/my_header.R")
|
|
#########################################################
|
|
# TASK: Generate averaged stability values
|
|
# across all stability tools
|
|
# for a given structure
|
|
#########################################################
|
|
|
|
#=======
|
|
# output
|
|
#=======
|
|
outdir_images = paste0("~/git/Writing/thesis/images/results/", tolower(gene))
|
|
outfile_mean_ens_st_aff = paste0(outdir_images, "/", tolower(gene)
|
|
, "_mean_ens_stability.csv")
|
|
print(paste0("Output file:", outfile_mean_ens_st_aff))
|
|
|
|
#%%===============================================================
|
|
|
|
#=============
|
|
# Input
|
|
#=============
|
|
df3_filename = paste0("/home/tanu/git/Data/", drug, "/output/", tolower(gene), "_merged_df3.csv")
|
|
df3 = read.csv(df3_filename)
|
|
length(df3$mutationinformation)
|
|
|
|
# mut_info checks
|
|
table(df3$mutation_info)
|
|
table(df3$mutation_info_orig)
|
|
table(df3$mutation_info_labels_orig)
|
|
|
|
# used in plots and analyses
|
|
table(df3$mutation_info_labels) # different, and matches dst_mode
|
|
table(df3$dst_mode)
|
|
|
|
# create column based on dst mode with different colname
|
|
table(is.na(df3$dst))
|
|
table(is.na(df3$dst_mode))
|
|
|
|
#===============
|
|
# Create column: sensitivity mapped to dst_mode
|
|
#===============
|
|
df3$sensitivity = ifelse(df3$dst_mode == 1, "R", "S")
|
|
table(df3$sensitivity)
|
|
|
|
length(unique((df3$mutationinformation)))
|
|
all_colnames = as.data.frame(colnames(df3))
|
|
common_cols = c("mutationinformation"
|
|
, "position"
|
|
, "dst_mode"
|
|
, "mutation_info_labels"
|
|
, "sensitivity"
|
|
, "ligand_distance"
|
|
, "interface_dist")
|
|
|
|
all_colnames$`colnames(df3)`[grep("scaled", all_colnames$`colnames(df3)`)]
|
|
all_colnames$`colnames(df3)`[grep("outcome", all_colnames$`colnames(df3)`)]
|
|
|
|
#===================
|
|
# stability cols
|
|
#===================
|
|
raw_cols_stability = c("duet_stability_change"
|
|
, "deepddg"
|
|
, "ddg_dynamut2"
|
|
, "ddg_foldx")
|
|
|
|
scaled_cols_stability = c("duet_scaled"
|
|
, "deepddg_scaled"
|
|
, "ddg_dynamut2_scaled"
|
|
, "foldx_scaled")
|
|
|
|
outcome_cols_stability = c("duet_outcome"
|
|
, "deepddg_outcome"
|
|
, "ddg_dynamut2_outcome"
|
|
, "foldx_outcome")
|
|
|
|
#===================
|
|
# affinity cols
|
|
#===================
|
|
raw_cols_affinity = c("ligand_affinity_change"
|
|
, "mmcsm_lig"
|
|
, "mcsm_ppi2_affinity"
|
|
, "mcsm_na_affinity")
|
|
|
|
scaled_cols_affinity = c("affinity_scaled"
|
|
, "mmcsm_lig_scaled"
|
|
, "mcsm_ppi2_scaled"
|
|
, "mcsm_na_scaled" )
|
|
|
|
outcome_cols_affinity = c( "ligand_outcome"
|
|
, "mmcsm_lig_outcome"
|
|
, "mcsm_ppi2_outcome"
|
|
, "mcsm_na_outcome")
|
|
|
|
#===================
|
|
# conservation cols
|
|
#===================
|
|
# raw_cols_conservation = c("consurf_score"
|
|
# , "snap2_score"
|
|
# , "provean_score")
|
|
#
|
|
# scaled_cols_conservation = c("consurf_scaled"
|
|
# , "snap2_scaled"
|
|
# , "provean_scaled")
|
|
#
|
|
# # CANNOT strictly be used, as categories are not identical with conssurf missing altogether
|
|
# outcome_cols_conservation = c("provean_outcome"
|
|
# , "snap2_outcome"
|
|
# #consurf outcome doesn't exist
|
|
# )
|
|
|
|
###########################################################
|
|
cols_to_consider = colnames(df3)[colnames(df3)%in%c(common_cols
|
|
, raw_cols_stability
|
|
, scaled_cols_stability
|
|
, outcome_cols_stability
|
|
, raw_cols_affinity
|
|
, scaled_cols_affinity
|
|
, outcome_cols_affinity)]
|
|
|
|
cols_to_extract = cols_to_consider[cols_to_consider%in%c(common_cols
|
|
, outcome_cols_stability)]
|
|
##############################################################
|
|
#####################
|
|
# Ensemble stability: outcome_cols_stability
|
|
#####################
|
|
# extract outcome cols and map numeric values to the categories
|
|
# Destabilising == 0, and stabilising == 1, so rescaling can let -1 be destabilising
|
|
df3_plot = df3[, cols_to_extract]
|
|
|
|
# assign numeric values to outcome
|
|
df3_plot[, outcome_cols_stability] <- sapply(df3_plot[, outcome_cols_stability]
|
|
, function(x){ifelse(x == "Destabilising", 0, 1)})
|
|
table(df3$duet_outcome)
|
|
table(df3_plot$duet_outcome)
|
|
#=====================================
|
|
# Stability (4 cols): average the scores
|
|
# across predictors ==> average by
|
|
# position ==> scale b/w -1 and 1
|
|
|
|
# column to average: ens_stability
|
|
#=====================================
|
|
cols_to_average = which(colnames(df3_plot)%in%outcome_cols_stability)
|
|
|
|
# ensemble average across predictors
|
|
df3_plot$ens_stability = rowMeans(df3_plot[,cols_to_average])
|
|
|
|
head(df3_plot$position); head(df3_plot$mutationinformation)
|
|
head(df3_plot$ens_stability)
|
|
table(df3_plot$ens_stability)
|
|
|
|
# ensemble average of predictors by position
|
|
mean_ens_stability_by_position <- df3_plot %>%
|
|
dplyr::group_by(position) %>%
|
|
dplyr::summarize(avg_ens_stability = mean(ens_stability))
|
|
|
|
# REscale b/w -1 and 1
|
|
#en_stab_min = min(mean_ens_stability_by_position['avg_ens_stability'])
|
|
#en_stab_max = max(mean_ens_stability_by_position['avg_ens_stability'])
|
|
|
|
# scale the average stability value between -1 and 1
|
|
# mean_ens_by_position['averaged_stability3_scaled'] = lapply(mean_ens_by_position['averaged_stability3']
|
|
# , function(x) ifelse(x < 0, x/abs(en3_min), x/en3_max))
|
|
|
|
mean_ens_stability_by_position['avg_ens_stability_scaled'] = lapply(mean_ens_stability_by_position['avg_ens_stability']
|
|
, function(x) {
|
|
scales::rescale(x, to = c(-1,1)
|
|
#, from = c(en_stab_min,en_stab_max))
|
|
, from = c(0,1))
|
|
})
|
|
cat(paste0('Average stability scores:\n'
|
|
, head(mean_ens_stability_by_position['avg_ens_stability'])
|
|
, '\n---------------------------------------------------------------'
|
|
, '\nAverage stability scaled scores:\n'
|
|
, head(mean_ens_stability_by_position['avg_ens_stability_scaled'])))
|
|
|
|
# convert to a data frame
|
|
mean_ens_stability_by_position = as.data.frame(mean_ens_stability_by_position)
|
|
|
|
#FIXME: sanity checks
|
|
# TODO: predetermine the bounds
|
|
# l_bound_ens = min(mean_ens_stability_by_position['avg_ens_stability_scaled'])
|
|
# u_bound_ens = max(mean_ens_stability_by_position['avg_ens_stability_scaled'])
|
|
#
|
|
# if ( (l_bound_ens == -1) && (u_bound_ens == 1) ){
|
|
# cat(paste0("PASS: ensemble stability scores averaged by position and then scaled"
|
|
# , "\nmin ensemble averaged stability: ", l_bound_ens
|
|
# , "\nmax ensemble averaged stability: ", u_bound_ens))
|
|
# }else{
|
|
# cat(paste0("FAIL: avergaed duet scores could not be scaled b/w -1 and 1"
|
|
# , "\nmin ensemble averaged stability: ", l_bound_ens
|
|
# , "\nmax ensemble averaged stability: ", u_bound_ens))
|
|
# quit()
|
|
# }
|
|
##################################################################
|
|
# output
|
|
#write.csv(combined_df, outfile_mean_ens_st_aff
|
|
write.csv(mean_ens_stability_by_position
|
|
, outfile_mean_ens_st_aff
|
|
, row.names = F)
|
|
cat("Finished writing file:\n"
|
|
, outfile_mean_ens_st_aff
|
|
, "\nNo. of rows:", nrow(mean_ens_stability_by_position)
|
|
, "\nNo. of cols:", ncol(mean_ens_stability_by_position))
|
|
|
|
# end of script
|
|
#===============================================================
|