## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(tidygedcom)

## ----real-file, eval = FALSE--------------------------------------------------
# # Unzip the Ancestry export
# unzip("W. Henderson Waugh Family Tree.zip", overwrite = TRUE)
# 
# # Peek at the raw file to orient yourself before parsing
# raw_ged <- readLines("W. Henderson Waugh Family Tree.ged")
# head(raw_ged, 15)
# 
# # Locate the anchor individual by name
# line_num <- which(grepl("1 NAME W. Henderson /Waugh/", raw_ged, fixed = TRUE))
# raw_ged[line_num:(line_num + 20)]
# 
# # Parse all INDI blocks
# ped <- readGedcom("W. Henderson Waugh Family Tree.ged",
#   remove_empty_cols = TRUE, verbose = FALSE
# )
# 
# # Add year columns robust to approximate dates
# ped$birth_year <- extractGedcomYear(ped$birth_date)
# ped$death_year <- extractGedcomYear(ped$death_date)
# 
# # Quick overview
# summarizeGedcom(ped)
# 
# # Parse family/marriage records
# fam <- readGedcomFamilies("W. Henderson Waugh Family Tree.ged", verbose = FALSE)

## ----bgmisc-workflow, eval = requireNamespace("BGmisc", quietly = TRUE) && requireNamespace("ggpedigree", quietly = TRUE)----
library(BGmisc)
library(ggpedigree)
sample_ged <- c(
  "0 HEAD",
  "1 GEDC",
  "2 VERS 5.5.1",
  "1 CHAR UTF-8",

  # William Pitt Waugh Sr. — the common paternal ancestor
  "0 @I1@ INDI",
  "1 NAME William Pitt /Waugh/",
  "1 SEX M",
  "1 BIRT",
  "2 DATE 28 APR 1775",
  "2 PLAC Adams County, Pennsylvania, USA",
  "1 DEAT",
  "2 DATE 14 AUG 1852",
  "2 PLAC Wilkes County, North Carolina, USA",
  "1 FAMS @F1@",
  "1 FAMS @F2@",

  # Matilda Grinton — mother of W. Henderson Waugh
  "0 @I2@ INDI",
  "1 NAME Matilda /Grinton/",
  "1 SEX F",
  "1 BIRT",
  "2 DATE ABT 1797",
  "2 PLAC North Carolina, USA",
  "1 FAMS @F1@",

  # W. Henderson Waugh — 2nd great-grandfather of focal person
  "0 @I3@ INDI",
  "1 NAME W. Henderson /Waugh/",
  "1 SEX M",
  "1 BIRT",
  "2 DATE ABT 1835",
  "2 PLAC Wilkes County, North Carolina, USA",
  "1 FAMC @F1@",
  "1 FAMS @F3@",

  # Martha Law Segraves — mother of William Pitt Waugh Jr.
  "0 @I4@ INDI",
  "1 NAME Martha Law /Segraves/",
  "1 SEX F",
  "1 BIRT",
  "2 DATE OCT 1814",
  "1 FAMS @F2@",

  # William Pitt Waugh Jr. (born William Segraves) — paternal half-brother of W. Henderson
  "0 @I5@ INDI",
  "1 NAME William Pitt /Waugh/ Jr.",
  "1 SEX M",
  "1 BIRT",
  "2 DATE 1844",
  "2 PLAC Wilkes County, North Carolina, USA",
  "1 DEAT",
  "2 DATE FEB 1880",
  "1 FAMC @F2@",
  "1 FAMS @F4@",

  # Laura Watkins — wife of W. Henderson Waugh
  "0 @I6@ INDI",
  "1 NAME Laura /Watkins/",
  "1 SEX F",
  "1 BIRT",
  "2 DATE ABT 1846",
  "2 PLAC North Carolina, USA",
  "1 FAMS @F3@",

  # John William (Bud) Waugh — son of W. Henderson; great-grandfather of focal person
  "0 @I7@ INDI",
  "1 NAME John William /Waugh/",
  "1 SEX M",
  "1 BIRT",
  "2 DATE ABT JUN 1880",
  "2 PLAC North Carolina, USA",
  "1 FAMC @F3@",

  # James Monroe Waugh — son of William Pitt Jr.; Y-DNA candidate branch
  "0 @I8@ INDI",
  "1 NAME James Monroe /Waugh/",
  "1 SEX M",
  "1 BIRT",
  "2 DATE 10 NOV 1867",
  "1 DEAT",
  "2 DATE 23 JUL 1937",
  "1 FAMC @F4@",

  # Family 1: William Pitt Sr. + Matilda Grinton -> W. Henderson Waugh
  "0 @F1@ FAM",
  "1 HUSB @I1@",
  "1 WIFE @I2@",
  "1 CHIL @I3@",

  # Family 2: William Pitt Sr. + Martha Segraves -> William Pitt Jr.
  # _SREL friend marks this as a non-marital relationship in Ancestry exports
  "0 @F2@ FAM",
  "1 HUSB @I1@",
  "1 WIFE @I4@",
  "1 CHIL @I5@",
  "1 _SREL friend",

  # Family 3: W. Henderson Waugh + Laura Watkins
  "0 @F3@ FAM",
  "1 HUSB @I3@",
  "1 WIFE @I6@",
  "1 CHIL @I7@",
  "1 MARR",
  "2 DATE 24 JUN 1877",
  "2 PLAC Wilkes County, North Carolina, USA",

  # Family 4: William Pitt Jr. + wife
  "0 @F4@ FAM",
  "1 HUSB @I5@",
  "1 CHIL @I8@",
  "0 TRLR"
)

