Skip to content

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

Open
@zkamvar

Description

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 ;;;;;;;;;;;;"

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions