seqinr dotplot - 改变轴

时间:2014-11-09 22:56:21

标签: r data-visualization sequence-analysis

我需要数据集:seq1和seq2(DNA序列)。我想做一个数据槽,比较两个序列并在两个序列匹配的位置放置一个点。我能够使用seqinr的dotplot来完成这个,但我无法做的是在轴上列出序列,这样你就可以看到哪些点匹配了。基本上,我想用序列字母替换数字。

有没有这样做?也许通过ggplot2?

Dotplot

这些是我的序列:

seq1 <- c("G","C","T","A","G","T","C","A","G","A","T","C","T","G","A","C","G","C","T","A")
seq2 <- c("G","A","T","G","G","T","C","A","C","A","T","C","T","G","C","C","G","C")

这就是我生成此图表的方式:

dotPlot(seq1, seq2, main = "Dot plot of 2 different sequences
\nwsize = 4, wstep = 1, nmatch = 3", wsize = 4, wstep = 1, nmatch = 3)

2 个答案:

答案 0 :(得分:1)

dotplot-function的修改版本(又名“快速而肮脏的黑客”)怎么样?首先,将以下代码复制并粘贴到R:

dotplot<-function (seq1, seq2, wsize = 1, wstep = 1, nmatch = 1, cols = c("white", 
    "black"), xlab = deparse(substitute(seq1)), ylab = deparse(substitute(seq2)), type=2,
    ...) {

    cat("This is a modification of the function dotPlot from package seqinr.\n")

    require(seqinr)

    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")
    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) {
        xy <- outer(wseq1, wseq2, "==")
    }
    else {
        "%==%" <- function(x, y) colSums(sapply(x, s2c) == sapply(y, 
            s2c)) >= nmatch
        xy <- outer(wseq1, wseq2, "%==%")
    }

    if(type==1) {
       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, axes=F, ...)
       box()
    }

    colnames(xy)<-wseq2
    rownames(xy)<-wseq1

    xy2<-matrix(nrow=length(seq1), ncol=length(seq2), data=FALSE)
    rownames(xy2)<-seq1
    colnames(xy2)<-seq2
    ind<-which(xy, arr.ind=T)
    xy2[ind]<-TRUE
    ind<-data.frame(ind, row.names=NULL)    

    res<-data.frame(row=c(), col=c())
    for(i in 1:nrow(ind)) {
       DF<-data.frame(row=seq(from=ind$row[i], to=ind$row[i]+wsize-1),
                      col=seq(from=ind$col[i], to=ind$col[i]+wsize-1))
       res<-rbind(res, DF)
    }
    xy2[as.matrix(res)]<-TRUE

    if(type==2) {
       image(x = seq(from = 1, to = length(seq1)), y = seq(from = 1, to = length(seq2)), z = xy2, col = cols, xlab = xlab, ylab = ylab, axes=F, ...)
       box()
       axis(side=1, at=1:length(seq1), labels=seq1)
       axis(side=2, at=1:length(seq2), labels=seq2, las=1)
    }

    out<-list(type1=xy, type2=xy2)
    return(out)
}

然后运行您提供的示例:

seq1 <- c("G","C","T","A","G","T","C","A","G","A","T","C","T","G","A","C","G","C","T","A")
seq2 <- c("G","A","T","G","G","T","C","A","C","A","T","C","T","G","C","C","G","C")
xy<-dotplot(seq1, seq2, wsize = 4, wstep = 1, nmatch = 3)

应该产生以下图:

enter image description here

简而言之,我稍微扩展了函数以在两个序列之间产生完整的矩阵。如果检查输出对象xy,您将看到原始(type1)矩阵和扩展的矩阵(type2)。对于非常长的序列,这种修饰是无效的,更不用说轴上的核苷酸/氨基酸标记将彼此重叠。您可以使用新参数type更改type1和type2之间的绘图类型。

答案 1 :(得分:0)

作为比较两个序列的替代方法,您可以考虑seqalign R包的TraMineR函数。

以下是一个例子:

library(TraMineR)
seq1 <- c("G","C","T","A","G","T","C","A","G","A","T","C",
        "T","G","A","C","G","C","T","A")
## Filling seq2 with "*" to equalize sequence length
seq2m <- c("G","A","T","G","G","T","C","A","C","A","T","C",
        "T","G","C","C","G","C","*","*")

## defining sequence object interpreting "*"s as mising states
seq <- seqdef(rbind(seq1,seq2m), missing="*")

## Setting all substitution costs as 2
cost <- matrix(rep(2,16),4,4)
diag(cost) <- 0
cost

## Comparing sequences 1 and 2 
sa <- seqalign(seq, 1:2, indel=1, sm=cost)
print(sa)
plot(sa)

Comparison of two sequences in terms of edit operations

该图显示了匹配元素(EQU)和将一个序列转换为另一个序列的最小编辑操作(SUB和IND)。