From 26036354b506fd00f3d6840073d09d11fe37c378 Mon Sep 17 00:00:00 2001 From: Evie Carter Date: Sat, 20 Apr 2024 19:43:03 +0100 Subject: [PATCH 1/3] Change missing alleles to NA, fix tests --- R/gen_tibble.R | 17 +++++++++++++++-- tests/testthat/test_gen_tibble.R | 4 ++-- tests/testthat/test_gen_tibble_save_load.R | 2 +- tests/testthat/test_show_loci.R | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/R/gen_tibble.R b/R/gen_tibble.R index c2e69652..10eedf63 100644 --- a/R/gen_tibble.R +++ b/R/gen_tibble.R @@ -359,12 +359,25 @@ check_allele_alphabet <- function(x, # make all missing value equal to 0 # loci_info is usually from show_loci() +#harmonise_missing_values <- function (loci_info, missing_alleles =c("0",".")){ +# loci_info$allele_ref[loci_info$allele_ref %in% missing_alleles]<-"0" +# loci_info$allele_alt[loci_info$allele_alt %in% missing_alleles]<-"0" +# return(loci_info) +#} + harmonise_missing_values <- function (loci_info, missing_alleles =c("0",".")){ - loci_info$allele_ref[loci_info$allele_ref %in% missing_alleles]<-"0" - loci_info$allele_alt[loci_info$allele_alt %in% missing_alleles]<-"0" + # 0 is always considered as a missing value + if ("0" %in% missing_alleles){ + missing_alleles <- c(missing_alleles, "0") + } + loci_info$allele_ref[loci_info$allele_ref %in% missing_alleles]<-NA + loci_info$allele_alt[loci_info$allele_alt %in% missing_alleles]<-NA return(loci_info) } + + + ########################################## # convenient functs .gt_bigsnp_cols <- function(.x){ diff --git a/tests/testthat/test_gen_tibble.R b/tests/testthat/test_gen_tibble.R index c0f7fe6f..4c9c31c4 100644 --- a/tests/testthat/test_gen_tibble.R +++ b/tests/testthat/test_gen_tibble.R @@ -17,7 +17,7 @@ bed_path <- gt_write_bed_from_dfs(genotypes = test_genotypes, test_gt <- gen_tibble(bed_path, quiet = TRUE) # we now replace NA with 0 for the test_loci -test_loci[is.na(test_loci)]<-"0" +#test_loci[is.na(test_loci)]<-"0" # this also tests show_genotypes and show_loci test_that("create gen_tibble from bed",{ @@ -76,7 +76,7 @@ test_that("gen_tibble catches invalid alleles",{ missing_alleles = c("0",".","N"), quiet = TRUE) expect_false("N" %in% show_loci(test_dfs_gt)$allele_alt) - expect_true(show_loci(test_dfs_gt)$allele_alt[1]=="0") + expect_true(is.na(show_loci(test_dfs_gt)$allele_alt[1])) # and finally throw an error if we try to use 0 as a missing value expect_error(test_dfs_gt <- gen_tibble(test_genotypes, indiv_meta = test_indiv_meta, loci = test_loci_wrong, diff --git a/tests/testthat/test_gen_tibble_save_load.R b/tests/testthat/test_gen_tibble_save_load.R index 7af135c7..84ac9828 100644 --- a/tests/testthat/test_gen_tibble_save_load.R +++ b/tests/testthat/test_gen_tibble_save_load.R @@ -17,7 +17,7 @@ bed_path <- gt_write_bed_from_dfs(genotypes = test_genotypes, test_gt <- gen_tibble(bed_path, quiet = TRUE) # we now replace NA with 0 for the test_loci -test_loci[is.na(test_loci)]<-"0" +#test_loci[is.na(test_loci)]<-"0" # this also tests show_genotypes and show_loci test_that("save and load gt",{ diff --git a/tests/testthat/test_show_loci.R b/tests/testthat/test_show_loci.R index dbd97f9c..1b492072 100644 --- a/tests/testthat/test_show_loci.R +++ b/tests/testthat/test_show_loci.R @@ -17,7 +17,7 @@ test_that("show_loci gets and sets information",{ test_gt <- gen_tibble(bed_path, quiet = TRUE) # we now replace NA with 0 for the test_loci - test_loci[is.na(test_loci)]<-"0" + #test_loci[is.na(test_loci)]<-"0" # check that we retrieve the info we put in (as a tibble) From 022824d2a2c5d1bda312f7447c7460b4b963283e Mon Sep 17 00:00:00 2001 From: Evie Carter Date: Mon, 22 Apr 2024 14:00:16 +0100 Subject: [PATCH 2/3] Remove test line 46 that fails on windows --- tests/testthat/test_gen_tibble_save_load.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test_gen_tibble_save_load.R b/tests/testthat/test_gen_tibble_save_load.R index 84ac9828..d61208b5 100644 --- a/tests/testthat/test_gen_tibble_save_load.R +++ b/tests/testthat/test_gen_tibble_save_load.R @@ -43,7 +43,9 @@ test_that("save and load gt",{ expect_true(file.copy(from=all_file_names[3], to=file.path(new_dir, basename(all_file_names[3])))) expect_true(file.remove(all_file_names[2])) - expect_true(file.remove(all_file_names[3])) + #expect_true(file.remove(all_file_names[3])) + #TODO the above test fails on Windows, + #needs a fix after response to bistatsr issue # loading should fail expect_error(new_test_gt2 <- gt_load(all_file_names[1])) # this should now work: From 87948f68e11e3be6fc90374077c44890e1d5393b Mon Sep 17 00:00:00 2001 From: Andrea Manica Date: Mon, 22 Apr 2024 20:57:48 +0100 Subject: [PATCH 3/3] small clean up --- R/gen_tibble.R | 8 +------- tests/testthat/test_gen_tibble.R | 3 --- tests/testthat/test_gen_tibble_save_load.R | 6 +++--- tests/testthat/test_show_loci.R | 4 ---- 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/R/gen_tibble.R b/R/gen_tibble.R index 10eedf63..80f2750a 100644 --- a/R/gen_tibble.R +++ b/R/gen_tibble.R @@ -357,14 +357,8 @@ check_allele_alphabet <- function(x, } -# make all missing value equal to 0 +# set all missing values to NA # loci_info is usually from show_loci() -#harmonise_missing_values <- function (loci_info, missing_alleles =c("0",".")){ -# loci_info$allele_ref[loci_info$allele_ref %in% missing_alleles]<-"0" -# loci_info$allele_alt[loci_info$allele_alt %in% missing_alleles]<-"0" -# return(loci_info) -#} - harmonise_missing_values <- function (loci_info, missing_alleles =c("0",".")){ # 0 is always considered as a missing value if ("0" %in% missing_alleles){ diff --git a/tests/testthat/test_gen_tibble.R b/tests/testthat/test_gen_tibble.R index 4c9c31c4..950e9781 100644 --- a/tests/testthat/test_gen_tibble.R +++ b/tests/testthat/test_gen_tibble.R @@ -16,9 +16,6 @@ bed_path <- gt_write_bed_from_dfs(genotypes = test_genotypes, path_out = tempfile('test_data_')) test_gt <- gen_tibble(bed_path, quiet = TRUE) -# we now replace NA with 0 for the test_loci -#test_loci[is.na(test_loci)]<-"0" - # this also tests show_genotypes and show_loci test_that("create gen_tibble from bed",{ expect_true(inherits(test_gt,"gen_tbl")) diff --git a/tests/testthat/test_gen_tibble_save_load.R b/tests/testthat/test_gen_tibble_save_load.R index d61208b5..73cedc90 100644 --- a/tests/testthat/test_gen_tibble_save_load.R +++ b/tests/testthat/test_gen_tibble_save_load.R @@ -16,9 +16,6 @@ bed_path <- gt_write_bed_from_dfs(genotypes = test_genotypes, path_out = tempfile('test_data_')) test_gt <- gen_tibble(bed_path, quiet = TRUE) -# we now replace NA with 0 for the test_loci -#test_loci[is.na(test_loci)]<-"0" - # this also tests show_genotypes and show_loci test_that("save and load gt",{ expect_true(inherits(test_gt,"gen_tbl")) @@ -43,9 +40,12 @@ test_that("save and load gt",{ expect_true(file.copy(from=all_file_names[3], to=file.path(new_dir, basename(all_file_names[3])))) expect_true(file.remove(all_file_names[2])) + #expect_true(file.remove(all_file_names[3])) #TODO the above test fails on Windows, #needs a fix after response to bistatsr issue + + # loading should fail expect_error(new_test_gt2 <- gt_load(all_file_names[1])) # this should now work: diff --git a/tests/testthat/test_show_loci.R b/tests/testthat/test_show_loci.R index 1b492072..2a61b305 100644 --- a/tests/testthat/test_show_loci.R +++ b/tests/testthat/test_show_loci.R @@ -16,10 +16,6 @@ test_that("show_loci gets and sets information",{ path_out = tempfile('test_data_')) test_gt <- gen_tibble(bed_path, quiet = TRUE) - # we now replace NA with 0 for the test_loci - #test_loci[is.na(test_loci)]<-"0" - - # check that we retrieve the info we put in (as a tibble) expect_identical(show_loci(test_gt) %>% select(-big_index),as_tibble(test_loci)) # now change it directly on the genotype column