Skip to content

Commit

Permalink
add circos.labels()
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Jan 7, 2021
1 parent 9ba9dd8 commit 4a95ffe
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: circlize
Type: Package
Title: Circular Visualization
Version: 0.4.12.1007
Version: 0.4.12.1008
Date: 2021-1-7
Author: Zuguang Gu
Maintainer: Zuguang Gu <[email protected]>
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export("circos.heatmap.link")
export("circos.info")
export("circos.initialize")
export("circos.initializeWithIdeogram")
export("circos.labels")
export("circos.lines")
export("circos.link")
export("circos.nested")
Expand Down Expand Up @@ -139,3 +140,4 @@ importFrom("stats", rnorm)
importFrom("stats", runif)
importFrom("utils", download.file)
importFrom("utils", read.table, packageDescription)

1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changes in version 0.4.12
* add `circos.connect()` function.
* `circos.heatmap()`: argument `cluster` can be a clustering object.
* `circos.heatmap()`: add `cell_width` to control the relative width of heatmap cells.
* add `circos.labels()`.

Changes in version 0.4.11
-----------------------------------------------------------------------
Expand Down
43 changes: 25 additions & 18 deletions R/circos.heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,19 @@ circos.heatmap.initialize = function(mat, split = NULL, cluster = TRUE,

if(cluster_is_dendrogram) {
for(se in get.all.sector.index()) {
cw = cell_width_list[[se]][order.dendrogram(dend)]
add.sector.meta.data("cell_width", cw, sector.index = se)
add.sector.meta.data("cell_middle", cumsum(cw) - cw/2, sector.index = se)

dend = dend_list[[se]]

od = order.dendrogram(dend)
cw = cell_width_list[[se]][od]
od2 = order(od)
add.sector.meta.data("cell_width", cw[od2], sector.index = se)
add.sector.meta.data("cell_middle", (cumsum(cw) - cw/2)[od2], sector.index = se)

dend = ComplexHeatmap::adjust_dend_by_x(dend, cumsum(cw) - cw/2)
add.sector.meta.data("row_dend", dend, sector.index = se)
add.sector.meta.data("dend", dend, sector.index = se)
add.sector.meta.data("row_order", order.dendrogram(dend), sector.index = se)
add.sector.meta.data("order", order.dendrogram(dend), sector.index = se)
add.sector.meta.data("row_order", od, sector.index = se)
add.sector.meta.data("order", od, sector.index = se)
if(!is.null(subset_list)) {
add.sector.meta.data("subset", subset_list[[se]], sector.index = se)
}
Expand All @@ -210,16 +213,19 @@ circos.heatmap.initialize = function(mat, split = NULL, cluster = TRUE,
}

for(se in get.all.sector.index()) {
cw = cell_width_list[[se]][order.dendrogram(dend_list[[se]])]
add.sector.meta.data("cell_width", cw, sector.index = se)
add.sector.meta.data("cell_middle", cumsum(cw) - cw/2, sector.index = se)

dend = dend_list[[se]]

od = order.dendrogram(dend)
cw = cell_width_list[[se]][od]
od2 = order(od)
add.sector.meta.data("cell_width", cw[od2], sector.index = se)
add.sector.meta.data("cell_middle", (cumsum(cw) - cw/2)[od2], sector.index = se)

dend = ComplexHeatmap::adjust_dend_by_x(dend, cumsum(cw) - cw/2)
add.sector.meta.data("row_dend", dend, sector.index = se)
add.sector.meta.data("dend", dend, sector.index = se)
add.sector.meta.data("row_order", order.dendrogram(dend_list[[se]]), sector.index = se)
add.sector.meta.data("order", order.dendrogram(dend_list[[se]]), sector.index = se)
add.sector.meta.data("row_order", od, sector.index = se)
add.sector.meta.data("order", od, sector.index = se)
if(!is.null(subset_list)) {
add.sector.meta.data("subset", subset_list[[se]], sector.index = se)
}
Expand Down Expand Up @@ -412,7 +418,8 @@ circos.heatmap = function(mat, split = NULL, col, na.col = "grey",
nr = nrow(m)

if(!is.null(rownames(m))) {
circos.text(CELL_META$cell_middle, rep(0, nr), rownames(m)[od], cex = rownames.cex[CELL_META$subset][od],
circos.text(CELL_META$cell_middle[od], rep(0, nr), rownames(m)[od],
cex = rownames.cex[CELL_META$subset][od],
font = rownames.font[CELL_META$subset][od], col = rownames.col[CELL_META$subset][od],
facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5))
}
Expand Down Expand Up @@ -469,18 +476,18 @@ circos.heatmap = function(mat, split = NULL, col, na.col = "grey",
} else {
# qqcat("@{sum(l)} white rectangles are not drawn.\n")
circos.rect(
(CELL_META$cell_middle - CELL_META$cell_width/2)[!l],
(CELL_META$cell_middle - CELL_META$cell_width/2)[od][!l],
(rep(nc - i, nr))[!l],
(CELL_META$cell_middle + CELL_META$cell_width/2)[!l],
(CELL_META$cell_middle + CELL_META$cell_width/2)[od][!l],
(rep(nc - i + 1, nr))[!l],
border = col_mat[, i][!l],
col = col_mat[, i][!l])
}
} else {
circos.rect(
CELL_META$cell_middle - CELL_META$cell_width/2,
(CELL_META$cell_middle - CELL_META$cell_width/2)[od],
rep(nc - i, nr),
CELL_META$cell_middle + CELL_META$cell_width/2,
(CELL_META$cell_middle + CELL_META$cell_width/2)[od],
rep(nc - i + 1, nr),
border = col_mat[, i], col = col_mat[, i])
}
Expand Down Expand Up @@ -518,7 +525,7 @@ circos.heatmap = function(mat, split = NULL, col, na.col = "grey",
nr = nrow(m)

if(!is.null(rownames(m))) {
circos.text(CELL_META$cell_middle, rep(1, nr), rownames(m)[od], cex = rownames.cex[CELL_META$subset][od],
circos.text(CELL_META$cell_middle[od], rep(1, nr), rownames(m)[od], cex = rownames.cex[CELL_META$subset][od],
font = rownames.font[CELL_META$subset][od], col = rownames.col[CELL_META$subset][od],
facing = "clockwise", niceFacing = TRUE, adj = c(1, 0.5))
}
Expand Down
4 changes: 2 additions & 2 deletions R/genomic.R
Original file line number Diff line number Diff line change
Expand Up @@ -2474,7 +2474,7 @@ circos.genomicHeatmap = function(
# -facing fFacing of the labels. The value can only be ``"clockwise"`` or ``"reverse.clockwise"``.
# -niceFacing Whether automatically adjust the facing of the labels.
# -col Color for the labels.
# -cex Aize of the labels.
# -cex Size of the labels.
# -font Font of the labels.
# -padding Padding of the labels, the value is the ratio to the height of the label.
# -connection_height Height of the connection track.
Expand Down Expand Up @@ -2583,7 +2583,7 @@ circos.genomicLabels = function(

op = circos.par("points.overflow.warning")
circos.par("points.overflow.warning" = FALSE)
side = match.arg(side)[1]
labels.side = side = match.arg(side)[1]
if(labels.side == "inside") {
# an empty track
circos.genomicTrackPlotRegion(bed2, ylim = c(0, 1),
Expand Down
62 changes: 62 additions & 0 deletions R/low_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -2596,3 +2596,65 @@ circos.connect = function(x0, y0, x1, y1,
}
}
}


# == title
# Add a label track
#
# == param
# -sectors A vector of sector names.
# -x Positions of the labels.
# -labels A vector of labels.
# -facing fFacing of the labels. The value can only be ``"clockwise"`` or ``"reverse.clockwise"``.
# -niceFacing Whether automatically adjust the facing of the labels.
# -col Color for the labels.
# -cex Size of the labels.
# -font Font of the labels.
# -padding Padding of the labels, the value is the ratio to the height of the label.
# -connection_height Height of the connection track.
# -line_col Color for the connection lines.
# -line_lwd Line width for the connection lines.
# -line_lty Line type for the connectioin lines.
# -labels_height Height of the labels track.
# -side Side of the labels track, is it in the inside of the track where the regions are marked?
# -labels.side Same as ``side``. It will replace ``side`` in the future versions.
# -track.margin Bottom and top margins.
#
# == details
# This function creates two tracks, one for the connection lines and one for the labels.
#
# If two labels are too close and overlap, this function automatically adjusts the positions of neighouring labels.
circos.labels = function(
sectors, x, labels,
facing = "clockwise",
niceFacing = TRUE,
col = par("col"),
cex = 0.8,
font = par("font"),
padding = 0.4,
connection_height = mm_h(5),
line_col = par("col"),
line_lwd = par("lwd"),
line_lty = par("lty"),
labels_height = min(c(cm_h(1.5), max(strwidth(labels, cex = cex, font = font)))),
side = c("inside", "outside"),
labels.side = side,
track.margin = circos.par("track.margin")) {

if(missing(sectors) && missing(x)) {
env = circos.par("__tempenv__")
if(identical(env$circos.heatmap.initialized, TRUE)) {
subset = unlist(lapply(env$sector.meta.data, function(x) x$subset))
x = unlist(lapply(env$sector.meta.data, function(x) x$cell_middle))
labels = labels[subset]
sectors = rep(names(env$sector.meta.data), times = sapply(env$sector.meta.data, function(x) length(x$subset)))
}
}
bed = data.frame(sectors, x, x)

circos.genomicLabels(bed, labels = labels, facing = facing, niceFacing = niceFacing, col = col, cex = cex,
font = font, padding = padding, connection_height = connection_height, line_col = line_col,
line_lwd = line_lwd, line_lty = line_lty, labels_height = labels_height, side = side, labels.side = labels.side,
track.margin = track.margin)
}

2 changes: 1 addition & 1 deletion man/circos.boxplot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ circos.boxplot(value, pos, outline = TRUE, box_width = 0.6,
\item{value}{A numeric vector, a matrix or a list. If it is a matrix, boxplots are made by columns (each column is a box).}
\item{pos}{Positions of the boxes.}
\item{outline}{Whether to draw outliers.}
\item{box_width}{Width of boxes. It assumes the bars locating at \code{x = 1, 2, ...}.}
\item{box_width}{Width of boxes.}
\item{col}{Filled color of boxes.}
\item{border}{Color for the border as well as the quantile lines.}
\item{lwd}{Line width.}
Expand Down
2 changes: 1 addition & 1 deletion man/circos.genomicLabels.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ circos.genomicLabels(
\item{facing}{fFacing of the labels. The value can only be \code{"clockwise"} or \code{"reverse.clockwise"}.}
\item{niceFacing}{Whether automatically adjust the facing of the labels.}
\item{col}{Color for the labels.}
\item{cex}{Aize of the labels.}
\item{cex}{Size of the labels.}
\item{font}{Font of the labels.}
\item{padding}{Padding of the labels, the value is the ratio to the height of the label.}
\item{connection_height}{Height of the connection track.}
Expand Down
57 changes: 57 additions & 0 deletions man/circos.labels.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
\name{circos.labels}
\alias{circos.labels}
\title{
Add a label track
}
\description{
Add a label track
}
\usage{
circos.labels(
sectors, x, labels,
facing = "clockwise",
niceFacing = TRUE,
col = par("col"),
cex = 0.8,
font = par("font"),
padding = 0.4,
connection_height = mm_h(5),
line_col = par("col"),
line_lwd = par("lwd"),
line_lty = par("lty"),
labels_height = min(c(cm_h(1.5), max(strwidth(labels, cex = cex, font = font)))),
side = c("inside", "outside"),
labels.side = side,
track.margin = circos.par("track.margin"))
}
\arguments{

\item{sectors}{A vector of sector names.}
\item{x}{Positions of the labels.}
\item{labels}{A vector of labels.}
\item{facing}{fFacing of the labels. The value can only be \code{"clockwise"} or \code{"reverse.clockwise"}.}
\item{niceFacing}{Whether automatically adjust the facing of the labels.}
\item{col}{Color for the labels.}
\item{cex}{Size of the labels.}
\item{font}{Font of the labels.}
\item{padding}{Padding of the labels, the value is the ratio to the height of the label.}
\item{connection_height}{Height of the connection track.}
\item{line_col}{Color for the connection lines.}
\item{line_lwd}{Line width for the connection lines.}
\item{line_lty}{Line type for the connectioin lines.}
\item{labels_height}{Height of the labels track.}
\item{side}{Side of the labels track, is it in the inside of the track where the regions are marked?}
\item{labels.side}{Same as \code{side}. It will replace \code{side} in the future versions.}
\item{track.margin}{Bottom and top margins.}

}
\details{
This function creates two tracks, one for the connection lines and one for the labels.

If two labels are too close and overlap, this function automatically adjusts the positions of neighouring labels.
}
\examples{
# There is no example
NULL

}

0 comments on commit 4a95ffe

Please sign in to comment.