tmp_ged <- tempfile(fileext = ".ged")
writeLines(sample_ged, tmp_ged)
ped <- readGedcom(tmp_ged, verbose = FALSE)

# Convert IDs to numeric (required by BGmisc)
ped_num <- ped
ped_num$personID <- as.numeric(ped_num$personID)
ped_num$momID <- as.numeric(ped_num$momID)
ped_num$dadID <- as.numeric(ped_num$dadID)

# Structure pedigree into family units and compute paternal line IDs
ged_ped <- ped2fam(ped_num, personID = "personID") |>
  ped2paternal(personID = "personID", momID = "momID", dadID = "dadID")

# Identify John William "Bud" Waugh as the focal person's great-grandfather
# (the deepest ancestor in our sample with a known paternal chain)
focal_ID <- ged_ped$personID[grepl("John William", ged_ped$name)]
y_line_ID <- ged_ped$patID[ged_ped$personID == focal_ID]

# Flag Y-line membership — green branch = focal person's line
ged_ped$y_line <- ifelse(ged_ped$patID == y_line_ID, "Y Line", "Other Lines")

## ----pedigree-plot, eval = requireNamespace("BGmisc", quietly = TRUE) && requireNamespace("ggpedigree", quietly = TRUE), fig.width = 7, fig.height = 4, fig.alt = "Pedigree plot of the Waugh family sample showing William Pitt Waugh Sr. at top with two branches: the focal person's Y line through W. Henderson Waugh on the left, and the Segraves Y-DNA candidate branch through William Pitt Waugh Jr. on the right."----
ggpedigree(
  ged_ped,
  personID = "personID",
  momID = "momID",
  dadID = "dadID",
  sexVar = "sex",
  config = list(
    label_include = TRUE,
    label_column = "name",
    label_text_size = 2,
    code_male = "M",
    code_female = "F",
    segment_lineage_include = TRUE,
    segment_lineage_focal_personID = focal_ID,
    segment_lineage_component = "paternal",
    segment_lineage_legend_title = "Patriline",
    add_phantoms = TRUE,
    founder_order_seed = 1L
  )
)

## ----cleanup------------------------------------------------------------------
unlink(tmp_ged)

