473 lines
No EOL
17 KiB
R
Executable file
473 lines
No EOL
17 KiB
R
Executable file
#!/usr/bin/env Rscript
|
|
#########################################################
|
|
# TASK: KS test for PS/DUET lineage distributions
|
|
#=======================================================================
|
|
#!/usr/bin/env Rscript
|
|
#source("~/git/LSHTM_analysis/config/alr.R")
|
|
source("~/git/LSHTM_analysis/config/embb.R")
|
|
#source("~/git/LSHTM_analysis/config/katg.R")
|
|
#source("~/git/LSHTM_analysis/config/gid.R")
|
|
#source("~/git/LSHTM_analysis/config/pnca.R")
|
|
#source("~/git/LSHTM_analysis/config/rpob.R")
|
|
|
|
# get plottting dfs
|
|
source("~/git/LSHTM_analysis/scripts/plotting/get_plotting_dfs.R")
|
|
|
|
source("~/git/LSHTM_analysis/scripts/plotting/plotting_colnames.R")
|
|
#=============
|
|
# Output
|
|
#=============
|
|
outdir_images = paste0("~/git/Writing/thesis/images/results/", tolower(gene), "/")
|
|
outdir_stats = "~/git/LSHTM_analysis/scripts/plotting/plotting_thesis/stats/"
|
|
|
|
# ks test by lineage
|
|
#ks_lineage = paste0(outdir, "/KS_lineage_all_muts.csv")
|
|
|
|
###########################
|
|
# Data for stats
|
|
# you need merged_df2 or merged_df2_comp
|
|
# since this is one-many relationship
|
|
# i.e the same SNP can belong to multiple lineages
|
|
# using the _comp dataset means
|
|
# we lose some muts and at this level, we should use
|
|
# as much info as available, hence use df with NA
|
|
###########################
|
|
|
|
# REASSIGNMENT
|
|
my_df = merged_df2
|
|
|
|
# delete variables not required
|
|
rm(merged_df2, merged_df2_comp, merged_df3, merged_df3_comp)
|
|
rm(merged_df2_lig, merged_df2_comp_lig, merged_df3_lig, merged_df3_comp_lig, my_df_u, my_df_u_lig)
|
|
|
|
# quick checks
|
|
colnames(my_df)
|
|
str(my_df)
|
|
|
|
# Ensure correct data type in columns to plot: need to be factor
|
|
#is.factor(my_df$lineage)
|
|
#my_df$lineage = as.factor(my_df$lineage)
|
|
#is.factor(my_df$lineage)
|
|
########################################################################
|
|
table(my_df$lineage); str(my_df$lineage)
|
|
|
|
# subset only lineages1-4
|
|
sel_lineages = c("L1"
|
|
, "L2"
|
|
, "L3"
|
|
, "L4")
|
|
|
|
# subset selected lineages
|
|
df_lin = subset(my_df, subset = lineage %in% sel_lineages)
|
|
|
|
table(df_lin$lineage)
|
|
table(df_lin$sensitivity)
|
|
|
|
table(df_lin$lineage, df_lin$sensitivity)
|
|
|
|
#==============
|
|
# dr_muts_col
|
|
#==============
|
|
#table(df_lin$mutation_info); str(df_lin$mutation_info)
|
|
|
|
# subset df with dr muts only
|
|
#df_lin_dr = subset(df_lin, mutation_info == dr_muts_col)
|
|
#table(df_lin_dr$mutation_info)
|
|
|
|
#==============
|
|
# other_muts_col
|
|
#==============
|
|
#df_lin_other = subset(df_lin, mutation_info == other_muts_col)
|
|
#table(df_lin_other$mutation_info)
|
|
|
|
#=======================================================================
|
|
# individual: CHECKS
|
|
lin1 = df_lin[df_lin$lineage == "L1",]$avg_stability_scaled
|
|
lin2 = df_lin[df_lin$lineage == "L2",]$avg_stability_scaled
|
|
lin3 = df_lin[df_lin$lineage == "L3",]$avg_stability_scaled
|
|
lin4 = df_lin[df_lin$lineage == "L4",]$avg_stability_scaled
|
|
|
|
ks.test(lin1, lin4)
|
|
|
|
|
|
ks.test(df_lin$avg_stability_scaled[df_lin$lineage == "L2"]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == "L3"])
|
|
|
|
#=======================================================================
|
|
my_lineages = levels(factor(df_lin$lineage)); my_lineages
|
|
#=======================================================================
|
|
# Loop
|
|
#=====================
|
|
# Lineage 1 comparisons
|
|
#=====================
|
|
my_lin1 = "L1"
|
|
#my_lineages_comp_l1 = c("L2", "L3", "L4")
|
|
my_lineages_comp_l1 = my_lineages[-match(my_lin1, my_lineages)]
|
|
|
|
ks_df_l1 = data.frame()
|
|
|
|
for (i in my_lineages_comp_l1){
|
|
cat(i)
|
|
l1_df = data.frame(comparison = NA, method = NA, ks_statistic = NA, ks_pvalue = NA
|
|
, n_samples = NA
|
|
, n_samples_total = NA)
|
|
|
|
lineage_comp = paste0(my_lin1, " vs ", i)
|
|
n_samples_lin = nrow(df_lin[df_lin$lineage == my_lin1,])
|
|
n_samples_i = nrow(df_lin[df_lin$lineage == i,])
|
|
n_samples_all = paste0(my_lin1, "(", n_samples_lin, ")"
|
|
, ", "
|
|
, i, "(", n_samples_i, ")")
|
|
|
|
n_samples_total = n_samples_lin + n_samples_i
|
|
|
|
ks_method = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin1]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$method
|
|
|
|
ks_statistic = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin1]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$statistic
|
|
|
|
ks_pvalue = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin1]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$p.value
|
|
|
|
# print(c(lineage_comp, ks_method, ks_statistic[[1]], ks_pval))
|
|
l1_df$comparison = lineage_comp
|
|
l1_df$method = ks_method
|
|
l1_df$ks_statistic = ks_statistic[[1]]
|
|
l1_df$ks_pvalue = ks_pvalue
|
|
l1_df$n_samples = n_samples_all
|
|
l1_df$n_samples_total= n_samples_total
|
|
|
|
print(l1_df)
|
|
ks_df_l1 = rbind(ks_df_l1,l1_df)
|
|
|
|
}
|
|
#####################################################################
|
|
|
|
#=====================
|
|
# Lineage 2 comparisons
|
|
#=====================
|
|
my_lin2 = "L2"
|
|
#my_lineages_comp_l2 = c("L1", lineage3", "L4")
|
|
my_lineages_comp_l2 = my_lineages[-match(my_lin2, my_lineages)]
|
|
|
|
ks_df_l2 = data.frame()
|
|
|
|
for (i in my_lineages_comp_l2){
|
|
cat(i)
|
|
l2_df = data.frame(comparison = NA, method = NA, ks_statistic = NA, ks_pvalue = NA
|
|
, n_samples = NA
|
|
, n_samples_total = NA)
|
|
|
|
lineage_comp = paste0(my_lin2, " vs ", i)
|
|
n_samples_lin = nrow(df_lin[df_lin$lineage == my_lin2,])
|
|
n_samples_i = nrow(df_lin[df_lin$lineage == i,])
|
|
n_samples_all = paste0(my_lin2, "(", n_samples_lin, ")"
|
|
, ", "
|
|
, i, "(", n_samples_i, ")")
|
|
|
|
n_samples_total = n_samples_lin + n_samples_i
|
|
|
|
ks_method = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin2]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$method
|
|
|
|
ks_statistic = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin2]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$statistic
|
|
|
|
ks_pvalue = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin2]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$p.value
|
|
|
|
# print(c(lineage_comp, ks_method, ks_statistic[[1]], ks_pval))
|
|
l2_df$comparison = lineage_comp
|
|
l2_df$method = ks_method
|
|
l2_df$ks_statistic = ks_statistic[[1]]
|
|
l2_df$ks_pvalue = ks_pvalue
|
|
l2_df$n_samples = n_samples_all
|
|
l2_df$n_samples_total = n_samples_total
|
|
|
|
print(l2_df)
|
|
ks_df_l2 = rbind(ks_df_l2, l2_df)
|
|
|
|
}
|
|
|
|
#=====================
|
|
# Lineage 3 comparisons
|
|
#=====================
|
|
my_lin3 = "L3"
|
|
#my_lineages_comp_l3 = c("L1", lineage2", "L4")
|
|
my_lineages_comp_l3 = my_lineages[-match(my_lin3, my_lineages)]
|
|
|
|
ks_df_l3 = data.frame()
|
|
|
|
for (i in my_lineages_comp_l3){
|
|
cat(i)
|
|
l3_df = data.frame(comparison = NA, method = NA, ks_statistic = NA, ks_pvalue = NA
|
|
, n_samples = NA
|
|
, n_samples_total = NA)
|
|
|
|
lineage_comp = paste0(my_lin3, " vs ", i)
|
|
n_samples_lin = nrow(df_lin[df_lin$lineage == my_lin3,])
|
|
n_samples_i = nrow(df_lin[df_lin$lineage == i,])
|
|
n_samples_all = paste0(my_lin3, "(", n_samples_lin, ")"
|
|
, ", "
|
|
, i, "(", n_samples_i, ")")
|
|
n_samples_total = n_samples_lin + n_samples_i
|
|
|
|
ks_method = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin3]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$method
|
|
|
|
ks_statistic = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin3]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$statistic
|
|
|
|
ks_pvalue = ks.test(df_lin$avg_stability_scaled[df_lin$lineage == my_lin3]
|
|
, df_lin$avg_stability_scaled[df_lin$lineage == i])$p.value
|
|
|
|
# print(c(lineage_comp, ks_method, ks_statistic[[1]], ks_pval))
|
|
l3_df$comparison = lineage_comp
|
|
l3_df$method = ks_method
|
|
l3_df$ks_statistic = ks_statistic[[1]]
|
|
l3_df$ks_pvalue = ks_pvalue
|
|
l3_df$n_samples = n_samples_all
|
|
l3_df$n_samples_total = n_samples_total
|
|
|
|
print(l3_df)
|
|
ks_df_l3 = rbind(ks_df_l3, l3_df)
|
|
|
|
}
|
|
######################################################################################
|
|
|
|
|
|
####################################################################
|
|
# combine all 4 ks_dfs
|
|
n_dfs = 3
|
|
if ( all.equal(nrow(ks_df_l1), nrow(ks_df_l2), nrow(ks_df_l3)) &&
|
|
all.equal(ncol(ks_df_l1), ncol(ks_df_l2), ncol(ks_df_l3)) ){
|
|
cat("\nPASS: Calculating expected rows and cols for sanity checks on combined_dfs")
|
|
expected_rows = nrow(ks_df_l1) * n_dfs
|
|
expected_cols = ncol(ks_df_l1)
|
|
ks_df_combined = rbind(ks_df_l1, ks_df_l2, ks_df_l3)
|
|
if ( nrow(ks_df_combined) == expected_rows && ncol(ks_df_combined) == expected_cols ){
|
|
cat("\nPASS: combined df successfully created"
|
|
, "\nnrow combined_df:", nrow(ks_df_combined)
|
|
, "\nncol combined_df:", ncol(ks_df_combined))
|
|
}
|
|
else{
|
|
cat("\nFAIL: Dim mismatch"
|
|
, "\nExpected rows:", expected_rows
|
|
, "\nGot:", nrow(ks_df_combined)
|
|
, "\nExpected cols:", expected_cols
|
|
, "\nGot:", ncol(ks_df_combined))
|
|
}
|
|
}else{
|
|
cat("\nFAIL: Could not generate expected_rows and/or expected_cols"
|
|
, "\nCheck hardcoded value of n_dfs")
|
|
}
|
|
|
|
#--------------
|
|
# formatting
|
|
#--------------
|
|
# add total_n number
|
|
#ks_df_combined$total_samples_analysed = nrow(df_lin)
|
|
|
|
# adding pvalue_signif
|
|
ks_df_combined$pvalue_signif = ks_df_combined$ks_pvalue
|
|
str(ks_df_combined$pvalue_signif)
|
|
|
|
ks_df_combined = dplyr::mutate(ks_df_combined
|
|
, pvalue_signif = case_when(pvalue_signif == 0.05 ~ "."
|
|
, pvalue_signif <=0.0001 ~ '****'
|
|
, pvalue_signif <=0.001 ~ '***'
|
|
, pvalue_signif <=0.01 ~ '**'
|
|
, pvalue_signif <0.05 ~ '*'
|
|
, TRUE ~ 'ns'))
|
|
|
|
# Remove duplicates
|
|
rows_to_remove = c("L2 vs L1", "L3 vs L1", "L3 vs L2")
|
|
|
|
ks_df_combined_f = ks_df_combined[-match(rows_to_remove, ks_df_combined$comparison),]
|
|
#################################################################################
|
|
#================================
|
|
# R vs S distribution: Overall
|
|
#=================================
|
|
df_lin_R = df_lin[df_lin$sensitivity == "R",]
|
|
df_lin_S = df_lin[df_lin$sensitivity == "S",]
|
|
|
|
overall_RS_df = data.frame(comparison = NA, method = NA, ks_statistic = NA, ks_pvalue = NA
|
|
, n_samples = NA
|
|
, n_samples_total = NA)
|
|
|
|
comp_type = "overall R vs S distributions"
|
|
|
|
n_samples_R = nrow(df_lin_R)
|
|
n_samples_S = nrow(df_lin_S)
|
|
n_samples_all = paste0("R(n=", n_samples_R, ")" , " ", "S(n=", n_samples_S, ")")
|
|
n_samples_total = n_samples_R+n_samples_S
|
|
|
|
ks.test(df_lin_R$avg_stability_scaled
|
|
, df_lin_S$avg_stability_scaled)
|
|
|
|
ks_method = ks.test(df_lin_R$avg_stability_scaled
|
|
, df_lin_S$avg_stability_scaled)$method
|
|
|
|
ks_statistic = ks.test(df_lin_R$avg_stability_scaled
|
|
, df_lin_S$avg_stability_scaled)$statistic
|
|
|
|
ks_pvalue = ks.test(df_lin_R$avg_stability_scaled
|
|
, df_lin_S$avg_stability_scaled)$p.value
|
|
|
|
overall_RS_df$comparison = comp_type
|
|
overall_RS_df$method = ks_method
|
|
overall_RS_df$ks_statistic = ks_statistic
|
|
overall_RS_df$ks_pvalue = ks_pvalue
|
|
overall_RS_df$n_samples = n_samples_all
|
|
overall_RS_df$n_samples_total= n_samples_total
|
|
|
|
#--------------
|
|
# formatting
|
|
#--------------
|
|
overall_RS_df$pvalue_signif = overall_RS_df$ks_pvalue
|
|
overall_RS_df = dplyr::mutate(overall_RS_df
|
|
, pvalue_signif = case_when(pvalue_signif == 0.05 ~ "."
|
|
, pvalue_signif <=0.0001 ~ '****'
|
|
, pvalue_signif <=0.001 ~ '***'
|
|
, pvalue_signif <=0.01 ~ '**'
|
|
, pvalue_signif <0.05 ~ '*'
|
|
, TRUE ~ 'ns'))
|
|
|
|
overall_RS_df
|
|
#####################################################################
|
|
if (all(colnames(ks_df_combined_f) == colnames(overall_RS_df))){
|
|
|
|
cat("\nPASS:combining KS test results")
|
|
}else{
|
|
stop("\nAbort: Cannot combine results for KS test")
|
|
}
|
|
|
|
ks_df_combined_f2 = rbind(ks_df_combined_f, overall_RS_df)
|
|
|
|
# ADD extra cols
|
|
ks_df_combined_f2$ks_comp_type = "between_lineages"
|
|
ks_df_combined_f2$gene_name = tolower(gene)
|
|
|
|
# #==============================
|
|
# # write output file: KS test
|
|
# #===============================
|
|
# Out_lineage_bwL = paste0(outdir_stats
|
|
# , tolower(gene)
|
|
# , "_ks_lineage_bw.csv")
|
|
#
|
|
# cat("Output of KS test bt lineage:", Out_lineage_bwL)
|
|
# write.csv(ks_df_combined_f2, Out_lineage_bwL, row.names = FALSE)
|
|
|
|
###########################################################################
|
|
#=======================
|
|
# Within lineage R vs S: MANUAL
|
|
#=======================
|
|
lin1 = df_lin[df_lin$lineage == my_lin1,]
|
|
ks.test(lin1$avg_stability_scaled[lin1$sensitivity == "R"]
|
|
, lin1$avg_stability_scaled[lin1$sensitivity == "S"])
|
|
|
|
lin2 = df_lin[df_lin$lineage == my_lin2,]
|
|
ks.test(lin2$avg_stability_scaled[lin2$sensitivity == "R"]
|
|
, lin2$avg_stability_scaled[lin2$sensitivity == "S"])
|
|
|
|
|
|
lin3 = df_lin[df_lin$lineage == "L3",]
|
|
ks.test(lin3$avg_stability_scaled[lin3$sensitivity == "R"]
|
|
, lin3$avg_stability_scaled[lin3$sensitivity == "S"])
|
|
|
|
lin4 = df_lin[df_lin$lineage == "L4",]
|
|
ks.test(lin4$avg_stability_scaled[lin4$sensitivity == "R"]
|
|
, lin4$avg_stability_scaled[lin4$sensitivity == "S"])
|
|
###################################################################
|
|
#=======================
|
|
# Within lineage R vs S: LOOP
|
|
#=======================
|
|
all_within_lin_df = data.frame()
|
|
|
|
for (i in c(my_lin1
|
|
, my_lin2
|
|
, my_lin3
|
|
)){
|
|
#cat(i)
|
|
|
|
within_lin_df = data.frame(comparison = NA, method = NA, ks_statistic = NA, ks_pvalue = NA
|
|
, n_samples = NA
|
|
, n_samples_total = NA)
|
|
|
|
within_lineage_comp = paste0(i, ": R vs S")
|
|
my_lin_df = df_lin[df_lin$lineage == i,]
|
|
|
|
lin_R_n = nrow(my_lin_df[my_lin_df$sensitivity == "R",])
|
|
lin_S_n = nrow(my_lin_df[my_lin_df$sensitivity == "S",])
|
|
lin_total_n = lin_R_n+lin_S_n
|
|
n_samples_all = paste0("R(n=", lin_R_n, ")" , ", ", "S(n=", lin_S_n, ")")
|
|
|
|
ks_method = ks.test(my_lin_df$avg_stability_scaled[my_lin_df$sensitivity == "R"]
|
|
, my_lin_df$avg_stability_scaled[my_lin_df$sensitivity == "S"])$method
|
|
|
|
ks_statistic = ks.test(my_lin_df$avg_stability_scaled[my_lin_df$sensitivity == "R"]
|
|
, my_lin_df$avg_stability_scaled[my_lin_df$sensitivity == "S"])$statistic
|
|
|
|
ks_pvalue =ks.test(my_lin_df$avg_stability_scaled[my_lin_df$sensitivity == "R"]
|
|
, my_lin_df$avg_stability_scaled[my_lin_df$sensitivity == "S"])$p.value
|
|
|
|
# print(c(lineage_comp, ks_method, ks_statistic[[1]], ks_pval))
|
|
within_lin_df$comparison = within_lineage_comp
|
|
within_lin_df$method = ks_method
|
|
within_lin_df$ks_statistic = ks_statistic[[1]]
|
|
within_lin_df$ks_pvalue = ks_pvalue
|
|
within_lin_df$n_samples = n_samples_all
|
|
within_lin_df$n_samples_total=lin_total_n
|
|
|
|
print(my_lin_df)
|
|
all_within_lin_df = rbind(all_within_lin_df, within_lin_df)
|
|
|
|
}
|
|
all_within_lin_df
|
|
|
|
all_within_lin_df$pvalue_signif = all_within_lin_df$ks_pvalue
|
|
str(all_within_lin_df$pvalue_signif)
|
|
|
|
all_within_lin_df = dplyr::mutate(all_within_lin_df
|
|
, pvalue_signif = case_when(pvalue_signif == 0.05 ~ "."
|
|
, pvalue_signif <=0.0001 ~ '****'
|
|
, pvalue_signif <=0.001 ~ '***'
|
|
, pvalue_signif <=0.01 ~ '**'
|
|
, pvalue_signif <0.05 ~ '*'
|
|
, TRUE ~ 'ns'))
|
|
|
|
all_within_lin_df
|
|
|
|
# ADD extra cols
|
|
all_within_lin_df$ks_comp_type = "within_lineages"
|
|
all_within_lin_df$gene_name = tolower(gene)
|
|
|
|
# #--------------------
|
|
# # write output file: KS test within grpup
|
|
# #----------------------
|
|
# Out_ks_withinL = paste0(outdir_stats
|
|
# , tolower(gene)
|
|
# , "_ks_lineage_within.csv")
|
|
# cat("Output of KS test within lineage:",Out_ks_withinL )
|
|
# write.csv(all_within_lin_df, Out_ks_withinL, row.names = FALSE)
|
|
|
|
##################################################################
|
|
|
|
if (all(colnames(ks_df_combined_f2) == colnames(Out_ks_withinL))){
|
|
|
|
cat("\nPASS:combining KS test results")
|
|
}else{
|
|
stop("\nAbort: Cannot combine results for KS test")
|
|
}
|
|
|
|
ks_df_combined_all = rbind(ks_df_combined_f2, all_within_lin_df)
|
|
|
|
#--------------------
|
|
# write output file: KS test within grpup
|
|
#----------------------
|
|
Out_ks_all = paste0(outdir_stats
|
|
, tolower(gene)
|
|
, "_ks_lineage_all_comp.csv")
|
|
cat("Output of KS test all comparisons:", Out_ks_all )
|
|
write.csv(ks_df_combined_all, Out_ks_all, row.names = FALSE) |