Skip to content

Commit

Permalink
Updating code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiromagno committed Jul 30, 2024
1 parent 65936fd commit b61470b
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 178 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ Suggests:
spelling,
testthat (>= 3.0.0)
Language: en-US
Config/Needs/website: patterninstitute/chic
Config/testthat/edition: 3
Roxygen: list(markdown = TRUE)
48 changes: 24 additions & 24 deletions R/amino_acid_pairs.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#' amino_acid_pairs(keep_self = FALSE)
#'
#' # Generate specific combinations of Ser against Ala and Trp.
#' amino_acid_pairs(x = 'Ser', y = c('Ala', 'Trp'))
#' @md
#' amino_acid_pairs(x = "Ser", y = c("Ala", "Trp"))
#'
#' @importFrom dplyr .data
#' @export
amino_acid_pairs <-
Expand All @@ -32,15 +32,15 @@ amino_acid_pairs <-
keep_self = TRUE,
keep_duplicates = TRUE,
keep_reverses = TRUE) {
if (!all_amino_acids(x)) {
stop("`x` must be a vector of three-letter code amino acids")
}

if(!all_amino_acids(x))
stop('`x` must be a vector of three-letter code amino acids')

if (!all_amino_acids(y))
stop('`y` must be a vector of three-letter code amino acids'
)
if (!all_amino_acids(y)) {
stop("`y` must be a vector of three-letter code amino acids")
}

# tbl <- tidyr::expand_grid(x = x, y = y)
# tbl <- tidyr::expand_grid(x = x, y = y)
tbl <- expand.grid(
y = y,
x = x,
Expand All @@ -50,20 +50,20 @@ amino_acid_pairs <-
tibble::as_tibble() |>
dplyr::relocate("x", "y")

tbl <- `if`(keep_self, tbl, dplyr::filter(tbl, x != y))
tbl <- `if`(keep_duplicates, tbl, dplyr::distinct(tbl))
tbl <- `if`(keep_self, tbl, dplyr::filter(tbl, x != y))
tbl <- `if`(keep_duplicates, tbl, dplyr::distinct(tbl))

tbl <-
if (keep_reverses) {
tbl # do nothing
} else {
tbl |>
dplyr::rowwise() |>
dplyr::mutate(key = paste(sort(c(x, y)), collapse = '-')) |>
dplyr::ungroup() |>
dplyr::distinct(.data$key, .keep_all = TRUE) |>
dplyr::select(-'key')
}
tbl <-
if (keep_reverses) {
tbl # do nothing
} else {
tbl |>
dplyr::rowwise() |>
dplyr::mutate(key = paste(sort(c(x, y)), collapse = "-")) |>
dplyr::ungroup() |>
dplyr::distinct(.data$key, .keep_all = TRUE) |>
dplyr::select(-"key")
}

return(tbl)
}
return(tbl)
}
6 changes: 4 additions & 2 deletions R/amino_acids.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#'
#' @export
amino_acids <- function() {
c("Ser", "Arg", "Leu", "Pro", "Thr", "Ala", "Val", "Gly", "Ile",
c(
"Ser", "Arg", "Leu", "Pro", "Thr", "Ala", "Val", "Gly", "Ile",
"Phe", "Tyr", "Cys", "His", "Gln", "Asn", "Lys", "Asp", "Glu",
"Met", "Trp")
"Met", "Trp"
)
}
10 changes: 4 additions & 6 deletions R/as_one_letter.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
#' @return A character vector of one-letter amino acid codes, e.g. `"S"`, `"R"`,
#' `"L"`, or `"B"`.
#'
#' @md
#' @examples
#' # Convert Ser to S, Arg to R and Pro to P.
#' as_one_letter(c('Ser', 'Arg', 'Pro'))
#' as_one_letter(c("Ser", "Arg", "Pro"))
#'
#' # The function `as_one_letter()` is case insensitive on the input but will
#' # always return the one-letter codes in uppercase.
#' as_one_letter(c('ser', 'ArG', 'PRO'))
#' as_one_letter(c("ser", "ArG", "PRO"))
#'
#' # Convert the codes of the 20 standard amino acids. Note that the function
#' # `amino_acids()` returns the three-letter codes of the 20 standard amino
Expand All @@ -27,16 +26,15 @@
#'
#' # Convert also special case codes Asx (Asparagine or Aspartic acid) and Glx
#' # (Glutamine or Glutamic acid)
#' as_one_letter(c('Asx', 'Glx'))
#' as_one_letter(c("Asx", "Glx"))
#'
#' # Invalid codes in the input are converted to NA.
#' # "Ser" is correctly mapped to "S" but "Serine" is not as it is not a
#' # three-letter amino acid code (the same applies to "Glucose").
#' as_one_letter(c('Ser', 'Serine', 'Glucose'))
#' as_one_letter(c("Ser", "Serine", "Glucose"))
#'
#' @export
as_one_letter <- function(x) {

three_to_one_letter_codes <- stats::setNames(one_letter_codes, nm = three_letter_codes)
unname(three_to_one_letter_codes[tools::toTitleCase(x)])
}
10 changes: 4 additions & 6 deletions R/as_three_letter.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@
#' @return A character vector of three-letter amino acid codes, e.g. `"Ser"`,
#' `"Arg"`, `"Leu"`, or `"Pro"`.
#'
#' @md
#' @examples
#' # Convert S to Ser, R to Arg and P to Pro.
#' as_three_letter(c('S', 'R', 'P'))
#' as_three_letter(c("S", "R", "P"))
#'
#' # The function `as_three_letter()` is case insensitive on the input but will
#' # always return the three-letter codes with the first letter in uppercase.
#' as_three_letter(c('S', 's', 'p', 'P'))
#' as_three_letter(c("S", "s", "p", "P"))
#'
#' # Convert also special case codes B (Asparagine or Aspartic acid) and Z
#' # (Glutamine or Glutamic acid)
#' as_three_letter(c('B', 'Z'))
#' as_three_letter(c("B", "Z"))
#'
#' # Invalid codes in the input are converted to NA.
#' # "S" is correctly mapped to "Ser" but "Ser" and "Serine" are not
#' # one-letter amino acid codes and are therefore converted to NA.
#' as_three_letter(c('S', 's', 'Ser', 'Serine'))
#' as_three_letter(c("S", "s", "Ser", "Serine"))
#'
#' @export
as_three_letter <- function(x) {

one_to_three_letter_codes <- stats::setNames(three_letter_codes, nm = one_letter_codes)
unname(one_to_three_letter_codes[toupper(x)])
}
96 changes: 46 additions & 50 deletions R/grantham_distance.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#' values that can be used with this formula. This data set is from Table 1,
#' Science (1974). 185(4154): 862--4 by R. Grantham.
#'
#' @md
#' @export
grantham_equation <-
function(c_i,
Expand All @@ -71,11 +70,10 @@ grantham_equation <-
beta = 0.1018,
gamma = 0.000399,
rho = 50.723) {

d_ij <- rho *
(alpha * (c_i - c_j) ^ 2 +
beta * (p_i - p_j) ^ 2 +
gamma * (v_i - v_j) ^ 2) ^ 0.5
(alpha * (c_i - c_j)^2 +
beta * (p_i - p_j)^2 +
gamma * (v_i - v_j)^2)^0.5

return(d_ij)
}
Expand Down Expand Up @@ -126,21 +124,19 @@ grantham_equation <-
#' @return A [tibble][tibble::tibble-package] of Grantham's distances for each
#' amino acid pair.
#'
#' @md
#'
#' @source \doi{10.1126/science.185.4154.862}.
#'
#' @examples
#' # Grantham's distance between Serine (Ser) and Glutamate (Glu)
#' grantham_distance('Ser', 'Glu')
#' grantham_distance("Ser", "Glu")
#'
#' # Grantham's distance between Serine (Ser) and Glutamate (Glu)
#' # with the "exact" method
#' grantham_distance('Ser', 'Glu', method = 'exact')
#' grantham_distance("Ser", "Glu", method = "exact")
#'
#' # `grantham_distance()` is vectorised
#' # amino acids are paired element-wise between `x` and `y`
#' grantham_distance(x = c('Pro', 'Gly'), y = c('Glu', 'Arg'))
#' grantham_distance(x = c("Pro", "Gly"), y = c("Glu", "Arg"))
#'
#' # Use `amino_acid_pairs()` to generate pairs (by default generates all pairs)
#' aa_pairs <- amino_acid_pairs()
Expand All @@ -150,39 +146,43 @@ grantham_equation <-
grantham_distance <-
function(x,
y,
method = c('original', 'exact'),
method = c("original", "exact"),
alpha = 1.833,
beta = 0.1018,
gamma = 0.000399,
rho = 50.723) {
if (!all_amino_acids(x)) {
stop("`x` should contain only amino acid three-letter codes.")
}

if(!all_amino_acids(x))
stop('`x` should contain only amino acid three-letter codes.')
if (!all_amino_acids(y)) {
stop("`y` should contain only amino acid three-letter codes.")
}

if(!all_amino_acids(y))
stop('`y` should contain only amino acid three-letter codes.')
# `rec`: recycled vectors `x` and `y`:
rec <- vctrs::vec_recycle_common(x = x, y = y)

# `rec`: recycled vectors `x` and `y`:
rec <- vctrs::vec_recycle_common(x = x, y = y)
# Check that `method` is either 'original' or 'exact'.
method <- match.arg(method)

# Check that `method` is either 'original' or 'exact'.
method <- match.arg(method)

if(identical(method, 'original'))
return(grantham_distance_original(x = rec$x,
y = rec$y))
else
return(
grantham_distance_exact(
if (identical(method, "original")) {
return(grantham_distance_original(
x = rec$x,
y = rec$y,
alpha = alpha,
beta = beta,
gamma = gamma,
rho = rho
y = rec$y
))
} else {
return(
grantham_distance_exact(
x = rec$x,
y = rec$y,
alpha = alpha,
beta = beta,
gamma = gamma,
rho = rho
)
)
)
}
}
}

