# Collision removal functionality

Giulia Pais1*

1San Raffaele Telethon Institute for Gene Therapy - SR-Tiget, Via Olgettina 60, 20132 Milano - Italia

* giuliapais1@gmail.com, calabria.andrea@hsr.it

#### 27 October 2020

#### Package

ISAnalytics 1.0.0

# 1 Introduction

## 1.1 How to install ISAnalytics

To install the package run the following code:

    if (!requireNamespace("BiocManager", quietly = TRUE)) {
          install.packages("BiocManager")
      }
    BiocManager::install("ISAnalytics")

To install from GitHub:

    if (!require(devtools)) {
        install.packages("devtools")
    }
    devtools::install_github("calabrialab/ISAnalytics", build_vignettes = TRUE)

    library(ISAnalytics)

## 1.2 Setting options

`ISAnalytics` has a verbose option that allows some functions to print additional information to the console while they’re executing. To disable this feature do:

    # DISABLE
    options("ISAnalytics.verbose" = FALSE)

    # ENABLE
    options("ISAnalytics.verbose" = TRUE)

Some functions also produce report in a user-friendly HTML format, to set this feature:

    # DISABLE HTML REPORTS
    options("ISAnalytics.widgets" = FALSE)

    # ENABLE HTML REPORTS
    options("ISAnalytics.widgets" = TRUE)

## 1.3 What is a collision and why should you care?

We’re not going into too much detail here, but we’re going to explain in a very simple way what is a “collision” and how the function in this package deal with them.

We say that an integration (aka a unique combination of chromosome, integration locus and strand) is a collision if this combination is shared between different independent samples: an independent sample is a unique combination of `ProjectID` and `SubjectID` (where subjects usually represent patients). The reason behind this is that it’s highly improbable to observe the very same integration in two different subjects and this phenomenon might be an indicator of some kind of contamination in the sequencing phase or in PCR phase, for this reason we might want to exclude such contamination from our analysis.
`ISAnalytics` provides a function that processes the imported data for the removal or reassignment of these “problematic” integrations, `remove_collisions`.

The processing is done on the sequence count matrix (after import) and matrices of other quantification types are re-aligned accordingly.

## 1.4 The logic behind the function

The `remove_collisions` function follows several logical steps to decide whether an integration is a collision and if it is it decides whether to re-assign it or remove it entirely based on different criterias.

### 1.4.1 Identifying the collisions

As we said before, a collision is a triplet made of `chr`, `integration locus` and `strand`, which is shared between different independent samples, aka a pair made of `ProjectID` and `SubjectID`. The function uses the information stored in the association file to assess which independent samples are present and counts the number of independent samples for each integration: those who have a count > 1 are considered collisions.

### 1.4.2 Re-assign vs remove

Once the collisions are identified, the function follows 3 steps where it tries to re-assign the combination to a single independent sample. The criterias are:

1. Compare dates: if it’s possible to have an absolute ordering on dates, the integration is re-assigned to the sample that has the earliest date. If two samples share the same date it’s impossible to decide, so the next criteria is tested
2. Compare replicate number: if a sample has the same integration in more than one replicate, it’s more probable the integration is not an artifact. If it’s possible to have an absolute ordering, the collision is re-assigned to the sample whose grouping is largest
3. Compare the sequence count value: if the previous criteria wasn’t sufficient to make a decision, for each group of independent samples it’s evaluated the sum of the sequence count value - for each group there is a cumulative value of the sequence count and this is compared to the value of other groups. If there is a single group which has a ratio n times bigger than other groups, this one is chosen for re-assignment. The factor n is passed as a parameter in the function ( `reads_ratio`), the default value is 10.

If none of the criterias were sufficient to make a decision, the integration is simply removed from the matrix.

# 2 Typical workflow

To know more about import functions take a look at the vignette “How to use import functions”.

## 2.1 Import the association file

Import your association file:

    options(ISAnalytics.widgets = FALSE)
    path_AF <- system.file("extdata", "ex_association_file.tsv",
        package = "ISAnalytics"
    )

    root_correct <- system.file("extdata", "fs.zip",
        package = "ISAnalytics"
    )
    root_correct <- unzip_file_system(root_correct, "fs")
    association_file <- import_association_file(path_AF, root_correct)

