diff --git a/R/gen_tibble.R b/R/gen_tibble.R index 85402def..eff5275c 100644 --- a/R/gen_tibble.R +++ b/R/gen_tibble.R @@ -123,6 +123,35 @@ gen_tibble_bed_rds <- function(x, ..., bigsnp_file = bigsnp_path, indiv_id = bigsnp_obj$fam$sample.ID) + # transfer some of the fam info to the metadata table if it is not missing (0 is the default missing value) + fam_info <- .gt_get_bigsnp(indiv_meta)$fam + if(!all(fam_info$paternal.id==0)){ + indiv_meta$paternal_ID <- fam_info$paternal.id + indiv_meta$paternal_ID[indiv_meta$paternal_ID==0]<-NA + } + if(!all(fam_info$maternal.id==0)){ + indiv_meta$maternal_ID <- fam_info$maternal.id + indiv_meta$maternal_ID[indiv_meta$maternal_ID==0]<-NA + } + if(!all(fam_info$sex==0)){ + indiv_meta$sex <- dplyr::case_match( + fam_info$sex, + 1 ~ "male", + 2 ~ "female", + .default = NA, + .ptype = factor(levels = c("female", "male")) + ) + } + if(!all(fam_info$affection %in% c(0,-9))){ + indiv_meta$phenotype <- dplyr::case_match( + fam_info$affection, + 1 ~ "control", + 2 ~ "case", + -9 ~ NA, + .default = NA, + .ptype = factor(levels = c("control", "case")) + ) + } new_gen_tbl <- tibble::new_tibble( indiv_meta, class = "gen_tbl" diff --git a/tests/testthat/test_gt_as_plink.R b/tests/testthat/test_gt_as_plink.R index c4d4f8b7..ca49ecb3 100644 --- a/tests/testthat/test_gt_as_plink.R +++ b/tests/testthat/test_gt_as_plink.R @@ -39,9 +39,9 @@ test_that("write a bed file",{ # write it as raw raw_path <- gt_as_plink(test_gt,file=tempfile(),type="raw") - # check why sex has been lost (it was not read in properly?!?) - # look at line 119 of gen_tibble - #tools::md5sum(raw_path)==tools::md5sum(system.file("extdata","pop_a.raw",package="tidypopgen")) - + raw_file_test <- read.table(raw_path, header= TRUE) + mat <- as.matrix(raw_file_test[,7:ncol(raw_file_test)]) + mat <- unname(mat) + expect_true(all.equal(mat,show_genotypes(test_gt))) }) diff --git a/vignettes/example_clustering_and_dapc.Rmd b/vignettes/example_clustering_and_dapc.Rmd index 49923c39..8a69afcd 100644 --- a/vignettes/example_clustering_and_dapc.Rmd +++ b/vignettes/example_clustering_and_dapc.Rmd @@ -3,7 +3,7 @@ title: "example workflow with tidypopgen" output: rmarkdown::html_vignette #pdf_document vignette: > - %\VignetteIndexEntry{example_workflow} + %\VignetteIndexEntry{example workflow with tidypopgen} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- diff --git a/vignettes/overview.Rmd b/vignettes/overview.Rmd index c1fb692e..7d8e3925 100644 --- a/vignettes/overview.Rmd +++ b/vignettes/overview.Rmd @@ -367,12 +367,14 @@ write a file in the appropriate format, and return the name of that file on comp For example, to export to a PLINK .bed file, we simply use: ```{r} -gt_as_plink(example_gt, bedfile = tempfile("new_bed_")) +gt_as_plink(example_gt, file = tempfile("new_bed_")) ``` This will also write a .bim and .fam file and save them together with the .bed file. Note that, from the main tibble, only on `id`, `population` and `sex` will be preserved in the .fam file. +It is also possible to write .ped and .raw files by specifying `type="ped"` or +`type="raw"` in `gt_as_plink()` (see the help page for `gt_as_plink()` for details). # Merging data