使用ggplot2绘制“序列标识”?

时间:2011-03-25 21:47:12

标签: r graphics ggplot2

是否(合理地)使用ggplot2绘制sequence logo plot

有一个基于“网格”称为“seqLogo”的程序包,但我想知道是否可能有ggplot2版本。

感谢。

enter image description here

6 个答案:

答案 0 :(得分:12)

我提交的ggplot2尝试有点类似于上面的莱比锡/贝瑞解决方案。这种格式是 little bit 更接近标准语标。

但我的解决方案,我认为任何ggplot2解决方案, 仍然不足,因为ggplot2无法控制绘制符号的宽高比 。这是(我认为)生成序列徽标所需的核心功能,ggplot2缺少这些功能。

另请注意:我使用了来自Jeremy Leipzig's answer的数据,但我没有对小样本量或%GC值不同于50%进行任何更正。

require(ggplot2)
require(reshape2)

 freqs<-matrix(data=c(0.25,0.65,0.87,0.92,0.16,0.16,0.04,0.98,0.98,1.00,0.02,0.10,0.10,0.80,0.98,0.91,0.07,0.07,0.11,0.05,0.04,0.00,0.26,0.17,0.00,0.01,0.00,0.00,0.29,0.17,0.01,0.03,0.00,0.00,0.32,0.32,0.53,0.26,0.07,0.02,0.53,0.18,0.96,0.01,0.00,0.00,0.65,0.01,0.89,0.17,0.01,0.09,0.59,0.12,0.11,0.04,0.02,0.06,0.05,0.49,0.00,0.00,0.02,0.00,0.04,0.72,0.00,0.00,0.01,0.00,0.02,0.49),byrow=TRUE,nrow=4,dimnames=list(c('A','C','G','T')))

freqdf <- as.data.frame(t(freqs))

freqdf$pos = as.numeric(as.character(rownames(freqdf)))

freqdf$height <- apply(freqdf[,c('A', 'C','G','T')], MARGIN=1,
                       FUN=function(x){2-sum(log(x^x,base=2))})

logodf <- data.frame(A=freqdf$A*freqdf$height, C=freqdf$C*freqdf$height,
                     G=freqdf$G*freqdf$height, T=freqdf$T*freqdf$height, 
                     pos=freqdf$pos)

lmf <- melt(logodf, id.var='pos')

quartz(height=3, width=8)

ggplot(data=lmf, aes(x=as.numeric(as.character(pos)), y=value))  +
    geom_bar(aes(fill=variable,order=value), position='stack', 
        stat='identity', alpha=0.5) +
    geom_text(aes(label=variable, size=value, order=value, vjust=value),
        position='stack') +
    theme_bw()

quartz.save('StackOverflow_5438474.png', type='png')

生成此图表:

Not bad, but not quite a sequence logo plot

答案 1 :(得分:11)

我已经实施了Charles Berry设计的替代方案,它解决了下面评论部分中讨论的seqLogos的一些弱点。它使用ggplot2:

library("devtools")
install_github("leipzig/berrylogo")
library("berrylogo")
freqs<-matrix(data=c(0.25,0.65,0.87,0.92,0.16,0.16,0.04,0.98,0.98,1.00,0.02,0.10,0.10,0.80,0.98,0.91,0.07,0.07,0.11,0.05,0.04,0.00,0.26,0.17,0.00,0.01,0.00,0.00,0.29,0.17,0.01,0.03,0.00,0.00,0.32,0.32,0.53,0.26,0.07,0.02,0.53,0.18,0.96,0.01,0.00,0.00,0.65,0.01,0.89,0.17,0.01,0.09,0.59,0.12,0.11,0.04,0.02,0.06,0.05,0.49,0.00,0.00,0.02,0.00,0.04,0.72,0.00,0.00,0.01,0.00,0.02,0.49),byrow=TRUE,nrow=4,dimnames=list(c('A','C','G','T')))
p<-berrylogo(freqs,gc_content=.41)
print(p)

enter image description here

答案 2 :(得分:8)

ggseqlogo 应该是您正在寻找的内容。我希望这可以减轻我确信你们很多人在R中绘制序列标识时所遇到的一些挫折

答案 3 :(得分:4)

就我而言,没有直接的方法在ggplot2中这样做。

但是,请查看RWebLogo。它是我为WebLogo python库编写的R包装器。您可以从CRAN下载github

来下载它

简单示例:

# Load package
library('RWebLogo')

# Sample alignment
aln <- c('CCAACCCAA', 'CCAACCCTA', 'AAAGCCTGA', 'TGAACCGGA')
# Plot logo to file
weblogo(seqs=aln, file.out='logo.pdf')

# Plot logo to R graphics device (uses generated jpeg logo and raster package)
weblogo(seqs=aln, plot=TRUE, open=FALSE, format='jpeg', resolution=600)

有关更多选项,请参阅?weblogo?plotlogo

答案 4 :(得分:3)

这是另一种选择。 motiflogo是由ggplot2实现的motif(序列)徽标的新表示。可以考虑两个方面。

  1. 作为规范主题标识表示
  2. 作为特定于SNP的图案徽标表示
  3. a canonical motif logo representation a SNP-specific motif logo representation

答案 5 :(得分:2)

现在有一个gglogo包(也在CRAN上,是Heike Hofmann的另一个惊人的ggplot2扩展)。

这个包产生如下图:

NumberFormat

enter image description here

示例来自https://github.com/heike/gglogo/blob/master/visual_test/logos.R,此处的包装上有一份手稿:https://github.com/heike/logopaper/blob/master/logos.Rmd