Important notes on the association file:

- You have to be sure your association file is properly filled out. The function requires you to specify a date column (by default “SequencingDate”), you have to ensure this column doesn’t contain NA values or incorrect values.
- You have to ensure that your association file contains ALL the information regarding CompleteAmplificationIDs present in the matrices you’re analyzing - an error is thrown otherwise
- If you have the verbose option set to true and your association file holds additional information on other samples which for any reason are not present in the matrix you’re analyzing, you’ll be notified with a console message.

## 2.2 Import the matrices for your analysis

    # This imports both sequence count and fragment estimate matrices
    matrices <- import_parallel_Vispa2Matrices_auto(
        association_file = association_file, root = NULL,
        quantification_type = c("fragmentEstimate", "seqCount"),
        matrix_type = "annotated", workers = 2, patterns = NULL,
        matching_opt = "ANY"
    )

As stated in the introduction, it is fundamental that the sequence count matrix is present for the collision removal process to take place.

## 2.3 Process the collisions

You can process the collisions in 3 different ways.

### 2.3.1 Pass the entire named list to the function

    # Pass the whole named list
    matrices_processed <- remove_collisions(x = matrices,
                                            association_file = association_file,
                                            date_col = "SequencingDate",
                                            reads_ratio = 10)
    #> Found additional data relative to some projects that are not included in the imported matrices. Here is a summary
    #> # A tibble: 1 x 3
    #>   ProjectID   PoolID         CompleteAmplificationID
    #>   <chr>       <chr>          <chr>
    #> 1 PROJECT1101 ABY-LR-PL4-PO… PROJECT1101_ABY-LR-PL4-POOL54_LTR51LC51_VA2020-mix14_VA2020-mix14_lenti_737.pCCLsin.PPT.SF…
    #> Identifying collisions...
    #> Processing collisions...
    #> Realigning matrices...
    #> Finished!

If you have the “widgets” option active, a report file is produced at the end that shows the before and after for each subject (and some other details). This report is an HTML widget, so you can save it or export it for future reference if you need it.
In this case, collision removal is done on the sequence count matrix and other matrices are re-aligned automatically.

### 2.3.2 Give only the sequence count matrix as input

    # Pass the sequence count matrix only
    matrices_processed_single <- remove_collisions(x = matrices$seqCount,
                                                   association_file =
                                                     association_file,
                                                   date_col = "SequencingDate",
                                                   reads_ratio = 10)
    #> Found additional data relative to some projects that are not included in the imported matrices. Here is a summary
    #> # A tibble: 1 x 3
    #>   ProjectID   PoolID         CompleteAmplificationID
    #>   <chr>       <chr>          <chr>
    #> 1 PROJECT1101 ABY-LR-PL4-PO… PROJECT1101_ABY-LR-PL4-POOL54_LTR51LC51_VA2020-mix14_VA2020-mix14_lenti_737.pCCLsin.PPT.SF…
    #> Identifying collisions...
    #> Processing collisions...
    #> Finished! You provided a single sequence count as matrix, to re-align other related matrices see ?realign_after_collisions

If you have the “verbose” option active, a console message will remind you to align other matrices if you have them at a later time.

### 2.3.3 Support for multi-quantification matrices

