Grouping track in the package circlize in R

落爺英雄遲暮 提交于 2019-12-23 02:38:36

问题


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

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).

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)

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

What am I doing wrong?

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!

Thank you for the help!


回答1:


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.

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()

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()



来源:https://stackoverflow.com/questions/29617050/grouping-track-in-the-package-circlize-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!