绘制R中不同文件的重叠位置

时间:2013-02-01 12:55:42

标签: r plot ggplot2 lattice

我有一个包含StartEnd位置的两个大文件和两个示例列(数字)。

File 1:

Start  End  Sample1  Sample2
1         60      1       4
100       200     2       1
201       250     1       4
300       450     1       1

File 2:

Start  End  Sample1  Sample2
40         60      1       1
70        180      1       1
240       330      2       1
340       450      1       4
500       900      1       4
980       1200     2       1

首先,我想从第一个文件中获取第一个StartEnd位置并制作一个段图。该图还必须考虑第一个文件中每个位置的Start-20End+20

然后我想从第二个文件中取出重叠 StartEnd位置并将其绘制在上图中。通过这种方式,将会有许多基于第一个文件中StartEnd位置的图表,而且还会有单独的图表。

每个细分受众群的color将基于两个样本数字(例如,如果文件的1 and 4段的颜色为red,则1 and 1green 1}}段的颜色为overlap_func <- function(dataset1,dataset2) { for(i in 1:nrow(dataset1)) { loop_start <- dataset1[i,"Start"] loop_end <- dataset1[i,"End"] p <- dataset2[,c(1,2)] dataset1_pos <- data.frame(loop_start,loop_end) dataset2_filter <- p[p$Start >= (loop_start-(loop_start/2)) & p$End <= (loop_end+ (loop_end/2)), ] data_in_loop <- rbind(dataset1_pos,dataset2_filter) plot_function(data_in_loop,loop_start,loop_end) } } plot_function <- function(loop_data,start,end){ pos <- 1:nrow(loop_data) dat1 <- cbind(pos,loop_data) colnames(dat1) <- c("pos","start","end") pdf(file=paste0("path where plots are generated","_",start,"-",end,"_","overlap.pdf")) plot(dat1$pos, type = 'n', xlim = range(c(start-(start/2), end+(end/2)))) segments(dat1$start, dat1$pos, dat1$end, dat1$pos) dev.off() } df1 <- read.table(header=T, text="Start End Sample1 Sample2 1 60 1 4 100 200 2 1 201 250 1 4 300 450 1 1") df2 <- read.table(header=T, text="Start End Sample1 Sample2 40 60 1 1 70 180 1 1 240 330 2 1 340 450 1 4 500 900 1 4 980 1200 2 1") overlap_func(df1,df2) ,依此类推)。

如果有人让我了解如何在R中为此做出功能,我将非常感激。

提前致谢。

PS我附加了enter image description here输出的图纸。我只展示了两个结果。

下面是我编写的代码,但它给出了错误

match.names(clabs,names(xi))出错:   名称与以前的名称不匹配

另外,我需要为dataset1线段和绿色指定红色 dataset2线段。我将如何在下面的代码中实现它?

{{1}}

2 个答案:

答案 0 :(得分:2)

这样的东西?

df1 <- read.table(header=T, text="Start  End  Sample1  Sample2
1         60      1       4
100       200     2       1
201       250     1       4
300       450     1       1")

df2 <- read.table(header=T, text="Start  End  Sample1  Sample2
40         60      1       1
70        180      1       1
240       330      2       1
340       450      1       4
500       900      1       4
980       1200     2       1")

require(IRanges)
require(ggplot2)
require(plyr)

df1$id <- factor(1:nrow(df1))
ir2 <- IRanges(df2$Start, df2$End)
out <- ddply(df1, .(id), function(x) {
    ir1 <- IRanges(x$Start, x$End)
    o.idx <- as.data.frame(findOverlaps(ir1, ir2))$subjectHits
    df.out <- rbind(x[, 1:4], df2[o.idx, ])
    df.out$id1 <- x$id
    df.out$id2 <- seq_len(nrow(df.out))
    df.out
})
out$id1 <- factor(out$id1)
out$id2 <- factor(out$id2)
out$id3 <- factor(1:nrow(out))

p <- ggplot(out, aes(x = Start, y = id3 , colour = id2)) 
p <- p + geom_segment(aes(xend = End, ystart = id3, yend = id3))
p <- p + scale_colour_brewer(palette = "Set1")
p

gglot2_no_facet_geom_segment

修改:查看更新的图纸后,这可能是您想要的吗?

p + facet_wrap( ~ id1, scales="free")

ggplot2_facet_geom_segment

编辑:将facet中的每个绘图保存在单独的文件中。您可以通过在id1

上拆分每次生成绘图来完成此操作
d_ply(out, .(id1), function(ww) {
    p <- ggplot(ww, aes(x = Start, y = id3 , colour = id2)) 
    p <- p + geom_segment(aes(xend = End, ystart = id3, yend = id3))
    p <- p + scale_colour_brewer(palette = "Set1")
    fn <- paste0("~/Downloads/id", as.numeric(as.character(ww$id1[1])), ".pdf")
    ggsave(fn, p)
})

相应地在fn中设置路径。

答案 1 :(得分:1)

我尝试使用lattice包解决此问题。我特意使用函数Shingle来了解要比较的间隔。我希望我可以合并2个带状疱疹,但我不能。所以,一旦我有了我的第一个情节,我就使用(如上面的解决方案)IRanges包来计算重叠。这个想法有一个最终dotplot

## I red the input data
dat <- read.table(text = 'Start  End  Sample1  Sample2
1         60      1       4
100       200     2       1
201       250     1       4
300       450     1       1', header = T) 

dat1 <- read.table(text = 'Start  End  Sample1  Sample2
40         60      1       1
70        180      1       1
240       330      2       1
340       450      1       4
500       900      1       4
980       1200     2       1', header = T) 


## I create my 2 shingles
dat.sh <- shingle(x = dat[,3], intervals = dat[,c(1,2)])
dat1.sh <- shingle(x = dat1[,3], intervals = dat1[,c(1,2)])
## compute max value for plot comparison
max.value <- max(c(dat$End,dat1$End))
## I plot the 2 series with differents color
p1<- plot(dat.sh, xlim= c(0,max.value),col = 'red')
p2 <- plot(dat1.sh,xlim= c(0,max.value), col ='green')
library(gridExtra)
grid.arrange(p1,p2)

这是比较我的间隔的快速方法。

enter image description here

这看起来不错,但我无法进一步处理木瓦,因为我无法将它们合并到同一个地块中。因此,我将使用IRanges包来计算重叠。

library(IRanges)
rang1 <- IRanges(start=dat[,1], end = dat[,2])
rang2 <- IRanges(start=dat1[,1], end = dat1[,2])
dat.plot     <- dat1                    # use the first data.frame
dat.plot$group <- 'origin'
dat.plot$id <- rownames(dat1)           ## add an Id for each row
rang.o <- findOverlaps(rang2,rang1)     # get overlaps
dat.o <- dat1[rang.o@queryHits,]        ## construct overlaps data.frame 
dat.o$id <- rang.o@subjectHits
dat.o$group <- 'overlap'
dat.plot <- rbind(dat.plot,dat.o)      ## union of all 
dotplot(id ~End-Start|group  , data=dat.plot, 
                               groups = col,type = c("p", "h"))

enter image description here