Skip to content
Merged

Dev #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: immunogenetr
Title: A Comprehensive Toolkit for Clinical HLA Informatics
Version: 0.1.0
Version: 0.2.0
Authors@R: c(
person("Nicholas", "Brown", , "nicholas.brown@pennmedicine.upenn.edu", role = c("cre", "aut"),
comment = c(ORCID = "0000-0002-0046-2315")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# immunogenetr 0.2.0

* Updated `HLA_prefix_add` and `HLA_prefix_remove` to work on all alleles in a GL string. Also added the option of keeping locus designations in `HLA_prefix_remove`.

# immunogenetr 0.1.0

* Initial CRAN submission.
23 changes: 15 additions & 8 deletions R/HLA_prefix_add.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' @title HLA_prefix_add
#'
#' @description This function adds a specified prefix to the beginning of
#' each value in the identified columns of the given data frame. Useful for adding
#' HLA or gene prefixes.
#' each HLA type, and works on a single allele or all alleles in a GL string.
#' Useful for adding HLA or gene prefixes.
#'
#' @param data A string with a single HLA allele.
#' @param prefix A character string to be added as a prefix to the column values.
#' @param data A string with a single HLA allele, a GL string of HLA alleles,
#' or a character vector containing either of the previous.
#' @param prefix A character string to be added as a prefix to the alleles.
#' Default is "HLA-".
#'
#' @return A data frame with the specified prefix added to the values in the
#' selected columns.
#' @return A vector with the specified prefix added to the values.
#'
#' @examples
#' # The HLA_typing_LIS dataset contains a table as might be found in a clinical
Expand All @@ -28,8 +28,15 @@
#' @export
#'
#' @importFrom stringr str_replace
#' @importFrom dplyr mutate
#' @importFrom dplyr %>%

HLA_prefix_add <- function(data, prefix = "HLA-") {
# Add string to beginning of typing
str_replace(data, "^", prefix)
# Expand the allele of GL string
data %>%
GLstring_expand_longer() %>%
# Add string to beginning of typing
mutate(value = str_replace(value, "^", prefix)) %>%
# Collapse the GL string
ambiguity_table_to_GLstring()
}
40 changes: 28 additions & 12 deletions R/HLA_prefix_remove.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#' @title HLA_prefix_remove
#'
#' @description This function removes HLA and locus prefixes from a string of HLA typing:
#' "HLA-A2" changes to "2".
#' @description This function removes HLA and optionally locus prefixes from a string of HLA typing:
#' "HLA-A2" changes to "A2" or "2". By default, HLA and locus prefixes are removed. This function
#' also works on each allele in a GL string.
#'
#' @param data A string with a single HLA allele.
#' @param data A string with a single HLA allele, a GL string of HLA alleles,
#' or a character vector containing either of the previous.
#' @param keep_locus A logical value indicating whether to retain any locus values.
#' The default value is FALSE.
#'
#' @return A string modified to remove HLA and locus prefixes.
#' @return A vector modified to remove HLA and optionally locus prefixes.
#'
#' @examples
#' # The HLA_typing_1 dataset contains a table with HLA typing spread across multiple columns:
Expand All @@ -24,17 +28,29 @@
#' @export
#'
#' @importFrom dplyr %>%
#' @importFrom dplyr mutate
#' @importFrom stringr str_replace

HLA_prefix_remove <- function(data) {
HLA_prefix_remove <- function(data, keep_locus = FALSE) {
# Removes any HLA and locus prefixes from typing results.
data %>%
step_1 <- data %>%
GLstring_expand_longer() %>%
# replaces "HLA-" with an empty string in each cell.
str_replace("HLA-", "") %>%
# replaces any sequences of alphabetic characters at the start of the string with an empty string
str_replace("^[:alpha:]+", "") %>%
# replaces any sequences of digits followed by an asterisk with an empty string
str_replace(., "[:digit:]*\\*", "")
mutate(value = str_replace(value, "HLA-", ""))

if (keep_locus) {
step_2 <- step_1
} else {
# Remove the locus.
step_2 <- step_1 %>%
# replaces any sequences of alphabetic characters at the start of the string with an empty string
mutate(value = str_replace(value, "^[:alpha:]+", "")) %>%
# replaces any sequences of digits followed by an asterisk with an empty string
mutate(value = str_replace(value, "[:digit:]*\\*", ""))
}

# Reassemble the GL string
ambiguity_table_to_GLstring(step_2)
}

globalVariables(c("."))
globalVariables(c("step_1", "step_2"))
2 changes: 1 addition & 1 deletion R/HLA_validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
#' @param data A string containing an HLA allele.
#'
#' @return A string with a valid HLA allele.
#' @return A string with a valid HLA allele or NA if no valid allele was present.
#'
#' @examples
#' HLA_validate("HLA-A2")
Expand Down
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

0 errors ✔ | 0 warnings ✔ | 0 notes ✔

* This is a new release.
* Updated `HLA_prefix_add` and `HLA_prefix_remove` to work on all alleles in a GL string. Also added the option of keeping locus designations in `HLA_prefix_remove`.
1 change: 1 addition & 0 deletions immunogenetr.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 4b2ac85c-cc00-4118-bdf2-a85d3de96ab2

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
12 changes: 6 additions & 6 deletions man/HLA_prefix_add.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions man/HLA_prefix_remove.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/HLA_validate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-HLA_prefix_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ test_that("HLA_prefix_add correctly adds prefixes", {

result_special_chars <- HLA_prefix_add("**01:01", "HLA-")
expect_equal(result_special_chars, "HLA-**01:01")

result_gl_string <- HLA_prefix_add("A*02:01/A*02:02+A*68:01^B*57:01+B*07:02", "HLA-")
expect_equal(result_gl_string, "HLA-A*02:01/HLA-A*02:02+HLA-A*68:01^HLA-B*57:01+HLA-B*07:02")

result_gl_string_vector <- HLA_prefix_add(c("A*02:01/A*02:02+A*68:01^B*57:01+B*07:02", "A*02:01", "B*07:01/B*07:02+B*15:01"), "HLA-")
expect_equal(result_gl_string_vector, c("HLA-A*02:01/HLA-A*02:02+HLA-A*68:01^HLA-B*57:01+HLA-B*07:02", "HLA-A*02:01", "HLA-B*07:01/HLA-B*07:02+HLA-B*15:01"))
})
6 changes: 6 additions & 0 deletions tests/testthat/test-HLA_prefix_remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ test_that("HLA_prefix_remove correctly removes prefixes", {

result_special_chars <- HLA_prefix_remove("HLA-**01:01")
expect_equal(result_special_chars, "*01:01")

result_GL_string <- HLA_prefix_remove(c("HLA-A*02:01", "HLA-A2", "HLA-A*02:01/HLA-A*02:02+HLA-A*68:01^HLA-B*57:01+HLA-B*07:02"), keep_locus = TRUE)
expect_equal(result_GL_string, c("A*02:01", "A2", "A*02:01/A*02:02+A*68:01^B*57:01+B*07:02"))

result_GL_string_2 <- HLA_prefix_remove(c("HLA-A*02:01", "HLA-A2", "HLA-A*02:01/HLA-A*02:02+HLA-A*68:01^HLA-B*57:01+HLA-B*07:02"), keep_locus = FALSE)
expect_equal(result_GL_string_2, c("02:01", "2", "02:01/02:02+68:01^57:01+07:02"))
})