#!/usr/bin/env Rscript  
#########################################################
# TASK: Script to format data for Logo_plots
#########################################################
#-------------------------
# choose df for logoplot
#-------------------------
logo_data = merged_df3
#logo_data = merged_df3_comp
#logo_data = merged_df2 # can't be used because of multiple snps

# quick checks
colnames(logo_data)
str(logo_data)

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")] 

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))