If you’d like to avoid the re-alignment phase, you can call collision removal on a multi-quantification matrix obtained via the function `comparison_matrix`:

    # Obtain multi-quantification matrix
    multi <- comparison_matrix(matrices)
    multi
    #> # A tibble: 5,904 x 8
    #>    chr   integration_locus strand GeneName GeneStrand CompleteAmplificationID                  fragmentEstimate seqCount
    #>    <chr>             <int> <chr>  <chr>    <chr>      <chr>                                               <dbl>    <int>
    #>  1 1             100903226 +      CDC14A   +          CLOEXP_POOL6_LTR30LC30_VA2020-mix11_VA2…             1.00        1
    #>  2 1             101724095 -      S1PR1    +          CLOEXP_POOL6_LTR10LC10_VA2020-mix10_VA2…             2.00        2
    #>  3 1             108208779 +      VAV3     -          CLOEXP_POOL6_LTR32LC32_VA2020-mix13_VA2…             1.00        1
    #>  4 1             108412817 -      VAV3     -          CLOEXP_POOL6_LTR10LC10_VA2020-mix10_VA2…             1.00        1
    #>  5 1             108412817 -      VAV3     -          CLOEXP_POOL6_LTR12LC12_VA2020-mix12_VA2…             1.00        1
    #>  6 1             108511440 +      VAV3-AS1 +          CLOEXP_POOL6_LTR49LC49_VA2020-mix11_VA2…             1.00        1
    #>  7 1             109689050 +      KIAA1324 +          CLOEXP_POOL6_LTR29LC29_VA2020-mix10_VA2…             1.00        1
    #>  8 1             111411778 +      CD53     +          CLOEXP_POOL6_LTR10LC10_VA2020-mix10_VA2…             1.00        1
    #>  9 1             111411778 +      CD53     +          CLOEXP_POOL6_LTR32LC32_VA2020-mix13_VA2…             1.00        1
    #> 10 1             111466980 -      LRIF1    -          CLOEXP_POOL6_LTR10LC10_VA2020-mix10_VA2…             1.00        1
    #> # … with 5,894 more rows
    matrices_processed_multi <- remove_collisions(x = multi,
                                                  association_file =
                                                     association_file,
                                                   date_col = "SequencingDate",
                                                   reads_ratio = 10,
                                                  seq_count_col = "seqCount")
    #> Found additional data relative to some projects that are not included in the imported matrices. Here is a summary
    #> # A tibble: 1 x 3
    #>   ProjectID   PoolID         CompleteAmplificationID
    #>   <chr>       <chr>          <chr>
    #> 1 PROJECT1101 ABY-LR-PL4-PO… PROJECT1101_ABY-LR-PL4-POOL54_LTR51LC51_VA2020-mix14_VA2020-mix14_lenti_737.pCCLsin.PPT.SF…
    #> Identifying collisions...
    #> Processing collisions...
    #> Finished!

As you can see, `comparison_matrix` produces a single integration matrix from the named list of single quantification matrices. This is the recommended approach if you don’t have specific needs as it negates the necessity of realigning matrices altogether.

## 2.4 Re-align other matrices

If you have opted for the second way, to realign other matrices you have to call the function `realign_after_collisions`, passing as input the processed sequence count matrix and the named list of other matrices to realign.
NOTE: the names in the list must be quantification types.

    seq_count_proc <- matrices_processed_single
    other_matrices <- matrices[!names(matrices) %in% "seqCount"]
    # Select only matrices that are not relative to sequence count
    other_realigned <- realign_after_collisions(seq_count_proc, other_matrices)

# 3 Reproducibility

The ISAnalytics package (calabrialab, 2020) was made possible thanks to:

- R (R Core Team, 2020)
- BiocStyle (Oleś, Morgan, and Huber, 2020)
- knitcitations (Boettiger, 2019)
- knitr (Xie, 2020)
- rmarkdown (Allaire, Xie, McPherson, Luraschi, et al., 2020)
- sessioninfo (Csárdi, core, Wickham, Chang, et al., 2018)
- testthat (Wickham, 2011)

This package was developed using biocthis.

