diff --git a/R/fgetl-fopen.R b/R/fgetl-fopen.R new file mode 100644 index 0000000..176a330 --- /dev/null +++ b/R/fgetl-fopen.R @@ -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) \ No newline at end of file diff --git a/R/greedyMix.R b/R/greedyMix.R index edac561..a9573cf 100644 --- a/R/greedyMix.R +++ b/R/greedyMix.R @@ -149,13 +149,26 @@ greedyMix <- function( kunnossa <- testaaGenePopData(filename_pathname) 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'); # set(h0,'String',filename); clear h0; -# [data, rowsFromInd, alleleCodes, noalle, adjprior, priorTerm] = handleData(data); # TODO:trans -# [Z,dist] = newGetDistances(data,rowsFromInd); # TODO: trans + browser()#TEMP + 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)) { save_preproc <- questdlg( quest = 'Do you wish to save pre-processed data?', diff --git a/R/handleData.R b/R/handleData.R index 40d39e7..5bfd072 100644 --- a/R/handleData.R +++ b/R/handleData.R @@ -20,12 +20,11 @@ handleData <- function(raw_data) { # 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 # koodit saavat arvoja v?lill?1,...,noalle(j). - data <- raw_data nloci <- size(raw_data, 2) - 1 dataApu <- data[, 1:nloci] - nollat <- find(dataApu==0) + nollat <- find(dataApu == 0) if (!isempty(nollat)) { isoinAlleeli <- max(max(dataApu)) dataApu[nollat] <- isoinAlleeli + 1 @@ -39,9 +38,12 @@ handleData <- function(raw_data) { alleelitLokuksessa <- cell(nloci, 1) for (i in 1:nloci) { alleelitLokuksessaI <- unique(data[, i]) - alleelitLokuksessa[i, 1] <- alleelitLokuksessaI[ - find(alleelitLokuksessaI >= 0) - ] + alleelitLokuksessaI_pos <- find(alleelitLokuksessaI >= 0) + alleelitLokuksessa[i, 1] <- ifelse( + test = length(alleelitLokuksessaI_pos) > 0, + yes = alleelitLokuksessaI[alleelitLokuksessaI_pos], + no = 0 + ) noalle[i] <- length(alleelitLokuksessa[i, 1]) } alleleCodes <- zeros(max(noalle), nloci) @@ -65,10 +67,10 @@ handleData <- function(raw_data) { emptyRow <- repmat(a, c(1, ncols)) lessThanMax <- find(rowsFromInd < maxRowsFromInd) missingRows <- maxRowsFromInd * nind - nrows - data <- as.matrix(c(data, zeros(missingRows, ncols))) + data <- rbind(data, zeros(missingRows, ncols)) pointer <- 1 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 newData <- data @@ -84,12 +86,12 @@ handleData <- function(raw_data) { priorTerm <- priorTerm + noalle[j] * lgamma(1 / noalle[j]) } out <- list( - newData = newData, + newData = newData, rowsFromInd = rowsFromInd, alleleCodes = alleleCodes, - noalle = noalle, - adjprior = adjprior, - priorTerm = priorTerm + noalle = noalle, + adjprior = adjprior, + priorTerm = priorTerm ) return(out) } \ No newline at end of file diff --git a/man/fgetl.Rd b/man/fgetl.Rd new file mode 100644 index 0000000..cd49776 --- /dev/null +++ b/man/fgetl.Rd @@ -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 +} diff --git a/man/fopen.Rd b/man/fopen.Rd new file mode 100644 index 0000000..3e2d3a7 --- /dev/null +++ b/man/fopen.Rd @@ -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 +}