## ----include=FALSE, results = 'hide'------------------------------------------ knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----setup, results = 'hide',warning = FALSE, message = FALSE----------------- library(LTFGRS) library(dplyr) library(lubridate) library(rmarkdown) ## ----------------------------------------------------------------------------- # population parameters and seed set.seed(555) h2 = .5 # heritability K = .3 # population prevalence ## ----------------------------------------------------------------------------- # assuming we have been provided a CIP object of the following style: CIP = expand.grid(list(age = 1:100, birth_year = 1900:2024, sex = 0:1)) %>% group_by(sex, birth_year) %>% mutate(cip = (1:n() - 1)/n() * K) %>% ungroup() %>% print(n = 10) ## ----------------------------------------------------------------------------- # 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" ) ## ----------------------------------------------------------------------------- # in real trio data, we would have a row for every individual in the population, and NA if parents are unknown: to_add_momcol = tibble(id = unique(family$momcol[!(family$momcol %in% family$id)]), momcol = NA, dadcol = NA) to_add_dadcol = tibble(id = unique(family$dadcol[!(family$dadcol %in% family$id)]), momcol = NA, dadcol = NA) trio = bind_rows(family, to_add_momcol, to_add_dadcol) ## ----echo=FALSE--------------------------------------------------------------- paged_table(trio) ## ----------------------------------------------------------------------------- # creating a graph for the family graph = prepare_graph(.tbl = trio, icol = "id", mcol = "momcol", fcol = "dadcol") # calculating the kinship matrix based on the graph cov_mat = get_covmat(fam_graph = graph, h2 = h2, index_id = "pid") # creating a phenotype for the family liabs = MASS::mvrnorm(n = 1, mu = rep(0, nrow(cov_mat)), Sigma = cov_mat) ## ----------------------------------------------------------------------------- # these values are simulated only for illustrative purposes and not to make sense(!) pheno = tibble( id = names(liabs), status = liabs > qnorm(K, lower.tail = F), # no consideration for generation etc in fdato or birth_year: fdato = dmy(paste0(sample(1:28, length(liabs), replace = TRUE), "/", sample(1:12, length(liabs), replace = T), "/", sample(1940:2000, length(liabs), replace = TRUE))), birth_year = year(fdato), # age of onset only after fdato adhd = purrr::map2_chr(.x = status, .y = birth_year, ~ if(.x) paste0(sample(1:28, 1), "/", sample(1:12, 1), "/", sample((.y + 1):2010, 1)) else NA), # end of follow up assigned here indiv_eof = dmy("31/12/2010")) %>% # blanket time stop, meant to simulate end of registers mutate( # Assigning sex to each individual sex = case_when( id %in% family$momcol ~ 1, id %in% family$dadcol ~ 0, TRUE ~ sample(0:1, n(), replace = TRUE)), # converting to date format adhd = dmy(adhd), # eof either blanket time stop or event date indiv_eof = pmin(indiv_eof, adhd, na.rm = TRUE), # calculating age at the end of follow up age = as.numeric(difftime(indiv_eof, fdato, units = "days")) / 365.25) %>% filter(id != "pid_g") # remove the genetic liability of the proband ## ----echo=FALSE--------------------------------------------------------------- paged_table(pheno) ## ----results = 'hide'--------------------------------------------------------- thresholds = prepare_thresholds( .tbl = pheno, CIP = CIP, age_col = "age" , status_col = "status", lower_equal_upper = FALSE, personal_thr = TRUE, interpolation = "xgboost" ) ## ----------------------------------------------------------------------------- graph = prepare_graph(.tbl = trio, icol = "id", mcol = "momcol", fcol = "dadcol", node_attributes = select(thresholds, id, sex, K_i, lower, upper)) ## ----------------------------------------------------------------------------- # Identify family members of degree n family_graphs = get_family_graphs(pop_graph = graph, ndegree = 2, proband_vec = pheno$id, fid = "fid", fam_graph_col = "fam_graph") ## ----------------------------------------------------------------------------- family_graphs = left_join(family_graphs, trio, by = c("fid" = "id")) family_graphs %>% print(n = 4) ## ----------------------------------------------------------------------------- family_graphs$fam_graph[[1]] ## ----------------------------------------------------------------------------- kendler_fgrs = kendler_simplified( family_graphs = family_graphs, family_graphs_col = "fam_graph", pid = "pid", fid = "fid", dadcol = "dadcol", momcol = "momcol", env_cor_sib = .5, env_cor_f = .5, env_cor_m = .5) ## ----echo=FALSE--------------------------------------------------------------- paged_table(kendler_fgrs) ## ----------------------------------------------------------------------------- # extract graph pid_graph = (family_graphs %>% filter(fid == "pid") %>% pull(fam_graph))[[1]] # extract attributes from graph and format for tbl input: tbl_input = igraph::vertex.attributes(pid_graph) %>% as_tibble() %>% rename(pid = name) %>% mutate(fid = "pid", # roles are relative to the proband of the family. Only using pid here: role = stringr::str_replace(pid, "uncle|aunt", "au"), role = case_when( role == "mom" ~ "m", role == "dad" ~ "f", role == "sib" ~ "s", role == "pid" ~ "o", TRUE ~ role )) %>% relocate(fid, pid, role) %>% left_join(trio, by = c("fid" = "id")) # more than 1 pau role, making sure we know they are different: which_pau = which(tbl_input$role == "pau") tbl_input$role[which_pau] = paste0(tbl_input$role[which_pau], 1:2) ## ----echo=FALSE--------------------------------------------------------------- paged_table(tbl_input) ## ----------------------------------------------------------------------------- kendler_simplified(.tbl = tbl_input, role = "role", dadcol = "dadcol", momcol = "momcol")