playing with MSA plots to allow filtering of positions, arghhh

This commit is contained in:
Tanushree Tunstall 2022-01-18 15:30:41 +00:00
parent 08bd8a2ee5
commit 00094f036a
6 changed files with 209 additions and 46 deletions

View file

@ -20,33 +20,116 @@
LogoPlotMSA <- function(msaSeq_mut
, msaSeq_wt
, plot_positions
, msa_method = 'bits' # or probability
, my_logo_col = "chemistry"
, x_lab = "Wild-type position"
, y_lab = "Count"
, x_ats = 13 # text size
, x_tangle = 90 # text angle
, y_ats = 13
, y_tangle = 0
, x_tts = 13 # title size
, y_tts = 13
, leg_pos = "top" # can be top, left, right and bottom or c(0.8, 0.9)
, leg_dir = "horizontal" #can be vertical or horizontal
, leg_ts = 16 # leg text size
, leg_tts = 16 # leg title size
, my_logo_col = "chemistry"
, x_lab = "Wild-type position"
, y_lab = ""
, x_ats = 13 # text size
, x_tangle = 90 # text angle
, y_ats = 13
, y_tangle = 0
, x_tts = 13 # title size
, y_tts = 13
, leg_pos = "top" # can be top, left, right and bottom or c(0.8, 0.9)
, leg_dir = "horizontal" #can be vertical or horizontal
, leg_ts = 16 # leg text size
, leg_tts = 16 # leg title size
)
{
############################################
# Data processing for logo plot for nsSNPS
############################################
############################################
# Data processing for logo plot for nsSNPS
###########################################
cat("\nLength of MSA", length(msaSeq_mut)
, "\nlength of WT seq:", length(msaSeq_wt))
if(missing(plot_positions)){
#if(is.null(plot_positions)){
cat("\nPlotting entire MSA")
msa_seq_plot = msaSeq_mut
wt_seq_plot = msaSeq_wt
} else {
cat("\nUser specified plotting positions for MSA:"
, "These are:", plot_positions)
cat("\nLength of MSA", nrow(msaSeq_mut)
, "\nlength of WT seq:", nrow(msaSeq_wt))
#-----------
# MSA: mut
#-----------
cat("\nGenerating MSA: filtered positions")
msa_interim = sapply(msaSeq_mut, function(x) unlist(strsplit(x,"")))
######################################
# Generating plots for muts and wt
#####################################
if (any(is.na(msa_interim[plot_positions]))){
cat("Plot_positions selected:", length(plot_positions))
i_ofr = plot_positions[is.na(msa_interim[plot_positions])]
cat("\nIndex out of range: 1 or more"
, "\nThese are:", i_ofr
, "\nOmitting these and proceeding...")
i_extract = na.omit(msa_interim[plot_positions])
cat("\nFinal positions being plottted:", length(i_extract)
, "\nNo. of positions dropped from request:", length(i_ofr))
}else{
cat("\nAll positions within range"
, "\nProceeing with generating requested position MSA seqs...")
i_extract = plot_positions
}
matP1 = msa_interim[i_extract, 1:ncol(msa_interim)]
dfP1 = data.frame(t(matP1))
names(dfP1) = i_extract
cols_to_paste = names(dfP1)
dfP1['chosen_seq'] = apply( dfP1[ , cols_to_paste]
, 1
, paste, sep = ''
, collapse = "")
msa_seq_plot = dfP1$chosen_seq
#-----------
# WT: fasta
#-----------
cat("\nGenerating WT fasta: filtered positions")
wt_interim = sapply(msaSeq_wt, function(x) unlist(strsplit(x,"")))
if (any(is.na(wt_interim[plot_positions]))){
cat("Plot_positions selected:", length(plot_positions))
i2_ofr = plot_positions[is.na(wt_interim[plot_positions])]
cat("\nIndex out of range: 1 or more"
, "\nThese are:", i2_ofr
, "\nOmitting these and proceeding...")
i2_extract = na.omit(wt_interim[plot_positions])
cat("\nFinal positions being plottted:", length(i2_extract)
, "\nNo. of positions dropped from request:", length(i2_ofr))
}else{
cat("\nAll positions within range"
, "\nProceeing with generating requested position MSA seqs...")
i2_extract = plot_positions
}
matP2 = wt_interim[i_extract, 1:ncol(wt_interim)]
dfP2 = data.frame(t(matP2))
names(dfP2) = i2_extract
cols_to_paste_P2 = names(dfP2)
dfP2['chosen_seq'] = apply( dfP2[ , cols_to_paste_P2]
, 1
, paste, sep = ''
, collapse = "")
wt_seq_plot = dfP2$chosen_seq
}
######################################
# Generating plots for muts and wt
#####################################
LogoPlotMSAL <- list()
if (my_logo_col %in% c('clustalx','taylor')) {
@ -78,12 +161,16 @@ LogoPlotMSA <- function(msaSeq_mut
#-------------------
# Mutant logo plot
#-------------------
p0 = ggseqlogo(msaSeq_mut
#msaSeq_mut$V1
p0 = ggseqlogo(msa_seq_plot
, facet = "grid"
, method = msa_method
, col_scheme = my_logo_col
, seq_type = 'aa')
, seq_type = 'aa') +
scale_x_discrete(x_lab
, breaks = i_extract
, labels = i_extract
#, limits = min(i_extract): max(i_extract))
, limits = factor(i_extract))
# further customisation
msa_mut_logo_P = p0 + theme(legend.position = leg_pos
@ -111,6 +198,7 @@ LogoPlotMSA <- function(msaSeq_mut
, plot.background = element_rect(fill = theme_bgc))
cat('\nDone: msa_mut_logo_P')
#return(msa_mut_logoP)
LogoPlotMSAL[['msa_mut_logoP']] <- msa_mut_logo_P
@ -118,12 +206,16 @@ LogoPlotMSA <- function(msaSeq_mut
#---------------------------------
# Wild-type MSA: gene_fasta file
#---------------------------------
p1 = ggseqlogo(msaSeq_wt
#msaSeq_wt$V1
p1 = ggseqlogo(wt_seq_plot
, facet = "grid"
, method = msa_method
, col_scheme = my_logo_col
, seq_type = 'aa')
, seq_type = 'aa')+
scale_x_discrete(x_lab
, breaks = i_extract
, labels = i_extract
#, limits = min(i_extract): max(i_extract))
, limits = factor(i_extract))
# further customisation
msa_wt_logo_P = p1 +