264 lines
7.9 KiB
R
264 lines
7.9 KiB
R
#!/usr/bin/env Rscript
|
|
#########################################################
|
|
# TASK: producing logoplot
|
|
# from data and/or from sequence
|
|
|
|
#########################################################
|
|
#=======================================================================
|
|
# working dir and loading libraries
|
|
getwd()
|
|
setwd("~/git/LSHTM_analysis/scripts/plotting")
|
|
getwd()
|
|
|
|
source("Header_TT.R")
|
|
#library(ggplot2)
|
|
#library(data.table)
|
|
#library(dplyr)
|
|
|
|
#===========
|
|
# input
|
|
#===========
|
|
source("combining_dfs_plotting.R")
|
|
|
|
#===========
|
|
# output
|
|
#===========
|
|
|
|
logo_plot = "logo_plot.svg"
|
|
plot_logo_plot = paste0(plotdir,"/", logo_plot)
|
|
|
|
#==========================
|
|
# This will return:
|
|
|
|
# df with NA for pyrazinamide:
|
|
# merged_df2
|
|
# merged_df3
|
|
|
|
# df without NA for pyrazinamide:
|
|
# merged_df2_comp
|
|
# merged_df3_comp
|
|
#===========================
|
|
|
|
###########################
|
|
# Data for plots
|
|
# 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
|
|
|
|
# This will the first plotting df
|
|
# Then subset this to extract dr muts only (second plottig df)
|
|
###########################
|
|
|
|
#%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
# uncomment as necessary
|
|
# REASSIGNMENT
|
|
#my_data = merged_df2
|
|
#my_data = merged_df2_comp
|
|
my_data = merged_df3
|
|
my_data = merged_df3_comp
|
|
#%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
# delete variables not required
|
|
rm(merged_df2, merged_df2_comp)
|
|
#rm(merged_df3, merged_df3_comp)
|
|
|
|
# quick checks
|
|
colnames(my_data)
|
|
str(my_data)
|
|
|
|
c1 = unique(my_data$position)
|
|
nrow(my_data)
|
|
cat("No. of rows in my_data:", nrow(my_data)
|
|
, "\nDistinct positions corresponding to snps:", length(c1)
|
|
, "\n===========================================================")
|
|
|
|
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
# FIXME: Think and decide what you want to remove
|
|
# mut_pos_occurence < 1 or sample_pos_occurrence <1
|
|
|
|
# get freq count of positions so you can subset freq<1
|
|
require(data.table)
|
|
#setDT(my_data)[, mut_pos_occurrence := .N, by = .(position)] #265, 14
|
|
|
|
#extract freq_pos>1
|
|
#my_data_snp = my_data[my_data$occurrence!=1,]
|
|
|
|
#u = unique(my_data_snp$position) #73
|
|
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
# REASSIGNMENT to prevent changing code
|
|
my_data_snp = my_data
|
|
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
#=======================================================================
|
|
#%% logo plots from dataframe
|
|
|
|
#############
|
|
# PLOTS: ggseqlogo with custom height
|
|
# https://omarwagih.github.io/ggseqlogo/
|
|
#############
|
|
#require(ggplot2)
|
|
#require(tidyverse)
|
|
library(ggseqlogo)
|
|
|
|
foo = my_data_snp[, c("position", "mutant_type","duet_scaled", "or_mychisq"
|
|
, "mut_prop_polarity", "mut_prop_water") ]
|
|
|
|
my_data_snp$log10or = log10(my_data_snp$or_mychisq)
|
|
logo_data = my_data_snp[, c("position", "mutant_type", "or_mychisq", "log10or")]
|
|
|
|
logo_data_or = 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 = as.matrix(wide_df_or)
|
|
rownames(wide_df_or) = wide_df_or[,1]
|
|
wide_df_or = wide_df_or[,-1]
|
|
|
|
position_or = as.numeric(colnames(wide_df_or))
|
|
|
|
#===========================================
|
|
#custom height (OR) logo plot: CORRECT x-axis labelling
|
|
#============================================
|
|
# custom height (OR) logo plot: yayy works
|
|
|
|
ggseqlogo(wide_df_or, method="custom", seq_type="aa") + ylab("my custom height") +
|
|
theme(legend.position = "bottom"
|
|
, axis.text.x = element_text(size = 11
|
|
, angle = 90
|
|
, hjust = 1
|
|
, vjust = 0.4)
|
|
, axis.text.y = element_text(size = 15
|
|
, angle = 0
|
|
, hjust = 1
|
|
, vjust = 0))+
|
|
scale_x_discrete("Position"
|
|
#, breaks
|
|
, labels = position_or
|
|
, limits = factor(1:length(position_or))) +
|
|
ylab("Odds Ratio")
|
|
|
|
#%% end of logo plot with OR as height
|
|
|
|
#=======================================================================
|
|
# extracting data with log10R
|
|
logo_data_logor = my_data_snp[, c("position", "mutant_type", "log10or")]
|
|
wide_df_logor <- logo_data_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)
|
|
|
|
# FIXME
|
|
#my_ylim_up = as.numeric(max(wide_df_logor_m)) * 5
|
|
#my_ylim_low = as.numeric(min(wide_df_logor_m))
|
|
|
|
position_logor = as.numeric(colnames(wide_df_logor_m))
|
|
|
|
# custom height (log10OR) logo plot: yayy works
|
|
ggseqlogo(wide_df_logor_m, method="custom", seq_type="aa") + ylab("my custom height") +
|
|
theme(legend.position = "bottom"
|
|
, axis.text.x = element_text(size = 11
|
|
, angle = 90
|
|
, hjust = 1
|
|
, vjust = 0.4)
|
|
, axis.text.y = element_text(size = 15
|
|
, angle = 0
|
|
, hjust = 1
|
|
, vjust = 0))+
|
|
scale_x_discrete("Position"
|
|
#, breaks
|
|
, labels = position_logor
|
|
, limits = factor(1:length(position_logor)))+
|
|
ylab("Log (Odds Ratio)") +
|
|
scale_y_continuous(limits = c(0, 9))
|
|
|
|
#=======================================================================
|
|
#%% logo plot from sequence
|
|
|
|
#################
|
|
# Plot: LOGOLAS (ED plots)
|
|
# link: https://github.com/kkdey/Logolas
|
|
# on all pncA samples: output of mutate.py
|
|
################
|
|
library(Logolas)
|
|
|
|
# data was pnca_msa.txt
|
|
#FIXME: generate this file
|
|
|
|
seqs = read.csv("~/git/Data/pyrazinamide/output/pnca_msa.txt"
|
|
, header = FALSE
|
|
, stringsAsFactors = FALSE)$V1
|
|
|
|
|
|
# my_data: useful!
|
|
logomaker(seqs, type = "EDLogo", color_type = "per_symbol"
|
|
, return_heights = TRUE)
|
|
|
|
logomaker(seqs, type = "Logo", color_type = "per_symbol", return_heights = TRUE)
|
|
|
|
#%% end of script
|
|
#=======================================================================
|
|
#==============
|
|
# online logo:
|
|
#http://www.cbs.dtu.dk/biotools/Seq2Logo/ # good for getting pssm and psfm matrices
|
|
#https://weblogo.berkeley.edu/logo.cgi
|
|
#http://weblogo.threeplusone.com/create.cgi # weblogo3
|
|
|
|
#===============
|
|
# PSSM logos= example from logomaker
|
|
#===============
|
|
|
|
data(pssm)
|
|
logo_pssm(pssm, control = list(round_off = 0))
|
|
|
|
#=================
|
|
# PSSM: output from http://www.cbs.dtu.dk/biotools/Seq2Logo/
|
|
# of MSA: pnca_msa.txt
|
|
#==================
|
|
foo = read.csv("/home/tanu/git/Data/pyrazinamide/pssm_transpose.csv")
|
|
rownames(foo) = foo[,1]
|
|
df = subset(foo, select = -c(1) )
|
|
colnames(df)
|
|
colnames(df) = seq(1:length(df))
|
|
|
|
df_m = as.matrix(df)
|
|
logo_pssm(df_m, control = list(round_off = 0))
|
|
|
|
#=================
|
|
# # PSFM: output from http://www.cbs.dtu.dk/biotools/Seq2Logo/
|
|
# of MSA: pnca_msa.txt
|
|
#=================
|
|
# not designed for PSFM!
|
|
# may want to figure out how to do it!
|
|
logo_data = read.csv("/home/tanu/git/Data/pyrazinamide/psfm_transpose.csv")
|
|
rownames(logo_data) = logo_data[,1]
|
|
df2 = subset(logo_data, select = -c(1) )
|
|
colnames(df2)
|
|
colnames(df2) = seq(1:length(df2))
|
|
|
|
df2_m = as.matrix(df2)
|
|
logo_pssm(df2_m, control = list(round_off = 0))
|
|
|
|
|
|
#=================
|
|
# ggplots:
|
|
#https://stackoverflow.com/questions/5438474/plotting-a-sequence-logo-using-ggplot2
|
|
#=================
|
|
|
|
library(ggplot2)
|
|
library(gglogo)
|
|
ggplot(data = ggfortify(sequences, "peptide")) +
|
|
geom_logo(aes(x=position, y=bits, group=element,
|
|
label=element, fill=interaction(Polarity, Water)),
|
|
alpha = 0.6) +
|
|
scale_fill_brewer(palette="Paired") +
|
|
theme(legend.position = "bottom")
|