# hicVennDiagram Vignette: overview

#### Jianhong Ou

#### 29 October 2024

# Introduction

When comparing samples, it is common to perform the task of identifying overlapping loops among two or more sets of genomic interactions. Traditionally, this is achieved through the use of visualizations such as `vennDiagram` or `UpSet` plots. However, it is frequently observed that the total count displayed in these plots does not match the original counts for each individual list. The reason behind this discrepancy is that a single overlap may encompass multiple interactions for one or more samples. This issue is extensively discussed in the realm of overlapping caller for ChIP-Seq peaks.

The hicVennDiagram aims to provide a easy to use tool for overlapping interactions calculation and proper visualization methods. The hicVennDiagram generates plots specifically crafted to eliminate the deceptive visual representation caused by the counts method.

# Quick start

Here is an example using hicVennDiagram with 3 files in `BEDPE` format.

## Installation

First, install hicVennDiagram and other packages required to run the examples.

    library(BiocManager)
    BiocManager::install("hicVennDiagram")

## Load library

    library(hicVennDiagram)
    library(ggplot2)

    # list the BEDPE files
    file_folder <- system.file("extdata",
                               package = "hicVennDiagram",
                               mustWork = TRUE)
    file_list <- dir(file_folder, pattern = ".bedpe", full.names = TRUE)
    names(file_list) <- sub(".bedpe", "", basename(file_list))
    basename(file_list)

    ## [1] "group1.bedpe" "group2.bedpe" "group3.bedpe"

    venn <- vennCount(file_list)
    ## upset plot
    ## temp fix for https://github.com/krassowski/complex-upset/issues/195
    upset_themes_fix <- lapply(ComplexUpset::upset_themes, function(.ele){
        lapply(.ele, function(.e){
            do.call(theme, .e[names(.e) %in% names(formals(theme))])
        })
    })
    upsetPlot(venn,
              themes = upset_themes_fix)

    ## venn plot
    vennPlot(venn)

    ## use browser to adjust the text position, and shape colors.
    browseVenn(vennPlot(venn))

# Details about `vennCount`

The `vennCount` function borrows the power of `InteractionSet:findOverlaps` to calculate the overlaps and then summarizes the results for each category. Users may want to try different combinations of `maxgap` and `minoverlap` parameters to calculate the overlapping loops.

    venn <- vennCount(file_list, maxgap=50000, FUN = max) # by default FUN = min
    upsetPlot(venn, label_all=list(
                              na.rm = TRUE,
                              color = 'black',
                              alpha = .9,
                              label.padding = unit(0.1, "lines")
                          ),
              themes = upset_themes_fix)

# Plot for overlapping peaks output by `ChIPpeakAnno`

    library(ChIPpeakAnno)
    bed <- system.file("extdata", "MACS_output.bed", package="ChIPpeakAnno")
    gr1 <- toGRanges(bed, format="BED", header=FALSE)
    gff <- system.file("extdata", "GFF_peaks.gff", package="ChIPpeakAnno")
    gr2 <- toGRanges(gff, format="GFF", header=FALSE, skip=3)
    ol <- findOverlapsOfPeaks(gr1, gr2)
    overlappingPeaksToVennTable <- function(.ele){
        .venn <- .ele$venn_cnt
        k <- which(colnames(.venn)=="Counts")
        rownames(.venn) <- apply(.venn[, seq.int(k-1)], 1, paste, collapse="")
        colnames(.venn) <- sub("count.", "", colnames(.venn))
        vennTable(combinations=.venn[, seq.int(k-1)],
                  counts=.venn[, k],
                  vennCounts=.venn[, seq.int(ncol(.venn))[-seq.int(k)]])
    }
    venn <- overlappingPeaksToVennTable(ol)
    vennPlot(venn)

    ## or you can simply try vennPlot(vennCount(c(bed, gff)))
    upsetPlot(venn, themes = upset_themes_fix)

    ## change the font size of labels and numbers
    updated_theme <- ComplexUpset::upset_modify_themes(
                  ## get help by vignette('Examples_R', package = 'ComplexUpset')
                  list('intersections_matrix'=
                           ggplot2::theme(
                               ## font size of label: gr1/gr2
                               axis.text.y=ggplot2::element_text(size=24),
                               ## font size of label `group`
                               axis.title.x=ggplot2::element_text(size=24)),
                       'overall_sizes'=
                           ggplot2::theme(
                               ## font size of x-axis 0-200
                               axis.text=ggplot2::element_text(size=12),
                               ## font size of x-label `Set size`
                               axis.title=ggplot2::element_text(size=18)),
                       'Intersection size'=
                           ggplot2::theme(
                               ## font size of y-axis 0-150
                               axis.text=ggplot2::element_text(size=20),
                               ## font size of y-label `Intersection size`
                               axis.title=ggplot2::element_text(size=16)
                           ),
                       'default'=ggplot2::theme_minimal())
                  )
    updated_theme <- lapply(updated_theme, function(.ele){
        lapply(.ele, function(.e){
            do.call(theme, .e[names(.e) %in% names(formals(theme))])
        })
    })
    upsetPlot(venn,
              label_all=list(na.rm = TRUE, color = 'gray30', alpha = .7,
                             label.padding = unit(0.1, "lines"),
                             size = 8 #control the font size of the individual num
                             ),
              base_annotations=list('Intersection size'=
                                        ComplexUpset::intersection_size(
                                            ## font size of counts in the bar-plot
                                            text = list(size=6)
                                            )),
              themes = updated_theme
              )

