File: check_remove_muttypes.R

package info (click to toggle)
r-bioc-mutationalpatterns 3.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,360 kB
  • sloc: sh: 8; makefile: 2
file content (411 lines) | stat: -rw-r--r-- 12,514 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#' Checks that there are no variants with multiple alt alleles.
#'
#' @details
#' This function checks for a GRanges/GRangesList object, that there are no variants with multiple alternative alleles.
#' It works by checking that the number of variants is equal to the number of alternative alleles.
#' It trows an error when this is not the case.
#'
#' @param grl GRanges/GRangesList object
#'
#' @return Invisibly returns its input when it succeeds.
#' @noRd
#' @importFrom magrittr %>%

.check_no_multi_alts <- function(grl) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr <- unlist(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- grl
  } else {
    .not_gr_or_grl(grl)
  }
  .check_no_multi_alts_gr(gr)
  invisible(grl)
}

#' Checks that there are no variants with multiple alt alleles.
#'
#' @details
#' This function checks for a single GRanges object, that there are no variants with multiple alternative alleles.
#' It works by checking that the number of variants is equal to the number of alternative alleles.
#' It trows an error when this is not the case.
#'
#' @param gr GRanges object
#'
#' @return Invisibly returns its input when it succeeds.
#' @noRd
#' @importFrom magrittr %>%

.check_no_multi_alts_gr <- function(gr) {
  alt <- .get_alt(gr)
  nr_alts <- alt %>%
    unlist() %>%
    length()
  if (length(gr) != nr_alts) {
    stop(paste0(
      "There should not be any variants with multiple alternative alleles.\n",
      "You can remove these by using the `get_mut_type` function."
    ), call. = FALSE)
  }
  invisible(gr)
}

#' Checks that there are no SNVs/MNVs.
#'
#' @details
#' This function checks for a GRanges/GRangesList object, that there are no SNVs/MNVs.
#' It trows an error when this is not the case.
#'
#' @param grl GRanges/GrangesList object
#' @noRd
#' @return Invisibly returns its input when it succeeds.
#'

.check_no_substitutions <- function(grl) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr <- unlist(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- grl
  } else {
    .not_gr_or_grl(grl)
  }
  .check_no_substitutions_gr(gr)
  invisible(grl)
}

#' Checks that there are no SNVs/MNVs.
#'
#' @details
#' This function checks for a single GRanges object, that there are no SNVs/MNVs.
#' It trows an error when this is not the case.
#'
#' @param gr GRanges object
#' @noRd
#' @return Invisibly returns its input when it succeeds.
#'

.check_no_substitutions_gr <- function(gr) {
  snv_f <- .find_substitution(gr)
  nr_snv <- sum(snv_f)
  snv_present <- nr_snv >= 1
  if (snv_present) {
    stop(stringr::str_c("There seem to be ", 
                        nr_snv, 
                        " substitutions present in your data. ",
                        "Make sure to remove all substitutions with the `get_mut_type` function."), 
         call. = FALSE)
  }
  invisible(gr)
}


#' Checks that there are no SNVs/MNVs.
#'
#' @details
#' This function checks for a GRanges/GRangesList object, that there are no Indels.
#' It trows an error when this is not the case.
#'
#' @param grl GRanges/GrangesList object
#' @noRd
#' @return Invisibly returns its input when it succeeds.
#'
.check_no_indels <- function(grl) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr <- unlist(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- grl
  } else {
    .not_gr_or_grl(grl)
  }
  .check_no_indels_gr(gr)
  invisible(grl)
}

#' Checks that there are no Indels.
#'
#' @details
#' This function checks for a single GRanges object, that there are no Indels.
#' It trows an error when this is not the case.
#'
#' @param gr GRanges object
#' @noRd
#' @return Invisibly returns its input when it succeeds.
#'
.check_no_indels_gr <- function(gr) {
  snv_f <- .find_substitution(gr)
  nr_indel <- sum(!snv_f)
  snv_present <- nr_indel >= 1
  if (snv_present) {
    stop(paste0(
      "There seem to be at least ", nr_indel,
      " Indels present in your data. ",
      "Make sure to remove all Indels with the `get_mut_type` function."
    ), call. = FALSE)
  }
  invisible(gr)
}


