tests/testthat/test-label-and-plot.R

################################################################################
# testing get_generations() works with non-numeric string ids
################################################################################

# Regression test: get_generations() used to call as.numeric() on vertex ids
# pulled straight from the graph (a leftover from the original code, written
# for a system where ids were always numeric). Any id that isn't a pure
# digit string (e.g. "father", "mother") silently became NA, corrupting the
# whole relative-finding pipeline into an empty result - with only a
# generic "NAs introduced by coercion" warning pointing nowhere near the
# actual cause. A later type-reconciliation block at the end of the
# function looked like it should catch this, but converts the column TYPE
# back, not the already-lost DATA (as.character(as.numeric("father")) is
# still NA). Fixed by keeping ids as character throughout (igraph vertex
# names are always character natively). See R/labelleing_and_plotting_functions.R.

make_pedigree <- function(ids) {
  # father, mother -> proband, sib (full siblings)
  # mother, father2 -> half_sib (half-sibling via mother only)
  # pgf, pgm -> father; pgf, pgm -> aunt; aunt -> cousin
  edges <- rbind(
    c("father", "proband"), c("mother", "proband"),
    c("father", "sib"),     c("mother", "sib"),
    c("mother", "half_sib"), c("father2", "half_sib"),
    c("pgf", "father"), c("pgm", "father"),
    c("pgf", "aunt"),   c("pgm", "aunt"),
    c("aunt", "cousin")
  )
  edges[] <- ids[edges]
  igraph::graph_from_edgelist(edges, directed = TRUE)
}

get_labels_for_proband <- function(g, proband_id) {
  gens <- get_generations(g)
  rows <- gens[gens$id1 == proband_id, ]
  rows$fid <- proband_id
  lab <- label_relatives(rows)
  lab[order(lab$id2), c("id2", "gen.x", "gen.y", "k", "lab")]
}

test_that("get_generations correctly labels a pedigree with alphabetic string ids", {
  ids <- setNames(
    c("father", "mother", "proband", "sib", "half_sib", "father2", "pgf", "pgm", "aunt", "cousin"),
    c("father", "mother", "proband", "sib", "half_sib", "father2", "pgf", "pgm", "aunt", "cousin")
  )
  g <- make_pedigree(ids)
  res <- get_labels_for_proband(g, "proband")

  expect_equal(res$lab[res$id2 == "father"],   "P")
  expect_equal(res$lab[res$id2 == "mother"],   "P")
  expect_equal(res$lab[res$id2 == "sib"],      "S")
  expect_equal(res$lab[res$id2 == "half_sib"], "HS")
  expect_equal(res$lab[res$id2 == "pgf"],      "GP")
  expect_equal(res$lab[res$id2 == "aunt"],     "Pib")
  expect_equal(res$lab[res$id2 == "cousin"],   "1C")

  expect_equal(res$k[res$id2 == "father"],   0.25)
  expect_equal(res$k[res$id2 == "sib"],      0.25)
  expect_equal(res$k[res$id2 == "half_sib"], 0.125)
  expect_equal(res$k[res$id2 == "cousin"],   0.0625)
})

test_that("get_generations gives identical results for alphabetic and numeral-string ids", {
  alpha_ids <- setNames(
    c("father", "mother", "proband", "sib", "half_sib", "father2", "pgf", "pgm", "aunt", "cousin"),
    c("father", "mother", "proband", "sib", "half_sib", "father2", "pgf", "pgm", "aunt", "cousin")
  )
  num_ids <- setNames(
    c("3", "2", "1", "4", "5", "6", "7", "8", "9", "10"),
    c("father", "mother", "proband", "sib", "half_sib", "father2", "pgf", "pgm", "aunt", "cousin")
  )

  res_alpha <- get_labels_for_proband(make_pedigree(alpha_ids), "proband")
  res_num   <- get_labels_for_proband(make_pedigree(num_ids), "1")

  # id2 differs between the two schemes ("father" vs "3"), so align rows by
  # role (via each scheme's own id map) before comparing, rather than by
  # raw id2/row order.
  res_alpha$role <- names(alpha_ids)[match(res_alpha$id2, alpha_ids)]
  res_num$role   <- names(num_ids)[match(res_num$id2, num_ids)]
  res_alpha <- res_alpha[order(res_alpha$role), c("role", "gen.x", "gen.y", "k", "lab")]
  res_num   <- res_num[order(res_num$role), c("role", "gen.x", "gen.y", "k", "lab")]

  expect_equal(res_alpha, res_num, ignore_attr = TRUE)
})

################################################################################
# testing get_generations() does not warn when there are no collateral
# relatives to find
################################################################################

# Regression test: a graph with only direct-line ancestors/descendants (no
# siblings/cousins/etc.) legitimately produces an empty intermediate table
# partway through get_generations(), and group_by()/mutate()/min() on that
# empty table triggered base R's "no non-missing arguments to min;
# returning Inf" warning - harmless (the result was still correct) but
# noisy, since this can fire once per family in a batch loop. Fixed by
# guarding that step the same way an equivalent step later in the function
# already guards nrow(not_contained) > 0.

test_that("get_generations does not warn for a graph with no collateral relatives", {
  g <- igraph::graph_from_edgelist(rbind(c("father", "proband")), directed = TRUE)
  expect_no_warning(get_generations(g))
})

test_that("get_generations still errors on genuinely invalid input (sanity check that the guard above is narrowly scoped, not a blanket suppression)", {
  expect_error(get_generations(data.frame(x = 1)))
})

################################################################################

Try the LTFGRS package in your browser

Any scripts or data that you put into this service are public.

LTFGRS documentation built on Nov. 20, 2026, 5:10 p.m.