包装中的分组轨迹在R中循环 [英] Grouping track in the package circlize in R

查看:150
本文介绍了包装中的分组轨迹在R中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里创建了类似于circlize教程第8页上的图: http://cran.r-project.org/web/包/circlize/vignettes/genomic_plot.pdf

I have created a plot similar to the one on page 8 of the circlize tutorial here: http://cran.r-project.org/web/packages/circlize/vignettes/genomic_plot.pdf

现在,我试图在基因名称的上方覆盖一条额外的轨道,该轨道显示将基因分组为更大的类别(在下图中的插图中,我试图添加蓝线,希望得到带有circlize的漂亮图片).

Now I am trying to overlay an additional track on top of the gene names, which shows grouping of the genes into bigger categories (in the image below taken from the vignette, I am trying to add the blue lines, hoping to get a prettier image with circlize).

我有一个新的数据框,其中包含每个较宽区域的开始和结束以及标签(我正在尝试在这些区域的顶部添加标签).

I have a new data frame with the start and stop of each of these wider regions, and the labels (which I am trying to add on top of those regions).

我尝试从以前的绘图中获取xlim,ylim和index信息,但是由于它是一个新的数据帧,因此我遇到了麻烦.

I have tried taking the xlim, ylim, and index information from the previous plotting, but I am having trouble since it is a new data frame.

这就是我正在做的事情:说我的新数据框(与主数据具有相同的坐标)是这样的:

This is what I am doing: say my new dataframe (with the same coordinates as the main data) is this:

df = structure(list(Chr = c("chr1", "chr10", "chr12"), pos.start = c(2e+06, 2e+06, 2e+06), pos.end = c(3e+06, 6e+06, 3e+06), name = c("A", "B", "C")), .Names = c("Chr", "pos.start", "pos.end", "name"), row.names = c(1L, 2L, 3L), class = "data.frame")

使用示例数据集初始化我的主要数据集及其后的基因后,我试图添加如下轨迹:

After initialising the circos using my primary dataset and it's factors for genes as in the example vignette, I am trying to add the track like this:

circos.trackPlotRegion(ylim = c(0.5, 0.5), track.index=1,
panel.fun = function(x, y) {
chr = get.cell.meta.data("sector.index")
# find regions in this chromosome
regions  = unique(df[df$Chr == chr, , drop = FALSE]$name)
df2 = df[df$Chr == chr, , drop = FALSE]
for(i in seq_len(nrow(df2))) {
   region = which(regions %in% df2$name[i])
   circos.rect(region, 0.2, 
          region, 0.2, color="blue", border = NA)
}
}, bg.border = NA)

而且我不断收到此错误: "if(ncut){:错误无法解释为逻辑".

And I keep getting this error: "Error in if (ncut) { : argument is not interpretable as logical".

我在做什么错了?

我也尝试使用par(new = TRUE)覆盖整个新图,但是由于它会重新缩放以适合整个圆,因此我无法使其与之前的图完全重叠(而我仅定义了一些用于更大的区域). 如果有人在如何在R中画圆时指出如何绘制此重叠区域(使用定义这些较宽区域的不同数据集)的指针,我将不胜感激!

I also tried overlaying a whole new plot using par(new = TRUE), but I can't get it to overlap exactly to my previous plot since it rescales to fit the whole circle (while I only have some segments defined for the wider regions). If anybody has pointers on how to draw this overlaying region (using a different dataset defining these wider regions) in circlize in R I would very much appreciate it!

谢谢您的帮助!

推荐答案

如果您想覆盖整个行业,例如如果要划一条线覆盖两个基因(每个基因是图中的一个扇区),则可以尝试highlight.sector(). 该功能是最近添加到circlize的,您可以将circlize更新到最新版本.

If you want to cover the whole sectors, e.g. if you want to draw a line to cover the two genes (each gene is a sector in the plot), you may try highlight.sector(). The function is added to circlize very recently, you may update circlize to the newest version.

要使用highlight.sector(),应首先创建一个轨道(高度非常小),然后通过为轨道和扇区指定索引来突出显示此轨道中的两个基因.一个简单的示例如下(左图):

To use highlight.sector(), you should create a track (with very small height) first, then highlight the two genes in this track by specifying index for the tracks and sectors. An simple example is as follows (left figure):

#########################################
# example 1: cover the whole sectors
o.cell.padding = circos.par("cell.padding")

circos.initializeWithIdeogram(plotType = NULL)
circos.trackPlotRegion(ylim = c(0, 1), track.height = 0.01, bg.border = NA, 
    cell.padding = c(0, o.cell.padding[2], 0, o.cell.padding[4]))
circos.trackPlotRegion(ylim = c(0, 1))

highlight.sector(sector.index = c("chr1", "chr2"), track.index = 1, 
    col = "red", border = NA)
circos.clear()

如果仅要突出显示部分扇区,则应使用draw.sector().与rect()一样,draw.sector()期望两个角的坐标(在极坐标系中测量).但是,正确坐标的计算有些棘手. circlize()函数将通过从每个单元格中的数据坐标进行转换来为您提供极坐标.但是您需要将数据点映射到正确的扇区.您可以从以下示例中看到这个想法(右图)

If you want to highlight only part of the sectors, you should use draw.sector() instead. draw.sector(), like rect(), expects coordinates (measured in polar coordinate system) for the two corners. However, calculation for the correct coordinates is a little bit tricky. The circlize() function will give you polar coordinates by transforming from data coordinate in each cell. But you need to map the data point to the correct sector. You may see the idea from following example (right figure)

############################################
# example 2: cover only part of the sectors
o.cell.padding = circos.par("cell.padding")

circos.initializeWithIdeogram(plotType = NULL)
circos.trackPlotRegion(ylim = c(0, 1), track.height = 0.01, bg.border = NA, 
    cell.padding = c(0, o.cell.padding[2], 0, o.cell.padding[4]))
circos.trackPlotRegion(ylim = c(0, 1))

pos1 = circlize(1e8, 0, sector.index = "chr1", track.index = 1)
pos2 = circlize(1e8, 1, sector.index = "chr2", track.index = 1)
draw.sector(pos1[1, "theta"], pos2[1, "theta"], pos1[1, "rou"], pos2[1, "rou"], 
    clock.wise = TRUE, col = "red", border = NA)
circos.clear()

这篇关于包装中的分组轨迹在R中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