This is a post from [stackoverflow][1] here they show how to extract dedrogram such in form of respective cluster but this is with heatmap.2 function , i m trying to do the same with pheatmap , let's say a group of genes are forming clusters then i want to extract them 

Im doing something like this in my code but its giving me all of them togethter not cluster wise as seen in the heatmap 

    out = pheatmap(data, 
                   color=macolor,
                   
                   show_rownames = F,cluster_cols=T,cluster_rows=T, scale="row",
                   cex=1,clustering_distance_rows = "euclidean", cex=1,
                   clustering_distance_cols = "euclidean", clustering_method = "complete",border_color = FALSE)
    
    
    res <- data[c(out$tree_row[["order"]]),out$tree_col[["order"]]]

  [1]: https://stackoverflow.com/questions/28356354/write-table-with-row-name-ordered-after-clustering-in-rs-heatmap-2


any suggestion or help would be highly appreciated.

 

    #Create random data
    data <- replicate(20, rnorm(50))
    rownames(data) <- paste("Gene", c(1:nrow(data)))
    colnames(data) <- paste("Sample", c(1:ncol(data)))
    
    out <- pheatmap(data, 
          show_rownames=T, cluster_cols=T, cluster_rows=T, scale="row",
          cex=1, clustering_distance_rows="euclidean", cex=1,
          clustering_distance_cols="euclidean", clustering_method="complete", border_color=FALSE)

<a href="https://ibb.co/ckJ4Pb"><img src="https://preview.ibb.co/h2eYHw/pheatmap.png" alt="pheatmap" border="0"></a>

    #Re-order original data (genes) to match ordering in heatmap (top-to-bottom)
    rownames(data[out$tree_row[["order"]],])
     [1] "Gene 2"  "Gene 12" "Gene 9"  "Gene 33" "Gene 29" "Gene 43" "Gene 24"
     [8] "Gene 44" "Gene 3"  "Gene 21" "Gene 46" "Gene 26" "Gene 20" "Gene 45"
    [15] "Gene 18" "Gene 38" "Gene 22" "Gene 1"  "Gene 17" "Gene 7"  "Gene 6" 
    [22] "Gene 41" "Gene 30" "Gene 31" "Gene 13" "Gene 16" "Gene 11" "Gene 50"
    [29] "Gene 34" "Gene 37" "Gene 15" "Gene 25" "Gene 27" "Gene 39" "Gene 19"
    [36] "Gene 35" "Gene 4"  "Gene 49" "Gene 10" "Gene 28" "Gene 8"  "Gene 14"
    [43] "Gene 32" "Gene 42" "Gene 36" "Gene 48" "Gene 23" "Gene 47" "Gene 5" 
    [50] "Gene 40"
    
    
    #Re-order original data (samples) to match ordering in heatmap (left-to-right)
    colnames(data[,out$tree_col[["order"]]])
     [1] "Sample 3"  "Sample 6"  "Sample 1"  "Sample 17" "Sample 8"  "Sample 10"
     [7] "Sample 15" "Sample 4"  "Sample 18" "Sample 2"  "Sample 11" "Sample 9" 
    [13] "Sample 13" "Sample 12" "Sample 7"  "Sample 19" "Sample 14" "Sample 16"
    [19] "Sample 5"  "Sample 20"


-------------------------
---------------------

If you want something like gene-to-cluster assignment, you can 'cut' your row dendrogram into a pre-selected number of groups as follows:

    #2 groups
    sort(cutree(out$tree_row, k=2))
     Gene 1  Gene 2  Gene 3  Gene 9 Gene 12 Gene 17 Gene 18 Gene 20 Gene 21 Gene 22 
          1       1       1       1       1       1       1       1       1       1 
    Gene 24 Gene 26 Gene 29 Gene 33 Gene 38 Gene 43 Gene 44 Gene 45 Gene 46  Gene 4 
          1       1       1       1       1       1       1       1       1       2 
     Gene 5  Gene 6  Gene 7  Gene 8 Gene 10 Gene 11 Gene 13 Gene 14 Gene 15 Gene 16 
          2       2       2       2       2       2       2       2       2       2 
    Gene 19 Gene 23 Gene 25 Gene 27 Gene 28 Gene 30 Gene 31 Gene 32 Gene 34 Gene 35 
          2       2       2       2       2       2       2       2       2       2 
    Gene 36 Gene 37 Gene 39 Gene 40 Gene 41 Gene 42 Gene 47 Gene 48 Gene 49 Gene 50 
          2       2       2       2       2       2       2       2       2       2 
    
    #5 groups
    sort(cutree(out$tree_row, k=5))
     Gene 1  Gene 3 Gene 17 Gene 18 Gene 20 Gene 21 Gene 22 Gene 24 Gene 26 Gene 38 
          1       1       1       1       1       1       1       1       1       1 
    Gene 44 Gene 45 Gene 46  Gene 2  Gene 9 Gene 12 Gene 29 Gene 33 Gene 43  Gene 4 
          1       1       1       2       2       2       2       2       2       3 
    Gene 19 Gene 35 Gene 49  Gene 5  Gene 8 Gene 10 Gene 14 Gene 23 Gene 28 Gene 32 
          3       3       3       4       4       4       4       4       4       4 
    Gene 36 Gene 40 Gene 42 Gene 47 Gene 48  Gene 6  Gene 7 Gene 11 Gene 13 Gene 15 
          4       4       4       4       4       5       5       5       5       5 
    ...

You can also cut the tree at a pre-defined tree height, and extract the gene-to-cluster assignments at that height:

    plot(out$tree_row)
    abline(h=7, col="red", lty=2, lwd=2)

<a href="https://ibb.co/hDfCAG"><img src="https://preview.ibb.co/kA8M4b/dend.png" alt="dend" border="0"></a><br/>

    #Cut the row (gene) dendrogram at a Euclidean distance dis-similarity of 8
    sort(cutree(out$tree_row, h=7))
     Gene 1  Gene 3 Gene 17 Gene 18 Gene 20 Gene 21 Gene 22 Gene 24 Gene 26 Gene 38 
          1       1       1       1       1       1       1       1       1       1 
    Gene 44 Gene 45 Gene 46  Gene 2  Gene 9 Gene 12 Gene 29 Gene 33 Gene 43  Gene 4 
          1       1       1       2       2       2       2       2       2       3 
    Gene 19 Gene 35 Gene 49  Gene 5  Gene 8 Gene 10 Gene 14 Gene 23 Gene 28 Gene 32 
          3       3       3       4       4       4       4       4       4       4 
    Gene 36 Gene 40 Gene 42 Gene 47 Gene 48  Gene 6  Gene 7 Gene 13 Gene 16 Gene 30 
          4       4       4       4       4       5       5       5       5       5 
    Gene 31 Gene 41 Gene 11 Gene 15 Gene 25 Gene 27 Gene 34 Gene 37 Gene 39 Gene 50 
          5       5       6       6       6       6       6       6       6       6

