## ----message=FALSE, warning=FALSE---------------------------------------------
library(LTFGRS)
library(dplyr)
library(igraph)
library(kinship2)

## -----------------------------------------------------------------------------
# Setting seed:
set.seed(555)

# constructing family (trio) data
family = tribble(
  ~id, ~momcol, ~dadcol,
  "pid", "mom", "dad",
  "sib", "mom", "dad",
  "mhs", "mom", "dad2",
  "phs", "mom2", "dad",
  "mom", "mgm", "mgf",
  "dad", "pgm", "pgf",
  "dad2", "pgm2", "pgf2",
  "paunt", "pgm", "pgf",
  "pacousin", "paunt", "pauntH",
  "hspaunt", "pgm", "newpgf",
  "hspacousin", "hspaunt", "hspauntH",
  "puncle", "pgm", "pgf",
  "pucousin", "puncleW", "puncle",
  "maunt", "mgm", "mgf",
  "macousin", "maunt", "mauntH",
  "hsmuncle", "newmgm", "mgf",
  "hsmucousin", "hsmuncleW", "hsmuncle"
)

# simulating sex status on ambiguous individuals
thrs =  tibble(
 id = family %>% select(1:3) %>% unlist() %>% unique(),
 sex = case_when(
   id %in% family$momcol ~ "F",
   id %in% family$dadcol ~ "M",
   TRUE ~ NA)) %>% 
  mutate(sex = sapply(sex, function(x) ifelse(is.na(x), sample(c("M", "F"), 1), x)))

## -----------------------------------------------------------------------------
graph = prepare_graph(.tbl = family, 
                      node_attributes = thrs,
                      fcol = "dadcol",
                      mcol = "momcol",
                      icol = "id")
graph

## ----fig.alt = "Plot of the identified pedigree. Pedigree plotted with igraph package."----
# get_family_graphs wraps make_ego_graph returns a formatted tbl
fam_graph = get_family_graphs(pop_graph = graph,
                              ndegree = 2,
                              proband_vec = V(graph)$name)
fam_graph

## ----fig.alt = "Plot of the identified pedigree. Pedigree plotted with igraph package."----
plot(fam_graph$fam_graph[[1]], # choosing pid's family graph 
     layout = layout_as_tree,
     vertex.size = 27.5,
     vertex.shape = "rectangle",
     vertex.label.cex = .75,
     edge.arrow.size = .3) 

## -----------------------------------------------------------------------------
get_covmat(fam_graph$fam_graph[[1]], h2 = 1, index_id = "pid", add_ind = FALSE)

## -----------------------------------------------------------------------------
trio = graph_to_trio(graph = fam_graph$fam_graph[[1]], fixParents = TRUE)
trio

## ----fig.alt = "Plot of the identified pedigree. Pedigree plotted with kinship2 package."----
pedigree = with(trio,kinship2::pedigree(id = id, dadid = dadid,momid =  momid,sex =  sex))

plot(pedigree)

