Creating dot plots for alignment visualization can be executed using the following steps:
- Load the libraries and sequences:
library(Biostrings)
library(ggplot2)
library(dotplot)
seqs <- readAAStringSet(file.path(getwd(), "datasets", "ch4", "bhlh.fa"))
- Make a basic dot plot:
dotPlotg(as.character(seqs[[1]]), as.character(seqs[[2]] ))
- Change the dot plot and apply the ggplot2 themes and labels:
dotPlotg(as.character(seqs[[1]]), as.character(seqs[[2]] ), wsize=7, wstep=5, nmatch=4) +
theme_bw() +
labs(x=names(seqs)[1], y=names(seqs)[2] )
- Make a function that will create a dot plot from sequences provided and the sequence index:
make_dot_plot <- function(i=1, j=1, seqs = NULL){
seqi <- as.character(seqs[[i]])
seqj <- as.character(seqs[[j]])
namei <- names(seqs)[i]
namej <- names(seqs)[j]
return( dotPlotg(seqi, seqj ) + theme_bw() + labs(x=namei, y=namej) )
}
- Set up data structures to run the function:
combinations <- expand.grid(1:length(seqs),1:length(seqs))
plots <- vector("list", nrow(combinations) )
- Run the function on all the possible combinations of pairs of sequences:
for (r in 1:nrow(combinations)){
i <- combinations[r,]$Var1[[1]]
j <- combinations[r,]$Var2[[1]]
plots[[r]] <- make_dot_plot(i,j, seqs)
}
- Plot the grid of plots:
cowplot::plot_grid(plotlist = plots)