# RNAmodR: creating classes for additional modification detection from high throughput sequencing.

Felix G.M. Ernst

#### 2020-03-28

#### Package

RNAmodR 1.1.15

# 1 Introduction

For users interested in the general aspect of any `RNAmodR` based package please have a look at the main vignette of the package.

This vignette is aimed at developers and researchers, who want to use the functionality of the `RNAmodR` package to develop a new modification strategy based on high throughput sequencing data.

    library(RNAmodR)

Two classes have to be considered to establish a new analysis pipeline using `RNAmodR`. These are the `SequenceData` and the `Modifier` class.

# 2 A new `SequenceData` class

First, the `SequenceData` class has to be considered. Several classes are already implemented, which are:

- `End5SequenceData`
- `End3SequenceData`
- `EndSequenceData`
- `ProtectedEndSequenceData`
- `CoverageSequenceData`
- `PileupSequenceData`
- `NormEnd5SequenceData`
- `NormEnd3SequenceData`

If these cannot be reused, a new class can be implemented quite easily. First the DataFrame class, the Data class and a constructor has to defined. The only value, which has to be provided, is a default `minQuality` integer value and some basic information.

    setClass(Class = "ExampleSequenceDataFrame",
             contains = "SequenceDFrame")
    ExampleSequenceDataFrame <- function(df, ranges, sequence, replicate,
                                          condition, bamfiles, seqinfo){
      RNAmodR:::.SequenceDataFrame("Example",df, ranges, sequence, replicate,
                                   condition, bamfiles, seqinfo)
    }
    setClass(Class = "ExampleSequenceData",
             contains = "SequenceData",
             slots = c(unlistData = "ExampleSequenceDataFrame"),
             prototype = list(unlistData = ExampleSequenceDataFrame(),
                              unlistType = "ExampleSequenceDataFrame",
                              minQuality = 5L,
                              dataDescription = "Example data"))
    ExampleSequenceData <- function(bamfiles, annotation, sequences, seqinfo, ...){
      RNAmodR:::SequenceData("Example", bamfiles = bamfiles,
                             annotation = annotation, sequences = sequences,
                             seqinfo = seqinfo, ...)
    }

Second, the `getData` function has to be implemented. This is used to load the data from a bam file and must return a named list `IntegerList`, `NumericList` or `CompressedSplitDataFrameList` per file.

    setMethod("getData",
              signature = c(x = "ExampleSequenceData",
                            bamfiles = "BamFileList",
                            grl = "GRangesList",
                            sequences = "XStringSet",
                            param = "ScanBamParam"),
              definition = function(x, bamfiles, grl, sequences, param, args){
                ###
              }
    )

Third, the `aggregate` function has to be implemented. This function is used to aggregate data over replicates for all or one of the conditions. The resulting data is passed on to the `Modifier` class.

    setMethod("aggregateData",
              signature = c(x = "ExampleSequenceData"),
              function(x, condition = c("Both","Treated","Control")){
                ###
              }
    )

# 3 A new `Modifier` class

A new `Modifier` class is probably the main class, which needs to be implemented. Three variable have to be set. `mod` must be a single element from the `Modstrings::shortName(Modstrings::ModRNAString())`. `score` is the default score, which is used for several function. A column with this name should be returned from the `aggregate` function. `dataType` defines the `SequenceData` class to be used. `dataType` can contain multiple names of a `SequenceData` class, which are then combined to form a `SequenceDataSet`.

    setClass("ModExample",
             contains = c("RNAModifier"),
             prototype = list(mod = "X",
                              score = "score",
                              dataType = "ExampleSequenceData"))
    ModExample <- function(x, annotation, sequences, seqinfo, ...){
      RNAmodR:::Modifier("ModExample", x = x, annotation = annotation,
                         sequences = sequences, seqinfo = seqinfo, ...)
    }

`dataType` can also be a `list` of `character` vectors, which leads then to the creation of `SequenceDataList`. However, for now this is a hypothetical case and should only be used, if the detection of a modification requires bam files from two or more different methods to be used to detect one modification.

The `settings<-` function can be amended to save specifc settings ( `.norm_example_args` must be defined seperatly to normalize input arguments in any way one sees fit).

    setReplaceMethod(f = "settings",
                     signature = signature(x = "ModExample"),
                     definition = function(x, value){
                       x <- callNextMethod()
                       # validate special setting here
                       x@settings[names(value)] <- unname(.norm_example_args(value))
                       x
                     })

The `aggregateData` function is used to take the aggregated data from the `SequenceData` object and to calculate the specific scores, which are then stored in the `aggregate` slot.

    setMethod(f = "aggregateData",
              signature = signature(x = "ModExample"),
              definition =
                function(x, force = FALSE){
                  # Some data with element per transcript
                }
    )

