Skip to content

Commit

Permalink
fix ped
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed May 10, 2024
1 parent 02cc057 commit 41e5ef9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion R/gen_tibble_ped.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ read.pedfile <- function(file, n, snps, which, split="\t| +", sep=".",
# r2 <- as.raw(2)
# r3 <- as.raw(3)

r0 <- 3
r0 <- NA_integer_
r1 <- 0
r2 <- 1
r3 <- 2
Expand Down
4 changes: 3 additions & 1 deletion R/gt_as_plink.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ recode_genotype <- function(x, allele_ref, allele_alt){
genotypes <- c(paste(allele_ref, allele_ref),
paste(allele_ref, allele_alt),
paste(allele_alt, allele_alt))
dplyr::case_match(
x <- dplyr::case_match(
x,
"0" ~ genotypes[1],
"1" ~ genotypes[2],
"2" ~ genotypes[3]
)
x[is.na(x)]<-c("0 0")
x

}

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_gen_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ test_that("gen_tibble catches invalid alleles",{

test_that("gen_tibble from a bed file",{
bed_path <- system.file("extdata/pop_a.bed", package = "tidypopgen")
pop_a_gt <- gen_tibble(bed_path, quiet=TRUE)
pop_a_gt <- gen_tibble(bed_path, quiet=TRUE, backingfile = tempfile())
# now read the dosages created by plink when saving in raw format
raw_file_pop_a <- read.table(system.file("extdata/pop_a.raw", package = "tidypopgen"), header= TRUE)
mat <- as.matrix(raw_file_pop_a[,7:ncol(raw_file_pop_a)])
Expand All @@ -81,6 +81,6 @@ test_that("gen_tibble from a bed file",{
# now read in the ped file
ped_path <- system.file("extdata/pop_a.ped", package = "tidypopgen")
pop_a_ped_gt <- gen_tibble(ped_path, quiet=TRUE,backingfile = tempfile())

all.equal(show_genotypes(pop_a_gt),show_genotypes(pop_a_ped_gt))

})
5 changes: 2 additions & 3 deletions tests/testthat/test_gt_as_plink.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_indiv_meta <- data.frame (id=c("a","b","c"),
population = c("pop1","pop1","pop2"))
test_genotypes <- rbind(c(1,1,0,1,1,0),
c(2,1,0,0,0,0),
c(2,2,0,0,1,1))
c(2,2,0,0,NA,1))
test_loci <- data.frame(name=paste0("rs",1:6),
chromosome=paste0("chr",c(1,1,1,1,2,2)),
position=as.integer(c(3,5,65,343,23,456)),
Expand Down Expand Up @@ -35,11 +35,10 @@ test_that("write a bed file",{
test_gt3 <- gen_tibble(ped_path, quiet=TRUE)
# the gen tibble from the bed and ped should contain the same information
expect_true(all.equal(show_loci(test_gt3),show_loci(test_gt2), check.attributes=FALSE))
expect_true(all.equal(show_loci(test_gt3),show_loci(test_gt2)))
expect_true(all.equal(show_genotypes(test_gt3),show_genotypes(test_gt2)))

# write it as raw
raw_path <- gt_as_plink(pop_a_gt,file=tempfile(),type="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"))
Expand Down

0 comments on commit 41e5ef9

Please sign in to comment.