Merge branch 'fix-handleData' into import-genepop

This commit is contained in:
Waldir Leoncio 2020-07-31 09:56:08 +02:00
commit 9be0704d60
5 changed files with 102 additions and 14 deletions

27
R/fgetl-fopen.R Normal file
View file

@ -0,0 +1,27 @@
#' @title Read line from file, removing newline characters
#' @description Equivalent function to its homonymous Matlab equivalent.
#' @param file character vector to be read, usually an output of `fopen()`
#' @return If the file is nonempty, then fgetl returns tline as a character vector. If the file is empty and contains only the end-of-file marker, then fgetl returns tline as a numeric value -1.
#' @author Waldir Leoncio
#' @seealso fopen
#' @export
fgetl <- function(file) {
# ==========================================================================
# Validation
# ==========================================================================
if (length(file) <= 1) return(-1)
# ==========================================================================
# Returning file minus the first line
# ==========================================================================
out <- file[-1]
return(out)
}
#' @title Open file
#' @description Open a text file
#' @param filename Path and name of file to be open
#' @return The same as `readLines(filename)`
#' @author Waldir Leoncio
#' @seealso fgetl
#' @export
fopen <- function(filename) readLines(filename)

View file

@ -149,13 +149,26 @@ greedyMix <- function(
kunnossa <- testaaGenePopData(filename_pathname) kunnossa <- testaaGenePopData(filename_pathname)
if (kunnossa == 0) stop("testaaGenePopData returned 0") if (kunnossa == 0) stop("testaaGenePopData returned 0")
# [data,popnames]=lueGenePopData([pathname filename]); # TODO: trans data_popnames <- lueGenePopData(filename_pathname)
data <- data_popnames$data
popnames <- data_popnames$popnames
# h0 = findobj('Tag','filename1_text'); # h0 = findobj('Tag','filename1_text');
# set(h0,'String',filename); clear h0; # set(h0,'String',filename); clear h0;
# [data, rowsFromInd, alleleCodes, noalle, adjprior, priorTerm] = handleData(data); # TODO:trans browser()#TEMP
# [Z,dist] = newGetDistances(data,rowsFromInd); # TODO: trans list_dranap <- handleData(data) # FIXME: debug
data <- list_dranap$newData
rowsFromInd <- list_dranap$rowsFromInd
alleleCodes <- list_dranap$alleleCodes
noalle <- list_dranap$noalle
adjprior <- list_dranap$adjprior
priorTerm <- list_dranap$prioterm
list_Zd <- newGetDistances(data,rowsFromInd) # FIXME: debug
Z <- lizt_Zd$Z
dist <- lizt_Zd$dist
if (is.null(savePreProcessed)) { if (is.null(savePreProcessed)) {
save_preproc <- questdlg( save_preproc <- questdlg(
quest = 'Do you wish to save pre-processed data?', quest = 'Do you wish to save pre-processed data?',

View file

@ -20,12 +20,11 @@ handleData <- function(raw_data) {
# koodi pienimm?ksi koodiksi, joka isompi kuin mik??n k?yt?ss?oleva koodi. # koodi pienimm?ksi koodiksi, joka isompi kuin mik??n k?yt?ss?oleva koodi.
# T?m?n j?lkeen funktio muuttaa alleelikoodit siten, ett?yhden lokuksen j # T?m?n j?lkeen funktio muuttaa alleelikoodit siten, ett?yhden lokuksen j
# koodit saavat arvoja v?lill?1,...,noalle(j). # koodit saavat arvoja v?lill?1,...,noalle(j).
data <- raw_data data <- raw_data
nloci <- size(raw_data, 2) - 1 nloci <- size(raw_data, 2) - 1
dataApu <- data[, 1:nloci] dataApu <- data[, 1:nloci]
nollat <- find(dataApu==0) nollat <- find(dataApu == 0)
if (!isempty(nollat)) { if (!isempty(nollat)) {
isoinAlleeli <- max(max(dataApu)) isoinAlleeli <- max(max(dataApu))
dataApu[nollat] <- isoinAlleeli + 1 dataApu[nollat] <- isoinAlleeli + 1
@ -39,9 +38,12 @@ handleData <- function(raw_data) {
alleelitLokuksessa <- cell(nloci, 1) alleelitLokuksessa <- cell(nloci, 1)
for (i in 1:nloci) { for (i in 1:nloci) {
alleelitLokuksessaI <- unique(data[, i]) alleelitLokuksessaI <- unique(data[, i])
alleelitLokuksessa[i, 1] <- alleelitLokuksessaI[ alleelitLokuksessaI_pos <- find(alleelitLokuksessaI >= 0)
find(alleelitLokuksessaI >= 0) alleelitLokuksessa[i, 1] <- ifelse(
] test = length(alleelitLokuksessaI_pos) > 0,
yes = alleelitLokuksessaI[alleelitLokuksessaI_pos],
no = 0
)
noalle[i] <- length(alleelitLokuksessa[i, 1]) noalle[i] <- length(alleelitLokuksessa[i, 1])
} }
alleleCodes <- zeros(max(noalle), nloci) alleleCodes <- zeros(max(noalle), nloci)
@ -65,10 +67,10 @@ handleData <- function(raw_data) {
emptyRow <- repmat(a, c(1, ncols)) emptyRow <- repmat(a, c(1, ncols))
lessThanMax <- find(rowsFromInd < maxRowsFromInd) lessThanMax <- find(rowsFromInd < maxRowsFromInd)
missingRows <- maxRowsFromInd * nind - nrows missingRows <- maxRowsFromInd * nind - nrows
data <- as.matrix(c(data, zeros(missingRows, ncols))) data <- rbind(data, zeros(missingRows, ncols))
pointer <- 1 pointer <- 1
for (ind in t(lessThanMax)) { #K?y l?pi ne yksil?t, joilta puuttuu rivej? for (ind in t(lessThanMax)) { #K?y l?pi ne yksil?t, joilta puuttuu rivej?
miss = maxRowsFromInd-rowsFromInd(ind); # T?lt?yksil?lt?puuttuvien lkm. miss <- maxRowsFromInd - rowsFromInd(ind) # T?lt?yksil?lt?puuttuvien lkm.
} }
data <- sortrows(data, ncols) # Sorttaa yksil?iden mukaisesti data <- sortrows(data, ncols) # Sorttaa yksil?iden mukaisesti
newData <- data newData <- data
@ -84,12 +86,12 @@ handleData <- function(raw_data) {
priorTerm <- priorTerm + noalle[j] * lgamma(1 / noalle[j]) priorTerm <- priorTerm + noalle[j] * lgamma(1 / noalle[j])
} }
out <- list( out <- list(
newData = newData, newData = newData,
rowsFromInd = rowsFromInd, rowsFromInd = rowsFromInd,
alleleCodes = alleleCodes, alleleCodes = alleleCodes,
noalle = noalle, noalle = noalle,
adjprior = adjprior, adjprior = adjprior,
priorTerm = priorTerm priorTerm = priorTerm
) )
return(out) return(out)
} }

23
man/fgetl.Rd Normal file
View file

@ -0,0 +1,23 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fgetl.R
\name{fgetl}
\alias{fgetl}
\title{Read line from file, removing newline characters}
\usage{
fgetl(file)
}
\arguments{
\item{file}{character vector to be read, usually an output of `fopen()`}
}
\value{
If the file is nonempty, then fgetl returns tline as a character vector. If the file is empty and contains only the end-of-file marker, then fgetl returns tline as a numeric value -1.
}
\description{
Equivalent function to its homonymous Matlab equivalent.
}
\seealso{
fopen
}
\author{
Waldir Leoncio
}

23
man/fopen.Rd Normal file
View file

@ -0,0 +1,23 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fgetl.R
\name{fopen}
\alias{fopen}
\title{Open file}
\usage{
fopen(filename)
}
\arguments{
\item{filename}{Path and name of file to be open}
}
\value{
The same as `readLines(filename)`
}
\description{
Open a text file
}
\seealso{
fgetl
}
\author{
Waldir Leoncio
}