From 56d8a5c081625ece3a118d0d1d0f98054d10c280 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 28 Jul 2020 15:51:28 +0200 Subject: [PATCH 1/5] Unraveled internal functions of greedyMix --- R/greedyMix.R | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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?', From f036a33bca2b358608d2e26b4fc2723bbf6eb91d Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Fri, 31 Jul 2020 09:20:44 +0200 Subject: [PATCH 2/5] Initial idea for fgetl --- R/fgetl.R | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 R/fgetl.R diff --git a/R/fgetl.R b/R/fgetl.R new file mode 100644 index 0000000..5e4eec6 --- /dev/null +++ b/R/fgetl.R @@ -0,0 +1,33 @@ +#' @title Read line from file, removing newline characters +#' @description Equivalent function to its homonymous Matlab equivalent. +#' @param file file to be read +#' @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 +#' @export +fgetl <- function(file) { + # ========================================================================== + # Validation + # ========================================================================== + if (file == "") return(-1) + # ========================================================================== + # Determine next line to be read + # ========================================================================== + if (is.null(attr(file, "last_read_line"))) { + attr(file, "last_read_line") <- 1 + } else { + attr(file, "last_read_line") <- attr(file, "last_read_line") + 1 + } + # ========================================================================== + # Returning next line + # ========================================================================== + out <- file[attr(file, "last_read_line")] + 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 +#' @export +fopen <- function(filename) readLines(filename) \ No newline at end of file From 372500d75057103558f70edfc0f8e45335b3b7d9 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Fri, 31 Jul 2020 09:26:57 +0200 Subject: [PATCH 3/5] Reimplemented fgetl --- R/fgetl.R | 18 ++++++------------ man/fgetl.Rd | 23 +++++++++++++++++++++++ man/fopen.Rd | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 man/fgetl.Rd create mode 100644 man/fopen.Rd diff --git a/R/fgetl.R b/R/fgetl.R index 5e4eec6..176a330 100644 --- a/R/fgetl.R +++ b/R/fgetl.R @@ -1,26 +1,19 @@ #' @title Read line from file, removing newline characters #' @description Equivalent function to its homonymous Matlab equivalent. -#' @param file file to be read +#' @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 (file == "") return(-1) + if (length(file) <= 1) return(-1) # ========================================================================== - # Determine next line to be read + # Returning file minus the first line # ========================================================================== - if (is.null(attr(file, "last_read_line"))) { - attr(file, "last_read_line") <- 1 - } else { - attr(file, "last_read_line") <- attr(file, "last_read_line") + 1 - } - # ========================================================================== - # Returning next line - # ========================================================================== - out <- file[attr(file, "last_read_line")] + out <- file[-1] return(out) } @@ -29,5 +22,6 @@ fgetl <- function(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/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 +} From 8f4a3a72eeef301e8456cd2487e20c9b45c4c105 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Fri, 31 Jul 2020 09:27:18 +0200 Subject: [PATCH 4/5] Renamed file --- R/{fgetl.R => fgetl-fopen.R} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename R/{fgetl.R => fgetl-fopen.R} (100%) diff --git a/R/fgetl.R b/R/fgetl-fopen.R similarity index 100% rename from R/fgetl.R rename to R/fgetl-fopen.R From 154a491b431bb421c2bbc347a3210aa17822b0f6 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Fri, 31 Jul 2020 09:54:55 +0200 Subject: [PATCH 5/5] Fixed handleData --- R/handleData.R | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) 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