|
2 | 2 | # avoids "no visible bindings" warnings |
3 | 3 | if (getRversion() >= "2.15.1") { |
4 | 4 | utils::globalVariables(c("gene", "pos", "aa", "het", "phased", "read_count", |
5 | | - "combo", "variant")) |
| 5 | + "combo", "variant", "n_aa.x", "n_aa.y", |
| 6 | + "het.x", "het.y", |
| 7 | + "phased.x", "phased.y", |
| 8 | + "aa.x", "aa.y", |
| 9 | + "read_count.x", "read_count.y", |
| 10 | + "n_aa")) |
6 | 11 | } |
7 | 12 |
|
8 | | -# --- FUNCTION LIST --- |
9 | | -# check_variant_string |
10 | | -# check_position_string |
11 | | -# variant_to_long |
12 | | -# long_to_variant |
13 | | -# position_to_long |
14 | | -# long_to_position |
15 | | -# position_from_variant_string |
16 | | -# subset_position |
17 | | -# order_variant_string |
18 | | -# order_position_string |
19 | | -# count_unphased_hets |
20 | | -# count_phased_hets |
21 | | -# drop_read_counts |
22 | | -# compare_variant_string |
23 | | -# compare_position_string |
24 | | -# extract_single_locus_variants |
25 | | -# get_component_variants |
26 | | -# allowed_amino_acids |
27 | | - |
28 | 13 | #------------------------------------------------ |
29 | 14 | #' @title Check for a valid variant string |
30 | 15 | #' |
@@ -1330,6 +1315,48 @@ get_component_variants <- function(x) { |
1330 | 1315 | return(ret) |
1331 | 1316 | } |
1332 | 1317 |
|
| 1318 | +#------------------------------------------------ |
| 1319 | +#' @title Overlay one variant string onto another |
| 1320 | +#' |
| 1321 | +#' @description |
| 1322 | +#' All the positions and amino-acids from the first variant are combined with |
| 1323 | +#' the second, overwriting where positions are the same. |
| 1324 | +#' |
| 1325 | +#' @details |
| 1326 | +#' Note, inputs are not internally checked for being valid variant strings, it is up to the user to ensure this (see \code{?check_variant_string}). |
| 1327 | +#' |
| 1328 | +#' @param var1 the first variant string, which overwrites in the case of shared positions. |
| 1329 | +#' @param var2 the second variant string, which is overwritten in the case of shared positions. |
| 1330 | +#' |
| 1331 | +#' @import dplyr |
| 1332 | +#' |
| 1333 | +#' @export |
| 1334 | + |
| 1335 | +overlay_variant <- function(var1, var2) { |
| 1336 | + |
| 1337 | + mapply(function(x, y) { |
| 1338 | + if (is.na(x) & is.na(y)) { |
| 1339 | + return(NA) |
| 1340 | + } |
| 1341 | + if (is.na(x)) { |
| 1342 | + return(y) |
| 1343 | + } |
| 1344 | + if (is.na(y)) { |
| 1345 | + return(x) |
| 1346 | + } |
| 1347 | + full_join(variant_to_long(x)[[1]], variant_to_long(y)[[1]], by = join_by(gene, pos)) |> |
| 1348 | + mutate(n_aa = coalesce(n_aa.x, n_aa.y), |
| 1349 | + het = coalesce(het.x, het.y), |
| 1350 | + phased = coalesce(phased.x, phased.y), |
| 1351 | + aa = coalesce(aa.x, aa.y), |
| 1352 | + read_count = coalesce(read_count.x, read_count.y)) |> |
| 1353 | + select(gene, pos, n_aa, het, phased, aa, read_count) |> |
| 1354 | + list() |> |
| 1355 | + long_to_variant() |
| 1356 | + }, var1, var2) |
| 1357 | + |
| 1358 | +} |
| 1359 | + |
1333 | 1360 | #------------------------------------------------ |
1334 | 1361 | #' @title List allowed amino acids |
1335 | 1362 | #' |
|
0 commit comments