# selectKSigs: a package for selecting the number of mutational signatures

Zhi Yang1*

1Department of Preventive Medicine, University of Southern California, Los Angeles, USA

*zyang895@gmail.com

#### 2020-10-28

#### Abstract

Instructions on using selectKSigs on selecting the number of mutational signatures using a perplexity-based measure and cross-validation

# 1 Introduction

The R package HiLDA is developed under the Bayesian framework to select the number of mutational signatures based on perplexity and cross-validation. The mutation signature is defined based on the independent model proposed by Shiraishi’s et al.

## 1.1 Paper

- Shiraishi et al. A simple model-based approach to inferring and visualizing cancer mutation signatures, bioRxiv, doi: http://dx.doi.org/10.1101/019901.

- Zhi Yang, Paul Marjoram, Kimberly D. Siegmund. Selecting the number of mutational signatures using a perplexity-based measure and cross-validation.

# 2 Installing and loading the package

## 2.1 Installation

### 2.1.1 Bioconductor

selectKSigs requires several CRAN and Bioconductor R packages to be installed. Dependencies are usually handled automatically, when installing the package using the following commands:

    install.packages("BiocManager")
    BiocManager::install("selectKSigs")

[NOTE: Ignore the first line if you already have installed the BiocManager.]

You can also download the newest version from the GitHub using devtools:

    devtools::install_github("USCbiostats/selectKSigs")

# 3 Input data

`selectKSigs` is a package built on some basic functions from `pmsignature` including how to read the input data. Here is an example from `pmsignature` on the input data, mutation features are elements used for categorizing mutations such as:

- 6 substitutions (C>A, C>G, C>T, T>A, T>C and T>G)
- 2 flanking bases (A, C, G and T)
- transcription direction.

## 3.1 Mutation Position Format

    sample1 chr1  100   A   C
    sample1 chr1    200 A   T
    sample1 chr2    100 G   T
    sample2 chr1    300 T   C
    sample3 chr3    400 T   C

- The 1st column shows the name of samples
- The 2nd column shows the name of chromosome
- The 3rd column shows the coordinate in the chromosome
- The 4th column shows the reference base (A, C, G, or T).
- The 5th colum shows the alternate base (A, C, G, or T).

# 4 Workflow

## 4.1 Get input data

Here, inputFile is the path for the input file. numBases is the number of flanking bases to consider including the central base (if you want to consider two 5’ and 3’ bases, then set 5). Also, you can add transcription direction information using trDir. numSig sets the number of mutation signatures estimated from the input data. You will see a warning message on some mutations are being removed.

    library(HiLDA)

    ## Loading required package: ggplot2

    library(tidyr)
    library(ggplot2)
    library(dplyr)

    ##
    ## Attaching package: 'dplyr'

    ## The following objects are masked from 'package:stats':
    ##
    ##     filter, lag

    ## The following objects are masked from 'package:base':
    ##
    ##     intersect, setdiff, setequal, union

    inputFile <- system.file("extdata/esophageal.mp.txt.gz", package="HiLDA")
    G <- hildaReadMPFile(inputFile, numBases=5, trDir=TRUE)

    ## Warning in hildaReadMPFile(inputFile, numBases = 5, trDir = TRUE): Out of
    ## 24861 mutations, we could obtaintranscription direction information for 24728
    ## mutation. Other mutations are removed.

Also, we also provided a small simulated dataset which contains 10 mutational catalogs and used it for demonstrating the key functions in selectKSigs. We start with loading the sample dataset G stored as extdata/sample.rdata.

    library(selectKSigs)
    load(system.file("extdata/sample.rdata", package = "selectKSigs"))

### 4.1.1 Perform the selecting process

After we read in the sample data G, we can run the process from selectKSigs. Here, we specify the inputG as G, the number of cross-validation folds, kfold to be 3, the number of replications, nRep, to be 3, and the upper limit of the K values for exploration to be 7.

### 4.1.2 Visualizing the results

After we obtained the results, we can plot each measure by the range of K values that were refitted during the calculation. The optimal value of K is achieved at its minimum value highlighted in grey.

    results$Kvalue <- seq_len(nrow(results)) + 1
    results_df <- gather(results, Method, value, -Kvalue) %>%
      group_by(Method) %>%
      mutate(xmin = which.min(value) + 1 - 0.1,
             xmax = which.min(value) + 1 + 0.1)

    ggplot(results_df) +
      geom_point(aes(x = Kvalue, y = value, color = Method), size = 2) +
      facet_wrap(~ Method, scales = "free") +
      geom_rect(mapping = aes(xmin = xmin, xmax = xmax,
                              ymin = -Inf, ymax = Inf),
                fill = 'grey', alpha = 0.05) +
      theme_bw()+
      xlab("Number of signatures")