`R` session information.

    #> ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
    #>  setting  value
    #>  version  R version 4.0.3 (2020-10-10)
    #>  os       Ubuntu 18.04.5 LTS
    #>  system   x86_64, linux-gnu
    #>  ui       X11
    #>  language (EN)
    #>  collate  C
    #>  ctype    en_US.UTF-8
    #>  tz       America/New_York
    #>  date     2020-10-27
    #>
    #> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
    #>  package       * version date       lib source
    #>  assertthat      0.2.1   2019-03-21 [2] CRAN (R 4.0.3)
    #>  bibtex          0.4.2.3 2020-09-19 [2] CRAN (R 4.0.3)
    #>  BiocManager     1.30.10 2019-11-16 [2] CRAN (R 4.0.3)
    #>  BiocParallel    1.24.0  2020-10-27 [2] Bioconductor
    #>  BiocStyle     * 2.18.0  2020-10-27 [2] Bioconductor
    #>  bookdown        0.21    2020-10-13 [2] CRAN (R 4.0.3)
    #>  cli             2.1.0   2020-10-12 [2] CRAN (R 4.0.3)
    #>  colorspace      1.4-1   2019-03-18 [2] CRAN (R 4.0.3)
    #>  crayon          1.3.4   2017-09-16 [2] CRAN (R 4.0.3)
    #>  digest          0.6.27  2020-10-24 [2] CRAN (R 4.0.3)
    #>  dplyr           1.0.2   2020-08-18 [2] CRAN (R 4.0.3)
    #>  ellipsis        0.3.1   2020-05-15 [2] CRAN (R 4.0.3)
    #>  evaluate        0.14    2019-05-28 [2] CRAN (R 4.0.3)
    #>  fansi           0.4.1   2020-01-08 [2] CRAN (R 4.0.3)
    #>  forcats         0.5.0   2020-03-01 [2] CRAN (R 4.0.3)
    #>  fs              1.5.0   2020-07-31 [2] CRAN (R 4.0.3)
    #>  generics        0.0.2   2018-11-29 [2] CRAN (R 4.0.3)
    #>  ggplot2         3.3.2   2020-06-19 [2] CRAN (R 4.0.3)
    #>  ggrepel         0.8.2   2020-03-08 [2] CRAN (R 4.0.3)
    #>  glue            1.4.2   2020-08-27 [2] CRAN (R 4.0.3)
    #>  gtable          0.3.0   2019-03-25 [2] CRAN (R 4.0.3)
    #>  highr           0.8     2019-03-20 [2] CRAN (R 4.0.3)
    #>  hms             0.5.3   2020-01-08 [2] CRAN (R 4.0.3)
    #>  htmltools       0.5.0   2020-06-16 [2] CRAN (R 4.0.3)
    #>  htmlwidgets     1.5.2   2020-10-03 [2] CRAN (R 4.0.3)
    #>  httr            1.4.2   2020-07-20 [2] CRAN (R 4.0.3)
    #>  ISAnalytics   * 1.0.0   2020-10-27 [1] Bioconductor
    #>  jsonlite        1.7.1   2020-09-07 [2] CRAN (R 4.0.3)
    #>  knitcitations * 1.0.10  2019-09-15 [2] CRAN (R 4.0.3)
    #>  knitr           1.30    2020-09-22 [2] CRAN (R 4.0.3)
    #>  lattice         0.20-41 2020-04-02 [2] CRAN (R 4.0.3)
    #>  lifecycle       0.2.0   2020-03-06 [2] CRAN (R 4.0.3)
    #>  lubridate       1.7.9   2020-06-08 [2] CRAN (R 4.0.3)
    #>  magrittr      * 1.5     2014-11-22 [2] CRAN (R 4.0.3)
    #>  mnormt          2.0.2   2020-09-01 [2] CRAN (R 4.0.3)
    #>  munsell         0.5.0   2018-06-12 [2] CRAN (R 4.0.3)
    #>  nlme            3.1-150 2020-10-24 [2] CRAN (R 4.0.3)
    #>  pillar          1.4.6   2020-07-10 [2] CRAN (R 4.0.3)
    #>  pkgconfig       2.0.3   2019-09-22 [2] CRAN (R 4.0.3)
    #>  plyr            1.8.6   2020-03-03 [2] CRAN (R 4.0.3)
    #>  psych           2.0.9   2020-10-05 [2] CRAN (R 4.0.3)
    #>  purrr           0.3.4   2020-04-17 [2] CRAN (R 4.0.3)
    #>  R6              2.4.1   2019-11-12 [2] CRAN (R 4.0.3)
    #>  Rcpp            1.0.5   2020-07-06 [2] CRAN (R 4.0.3)
    #>  reactable       0.2.3   2020-10-04 [2] CRAN (R 4.0.3)
    #>  readr           1.4.0   2020-10-05 [2] CRAN (R 4.0.3)
    #>  RefManageR      1.2.12  2019-04-03 [2] CRAN (R 4.0.3)
    #>  rlang           0.4.8   2020-10-08 [2] CRAN (R 4.0.3)
    #>  rmarkdown       2.5     2020-10-21 [2] CRAN (R 4.0.3)
    #>  scales          1.1.1   2020-05-11 [2] CRAN (R 4.0.3)
    #>  sessioninfo   * 1.1.1   2018-11-05 [2] CRAN (R 4.0.3)
    #>  stringi         1.5.3   2020-09-09 [2] CRAN (R 4.0.3)
    #>  stringr         1.4.0   2019-02-10 [2] CRAN (R 4.0.3)
    #>  tibble          3.0.4   2020-10-12 [2] CRAN (R 4.0.3)
    #>  tidyr           1.1.2   2020-08-27 [2] CRAN (R 4.0.3)
    #>  tidyselect      1.1.0   2020-05-11 [2] CRAN (R 4.0.3)
    #>  tmvnsim         1.0-2   2016-12-15 [2] CRAN (R 4.0.3)
    #>  utf8            1.1.4   2018-05-24 [2] CRAN (R 4.0.3)
    #>  vctrs           0.3.4   2020-08-29 [2] CRAN (R 4.0.3)
    #>  withr           2.3.0   2020-09-22 [2] CRAN (R 4.0.3)
    #>  xfun            0.18    2020-09-29 [2] CRAN (R 4.0.3)
    #>  xml2            1.3.2   2020-04-23 [2] CRAN (R 4.0.3)
    #>  yaml            2.2.1   2020-02-01 [2] CRAN (R 4.0.3)
    #>  zip             2.1.1   2020-08-27 [2] CRAN (R 4.0.3)
    #>
    #> [1] /tmp/Rtmpqdg3Jr/Rinst3413112d5c7f
    #> [2] /home/biocbuild/bbs-3.12-bioc/R/library

