LSHTM_analysis/scripts/plotting/basic_barplots_PS.R

230 lines
6.7 KiB
R
Executable file

#!/usr/bin/env Rscript
#########################################################
# TASK: producing barplots
# basic barplots with count of mutations
# basic barplots with frequency of count of mutations
# Depends on
## plotting_globals.R (previously dir.R)
## plotting_data.R
#########################################################
# working dir
getwd()
setwd("~/git/LSHTM_analysis/scripts/plotting")
getwd()
# load libraries
#source("Header_TT.R")
library(ggplot2)
library(data.table)
library(dplyr)
require("getopt", quietly = TRUE) # cmd parse arguments
# load functions
source("plotting_globals.R")
source("plotting_data.R")
source("functions/stability_count_bp.R")
#########################################################
# command line args
#********************
# !!!FUTURE TODO!!!
# Can pass additional params of output/plot dir by user.
# Not strictly required for my workflow since it is optimised
# to have a streamlined input/output flow without filename worries.
#********************
spec = matrix(c(
"drug" ,"d", 1, "character",
"gene" ,"g", 1, "character",
"data" ,"f", 2, "character"
), byrow = TRUE, ncol = 4)
opt = getopt(spec)
#FIXME: detect if script running from cmd, then set these
drug = opt$drug
gene = opt$gene
infile = opt$data
# hardcoding when not using cmd
#drug = "streptomycin"
#gene = "gid"
if(is.null(drug)|is.null(gene)) {
stop("Missing arguments: --drug and --gene must both be specified (case-sensitive)")
}
#########################################################
# call functions with relevant args
#------------------------------------------
# import_dirs()
# should return the follwoing variables:
# datadir
# indir
# outdir
# plotdir
# dr_muts_col
# other_muts_col
# resistance_col
#--------------------------------------------
import_dirs(drug, gene)
#---------------------------------------------
# plotting_data()
# should return the following dfs:
# my_df
# my_df_u
# my_df_u_lig
# dup_muts
#----------------------------------------------
#infile = "/home/tanu/git/Data/streptomycin/output/gid_comb_stab_struc_params.csv"
#infile = ""
#if (!exists("infile") && exists("gene")){
if (!is.character(infile) && exists("gene")){
#in_filename_params = paste0(tolower(gene), "_all_params.csv")
in_filename_params = paste0(tolower(gene), "_comb_stab_struc_params.csv") # part combined for gid
infile = paste0(outdir, "/", in_filename_params)
cat("\nInput file not specified, assuming filename: ", infile, "\n")
}
# Get the DFs out of plotting_data()
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]]
#########################################################
cat(paste0("Directories imported:"
, "\ndatadir:", datadir
, "\nindir:", indir
, "\noutdir:", outdir
, "\nplotdir:", plotdir))
cat(paste0("\nVariables imported:"
, "\ndrug:", drug
, "\ngene:", gene
, "\n))
#, "\ngene_match:", gene_match
#, "\nLength of upos:", length(upos)
#, "\nAngstrom symbol:", angstroms_symbol))
#=======================================================================
#=======
# output
#=======
# plot 1
basic_bp_duet = paste0(tolower(gene), "_basic_barplot_PS.svg")
plot_basic_bp_duet = paste0(plotdir,"/", basic_bp_duet)
# plot 2
pos_count_duet = paste0(tolower(gene), "_position_count_PS.svg")
plot_pos_count_duet = paste0(plotdir, "/", pos_count_duet)
#=======================================================================
#================
# Data for plots
#================
# REASSIGNMENT as necessary
df = my_df_u
# sanity checks
str(df)
#=======================================================================
#****************
# Plot 1: Count of stabilising and destabilsing muts
#****************
svg(plot_basic_bp_duet)
print(paste0("plot1 filename:", basic_bp_duet))
#--------------
# start plot 1: call function
#--------------
stability_count_bp(plotdf = my_df_u
, df_colname = "duet_outcome"
, leg_title = "DUET outcome")
dev.off()
table(my_df_u$duet_outcome)
#=======================================================================
#****************
# Plot 2: frequency of positions
#****************
df_ncols = ncol(df)
df_nrows = nrow(df)
cat(paste0("original df dimensions:"
, "\nNo. of rows:", df_nrows
, "\nNo. of cols:", df_ncols
, "\nNow adding column: frequency of mutational positions"))
#setDT(df)[, .(pos_count := .N), by = .(position)]
setDT(df)[, pos_count := .N, by = .(position)]
rm(df_nrows, df_ncols)
df_nrows = nrow(df)
df_ncols = ncol(df)
cat(paste0("revised df dimensions:"
, "\nNo. of rows:", df_nrows
, "\nNo. of cols:", df_ncols))
# this is cummulative
table(df$pos_count)
# use group by on this
snpsBYpos_df <- df %>%
group_by(position) %>%
summarize(snpsBYpos = mean(pos_count))
table(snpsBYpos_df$snpsBYpos)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# FIXME, get this mutation_info, perhaps useful!
foo = select(df, mutationinformation
, wild_pos
, wild_type
, mutant_type
#, mutation_info # comes from meta data, notused yet
, position
, pos_count)
#write.csv(foo, "/pos_count_freq.csv")
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#--------------
# start plot 2
#--------------
svg(plot_pos_count_duet)
print(paste0("plot filename:", plot_pos_count_duet))
my_ats = 25 # axis text size
my_als = 22 # axis label size
# to make x axis display all positions
# not sure if to use with sort or directly
my_x = sort(unique(snpsBYpos_df$snpsBYpos))
g = ggplot(snpsBYpos_df, aes(x = snpsBYpos))
OutPlot_pos_count = g + geom_bar(aes (alpha = 0.5)
, show.legend = FALSE) +
scale_x_continuous(breaks = unique(snpsBYpos_df$snpsBYpos)) +
#scale_x_continuous(breaks = my_x) +
geom_label(stat = "count", aes(label = ..count..)
, color = "black"
, size = 10) +
theme(axis.text.x = element_text(size = my_ats
, angle = 0)
, axis.text.y = element_text(size = my_ats
, angle = 0
, hjust = 1)
, axis.title.x = element_text(size = my_als)
, axis.title.y = element_text(size = my_als)
, plot.title = element_blank()) +
labs(x = "Number of nsSNPs"
, y = "Number of Sites")
print(OutPlot_pos_count)
dev.off()
########################################################################
# end of PS barplots
########################################################################