added my_logolas.R

This commit is contained in:
Tanushree Tunstall 2022-01-24 13:46:50 +00:00
parent febb8f0f7f
commit 9aa62b33b1
5 changed files with 1423 additions and 93 deletions

View file

@ -27,6 +27,7 @@ LogoPlotMSA <- function(msaSeq_mut
, y_lab = ""
, x_ats = 13 # text size
, x_tangle = 90 # text angle
, x_axis_offset = 0.07 # dist b/w y-axis and plot start
, y_ats = 13
, y_tangle = 0
, x_tts = 13 # title size
@ -47,42 +48,66 @@ LogoPlotMSA <- function(msaSeq_mut
if(missing(plot_positions)){
#if(is.null(plot_positions)){
cat("\nPlotting entire MSA")
cat("\n======================="
, "\nPlotting entire MSA"
, "\n========================")
msa_seq_plot = msaSeq_mut
msa_all_interim = sapply(msa_seq_plot, function(x) unlist(strsplit(x,"")))
msa_all_interimDF = data.frame(msa_all_interim)
msa_all_pos = as.numeric(rownames(msa_all_interimDF))
wt_seq_plot = msaSeq_wt
wt_all_interim = sapply(wt_seq_plot, function(x) unlist(strsplit(x,"")))
wt_all_interimDF = data.frame(wt_all_interim)
wt_all_pos = as.numeric(rownames(wt_all_interimDF))
} else {
cat("\nUser specified plotting positions for MSA:"
, "These are:", plot_positions)
, "\nThese are:\n", plot_positions
, "\nSorting plot positions...")
plot_positions = sort(plot_positions)
cat("\nPlotting positions sorted:\n"
, plot_positions)
#-----------
# MSA: mut
#-----------
cat("\nGenerating MSA: filtered positions")
msa_interim = sapply(msaSeq_mut, function(x) unlist(strsplit(x,"")))
cat("\n==========================================="
, "\nGenerating MSA: filtered positions"
, "\n===========================================")
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{
msa_interim = sapply(msaSeq_mut, function(x) unlist(strsplit(x,"")))
msa_interimDF = data.frame(msa_interim)
msa_pos = as.numeric(rownames(msa_interimDF))
if (all(plot_positions%in%msa_pos)){
cat("\nAll positions within range"
, "\nProceeing with generating requested position MSA seqs...")
, "\nProceeding with generating requested position MSA seqs..."
, "\nNo. of positions in plot:", length(plot_positions))
i_extract = plot_positions
dfP1 = msa_interimDF[i_extract,]
}else{
cat("\nNo. of positions selected:", length(plot_positions))
i_ofr = plot_positions[!plot_positions%in%msa_pos]
cat("\n1 or more plot_positions out of range..."
, "\nThese are:\n", i_ofr
, "\nQuitting! Resubmit with correct plot_positions")
#i_extract = plot_positions[plot_positions%in%msa_pos]
#cat("\nFinal no. of positions being plottted:", length(i_extract)
# , "\nNo. of positions dropped from request:", length(i_ofr))
quit()
}
matP1 = msa_interim[i_extract, 1:ncol(msa_interim)]
dfP1 = data.frame(t(matP1))
#matP1 = msa_interim[i_extract, 1:ncol(msa_interim)]
#dfP1 = msa_interimDF[i_extract,]
dfP1 = data.frame(t(dfP1))
names(dfP1) = i_extract
cols_to_paste = names(dfP1)
dfP1['chosen_seq'] = apply( dfP1[ , cols_to_paste]
dfP1['chosen_seq'] = apply(dfP1[ , cols_to_paste]
, 1
, paste, sep = ''
, collapse = "")
@ -92,44 +117,46 @@ LogoPlotMSA <- function(msaSeq_mut
#-----------
# WT: fasta
#-----------
cat("\nGenerating WT fasta: filtered positions")
cat("\n========================================="
, "\nGenerating WT fasta: filtered positions"
,"\n===========================================")
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{
wt_interimDF = data.frame(wt_interim)
wt_pos = as.numeric(rownames(wt_interimDF))
if (all(plot_positions%in%wt_pos)){
cat("\nAll positions within range"
, "\nProceeing with generating requested position MSA seqs...")
, "\nProceeding with generating requested position MSA seqs..."
, "\nplot positions:", length(plot_positions))
i2_extract = plot_positions
}else{
cat("\nNo. of positions selected:", length(plot_positions))
i2_ofr = plot_positions[!plot_positions%in%wt_pos]
cat("\n1 or more plot_positions out of range..."
, "\nThese are:\n", i_ofr
, "\nQuitting! Resubmit with correct plot_positions")
#i2_extract = plot_positions[plot_positions%in%wt_pos]
#cat("\nFinal no. of positions being plottted:", length(i2_extract)
# , "\nNo. of positions dropped from request:", length(i2_ofr))
quit()
}
matP2 = wt_interim[i_extract, 1:ncol(wt_interim)]
dfP2 = data.frame(t(matP2))
#matP1 = msa_interim[i_extract, 1:ncol(msa_interim)]
dfP2 = wt_interimDF[i2_extract,]
dfP2 = data.frame(t(dfP2))
names(dfP2) = i2_extract
cols_to_paste_P2 = names(dfP2)
dfP2['chosen_seq'] = apply( dfP2[ , cols_to_paste_P2]
cols_to_paste2 = names(dfP2)
dfP2['chosen_seq'] = apply( dfP2[ , cols_to_paste2]
, 1
, paste, sep = ''
, collapse = "")
wt_seq_plot = dfP2$chosen_seq
}
}
######################################
# Generating plots for muts and wt
#####################################
######################################
# Generating plots for muts and wt
#####################################
LogoPlotMSAL <- list()
if (my_logo_col %in% c('clustalx','taylor')) {
@ -144,8 +171,9 @@ LogoPlotMSA <- function(msaSeq_mut
}
if (my_logo_col %in% c('chemistry', 'hydrophobicity')) {
cat('\nSelected colour scheme:', my_logo_col
, "\nUsing grey theme")
cat("\nstart of MSA"
, '\nSelected colour scheme:', my_logo_col
, "\nUsing grey theme")
theme_bgc = "grey"
xfont_bgc = "black"
@ -192,18 +220,22 @@ LogoPlotMSA <- function(msaSeq_mut
xlab(x_lab)
if (missing(plot_positions)){
msa_mut_logo_P = p0
msa_mut_logo_P = p0 +
scale_x_discrete(breaks = msa_all_pos
, expand = c(0.02,0)
, labels = msa_all_pos
, limits = factor(msa_all_pos))
}else{
msa_mut_logo_P = p0 +
scale_y_continuous(expand = c(0,0.09)) +
scale_x_discrete(breaks = i_extract
, expand = c(0.09,0)
, expand = c(x_axis_offset,0)
, labels = i_extract
, limits = factor(i_extract))
}
cat('\nDone: msa_mut_logo_P')
cat('\nDone: MSA plot for mutations')
#return(msa_mut_logoP)
LogoPlotMSAL[['msa_mut_logoP']] <- msa_mut_logo_P
@ -237,20 +269,24 @@ LogoPlotMSA <- function(msaSeq_mut
, plot.background = element_rect(fill = theme_bgc)) +
ylab("") + xlab("Wild-type position")
if (missing(plot_positions)){
msa_wt_logo_P = p1
msa_wt_logo_P = p1 +
scale_x_discrete(breaks = wt_all_pos
, expand = c(0.02,0)
, labels = wt_all_pos
, limits = factor(wt_all_pos) )
}else{
msa_wt_logo_P = p1 +
scale_y_continuous(expand = c(0,0.09)) +
scale_x_discrete(breaks = i2_extract
, expand = c(0.09,0)
, expand = c(x_axis_offset, 0)
, labels = i2_extract
, limits = factor(i2_extract))
}
cat('\nDone: msa_wt_logo_P')
cat('\nDone: MSA plot for WT')
#return(msa_wt_logoP)
LogoPlotMSAL[['msa_wt_logoP']] <- msa_wt_logo_P