# hicVennDiagram Vignette: overview

#### Jianhong Ou

#### 24 October 2023

# 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
    upsetPlot(venn)

    ## 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")
                          ))

# 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)

    upsetPlot(venn)

    ## change the font size of labels and numbers
    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 = 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)
                           ))
                  )
              )

# Session Info

    sessionInfo()

R version 4.3.1 (2023-06-16) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 22.04.3 LTS

Matrix products: default BLAS: /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB LC_COLLATE=C
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

time zone: America/New_York tzcode source: system (glibc)

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

other attached packages: [1] ChIPpeakAnno_3.36.0 ggplot2_3.4.4 GenomicRanges_1.54.0 [4] GenomeInfoDb_1.38.0 IRanges_2.36.0 S4Vectors_0.40.0
[7] BiocGenerics_0.48.0 hicVennDiagram_1.0.0

loaded via a namespace (and not attached): [1] DBI_1.1.3 bitops_1.0-7
[3] RBGL_1.78.0 formatR_1.14
[5] biomaRt_2.58.0 rlang_1.1.1
[7] magrittr_2.0.3 matrixStats_1.0.0
[9] compiler_4.3.1 RSQLite_2.3.1
[11] GenomicFeatures_1.54.0 png_0.1-8
[13] systemfonts_1.0.5 vctrs_0.6.4
[15] reshape2_1.4.4 polylabelr_0.2.0
[17] stringr_1.5.0 pkgconfig_2.0.3
[19] crayon_1.5.2 fastmap_1.1.1
[21] dbplyr_2.3.4 XVector_0.42.0
[23] ellipsis_0.3.2 labeling_0.4.3
[25] utf8_1.2.4 Rsamtools_2.18.0
[27] rmarkdown_2.25 graph_1.80.0
[29] ragg_1.2.6 bit_4.0.5
[31] xfun_0.40 zlibbioc_1.48.0
[33] cachem_1.0.8 jsonlite_1.8.7
[35] progress_1.2.2 blob_1.2.4
[37] DelayedArray_0.28.0 BiocParallel_1.36.0
[39] parallel_4.3.1 prettyunits_1.2.0
[41] R6_2.5.1 bslib_0.5.1
[43] stringi_1.7.12 ComplexUpset_1.3.3
[45] rtracklayer_1.62.0 jquerylib_0.1.4
[47] Rcpp_1.0.11 SummarizedExperiment_1.32.0 [49] knitr_1.44 VennDiagram_1.7.3
[51] splines_4.3.1 Matrix_1.6-1.1
[53] tidyselect_1.2.0 abind_1.4-5
[55] yaml_2.3.7 codetools_0.2-19
[57] curl_5.1.0 regioneR_1.34.0
[59] lattice_0.22-5 tibble_3.2.1
[61] plyr_1.8.9 InteractionSet_1.30.0
[63] Biobase_2.62.0 withr_2.5.1
[65] KEGGREST_1.42.0 evaluate_0.22
[67] survival_3.5-7 lambda.r_1.2.4
[69] futile.logger_1.4.3 BiocFileCache_2.10.0
[71] polyclip_1.10-6 xml2_1.3.5
[73] Biostrings_2.70.0 filelock_1.0.2
[75] pillar_1.9.0 BiocManager_1.30.22
[77] MatrixGenerics_1.14.0 generics_0.1.3
[79] RCurl_1.98-1.12 hms_1.1.3
[81] munsell_0.5.0 scales_1.2.1
[83] BiocStyle_2.30.0 glue_1.6.2
[85] tools_4.3.1 BiocIO_1.12.0
[87] BSgenome_1.70.0 GenomicAlignments_1.38.0
[89] XML_3.99-0.14 grid_4.3.1
[91] AnnotationDbi_1.64.0 colorspace_2.1-0
[93] GenomeInfoDbData_1.2.11 patchwork_1.1.3
[95] restfulr_0.0.15 cli_3.6.1
[97] rappdirs_0.3.3 textshaping_0.3.7
[99] futile.options_1.0.1 fansi_1.0.5
[101] eulerr_7.0.0 S4Arrays_1.2.0
[103] svglite_2.1.2 dplyr_1.1.3
[105] gtable_0.3.4 sass_0.4.7
[107] digest_0.6.33 SparseArray_1.2.0
[109] rjson_0.2.21 htmlwidgets_1.6.2
[111] farver_2.1.1 multtest_2.58.0
[113] memoise_2.0.1 htmltools_0.5.6.1
[115] lifecycle_1.0.3 httr_1.4.7
[117] MASS_7.3-60 bit64_4.0.5