treediff tests whether two families of trees
(hierarchical clusterings) have the same structure. The test is based on
the cophenetic distances of the trees and on aggregated Student’s tests,
with moderated variances (Neuvial et al., 2024).
treediff is a general purpose test for trees but has
been initially designed to compare 3D genomic structure based on Hi-C
data. In this context, trees are obtained using adjacency-constrained
clustering of Hi-C contact maps along the genome (as implemented in
Ambroise et al., 2019), where several maps from two conditions
are available.
This vignette illustrates the main features of the package on a small
Hi-C data set, stored in the extdata directory of the
hicream package:
HiC2Tree(), which goes from Hi-C files to
test results in a single call;HiCDataSet(),
normalizeCount(), clusterTree(), and
treediff());print() and
summary() methods and link with genomic coordinates).pighic datasetThe pighic dataset is an extract of the dataset
published in (Marti-Marimon et al., 2021). It contains 6 Hi-C
matrices (3 in each condition) obtained from two different developmental
stages of pig embryos (90 and 110 days of gestation) for two chromosomes
(1 and 2). Hi-C contact maps are provided in HiC-Pro format and an
additional file (the index) gives the genomic coordinates of
the bins.
replicates <- 1:3
conditions <- c("90", "110")
chromosomes <- 1:2
all_begins <- as.character(interaction(expand.grid(replicates, conditions),
sep = "-"))
allMat <- lapply(chromosomes, function(chr) {
paste0("Rep", all_begins, "-chr", chr, "_200000.bed")
})
files <- system.file("extdata", unlist(allMat), package = "hicream")
index <- system.file("extdata", "index.200000.longest18chr.abs.bed",
package = "hicream")
format <- rep("HiC-Pro", length(files))
binsize <- 200000
nb_replicates <- c(3, 3)
basename(files)
#> [1] "Rep1-90-chr1_200000.bed" "Rep2-90-chr1_200000.bed"
#> [3] "Rep3-90-chr1_200000.bed" "Rep1-110-chr1_200000.bed"
#> [5] "Rep2-110-chr1_200000.bed" "Rep3-110-chr1_200000.bed"
#> [7] "Rep1-90-chr2_200000.bed" "Rep2-90-chr2_200000.bed"
#> [9] "Rep3-90-chr2_200000.bed" "Rep1-110-chr2_200000.bed"
#> [11] "Rep2-110-chr2_200000.bed" "Rep3-110-chr2_200000.bed"Files must be ordered by chromosome, then by condition, then by
replicate. The vector nb_replicates gives the number of
replicates in each condition (here, 3 in the first condition,
90, and 3 in the second one, 110).
HiC2Tree()HiC2Tree() reads the files, normalizes the contact
counts, builds one tree per matrix and runs the treediff
test:
res <- HiC2Tree(files, format, binsize, index, chromosomes, nb_replicates)
names(res)
#> [1] "trees" "metadata" "index" "testRes"The output is a list with four entries:
trees: the list of all trees (one for each
matrix);metadata: a description of each tree (matrix,
chromosome and cluster);index: the genomic coordinates of the bins;testRes: the results of the treediff
test.res$metadata
#> names chromosome cluster mat
#> 1 tree1 1 1 mat_1
#> 2 tree2 1 1 mat_2
#> 3 tree3 1 1 mat_3
#> 4 tree4 1 1 mat_4
#> 5 tree5 1 1 mat_5
#> 6 tree6 1 1 mat_6
#> 7 tree7 2 1 mat_7
#> 8 tree8 2 1 mat_8
#> 9 tree9 2 1 mat_9
#> 10 tree10 2 1 mat_10
#> 11 tree11 2 1 mat_11
#> 12 tree12 2 1 mat_12Each chromosome is split into clusters of bins (here, one cluster per chromosome), and one tree is built for each cluster and each replicate. One test is performed for each cluster.
res$testRes
#>
#> Tree test based on aggregated t-tests
#>
#> data: trees1 and trees2
#> alternative hypothesis: the two sets of trees have different structure.
#>
#> p-values:
#> 1 0.2836782
#> 2 0.0000000The returned object has class treeTest. Its
summary() method gives more details on the distribution of
the \(p\)-values and of the individual
statistics:
summary(res$testRes)
#>
#> Summary
#> Class: treeTest
#>
#> Tree test based on aggregated t-tests
#>
#> data: trees1 and trees2
#> alternative hypothesis: the two sets of trees have different structure.
#>
#> p-values:
#> 1 0.2836782
#> 2 0.0000000
#>
#> p-value summary:
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00000 0.07092 0.14184 0.14184 0.21276 0.28368
#> indiv. statistics indiv. p-values
#> Min. :-12.7531 Min. :0.0000
#> 1st Qu.: 0.1170 1st Qu.:0.9069
#> Median : 0.1170 Median :0.9069
#> Mean : 0.1358 Mean :0.8928
#> 3rd Qu.: 0.1170 3rd Qu.:0.9069
#> Max. : 27.2227 Max. :0.9529The test finds no difference between the two conditions for the first chromosome (cluster 1) but a very significant one for the second chromosome (cluster 2).
HiC2Tree() is a wrapper that successively calls the
functions described in this section. They can be used separately, for
instance to provide a different normalization or to inspect intermediate
results.
HiCDataSet() parses the files (with the
HiCParser package) and returns the contact matrices
together with the bin coordinates:
data <- HiCDataSet(files, format, binsize, chromosomes, index)
names(data)
#> [1] "HiCDataSet" "indexData" "index_mat_chr"
length(data$HiCDataSet)
#> [1] 12
head(data$indexData)
#> seqnames start end width strand index
#> 1 1 224800001 225000000 200000 * 1125
#> 2 1 225000001 225200000 200000 * 1126
#> 3 1 225200001 225400000 200000 * 1127
#> 4 1 225400001 225600000 200000 * 1128
#> 5 1 225600001 225800000 200000 * 1129
#> 6 1 225800001 226000000 200000 * 1130
data$index_mat_chr
#> chromosome variables
#> 1 1 mat_1
#> 2 1 mat_2
#> 3 1 mat_3
#> 4 1 mat_4
#> 5 1 mat_5
#> 6 1 mat_6
#> 7 2 mat_7
#> 8 2 mat_8
#> 9 2 mat_9
#> 10 2 mat_10
#> 11 2 mat_11
#> 12 2 mat_12normalizeCount() normalizes the contact counts of all
matrices of a same chromosome together (cyclic loess normalization) and
returns one data frame per chromosome, with one column per matrix:
norm_counts <- normalizeCount(data$HiCDataSet)
length(norm_counts)
#> [1] 2
head(norm_counts[[1]])
#> chromosome index1 index2 mat_1 mat_2 mat_3 mat_4 mat_5
#> 1 1 1125 1125 1062.4253 1099.9781 1018.1186 1128.7022 1004.1958
#> 2 1 1125 1126 1292.2863 1289.5402 1135.6151 1545.1463 1307.1594
#> 3 1 1125 1127 631.7030 630.2060 630.7220 635.9711 633.7022
#> 4 1 1125 1128 320.0915 316.8550 323.6230 323.3463 296.5418
#> 5 1 1125 1129 343.9447 403.8594 391.2275 252.4207 349.5398
#> 6 1 1125 1130 340.8043 341.7083 325.9917 348.9478 385.4575
#> mat_6
#> 1 1083.6752
#> 2 1340.1839
#> 3 634.7460
#> 4 335.9719
#> 5 331.6393
#> 6 334.7257clusterTree() clusters the bins of each chromosome with
a constrained hierarchical clustering (only adjacent bins can be merged,
adjclust::adjClust()), cuts the chromosome into clusters
using the broken-stick rule and builds one tree for each cluster and
each matrix.
clust <- clusterTree(norm_counts)
names(clust)
#> [1] "trees" "metadata"
class(clust$trees[[1]])
#> [1] "hclust"
clust$metadata
#> names chromosome cluster mat
#> 1 tree1 1 1 mat_1
#> 2 tree2 1 1 mat_2
#> 3 tree3 1 1 mat_3
#> 4 tree4 1 1 mat_4
#> 5 tree5 1 1 mat_5
#> 6 tree6 1 1 mat_6
#> 7 tree7 2 1 mat_7
#> 8 tree8 2 1 mat_8
#> 9 tree9 2 1 mat_9
#> 10 tree10 2 1 mat_10
#> 11 tree11 2 1 mat_11
#> 12 tree12 2 1 mat_12treediff() takes two lists of trees. Within each list,
trees are ordered by cluster and then by replicate. The number of
replicates in each condition is given by replicates, and
one \(p\)-value is obtained for each
cluster.
Here, we first select the trees of each condition (the first 3 trees of each cluster belong to the first condition and the last 3 to the second one) and run the test on all clusters:
nb_clusters <- nrow(unique(clust$metadata[, c("chromosome", "cluster")]))
in_cond1 <- rep(rep(c(TRUE, FALSE), nb_replicates), nb_clusters)
trees1 <- clust$trees[in_cond1]
trees2 <- clust$trees[!in_cond1]
test <- treediff(trees1, trees2, replicates = nb_replicates)
test$p.value
#> [1] 0.2836782 0.0000000These are the same \(p\)-values as
the ones obtained with HiC2Tree():
The trees of the second condition may have a different number of
replicates than the trees of the first one: It is enough to give the
right values in replicates. Two options of
treediff() are also useful in practice:
scale = TRUE rescales all trees so that their heights
range between 0 and 1, which removes global differences in height
between the trees. In practice, we do not recommend using this option
unless you know what you are doing;order_labels = TRUE aligns the order of the leaves
between all trees, which is required when the trees have not been built
with the same leaf ordering.The p.value.indiv and statistic entries of
the output contain the Student’s test of each pair of leaves of the
trees (for all clusters). Their distribution can be used to check how
the signal is distributed:
The cluster-level \(p\)-values are
linked to genomic regions through the metadata and
index entries of the HiC2Tree() output. For
instance, the genomic span of the bins of each chromosome is:
Ambroise C., Dehman A., Neuvial P., Rigaill G. and Vialaneix N. (2019) Adjacency-constrained hierarchical clustering of a band similarity matrix with application to genomics. Algorithms for Molecular Biology, 14(22), 363-389.
Marti-Marimon, M., Vialaneix, N., Lahbib-Mansais, Y., Zytnicki, M., Camut, S., Robelin, D., Yerle-Bouissou, M., & Foissac, S. (2021). Major reorganization of chromosome conformation during muscle development in pig. Frontiers in Genetics, 12, 748239.
Neuvial P., Randriamihamison N., Chavent M., Foissac S. and Vialaneix N. (2024) A two-sample tree-based test for hierarchically organized genomic signals. Journal of the Royal Statistical Society, Series C, Forthcoming.
sessionInfo()
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04.1 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=fr_FR.UTF-8 LC_COLLATE=C
#> [5] LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: Europe/Paris
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] treediff_0.3.0
#>
#> loaded via a namespace (and not attached):
#> [1] bitops_1.1-0 pbapply_1.7-5
#> [3] gridExtra_2.3.1 testthat_3.3.2
#> [5] rlang_1.3.0 magrittr_2.0.5
#> [7] otel_0.3.0 matrixStats_1.5.0
#> [9] compiler_4.6.2 png_0.1-9
#> [11] vctrs_0.7.3 reshape2_1.4.5
#> [13] stringr_1.6.0 pkgconfig_2.0.3
#> [15] crayon_1.5.3 fastmap_1.2.0
#> [17] XVector_0.52.0 Rsamtools_2.28.0
#> [19] rmarkdown_2.32 UCSC.utils_1.8.0
#> [21] purrr_1.2.2 xfun_0.58
#> [23] cachem_1.1.0 cigarillo_1.2.1
#> [25] GenomeInfoDb_1.48.0 jsonlite_2.0.0
#> [27] rhdf5filters_1.24.1 DelayedArray_0.38.2
#> [29] Rhdf5lib_2.0.0 BiocParallel_1.46.0
#> [31] parallel_4.6.2 R6_2.6.1
#> [33] bslib_0.12.0 stringi_1.8.9
#> [35] RColorBrewer_1.1-3 limma_3.68.5
#> [37] reticulate_1.47.0 rtracklayer_1.72.0
#> [39] brio_1.1.5 GenomicRanges_1.64.0
#> [41] jquerylib_0.1.4 Rcpp_1.1.2
#> [43] Seqinfo_1.2.0 SummarizedExperiment_1.42.0
#> [45] knitr_1.52 IRanges_2.46.0
#> [47] Matrix_1.7-6 tidyselect_1.2.1
#> [49] abind_1.4-8 yaml_2.3.12
#> [51] viridis_0.6.5 codetools_0.2-20
#> [53] curl_8.1.0 lattice_0.23-1
#> [55] tibble_3.3.1 plyr_1.8.9
#> [57] InteractionSet_1.40.0 Biobase_2.72.0
#> [59] S7_1.0.0 csaw_1.46.0
#> [61] evaluate_1.0.5 capushe_1.1.3
#> [63] Biostrings_2.80.2 pillar_1.11.1
#> [65] MatrixGenerics_1.24.0 auk_0.10.0
#> [67] stats4_4.6.2 generics_0.1.4
#> [69] RCurl_1.98-1.20 S4Vectors_0.50.3
#> [71] ggplot2_4.0.4 sparseMatrixStats_1.24.0
#> [73] scales_1.4.0 gtools_3.9.5
#> [75] glue_1.8.1 metapod_1.20.0
#> [77] tools_4.6.2 dendextend_1.19.1
#> [79] BiocIO_1.22.0 hicream_0.0.4
#> [81] data.table_1.18.6.1 BSgenome_1.80.0
#> [83] locfit_1.5-9.12 GenomicAlignments_1.48.0
#> [85] XML_3.99-0.25 rhdf5_2.56.1
#> [87] grid_4.6.2 edgeR_4.10.7
#> [89] adjclust_0.6.11 Rhtslib_3.8.0
#> [91] restfulr_0.0.17 HiCParser_1.4.0
#> [93] cli_3.6.6 S4Arrays_1.12.1
#> [95] viridisLite_0.4.3 dplyr_1.2.1
#> [97] gtable_0.3.6 sass_0.4.10
#> [99] digest_0.6.39 BiocGenerics_0.58.1
#> [101] SparseArray_1.12.3 diffHic_1.44.0
#> [103] rjson_0.2.23 farver_2.1.2
#> [105] htmltools_0.5.9 lifecycle_1.0.5
#> [107] httr_1.4.9 statmod_1.5.2
#> [109] MASS_7.3-66