#' Removes variants with multiple alt alleles.
#'
#' @details
#' This function removes variants with multiple alternative alleles for a GRanges/GRangesList object.
#'
#' @param grl GRanges/GRangesList object
#' @noRd
#' @return A filtered version of the input GRanges/GRangesList object.
.remove_multi_alts_variants <- function(grl) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr_l <- as.list(grl)
    grl <- purrr::map(gr_l, .remove_multi_alts_variants_gr) %>%
      GRangesList()
    return(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- .remove_multi_alts_variants_gr(grl)
    return(gr)
  } else {
    .not_gr_or_grl(grl)
  }
}

#' Removes variants with multiple alt alleles.
#'
#' @details
#' This function removes variants with multiple alternative alleles for a single GRanges object.
#'
#' @param gr GRanges object
#' @noRd
#' @return A filtered version of the input GRanges object.
#'
.remove_multi_alts_variants_gr <- function(gr) {
  alt <- .get_alt(gr)
  gr <- gr[IRanges::elementNROWS(alt) == 1]
  return(gr)
}

#' Removes SNV/MNV variants.
#'
#' @details
#' This function removes SNV/MNV variants for a GRanges/GrangesList object.
#'
#' @param grl GRanges/GRangesList object
#' @noRd
#' @return A filtered version of the input GRanges/GrangesList object.
.remove_substitutions <- function(grl) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr_l <- as.list(grl)
    grl <- purrr::map(gr_l, .remove_substitutions_gr) %>%
      GRangesList()
    return(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- .remove_substitutions_gr(grl)
    return(gr)
  } else {
    .not_gr_or_grl(grl)
  }
}

#' Removes SNV/MNV variants.
#'
#' @details
#' This function removes SNV/MNV variants for a single GRanges object.
#'
#' @param gr GRanges object
#' @noRd
#' @return A filtered version of the input GRanges object.
#'
.remove_substitutions_gr <- function(gr) {
  snv_f <- .find_substitution(gr)
  gr <- gr[!snv_f]
  return(gr)
}

#' Removes Indel variants.
#'
#' @details
#' This function removes Indel variants for a GRanges/GrangesList object.
#'
#' @param grl GRanges/GRangesList object
#' @noRd
#' @return A filtered version of the input GRanges/GrangesList object.
.remove_indels <- function(grl) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr_l <- as.list(grl)
    grl <- purrr::map(gr_l, .remove_indels_gr) %>%
      GRangesList()
    return(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- .remove_indels_gr(grl)
    return(gr)
  } else {
    .not_gr_or_grl(grl)
  }
}

#' Removes Indel variants.
#'
#' @details
#' This function removes Indel variants for a single GRanges object.
#'
#' @param gr GRanges object
#' @noRd
#' @return A filtered version of the input GRanges object.
#'
.remove_indels_gr <- function(gr) {
  snv_f <- .find_substitution(gr)
  gr <- gr[snv_f]
  return(gr)
}

#' Identifies SNVs/MNVs
#'
#' @details
#' This function finds SNVs/MNVs for a single GRanges object.
#'
#'
#' @param gr GRanges object
#' @noRd
#' @return A boolean vector. It's TRUE for SNVs/MNVs and FALSE for Indels
#'
.find_substitution <- function(gr) {
  .check_no_multi_alts(gr)
  snv_f <- width(.get_ref(gr)) == width(unlist(.get_alt(gr)))
  return(snv_f)
}

