Skip to content

Commit

Permalink
preserve indiv info read from the bed file
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed May 10, 2024
1 parent 41e5ef9 commit 5f73bd9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
29 changes: 29 additions & 0 deletions R/gen_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_gt_as_plink.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})

2 changes: 1 addition & 1 deletion vignettes/example_clustering_and_dapc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
---
Expand Down
4 changes: 3 additions & 1 deletion vignettes/overview.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5f73bd9

Please sign in to comment.