ourMELONS/R/testaaGenePopData.R

68 lines
2.3 KiB
R
Raw Normal View History

2020-06-08 15:41:52 +02:00
#' @title Tests GenePop data
2020-06-24 11:48:23 +02:00
#' @param tiedostonNimi Filename
2020-07-28 11:45:28 +02:00
#' @return kunnossa (binary "ok" condition value) == 0 if the data is not valid genePop data. Otherwise, kunnossa == 1.
#' @details GenePop data are textfiles that follow the GenePop format. This function checks if such file is properly formatted as GenePop.
2020-06-08 15:41:52 +02:00
testaaGenePopData <- function(tiedostonNimi) {
# kunnossa == 0, jos data ei ole kelvollinen genePop data.
# Muussa tapauksessa kunnossa == 1.
kunnossa <- 0
2020-07-28 11:45:28 +02:00
if (file.exists(tiedostonNimi)) {
fid <- readLines(tiedostonNimi)
line1 <- fid[1] # ensimmäinen rivi
line2 <- fid[2] # toinen rivi
line3 <- fid[3] # kolmas
2020-06-08 15:41:52 +02:00
} else {
fid <- line1 <- line2 <- line3 <- -1
}
if (line1 == -1 | line2 == -1 | line3 == -1) {
stop('Incorrect file format 1168')
}
2020-06-24 11:49:29 +02:00
if (testaaPop(line1) == 1 | testaaPop(line2) == 1) {
2020-06-08 15:41:52 +02:00
stop('Incorrect file format 1172')
}
if (testaaPop(line3) == 1) {
2020-07-28 11:45:28 +02:00
# 2 rivi t<>ll<6C>in lokusrivi (2 rows then locus row)
nloci <- rivinSisaltamienMjonojenLkm(line2)
line4 <- fid[4]
2020-06-08 15:41:52 +02:00
if (line4 == -1) stop('Incorrect file format 1180')
if (!grepl(',', line4)) {
# Rivin nelj?t<>ytyy sis<69>lt<6C><74> pilkku.
stop('Incorrect file format 1185')
}
pointer <- 1
2020-07-28 11:45:28 +02:00
while (substring(line4, pointer, pointer) != ',') {
# Tiedet<65><74>n, ett?pys<79>htyy
2020-06-08 15:41:52 +02:00
pointer <- pointer + 1
}
2020-07-28 11:45:28 +02:00
line4 <- substring(line4, pointer + 1) # pilkun j<>lkeinen osa (the part after the comma)
2020-06-08 15:41:52 +02:00
nloci2 <- rivinSisaltamienMjonojenLkm(line4)
if (nloci2 != nloci) stop('Incorrect file format 1195')
} else {
2020-07-28 11:45:28 +02:00
line <- fid[4]
2020-06-08 15:41:52 +02:00
lineNumb <- 4
while (testaaPop(line) != 1 & line != -1) {
2020-07-28 11:45:28 +02:00
line <- fid[lineNumb + 1]
2020-06-08 15:41:52 +02:00
lineNumb <- lineNumb + 1
}
if (line == -1) stop('Incorrect file format 1206')
nloci <- lineNumb - 2
2020-07-28 11:45:28 +02:00
line4 <- fid[lineNumb + 1] # Eka rivi pop sanan j<>lkeen
2020-06-08 15:41:52 +02:00
if (line4 == -1) stop('Incorrect file format 1212')
if (!grepl(',', line4)) {
2020-07-28 11:45:28 +02:00
# Rivin t<>ytyy sis<69>lt<6C><74> pilkku. (The line must contain a comma)
2020-06-08 15:41:52 +02:00
stop('Incorrect file format 1217')
}
pointer <- 1
2020-07-28 11:45:28 +02:00
while (substring(line4, pointer, pointer) != ',') {
# Tiedet<65><74>n, ett?pys<79>htyy
2020-06-08 15:41:52 +02:00
pointer <- pointer + 1
}
2020-07-28 11:45:28 +02:00
line4 <- substring(line4, pointer + 1) # pilkun j<>lkeinen osa (the part after the comma)
2020-06-08 15:41:52 +02:00
nloci2 <- rivinSisaltamienMjonojenLkm(line4)
if (nloci2 != nloci) stop('Incorrect file format 1228')
}
kunnossa <- 1
return(kunnossa)
}