Skip to content

Commit

Permalink
update read.genalex and add tests to fix #128
Browse files Browse the repository at this point in the history
  • Loading branch information
zkamvar committed Apr 2, 2017
1 parent 7fc9094 commit ca321db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -43,6 +43,8 @@ BUG FIX
(see https://github.com/grunwaldlab/poppr/issues/122).
* Haplodiploids imported in genalex files will be properly treated
(see https://github.com/grunwaldlab/poppr/issues/124).
* `read.genalex()` will now implicitly check for the correct number of
individuals in the data (see https://github.com/grunwaldlab/poppr/issues/128).

MISC
----
Expand Down
10 changes: 10 additions & 0 deletions R/file_handling.r
Expand Up @@ -267,6 +267,16 @@ read.genalex <- function(genalex, ploidy = 2, geo = FALSE, region = FALSE,
warning(msg, immediate. = TRUE)
}
gena <- gena[data_rows, data_cols]
if (nrow(gena) != ninds) {
theData <- if (inherits(genalex, "character")) genalex else deparse(substitute(genalex))
msg <- paste0("\n The number of rows in your data do not match the number ",
"of individuals specified.",
"\n\t", ninds, " individuals specified",
"\n\t", nrow(gena), " rows in data",
"\n Please inspect ", theData, " to ensure it's a properly ",
"formatted GenAlEx file.\n")
stop(msg)
}
#----------------------------------------------------------------------------#
# Checking for extra information such as Regions or XY coordinates
#----------------------------------------------------------------------------#
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-import.R
Expand Up @@ -62,6 +62,15 @@ A009 7_09_BB 224 97 159 160 133 156 126 119 147 227 261 134
A006 7_09_BB 224 97 159 160 133 156 126 119 147 235 261 134
A013 7_09_BB 224 97 163 160 133 156 126 119 147 235 257 134"

bad_genalex <- "1 6 1 6
Ind Pop CHMFc4
A004 7_09_BB 224
A002 7_09_BB 224
A011 7_09_BB 224
A009 7_09_BB 224
A006 7_09_BB 224
A013 7_09_BB 224 "

test_that("basic text connections work", {
gen <- read.genalex(textConnection(y), sep = "\t")
expect_equivalent(tab(gen), tab(monpop[1:6, drop = TRUE]))
Expand Down Expand Up @@ -115,6 +124,17 @@ Ind,Pop,RM127, ,RM22, ,RM22, ,RM127,
expect_warning(read.genalex(textConnection(f)), "col 7: RM22 -> RM22_1")
})

test_that("improperly-formatted data causes an error", {
skip_on_cran()
msg <- "^.+?6 individuals.+?5 rows.+?Please inspect "
tcmsg <- paste0(msg, "textConnection\\(bad_genalex\\).+?$")
expect_error(read.genalex(textConnection(bad_genalex), sep = "\t"), tcmsg)
f <- tempfile()
writeLines(bad_genalex, f)
fmsg <- paste0(msg, f, ".+?$")
expect_error(read.genalex(f, sep = "\t"), fmsg)
})

context("Data export tests")

test_that("genclone objects can be saved and restored", {
Expand Down

0 comments on commit ca321db

Please sign in to comment.