The `findMod` function takes the aggregate data and searches for modifications, which are then returned as a GRanges object and stored in the `modifications` slot.

    setMethod("findMod",
              signature = c(x = "ModExample"),
              function(x){
                # an element per modification found.
              }
    )

## 3.1 A new `ModifierSet` class

The `ModifierSet` class is implemented very easily by defining the class and the constructor. The functionality is defined by the `Modifier` class.

    setClass("ModSetExample",
             contains = "ModifierSet",
             prototype = list(elementType = "ModExample"))
    ModSetExample <- function(x, annotation, sequences, seqinfo, ...){
      RNAmodR:::ModifierSet("ModExample", x = x, annotation = annotation,
                            sequences = sequences, seqinfo = seqinfo, ...)
    }

# 4 Visualization functions

Additional functions, which need to be implemented, are `getDataTrack` for the new `SequenceData` and new `Modifier` classes and `plotData`/`plotDataByCoord` for the new `Modifier` and `ModifierSet` classes. `name` defines a transcript name found in `names(ranges(x))` and `type` is the data type typically found as a column in the `aggregate` slot.

    setMethod(
      f = "getDataTrack",
      signature = signature(x = "ExampleSequenceData"),
      definition = function(x, name, ...) {
        ###
      }
    )
    setMethod(
      f = "getDataTrack",
      signature = signature(x = "ModExample"),
      definition = function(x, name, type, ...) {
      }
    )
    setMethod(
      f = "plotDataByCoord",
      signature = signature(x = "ModExample", coord = "GRanges"),
      definition = function(x, coord, type = "score", window.size = 15L, ...) {
      }
    )
    setMethod(
      f = "plotData",
      signature = signature(x = "ModExample"),
      definition = function(x, name, from, to, type = "score", ...) {
      }
    )
    setMethod(
      f = "plotDataByCoord",
      signature = signature(x = "ModSetExample", coord = "GRanges"),
      definition = function(x, coord, type = "score", window.size = 15L, ...) {
      }
    )
    setMethod(
      f = "plotData",
      signature = signature(x = "ModSetExample"),
      definition = function(x, name, from, to, type = "score", ...) {
      }
    )

If unsure, how to modify these functions, have a look a the code in the `Modifier-Inosine-viz.R` file of this package.

# 5 Summary

As suggested directly above, for a more detailed example have a look at the `ModInosine` class source code found in the `Modifier-Inosine-class.R` and `Modifier-Inosine-viz.R` files of this package.

