## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)

## ----results = 'hide',warning = FALSE, message = FALSE------------------------
library(LTFGRS)
library(igraph)
library(dplyr)

## -----------------------------------------------------------------------------
# hand curated trio information, taken from LTFHPlus vignette:
# https://emilmip.github.io/LTFHPlus/articles/FromTrioToFamilies.html
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"
)

# creating a graph for the family
graph = prepare_graph(.tbl = family, icol = "id", mcol = "momcol", fcol = "dadcol")


## -----------------------------------------------------------------------------
print(graph)

## -----------------------------------------------------------------------------
delete_edges(graph, E(graph)[which_mutual(graph)])

## -----------------------------------------------------------------------------
str(graph)

## -----------------------------------------------------------------------------
graph = set_vertex_attr(graph = graph, # graph to add attribute to
                        name  = "status",  # name of attribute to be added
                        index = V(graph), # which vertices to add attribute to, V(graph) adds to all vertices
                        value = sample(c(FALSE, TRUE), size = vcount(graph), replace = TRUE, prob = c(0.95, 0.05))) # value of the attiribute to be added
print(graph)

## -----------------------------------------------------------------------------
vertex_attr(graph)

## -----------------------------------------------------------------------------
# adding another vertex attribute with a list-style assignment
V(graph)$age = sample(20:80, size = vcount(graph), replace = TRUE)
vertex_attr(graph)

## -----------------------------------------------------------------------------
delete_vertex_attr(graph = graph, name = "age") %>% # age is not removed from graph, since it is not stored.
  vertex_attr()

## -----------------------------------------------------------------------------
fam_graphs = get_family_graphs(pop_graph = graph, ndegree = 1, proband_vec = V(graph)$name)

## -----------------------------------------------------------------------------
get_FH = function(cur_proband, cur_graph) {
  fam_indx = which(V(cur_graph)$name != cur_proband)
  any(vertex_attr(cur_graph, name = "status", index = V(cur_graph)$name[fam_indx]))
}

## -----------------------------------------------------------------------------
fam_graphs %>% 
  mutate(FH = purrr::map2_lgl(.x = fid, .y = fam_graph,
                         .f = ~ get_FH(cur_proband = .x, cur_graph = .y)))

## -----------------------------------------------------------------------------
delete_vertices(graph = graph,
                v = V(graph)[which(vertex_attr(graph, name = "age") > 50)])