#' Throw an error for when an argument should be a GRanges/GRangesList object.
#'
#' @details
#' This function is called by other functions, when an argument should be a GRanges/GrangesList object, but isn't.
#' It throws an error showing the actual class of the argument.
#'
#' @param arg Argument. Any object that should have been a GRanges/GrangesList, but isn't.
#' @noRd
#' @return An error
#'
.not_gr_or_grl <- function(arg) {
  arg_name <- deparse(substitute(arg))
  arg_class <- class(arg)[[1]]
  stop(stringr::str_c(arg_name, " should be a CompressedGRangesList or GRanges object, instead it is a ", arg_class, " object.
               Please provide an object of the correct class."))
}

#' Throw an error for when an argument should be a GRanges object.
#'
#' @details
#' This function is called by other functions, when an argument should be a GRanges object, but isn't.
#' It throws an error showing the actual class of the argument.
#'
#' @param arg Argument. Any object that should have been a GRanges, but isn't.
#' @noRd
#' @return An error
#'
.not_gr <- function(arg) {
  arg_name <- deparse(substitute(arg))
  arg_class <- class(arg)[[1]]
  stop(stringr::str_c(arg_name, " should be a GRanges object, instead it is a ", arg_class, " object.
               Please provide an object of the correct class."))
}





#' Check that the seqnames of a GRangesList or GRanges object are present in a ref_genome
#'
#' @details
#' This function tests that the variants in a GRangesList or GRanges object have seqnames,
#' that match the supplied ref_genome.
#' If this is not the case an error is thrown, with suggestions how to fix this.
#' This function also check whether variants in the input overlap with the reference genome.
#' If this is not the case, then the wrong reference object might be used.
#'
#' @param grl GRangesList or GRanges object
#' @param ref_genome BSGenome reference genome object
#'
#' @noRd
#'
#' @return Invisibly returns the input grl
#'
.check_chroms <- function(grl, ref_genome) {
  if (inherits(grl, "CompressedGRangesList")) {
    gr <- BiocGenerics::unlist(grl)
  } else if (inherits(grl, "GRanges")) {
    gr <- grl
  } else {
    .not_gr_or_grl(grl)
  }

  # Get seq names
  gr_seqnames <- as.vector(seqnames(gr))
  ref <- BSgenome::getBSgenome(ref_genome)
  ref_seqnames <- seqnames(ref)

  # Check if the gr and reference have the same genome name.
  genome_name_gr <- unique(GenomeInfoDb::genome(gr))
  genome_name_ref <- unique(GenomeInfoDb::genome(ref))
  if (is.na(genome_name_gr) | genome_name_gr != genome_name_ref) {
    stop(paste0(
      "The input GRanges (your vcf data) and the ref_genome do not have the same genome name.\n",
      "This problem is known to occur when you use an outside function to read in vcfs.\n",
      "The input GRanges has the genome name: '", genome_name_gr, "'\n",
      "The ref_genome has the genome name: '", genome_name_ref, "'\n",
      "With `GenomeInfoDb::genome(your input GRanges) = '", genome_name_ref, "'`\n",
      "you can view and change the genome name of your data to that of the ref_genome."
    ), call. = FALSE)
  }

  # Check if there is any overlap in chromosome names
  shared_chroms <- intersect(gr_seqnames, ref_seqnames)
  if (!length(shared_chroms)) {
    stop("The input GRanges and the ref_genome share no seqnames (chromosome names). 
             Do they use the same seqlevelsStyle? An example of how to fix this is show below:
             You can change the seqlevelStyle with: `seqlevelsStyle(grl) = 'UCSC'", call. = FALSE)
  }

  # Check if there seqlevels in the input granges that are not in the reference.
  gr_seqlevels <- levels(seqnames(gr))
  gr_seqlevels_notref <- unique(gr_seqlevels[!gr_seqlevels %in% ref_seqnames])
  if (length(gr_seqlevels_notref)) {
    gr_seqlevels_notref <- stringr::str_c(gr_seqlevels_notref, collapse = ", ")
    stop(stringr::str_c(
      "The following seqlevels (chromosome names) occur in the input GRanges,",
      "but are not present in the ref_genome: ", gr_seqlevels_notref, ". An example of how to fix this is show below.
        First select the chromosomes you want to keep with: 
        `chromosomes = paste0('chr', c(1:22,'X'))`
        You can then remove variants in other chromosomes with: 
        `seqlevels(grl, pruning.mode = 'tidy') = chromosomes`"
    ), call. = FALSE)
  }

  # Check if there are variants in the input granges that don't overlap with the reference
  ref_gr <- GenomicRanges::GRanges(as.vector(seqnames(ref)), IRanges::IRanges(start = 1, end = GenomeInfoDb::seqlengths(ref)))
  hits <- GenomicRanges::findOverlaps(gr, ref_gr)
  if (length(hits)) {
    gr_nomatch <- gr[-S4Vectors::queryHits(hits)]
  } else {
    gr_nomatch <- gr
  }
  nr_nomatch <- length(gr_nomatch)
  if (nr_nomatch) {
    stop(stringr::str_c(
      "There are ", nr_nomatch, " variants that don't overlap with ",
      "the chromosome lengths of the chosen reference genome.\n",
      "Did you select the correct reference genome?"
    ), call. = FALSE)
  }

  invisible(grl)
}


#' Check whether input is a scalar na.
#'
#' @param x Input. Function checks if it is a scalar NA
#'
#' @return Boolean
#' @noRd
#'
.is_na <- function(x) {
  purrr::is_scalar_vector(x) && is.na(x)
}