# 4 Bibliography

This vignette was generated using BiocStyle (Oleś, Morgan, and Huber, 2020) with knitr (Xie, 2020) and rmarkdown (Allaire, Xie, McPherson, Luraschi, et al., 2020) running behind the scenes.

Citations made with knitcitations (Boettiger, 2019).

[1] J. Allaire, Y. Xie, J. McPherson, J. Luraschi, et al. rmarkdown: Dynamic Documents for R. R package version 2.5. 2020. <URL: https://github.com/rstudio/rmarkdown>.

[2] C. Boettiger. knitcitations: Citations for ‘Knitr’ Markdown Files. R package version 1.0.10. 2019. <URL: https://CRAN.R-project.org/package=knitcitations>.

[3] G. Csárdi, R. core, H. Wickham, W. Chang, et al. sessioninfo: R Session Information. R package version 1.1.1. 2018. <URL: https://CRAN.R-project.org/package=sessioninfo>.

[4] A. Oleś, M. Morgan, and W. Huber. BiocStyle: Standard styles for vignettes and other Bioconductor documents. R package version 2.18.0. 2020. <URL: https://github.com/Bioconductor/BiocStyle>.

[5] R Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing. Vienna, Austria, 2020. <URL: https://www.R-project.org/>.

[6] H. Wickham. “testthat: Get Started with Testing”. In: The R Journal 3 (2011), pp. 5-10. <URL: https://journal.r-project.org/archive/2011-1/RJournal_2011-1_Wickham.pdf>.

[7] Y. Xie. knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.30. 2020. <URL: https://yihui.org/knitr/>.

[8] calabrialab. Analyze gene therapy vector insertion sites data identified from genomics next generation sequencing reads for clonal tracking studies. https://github.com/calabrialab/ISAnalytics - R package version 1.0.0. 2020. DOI: 10.18129/B9.bioc.ISAnalytics. <URL: http://www.bioconductor.org/packages/ISAnalytics>.