Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[read.genalex()]: Detect semicolon-separated files and throw error with correct command #252

Open
zkamvar opened this issue May 6, 2022 · 0 comments

Comments

@zkamvar
Copy link
Member

zkamvar commented May 6, 2022

If a user reads in a genalex file with semicolons instead of commas, they will get the following error:

Error in if (nrow(gena) != ninds) { : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In read.genalex("genalex.csv") : NAs introduced by coercion

This happens in this block:

poppr/R/file_handling.r

Lines 213 to 236 in 94c745a

all.info <- strsplit(readLines(genalex, n = 2), sep)
cskip <- ifelse(inherits(genalex, "connection"), 0, 2)
gena <- utils::read.table(
genalex,
sep = sep,
header = TRUE,
skip = cskip,
colClasses = "character",
stringsAsFactors = FALSE,
check.names = FALSE,
quote = "\""
)
for (i in seq_along(gena)) {
the_col <- trimws(gena[[i]])
the_col[the_col == ""] <- NA_character_
gena[[i]] <- the_col
}
num.info <- as.numeric(all.info[[1]])
pop.info <- all.info[[2]][-c(1:3)]
num.info <- num.info[!is.na(num.info)]
pop.info <- pop.info[!pop.info %in% c("", NA)]
nloci <- num.info[1]
ninds <- num.info[2]
npops <- num.info[3]

all.info contains the first two lines of the genalex file, which determines the checks for the number of samples, loci, and populations. If the user uses ; as a separator in the file, but does not specify sep = ";", then all.info remains a two-element vector and ninds becomes missing:

> all.info                                                                            
[[1]]
[1] "9;72;5;10;26;20;11;5;;;;;;;;;;;;"

[[2]]
[1] ";;;Pop 1 ;Pop 2 ;Pop 3 ;Pop 4 ;Pop 5 ;;;;;;;;;;;;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant