From 81ab3fe5ba3a8b7c6f517ce6face85bdaa3fe4cd Mon Sep 17 00:00:00 2001 From: Tanushree Tunstall Date: Mon, 14 Jun 2021 09:22:05 +0100 Subject: [PATCH] added function and test for aa_prop_bp.R --- scripts/functions/aa_prop_bp.R | 22 ++++++++++ scripts/functions/test_aa_prop_bp.R | 62 +++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 scripts/functions/aa_prop_bp.R create mode 100644 scripts/functions/test_aa_prop_bp.R diff --git a/scripts/functions/aa_prop_bp.R b/scripts/functions/aa_prop_bp.R new file mode 100644 index 0000000..3eb9da6 --- /dev/null +++ b/scripts/functions/aa_prop_bp.R @@ -0,0 +1,22 @@ +library(ggplot2) +library(tidyverse) +library(data.table) +######################################################################## +# TASK: barplots to colour nssnps by chosen aa_property +######################################################################## +aa_prop_bp <- function(plotdf + , position_colname + , fill_colname + , fill_colours + , leg_name = "LEGEND"){ + mp = ggplot(df, aes(x = as.factor(eval(parse(text = pos_colname))) + #, y = ..count.. + , fill = eval(parse(text = fill_colname)) + )) + + geom_bar(position = 'identity') + + scale_fill_manual(values = fill_colours + , name = leg_name) + return(mp) +} + + diff --git a/scripts/functions/test_aa_prop_bp.R b/scripts/functions/test_aa_prop_bp.R new file mode 100644 index 0000000..29931cc --- /dev/null +++ b/scripts/functions/test_aa_prop_bp.R @@ -0,0 +1,62 @@ +library(ggplot2) +library(tidyverse) +library(data.table) + +setwd("~/git/LSHTM_analysis/scripts/plotting/functions") +getwd() +############################################################# +#=========================================== +# load functions, data, dirs, hardocded vars +# that will be used in testing the functions +#=========================================== +source("../plotting_data.R") +infile = "/home/tanu/git/Data/streptomycin/output/" +pd_df = plotting_data(infile) +my_df = pd_df[[1]] +my_df_u = pd_df[[2]] +my_df_u_lig = pd_df[[3]] +dup_muts = pd_df[[4]] + +source("../plotting_globals.R") +drug = "streptomycin" +gene = "gid" + +import_dirs(drug, gene) + +#===================== +# functions to test +#===================== +source("stability_count_bp.R") +source("position_count_bp.R") +################################################################# +############################################## +# read a sample file containing muts and prop +############################################### +df<- read.csv(file.choose()) + +setDT(df)[, pos_count := .N, by = .(position)] +foo = data.frame(df$position, df$pos_count) + +#snpsBYpos_df <- df %>% +# group_by(position) %>% +# summarize(snpsBYpos = mean(pos_count)) + +# subset df without duplicates for position +df2 = df[!duplicated(df$position)] +################################################################## +# --------------------------------------- +# barplot for nssnps, coloured by aa prop +# --------------------------------------- +pos_colname = "position" +aa_prop_colname = "mut_prop_water" +aa_prop_colours = c("black", "blue") +my_legname = "aa_prop: water" + +# call function +aa_prop_bp(plotdf = df + , position_colname = pos_colname + , fill_colname = aa_prop_colname + , fill_colours = aa_prop_cols + , leg_name = my_legname) + +#===============================================================