## 4.2 Session info

Here is the output of `sessionInfo()` for reproducibility in the future.

    sessionInfo()

    ## R version 4.0.3 (2020-10-10)
    ## 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] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] selectKSigs_1.2.0 dplyr_1.0.2       tidyr_1.1.2       HiLDA_1.4.0
    ## [5] ggplot2_3.3.2     BiocStyle_2.18.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] bitops_1.0-6
    ##  [2] matrixStats_0.57.0
    ##  [3] bit64_4.0.5
    ##  [4] progress_1.2.2
    ##  [5] httr_1.4.2
    ##  [6] GenomeInfoDb_1.26.0
    ##  [7] tools_4.0.3
    ##  [8] R6_2.4.1
    ##  [9] R2WinBUGS_2.1-21
    ## [10] DBI_1.1.0
    ## [11] BiocGenerics_0.36.0
    ## [12] colorspace_1.4-1
    ## [13] withr_2.3.0
    ## [14] tidyselect_1.1.0
    ## [15] prettyunits_1.1.1
    ## [16] bit_4.0.4
    ## [17] curl_4.3
    ## [18] compiler_4.0.3
    ## [19] Biobase_2.50.0
    ## [20] xml2_1.3.2
    ## [21] DelayedArray_0.16.0
    ## [22] labeling_0.4.2
    ## [23] rtracklayer_1.50.0
    ## [24] bookdown_0.21
    ## [25] scales_1.1.1
    ## [26] askpass_1.1
    ## [27] rappdirs_0.3.1
    ## [28] stringr_1.4.0
    ## [29] digest_0.6.27
    ## [30] Rsamtools_2.6.0
    ## [31] rmarkdown_2.5
    ## [32] BSgenome.Hsapiens.UCSC.hg19_1.4.3
    ## [33] XVector_0.30.0
    ## [34] pkgconfig_2.0.3
    ## [35] htmltools_0.5.0
    ## [36] MatrixGenerics_1.2.0
    ## [37] dbplyr_1.4.4
    ## [38] BSgenome_1.58.0
    ## [39] rlang_0.4.8
    ## [40] RSQLite_2.2.1
    ## [41] farver_2.0.3
    ## [42] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2
    ## [43] generics_0.0.2
    ## [44] BiocParallel_1.24.0
    ## [45] gtools_3.8.2
    ## [46] RCurl_1.98-1.2
    ## [47] magrittr_1.5
    ## [48] GenomeInfoDbData_1.2.4
    ## [49] Matrix_1.2-18
    ## [50] Rcpp_1.0.5
    ## [51] munsell_0.5.0
    ## [52] S4Vectors_0.28.0
    ## [53] abind_1.4-5
    ## [54] lifecycle_0.2.0
    ## [55] stringi_1.5.3
    ## [56] yaml_2.2.1
    ## [57] SummarizedExperiment_1.20.0
    ## [58] zlibbioc_1.36.0
    ## [59] BiocFileCache_1.14.0
    ## [60] grid_4.0.3
    ## [61] blob_1.2.1
    ## [62] parallel_4.0.3
    ## [63] forcats_0.5.0
    ## [64] crayon_1.3.4
    ## [65] lattice_0.20-41
    ## [66] Biostrings_2.58.0
    ## [67] cowplot_1.1.0
    ## [68] GenomicFeatures_1.42.0
    ## [69] hms_0.5.3
    ## [70] magick_2.5.0
    ## [71] knitr_1.30
    ## [72] pillar_1.4.6
    ## [73] GenomicRanges_1.42.0
    ## [74] boot_1.3-25
    ## [75] biomaRt_2.46.0
    ## [76] stats4_4.0.3
    ## [77] XML_3.99-0.5
    ## [78] R2jags_0.6-1
    ## [79] glue_1.4.2
    ## [80] evaluate_0.14
    ## [81] BiocManager_1.30.10
    ## [82] vctrs_0.3.4
    ## [83] gtable_0.3.0
    ## [84] openssl_1.4.3
    ## [85] purrr_0.3.4
    ## [86] assertthat_0.2.1
    ## [87] xfun_0.18
    ## [88] coda_0.19-4
    ## [89] rjags_4-10
    ## [90] tibble_3.0.4
    ## [91] GenomicAlignments_1.26.0
    ## [92] AnnotationDbi_1.52.0
    ## [93] memoise_1.1.0
    ## [94] IRanges_2.24.0
    ## [95] ellipsis_0.3.1