separating mcsm_mean_stability_ensemble from combined script

This commit is contained in:
Tanushree Tunstall 2022-07-31 19:24:35 +01:00
parent 06e5363112
commit 1bf66b145c
2 changed files with 99 additions and 180 deletions

View file

@ -10,7 +10,7 @@
# rendering on chimera
# read mcsm mean stability value files
# extract the respecitve mean values and assign to the
# extract the respective mean values and assign to the
# b-factor column within their respective pdbs
# generate some distribution plots for inspection
@ -52,7 +52,8 @@ cat(gene_match)
datadir = paste0("~/git/Data")
indir = paste0(datadir, "/", drug, "/input")
outdir = paste0("~/git/Data", "/", drug, "/output")
outdir_plots = paste0("~/git/Data", "/", drug, "/output/plots")
#outdir_plots = paste0("~/git/Data", "/", drug, "/output/plots")
outdir_plots = paste0("~/git/Writing/thesis/images/results/", tolower(gene))
#======
# input
@ -61,14 +62,19 @@ in_filename_pdb = paste0(tolower(gene), "_complex.pdb")
infile_pdb = paste0(indir, "/", in_filename_pdb)
cat(paste0("Input file:", infile_pdb) )
in_filename_mean_stability = paste0(tolower(gene), "_mean_stability.csv")
infile_mean_stability = paste0(outdir, "/", in_filename_mean_stability)
#in_filename_mean_stability = paste0(tolower(gene), "_mean_stability.csv")
#infile_mean_stability = paste0(outdir, "/", in_filename_mean_stability)
in_filename_mean_stability = paste0(tolower(gene), "_mean_ens_stab_aff.csv")
infile_mean_stability = paste0(outdir_plots, "/", in_filename_mean_stability)
cat(paste0("Input file:", infile_mean_stability) )
#=======
# output
#=======
out_filename_duet_mspdb = paste0(tolower(gene), "_complex_bduet_ms.pdb")
#out_filename_duet_mspdb = paste0(tolower(gene), "_complex_bduet_ms.pdb")
out_filename_duet_mspdb = paste0(tolower(gene), "_complex_b_stab_ms.pdb")
outfile_duet_mspdb = paste0(outdir_plots, "/", out_filename_duet_mspdb)
print(paste0("Output file:", outfile_duet_mspdb))
@ -77,6 +83,8 @@ outfile_lig_mspdb = paste0(outdir_plots, "/", out_filename_lig_mspdb)
print(paste0("Output file:", outfile_lig_mspdb))
#%%===============================================================
#NOTE: duet here refers to the ensemble stability values
###########################
# Read file: average stability values
# or mcsm_normalised file
@ -133,17 +141,17 @@ par(oma = c(3,2,3,0)
#, mfrow = c(3,2)
, mfrow = c(3,4))
#************
#=============
# Row 1 plots: original B-factors
# duet and affinity
#************
#=============
hist(df_duet$b
, xlab = ""
, main = "Bfactor duet")
, main = "Bfactor stability")
plot(density(df_duet$b)
, xlab = ""
, main = "Bfactor duet")
, main = "Bfactor stability")
hist(df_lig$b
@ -154,32 +162,36 @@ plot(density(df_lig$b)
, xlab = ""
, main = "Bfactor affinity")
#************
#=============
# Row 2 plots: original mean stability values
# duet and affinity
#************
hist(my_df$averaged_duet
#=============
#hist(my_df$averaged_duet
hist(my_df$avg_ens_stability_scaled
, xlab = ""
, main = "mean duet values")
, main = "mean stability values")
plot(density(my_df$averaged_duet)
#plot(density(my_df$averaged_duet)
plot(density(my_df$avg_ens_stability_scaled)
, xlab = ""
, main = "mean duet values")
, main = "mean stability values")
hist(my_df$averaged_affinity
#hist(my_df$averaged_affinity
hist(my_df$avg_ens_affinity_scaled
, xlab = ""
, main = "mean affinity values")
plot(density(my_df$averaged_affinity)
#plot(density(my_df$averaged_affinity)
plot(density(my_df$avg_ens_affinity_scaled)
, xlab = ""
, main = "mean affinity values")
#************
#==============
# Row 3 plots: replaced B-factors with mean stability values
# After actual replacement in the b factor column
#*************
#=========================================================
#===============
################################################################
#=========
# step 0_P1: DONT RUN once you have double checked the matched output
#=========
@ -192,49 +204,54 @@ plot(density(my_df$averaged_affinity)
#=========
# Be brave and replace in place now (don"t run sanity check)
# this makes all the B-factor values in the non-matched positions as NA
df_duet$b = my_df$averaged_duet_scaled[match(df_duet$resno, my_df$position)]
df_lig$b = my_df$averaged_affinity_scaled[match(df_lig$resno, my_df$position)]
#df_duet$b = my_df$averaged_duet_scaled[match(df_duet$resno, my_df$position)]
#df_lig$b = my_df$averaged_affinity_scaled[match(df_lig$resno, my_df$position)]
df_duet$b = my_df$avg_ens_stability_scaled[match(df_duet$resno, my_df$position)]
df_lig$b = my_df$avg_ens_affinity_scaled[match(df_lig$resno, my_df$position)]
#=========
# step 2_P1
#=========
# count NA in Bfactor
b_na_duet = sum(is.na(df_duet$b)) ; b_na_duet
b_na_lig = sum(is.na(df_lig$b)) ; b_na_lig
b_na_lig = sum(is.na(df_lig$b)) ; b_na_lig
# count number of 0"s in Bactor
sum(df_duet$b == 0)
sum(df_lig$b == 0)
sum(df_lig$b == 0)
# replace all NA in b factor with 0
df_duet$b[is.na(df_duet$b)] = 0
df_lig$b[is.na(df_lig$b)] = 0
na_rep = 2
df_duet$b[is.na(df_duet$b)] = na_rep
df_lig$b[is.na(df_lig$b)] = na_rep
# sanity check: should be 0 and True
# duet and lig
if ( (sum(df_duet$b == 0) == b_na_duet) && (sum(df_lig$b == 0) == b_na_lig) ) {
print ("PASS: NA's replaced with 0s successfully in df_duet and df_lig")
} else {
print("FAIL: NA replacement in df_duet NOT successful")
quit()
}
# # sanity check: should be 0 and True
# # duet and lig
# if ( (sum(df_duet$b == na_rep) == b_na_duet) && (sum(df_lig$b == na_rep) == b_na_lig) ) {
# print ("PASS: NA's replaced with 0s successfully in df_duet and df_lig")
# } else {
# print("FAIL: NA replacement in df_duet NOT successful")
# quit()
# }
#
# max(df_duet$b); min(df_duet$b)
#
# # sanity checks: should be True
# if( (max(df_duet$b) == max(my_df$avg_ens_stability_scaled)) & (min(df_duet$b) == min(my_df$avg_ens_stability_scaled)) ){
# print("PASS: B-factors replaced correctly in df_duet")
# } else {
# print ("FAIL: To replace B-factors in df_duet")
# quit()
# }
max(df_duet$b); min(df_duet$b)
# sanity checks: should be True
if( (max(df_duet$b) == max(my_df$averaged_duet_scaled)) & (min(df_duet$b) == min(my_df$averaged_duet_scaled)) ){
print("PASS: B-factors replaced correctly in df_duet")
} else {
print ("FAIL: To replace B-factors in df_duet")
quit()
}
if( (max(df_lig$b) == max(my_df$averaged_affinity_scaled)) & (min(df_lig$b) == min(my_df$averaged_affinity_scaled)) ){
print("PASS: B-factors replaced correctly in df_lig")
} else {
print ("FAIL: To replace B-factors in df_lig")
quit()
}
# if( (max(df_lig$b) == max(my_df$avg_ens_affinity_scaled)) & (min(df_lig$b) == min(my_df$avg_ens_affinity_scaled)) ){
# print("PASS: B-factors replaced correctly in df_lig")
# } else {
# print ("FAIL: To replace B-factors in df_lig")
# quit()
# }
#=========
# step 3_P1
@ -255,6 +272,8 @@ if ( (dim(df_duet)[1] == dim(d2_duet)[1]) & (dim(df_lig)[1] == dim(d2_lig)[1]) &
# assign it back to the pdb file
my_pdb_duet[['atom']] = df_duet
max(df_duet$b); min(df_duet$b)
table(df_duet$b)
sum(is.na(df_duet$b))
my_pdb_lig[['atom']] = df_lig
max(df_lig$b); min(df_lig$b)
@ -268,9 +287,9 @@ write.pdb(my_pdb_duet, outfile_duet_mspdb)
cat(paste0("output file ligand mean stability pdb:", outfile_lig_mspdb))
write.pdb(my_pdb_lig, outfile_lig_mspdb)
#********************************
#============================
# Add the 3rd histogram and density plots for comparisons
#********************************
#============================
# Plots continued...
# Row 3 plots: hist and density of replaced B-factors with stability values
hist(df_duet$b
@ -296,16 +315,17 @@ mtext(text = "Frequency"
, line = 0
, outer = TRUE)
mtext(text = "Stability Distribution"
mtext(text = paste0(tolower(gene), ": Stability Distribution")
, side = 3
, line = 0
, outer = TRUE)
#********************************
#============================================
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# NOTE: This replaced B-factor distribution has the same
# x-axis as the PredAff normalised values, but the distribution
# is affected since 0 is overinflated. This is because all the positions
# is affected since 0 is overinflated/or hs an additional blip because
# of the positions not associated with resistance. This is because all the positions
# where there are no SNPs have been assigned 0???
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!