174 lines
7.1 KiB
R
174 lines
7.1 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")
|
|
|
|
all_colnames$`colnames(df3)`[grep("scaled", all_colnames$`colnames(df3)`)]
|
|
scaled_cols = c("duet_scaled" , "duet_stability_change"
|
|
, "deepddg_scaled" , "deepddg"
|
|
, "ddg_dynamut2_scaled" , "ddg_dynamut2"
|
|
, "foldx_scaled" , "ddg_foldx"
|
|
, "affinity_scaled" , "ligand_affinity_change"
|
|
, "mmcsm_lig_scaled" , "mmcsm_lig"
|
|
, "mcsm_ppi2_scaled" , "mcsm_ppi2_affinity"
|
|
, "mcsm_na_scaled" , "mcsm_na_affinity"
|
|
#, "consurf_scaled" , "consurf_score"
|
|
#, "snap2_scaled" , "snap2_score"
|
|
#, "provean_scaled" , "provean_score"
|
|
)
|
|
all_colnames$`colnames(df3)`[grep("outcome", all_colnames$`colnames(df3)`)]
|
|
outcome_cols_aff = c("duet_outcome"
|
|
, "deepddg_outcome"
|
|
, "ddg_dynamut2_outcome"
|
|
, "foldx_outcome"
|
|
#, "ddg_foldx", "foldx_scaled"
|
|
, "ligand_outcome"
|
|
, "mmcsm_lig_outcome"
|
|
, "mcsm_ppi2_outcome"
|
|
, "mcsm_na_outcome"
|
|
# consurf outcome doesn't exist
|
|
#,"provean_outcome"
|
|
#,"snap2_outcome"
|
|
)
|
|
cols_to_consider = colnames(df3)[colnames(df3)%in%c(common_cols, scaled_cols,outcome_cols)]
|
|
cols_to_extract = cols_to_consider[cols_to_consider%in%c(common_cols, outcome_cols)]
|
|
|
|
##############################################################
|
|
#####################
|
|
# Ensemble 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] <- sapply(df3_plot[, outcome_cols]
|
|
, 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)
|
|
|
|
# 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
|
|
#===============================================================
|