# Session Info

    sessionInfo()

R version 4.4.1 (2024-06-14 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows Server 2022 x64 (build 20348)

Matrix products: default

locale: [1] LC_COLLATE=C
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

time zone: America/New_York tzcode source: internal

attached base packages: [1] stats4 stats graphics grDevices utils datasets methods
[8] base

other attached packages: [1] ChIPpeakAnno_3.40.0 ggplot2_3.5.1 GenomicRanges_1.58.0 [4] GenomeInfoDb_1.42.0 IRanges_2.40.0 S4Vectors_0.44.0
[7] BiocGenerics_0.52.0 hicVennDiagram_1.4.0

loaded via a namespace (and not attached): [1] eulerr_7.0.2 jsonlite_1.8.9
[3] magrittr_2.0.3 GenomicFeatures_1.58.0
[5] farver_2.1.2 rmarkdown_2.28
[7] BiocIO_1.16.0 zlibbioc_1.52.0
[9] ragg_1.3.3 vctrs_0.6.5
[11] multtest_2.62.0 memoise_2.0.1
[13] Rsamtools_2.22.0 RCurl_1.98-1.16
[15] htmltools_0.5.8.1 S4Arrays_1.6.0
[17] progress_1.2.3 lambda.r_1.2.4
[19] curl_5.2.3 ComplexUpset_1.3.3
[21] SparseArray_1.6.0 sass_0.4.9
[23] bslib_0.8.0 htmlwidgets_1.6.4
[25] plyr_1.8.9 httr2_1.0.5
[27] futile.options_1.0.1 cachem_1.1.0
[29] GenomicAlignments_1.42.0 lifecycle_1.0.4
[31] pkgconfig_2.0.3 Matrix_1.7-1
[33] R6_2.5.1 fastmap_1.2.0
[35] GenomeInfoDbData_1.2.13 MatrixGenerics_1.18.0
[37] digest_0.6.37 colorspace_2.1-1
[39] patchwork_1.3.0 AnnotationDbi_1.68.0
[41] regioneR_1.38.0 textshaping_0.4.0
[43] RSQLite_2.3.7 filelock_1.0.3
[45] labeling_0.4.3 fansi_1.0.6
[47] httr_1.4.7 polyclip_1.10-7
[49] abind_1.4-8 compiler_4.4.1
[51] bit64_4.5.2 withr_3.0.2
[53] BiocParallel_1.40.0 DBI_1.2.3
[55] highr_0.11 biomaRt_2.62.0
[57] MASS_7.3-61 rappdirs_0.3.3
[59] DelayedArray_0.32.0 rjson_0.2.23
[61] tools_4.4.1 glue_1.8.0
[63] VennDiagram_1.7.3 restfulr_0.0.15
[65] InteractionSet_1.34.0 grid_4.4.1
[67] polylabelr_0.2.0 reshape2_1.4.4
[69] generics_0.1.3 BSgenome_1.74.0
[71] gtable_0.3.6 tidyr_1.3.1
[73] ensembldb_2.30.0 data.table_1.16.2
[75] hms_1.1.3 xml2_1.3.6
[77] utf8_1.2.4 XVector_0.46.0
[79] pillar_1.9.0 stringr_1.5.1
[81] splines_4.4.1 dplyr_1.1.4
[83] BiocFileCache_2.14.0 lattice_0.22-6
[85] survival_3.7-0 rtracklayer_1.66.0
[87] bit_4.5.0 universalmotif_1.24.0
[89] tidyselect_1.2.1 RBGL_1.82.0
[91] Biostrings_2.74.0 knitr_1.48
[93] ProtGenerics_1.38.0 SummarizedExperiment_1.36.0 [95] svglite_2.1.3 futile.logger_1.4.3
[97] xfun_0.48 Biobase_2.66.0
[99] matrixStats_1.4.1 stringi_1.8.4
[101] UCSC.utils_1.2.0 lazyeval_0.2.2
[103] yaml_2.3.10 evaluate_1.0.1
[105] codetools_0.2-20 tibble_3.2.1
[107] BiocManager_1.30.25 graph_1.84.0
[109] cli_3.6.3 systemfonts_1.1.0
[111] munsell_0.5.1 jquerylib_0.1.4
[113] Rcpp_1.0.13 dbplyr_2.5.0
[115] png_0.1-8 XML_3.99-0.17
[117] parallel_4.4.1 blob_1.2.4
[119] prettyunits_1.2.0 AnnotationFilter_1.30.0
[121] bitops_1.0-9 pwalign_1.2.0
[123] scales_1.3.0 purrr_1.0.2
[125] crayon_1.5.3 BiocStyle_2.34.0
[127] rlang_1.1.4 KEGGREST_1.46.0
[129] formatR_1.14