#' Grantham's distance (original)
#'
Expand All @@ -196,12 +196,10 @@ grantham_distance <-
#' @return A [tibble][tibble::tibble-package] of Grantham's distances for each
#' amino acid pair.
#'
#' @md
#' @source \doi{10.1126/science.185.4154.862}.
#' @keywords internal
#' @export
grantham_distance_original <- function(x, y) {

amino_acid_pairs <- matrix(c(aa_idx(x), aa_idx(y)), ncol = 2)
tbl <- tibble::tibble(x = x, y = y, d = grantham_distances_matrix[amino_acid_pairs])

Expand All @@ -210,8 +208,6 @@ grantham_distance_original <- function(x, y) {

#' Grantham's distance (exact)
#'
#' @md
#'
#' @description
#' This function calculates the Grantham's distance for pairs of amino acids. It
#' uses the values for the amino acid properties as published in Table 1 of
Expand Down Expand Up @@ -246,7 +242,7 @@ grantham_distance_original <- function(x, y) {
#' @seealso [grantham_equation()]
#'
#' @examples
#' grantham_distance_exact(c('Ser', 'Ser'), c('Pro', 'Trp'))
#' grantham_distance_exact(c("Ser", "Ser"), c("Pro", "Trp"))
#'
#' @keywords internal
#' @export
Expand All @@ -256,24 +252,24 @@ grantham_distance_exact <- function(x,
beta = 0.1018,
gamma = 0.000399,
rho = 50.723) {

# Filter the properties table for the queried amino acids
x_tbl <- amino_acids_properties[aa_idx(x), ]
y_tbl <- amino_acids_properties[aa_idx(y), ]

# Grantham's distance computed from the amino acids' properties as provided in
# Table 1 of Grantham (1974).
d <- grantham_equation(c_i = x_tbl$c,
c_j = y_tbl$c,
p_i = x_tbl$p,
p_j = y_tbl$p,
v_i = x_tbl$v,
v_j = y_tbl$v,
alpha = alpha,
beta = beta,
gamma = gamma,
rho = rho
)
d <- grantham_equation(
c_i = x_tbl$c,
c_j = y_tbl$c,
p_i = x_tbl$p,
p_j = y_tbl$p,
v_i = x_tbl$v,
v_j = y_tbl$v,
alpha = alpha,
beta = beta,
gamma = gamma,
rho = rho
)

tbl <- tibble::tibble(x = x, y = y, d = d)

Expand Down
7 changes: 4 additions & 3 deletions R/sltm_k.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#' @param n Dimension of a `n` by `n` square matrix.
#'
#' @return An integer vector of linear positions in column-major order.
#' @md
#'
#' @examples
#' sltm_k(3)
#'
#' @noRd
#' @keywords internal
sltm_k <- function(n) {
if(!(n > 1)) stop('`n` must be greater than 1')
if (!(n > 1)) stop("`n` must be greater than 1")

utils::combn(seq_len(n), 2, function(ij) {ij2k(i = ij[2], j = ij[1], n)})
utils::combn(seq_len(n), 2, function(ij) {
ij2k(i = ij[2], j = ij[1], n)
})
}
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ Grantham distance between two amino acids:
```{r}
library(grantham)
grantham_distance(x = 'Ser', y = 'Phe')
grantham_distance(x = "Ser", y = "Phe")
```

The function `grantham_distance()` is vectorised with amino acids being matched element-wise to form pairs for comparison:

```{r}
grantham_distance(x = c('Ser', 'Arg'), y = c('Phe', 'Leu'))
grantham_distance(x = c("Ser", "Arg"), y = c("Phe", "Leu"))
```

The two vectors of amino acids must have compatible sizes in the sense of
Expand All @@ -59,7 +59,7 @@ one of them is of length one, and it is recycled up to the length of the other.

```{r}
# `'Ser'` is recycled to match the length of the second vector, i.e. 3.
grantham_distance(x = 'Ser', y = c('Phe', 'Leu', 'Arg'))
grantham_distance(x = "Ser", y = c("Phe", "Leu", "Arg"))
```

Use the function `amino_acid_pairs()` to generate all 20 x 20 amino acid pairs:
Expand Down
Loading

0 comments on commit b61470b

Please sign in to comment.