# 6 Sessioninfo

    sessionInfo()

    ## R Under development (unstable) (2020-03-10 r77920)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2012 R2 x64 (build 9600)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.1252
    ## [3] LC_MONETARY=English_United States.1252
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.1252
    ##
    ## attached base packages:
    ## [1] parallel  stats4    stats     graphics  grDevices utils     datasets
    ## [8] methods   base
    ##
    ## other attached packages:
    ##  [1] RNAmodR_1.1.15           Modstrings_1.3.12        RNAmodR.Data_1.1.2
    ##  [4] ExperimentHubData_1.13.1 AnnotationHubData_1.17.4 futile.logger_1.4.3
    ##  [7] ExperimentHub_1.13.5     AnnotationHub_2.19.7     BiocFileCache_1.11.4
    ## [10] dbplyr_1.4.2             GenomicFeatures_1.39.7   AnnotationDbi_1.49.1
    ## [13] Biobase_2.47.3           Rsamtools_2.3.7          Biostrings_2.55.7
    ## [16] XVector_0.27.2           rtracklayer_1.47.0       GenomicRanges_1.39.3
    ## [19] GenomeInfoDb_1.23.16     IRanges_2.21.8           S4Vectors_0.25.14
    ## [22] BiocGenerics_0.33.3      BiocStyle_2.15.6
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] RUnit_0.4.32                  tidyselect_1.0.0
    ##   [3] RSQLite_2.2.0                 htmlwidgets_1.5.1
    ##   [5] grid_4.0.0                    BiocParallel_1.21.2
    ##   [7] munsell_0.5.0                 codetools_0.2-16
    ##   [9] colorspace_1.4-1              OrganismDbi_1.29.1
    ##  [11] highr_0.8                     knitr_1.28
    ##  [13] rstudioapi_0.11               ROCR_1.0-7
    ##  [15] assertive.base_0.0-7          labeling_0.3
    ##  [17] optparse_1.6.4                GenomeInfoDbData_1.2.2
    ##  [19] bit64_0.9-7                   farver_2.0.3
    ##  [21] vctrs_0.2.4                   lambda.r_1.2.4
    ##  [23] xfun_0.12                     biovizBase_1.35.1
    ##  [25] R6_2.4.1                      assertive.sets_0.0-3
    ##  [27] AnnotationFilter_1.11.0       bitops_1.0-6
    ##  [29] DelayedArray_0.13.8           assertthat_0.2.1
    ##  [31] promises_1.1.0                scales_1.1.0
    ##  [33] nnet_7.3-13                   gtable_0.3.0
    ##  [35] biocViews_1.55.7              ensembldb_2.11.3
    ##  [37] rlang_0.4.5                   splines_4.0.0
    ##  [39] lazyeval_0.2.2                acepack_1.4.1
    ##  [41] dichromat_2.0-0               checkmate_2.0.0
    ##  [43] BiocManager_1.30.10           yaml_2.2.1
    ##  [45] reshape2_1.4.3                backports_1.1.5
    ##  [47] httpuv_1.5.2                  Hmisc_4.4-0
    ##  [49] RBGL_1.63.1                   tools_4.0.0
    ##  [51] bookdown_0.18                 ggplot2_3.3.0
    ##  [53] gplots_3.0.3                  assertive.strings_0.0-3
    ##  [55] RColorBrewer_1.1-2            assertive.reflection_0.0-4
    ##  [57] Rcpp_1.0.4                    plyr_1.8.6
    ##  [59] base64enc_0.1-3               progress_1.2.2
    ##  [61] zlibbioc_1.33.1               purrr_0.3.3
    ##  [63] RCurl_1.98-1.1                prettyunits_1.1.1
    ##  [65] rpart_4.1-15                  openssl_1.4.1
    ##  [67] SummarizedExperiment_1.17.5   cluster_2.1.0
    ##  [69] colorRamps_2.3                assertive.models_0.0-2
    ##  [71] magrittr_1.5                  magick_2.3
    ##  [73] data.table_1.12.8             assertive.data_0.0-3
    ##  [75] futile.options_1.0.1          ProtGenerics_1.19.3
    ##  [77] matrixStats_0.56.0            hms_0.5.3
    ##  [79] mime_0.9                      evaluate_0.14
    ##  [81] xtable_1.8-4                  XML_3.99-0.3
    ##  [83] jpeg_0.1-8.1                  gridExtra_2.3
    ##  [85] compiler_4.0.0                biomaRt_2.43.4
    ##  [87] tibble_2.1.3                  assertive.datetimes_0.0-2
    ##  [89] KernSmooth_2.23-16            crayon_1.3.4
    ##  [91] htmltools_0.4.0               later_1.0.0
    ##  [93] assertive_0.3-5               Formula_1.2-3
    ##  [95] snow_0.4-3                    DBI_1.1.0
    ##  [97] formatR_1.7                   assertive.files_0.0-2
    ##  [99] rappdirs_0.3.1                assertive.numbers_0.0-2
    ## [101] Matrix_1.2-18                 getopt_1.20.3
    ## [103] assertive.types_0.0-3         assertive.matrices_0.0-2
    ## [105] assertive.data.uk_0.0-2       gdata_2.18.0
    ## [107] Gviz_1.31.12                  pkgconfig_2.0.3
    ## [109] GenomicAlignments_1.23.2      foreign_0.8-77
    ## [111] assertive.data.us_0.0-2       stringdist_0.9.5.5
    ## [113] AnnotationForge_1.29.2        BiocCheck_1.23.1
    ## [115] stringr_1.4.0                 VariantAnnotation_1.33.2
    ## [117] digest_0.6.25                 graph_1.65.2
    ## [119] assertive.code_0.0-3          rmarkdown_2.1.1
    ## [121] htmlTable_1.13.3              curl_4.3
    ## [123] shiny_1.4.0.2                 gtools_3.8.1
    ## [125] lifecycle_0.2.0               jsonlite_1.6.1
    ## [127] askpass_1.1                   BSgenome_1.55.4
    ## [129] pillar_1.4.3                  lattice_0.20-40
    ## [131] fastmap_1.0.1                 httr_1.4.1
    ## [133] survival_3.1-11               interactiveDisplayBase_1.25.0
    ## [135] glue_1.3.2                    png_0.1-7
    ## [137] BiocVersion_3.11.1            bit_1.1-15.2
    ## [139] assertive.properties_0.0-4    stringi_1.4.6
    ## [141] blob_1.2.1                    latticeExtra_0.6-29
    ## [143] caTools_1.18.0                memoise_1.1.0
    ## [145] rBiopaxParser_2.27.0          dplyr_0.8.5