作者:Zuguang Gu
翻译:Steven Shen
原文:https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html#get-orders-and-dendrograms-from-heatmap

2.12 获取顺序和树状图

2.12 Get orders and dendrograms

The row/column orders of the heatmap can be obtained by row_order()/column_order() functions. You can directly apply to the heatmap object returned by Heatmap() or to the object returned by draw(). In following, we take row_order() as example.

热图的行/列顺序可以通过 row_order() / column_order() 函数获得。您可以直接应用于 Heatmap() 返回的热图对象或 draw() 返回的对象。在下面,我们以 row_order() 为例。

  1. small_mat = mat[1:9, 1:9]
  2. ht1 = Heatmap(small_mat)
  3. row_order(ht1)
  4. ## [1] 9 6 7 8 3 4 1 2 5
  1. ht2 = draw(ht1)
  2. row_order(ht2)
  3. ## [1] 9 6 7 8 3 4 1 2 5

As explained in previous section, Heatmap() function does not perform clustering, thus, when directly apply row_order() on ht1, clustering will be first performed. Later when making the heatmap by draw(ht1), the clustering will be applied again. This might be a problem that if you set k-means clustering in the heatmap. Since the clustering is applied twice, k-means might give you different clusterings, which means, you might have different results from row_order() and you might have different heatmap.

如前一节所述, Heatmap() 函数不执行聚类,因此,当直接在 ht1 上应用 row_order() 时,将首先执行聚类。稍后通过 draw(ht1) 制作热图时,将再次应用聚类。如果您在热图中设置 k-means 聚类,这可能是一个问题。由于聚类应用了两次,因此 k-means 可能会为您提供不同的聚类,这意味着您可能会有与 row_order() 不同的结果,并且您可能具有不同的热图。

In following chunk of code, o1, o2 and o3 might be different because each time, k-means clustering is performed.

在下面的代码块中, o1o2o3 可能不同,因为每次都运行都执行了 k 均值聚类。

  1. # code only for demonstration
  2. ht1 = Heatmap(small_mat, row_km = 2)
  3. o1 = row_order(ht1)
  4. o2 = row_order(ht1)
  5. ht2 = draw(ht1)
  6. o3 = row_order(ht2)
  7. o4 = row_order(ht2)

draw() function returns the heatmap (or more precisely, the heatmap list) which has already been reordered, and applying row_order() just extracts the row order from the object, which ensures the row order is exactly the same as the one shown in the heatmap. In above code, o3 is always identical to o4.

draw() 函数返回已经重新排序的热图(或者更准确地说,热图列表),并且应用 row_order() 只是从对象中提取行顺序,这确保了行顺序与显示的顺序完全相同热图。 在上面的代码中, o3 总是与 o4 相同。

So, the preferable way to get row/column orders is as follows.

因此,获得行/列顺序的最佳方法如下。

  1. # code only for demonstration
  2. ht = Heatmap(small_mat)
  3. ht = draw(ht)
  4. row_order(ht)
  5. column_order(ht)

If rows/columns are split, row order or column order will be a list.

如果拆分行/列,则行顺序或列顺序将是列表。

  1. ht = Heatmap(small_mat, row_km = 2, column_km = 3)
  2. ht = draw(ht)
  3. row_order(ht)
  4. ## $`1`
  5. ## [1] 1 2 5
  6. ##
  7. ## $`2`
  8. ## [1] 9 6 7 8 3 4
  1. column_order(ht)
  2. ## $`1`
  3. ## [1] 5 9
  4. ##
  5. ## $`2`
  6. ## [1] 2 7 6
  7. ##
  8. ## $`3`
  9. ## [1] 8 1 3 4

Similarly, the row_dend()/column_dend() functions return the dendrograms. It returns a single dendrogram or a list of dendrograms depending on whether the heatmap is split.

类似地, row_dend() / column_dend() 函数返回树形图。它返回单个树形图或树形图列表,具体取决于热图是否被分割。

  1. ht = Heatmap(small_mat, row_km = 2)
  2. ht = draw(ht)
  3. row_dend(ht)
  4. ## $`1`
  5. ## 'dendrogram' with 2 branches and 3 members total, at height 2.718561
  6. ##
  7. ## $`2`
  8. ## 'dendrogram' with 2 branches and 6 members total, at height 3.109169
  1. column_dend(ht)
  2. ## 'dendrogram' with 2 branches and 9 members total, at height 5.191887

row_order(), column_order(), row_dend() and column_dend() also work for a list of heatmaps, it will be introduced in Section 4.12.

row_order()column_order()row_dend()column_dend() 也适用于热图列表,它将在 4.12 节中介绍。

——本章节完· 2019-07-08——