File: dotPlot.R

package info (click to toggle)
r-cran-seqinr 3.3-3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,844 kB
  • ctags: 69
  • sloc: ansic: 1,955; makefile: 13
file content (32 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
dotPlot <- function(seq1, seq2, wsize = 1, wstep = 1, nmatch = 1, col = c("white", "black"), 
xlab = deparse(substitute(seq1)), ylab = deparse(substitute(seq2)), ...){
 #
 # Check arguments:
 #
 if(nchar(seq1[1]) > 1) stop("seq1 should be provided as a vector of single chars")
 if(nchar(seq2[1]) > 1) stop("seq2 should be provided as a vector of single chars")
 if(wsize < 1) stop("non allowed value for wsize")
 if(wstep < 1) stop("non allowed value for wstep")
 if(nmatch < 1) stop("non allowed value for nmatch")
 if(nmatch > wsize) stop("nmatch > wsize is not allowed")
 #
 # sliding window on sequences:
 #
 mkwin <- function(seq, wsize, wstep){
  sapply(seq(from = 1, to = length(seq) - wsize + 1, by = wstep), function(i) c2s(seq[i:(i + wsize - 1)]))
 }
 wseq1 <- mkwin(seq1, wsize, wstep)
 wseq2 <- mkwin(seq2, wsize, wstep)
 if( nmatch == wsize ){
   # perfect match case
   xy <- outer(wseq1, wseq2, "==")
 } else {
   # partial match case
   "%==%" <- function(x, y) colSums(sapply(x, s2c) == sapply(y, s2c)) >= nmatch
   xy <- outer(wseq1, wseq2, "%==%")
 }
 image(x = seq(from = 1, to = length(seq1), length = length(wseq1)), 
       y = seq(from = 1, to = length(seq2), length = length(wseq2)),
       z = xy, col = col, xlab = xlab, ylab = ylab, ...)
  box()
}