From 4d2a00403bdbcfbab126ee1096029ae36ccab112 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 19 Nov 2020 07:26:02 +0100 Subject: [PATCH 1/4] Fixed ignoring valid MATLAB-named files --- .Rbuildignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.Rbuildignore b/.Rbuildignore index 548f11c..740b69e 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,6 +1,6 @@ LICENSE TODO.md -matlab +^matlab CHANGELOG.md CITATION.cff .travis.yml From 97af90d05d05dae640f3e9d93870a64c6bc896f2 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 19 Nov 2020 07:49:30 +0100 Subject: [PATCH 2/4] Minor improvements to translator function --- R/matlab2r.R | 3 ++- man/matlab2r.Rd | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 man/matlab2r.Rd diff --git a/R/matlab2r.R b/R/matlab2r.R index e041634..f69dcb8 100644 --- a/R/matlab2r.R +++ b/R/matlab2r.R @@ -4,6 +4,7 @@ #' @param saveOutput if `TRUE`, `filename` is overwritten. Defaults to `FALSE` #' @return text converted to R, printed to screen or replacing input file #' @author Waldir Leoncio +#' @importFrom utils write.table #' @export matlab2r <- function(filename, saveOutput = FALSE) { # Verification @@ -21,7 +22,7 @@ matlab2r <- function(filename, saveOutput = FALSE) { txt <- gsub("for (.+)=(.+)", "for (\\1 in \\2) {", txt) txt <- gsub("end", "}", txt) txt <- gsub("(.),(\\S)", "\\1, \\2", txt) - # TODO: replace forms like (:,:) with [, ] + # TODO: replace forms like (:,:) with [, ] if they come before <- # TODO: add argument to skip some of these rules txt <- gsub("if (.+)", "if (\\1) {", txt) # FIXME: paste comments after { txt <- gsub("else$", "} else {", txt) diff --git a/man/matlab2r.Rd b/man/matlab2r.Rd new file mode 100644 index 0000000..e5a3f9e --- /dev/null +++ b/man/matlab2r.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/matlab2r.R +\name{matlab2r} +\alias{matlab2r} +\title{Convert Matlab function to R} +\usage{ +matlab2r(filename, saveOutput = FALSE) +} +\arguments{ +\item{filename}{name of the file} + +\item{saveOutput}{if `TRUE`, `filename` is overwritten. Defaults to `FALSE`} +} +\value{ +text converted to R, printed to screen or replacing input file +} +\description{ +Performs basic syntax conversion from Matlab to R +} +\author{ +Waldir Leoncio +} From 46a9249e5ad7df702c19df9c8f26f6bc65f0adf1 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 19 Nov 2020 07:53:36 +0100 Subject: [PATCH 3/4] Fixed syntax --- NAMESPACE | 2 ++ R/admixture_initialization.R | 4 ++-- R/cluster_own.R | 2 +- R/computeDiffInCounts.R | 4 ++-- R/indMix.R | 16 ++++++++-------- R/initialPopCounts.R | 4 ++-- R/laskeLoggis.R | 2 +- R/laskeOsaDist.R | 8 +++++++- man/laskeOsaDist.Rd | 21 +++++++++++++++++++++ 9 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 man/laskeOsaDist.Rd diff --git a/NAMESPACE b/NAMESPACE index 42b0063..0be8af8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ export(linkage) export(logml2String) export(lueGenePopData) export(lueNimi) +export(matlab2r) export(noIndex) export(ownNum2Str) export(poistaLiianPienet) @@ -49,3 +50,4 @@ export(writeMixtureInfo) importFrom(methods,is) importFrom(stats,runif) importFrom(utils,read.delim) +importFrom(utils,write.table) diff --git a/R/admixture_initialization.R b/R/admixture_initialization.R index 4d9f6c8..c69dd63 100644 --- a/R/admixture_initialization.R +++ b/R/admixture_initialization.R @@ -6,12 +6,12 @@ admixture_initialization <- function (data_matrix, nclusters, Z) { size_data <- size(data_matrix) nloci <- size_data[2] - 1 - n <- max(data_matrix[, end]) + n <- max(data_matrix[, ncol(data_matrix)]) T <- cluster_own(Z, nclusters) initial_partition <- zeros(size_data[1], 1) for (i in 1:n) { kori <- T[i] - here <- find(data_matrix[,end] == i) + here <- find(data_matrix[, ncol(data_matrix)] == i) for (j in 1:length(here)) { initial_partition[here[j], 1] <- kori } diff --git a/R/cluster_own.R b/R/cluster_own.R index b94b7b8..0d64102 100644 --- a/R/cluster_own.R +++ b/R/cluster_own.R @@ -15,7 +15,7 @@ cluster_own <- function(Z, nclust) { for (k in (m - maxclust + 1):(m - 1)) { i = Z(k, 1) # left tree if (i <= m) { # original node, no leafs - T(i) = clsnum + T[i] = clsnum clsnum = clsnum + 1 } else if (i < (2 * m - maxclust + 1)) { # created before cutoff, search down the tree T <- clusternum(Z, T, i - m, clsnum) diff --git a/R/computeDiffInCounts.R b/R/computeDiffInCounts.R index de19e30..af53f61 100644 --- a/R/computeDiffInCounts.R +++ b/R/computeDiffInCounts.R @@ -9,8 +9,8 @@ computeDiffInCounts <- function(rows, max_noalle, nloci, data) { notEmpty <- find(row>=0) if (length(notEmpty) > 0) { - diffInCounts(row(notEmpty) + (notEmpty - 1) * max_noalle) <- - diffInCounts(row(notEmpty) + (notEmpty - 1) * max_noalle) + 1 + diffInCounts[row(notEmpty) + (notEmpty - 1) * max_noalle] <- + diffInCounts[row(notEmpty) + (notEmpty - 1) * max_noalle] + 1 } } return(diffInCounts) diff --git a/R/indMix.R b/R/indMix.R index 6b11bf9..3fc2359 100644 --- a/R/indMix.R +++ b/R/indMix.R @@ -52,7 +52,7 @@ indMix <- function(c, npops, dispText) { } else { npopsTaulu <- as.numeric(npopstext) ykkoset <- find(npopsTaulu == 1) - npopsTaulu(ykkoset) <- list() # Mik�li ykk�si� annettu yl�rajaksi, ne poistetaan. + npopsTaulu[ykkoset] <- list() # Mik�li ykk�si� annettu yl�rajaksi, ne poistetaan. if (isempty(npopsTaulu)) { logml <- 1 partitionSummary <- 1 @@ -68,7 +68,7 @@ indMix <- function(c, npops, dispText) { nruns <- length(npopsTaulu) initData <- data - data <- data[,1:(end - 1)] + data <- data[,1:(ncol(data) - 1)] logmlBest <- -1e50 partitionSummary <- -1e50 * ones(30, 2) # Tiedot 30 parhaasta partitiosta (npops ja logml) @@ -76,13 +76,13 @@ indMix <- function(c, npops, dispText) { worstLogml <- -1e50 worstIndex <- 1 for (run in 1:nruns) { - npops <- npopsTaulu(run) + npops <- npopsTaulu[run] if (dispText) { dispLine() print( paste0( - 'Run ', num2str(run), '/', num2str(nruns), - ', maximum number of populations ', num2str(npops), '.' + 'Run ', as.character(run), '/', as.character(nruns), + ', maximum number of populations ', as.character(npops), '.' ) ) } @@ -120,7 +120,7 @@ indMix <- function(c, npops, dispText) { print( paste0( 'Mixture analysis started with initial', - num2str(npops), + as.character(npops), 'populations.' ) ) @@ -130,7 +130,7 @@ indMix <- function(c, npops, dispText) { muutoksia <- 0 if (dispText) { - print(paste('Performing steps:', num2str(roundTypes))) + print(paste('Performing steps:', as.character(roundTypes))) } for (n in 1:length(roundTypes)) { @@ -545,7 +545,7 @@ indMix <- function(c, npops, dispText) { npops <- poistaTyhjatPopulaatiot(npops) POP_LOGML <- computePopulationLogml(1:npops, adjprior, priorTerm) if (dispText) { - print(paste('Found partition with', num2str(npops), 'populations.')) + print(paste('Found partition with', as.character(npops), 'populations.')) print(paste('Log(ml) =', as.character(logml))) print(' ') } diff --git a/R/initialPopCounts.R b/R/initialPopCounts.R index 2cb4806..2bd08ed 100644 --- a/R/initialPopCounts.R +++ b/R/initialPopCounts.R @@ -7,11 +7,11 @@ initialPopCounts <- function(data, npops, rows, noalle, adjprior) { for (j in 1:nloci) { i_rivit <- rows(i, 1):rows(i, 2) havainnotLokuksessa <- find(data[i_rivit, j] >= 0) - sumcounts(i, j) <- length(havainnotLokuksessa) + sumcounts[i, j] <- length(havainnotLokuksessa) for (k in 1:noalle[j]) { alleleCode <- k N_ijk <- length(find(data[i_rivit, j] == alleleCode)) - counts(k, j, i) <- N_ijk + counts[k, j, i] <- N_ijk } } } diff --git a/R/laskeLoggis.R b/R/laskeLoggis.R index 7bcd26f..672dec9 100644 --- a/R/laskeLoggis.R +++ b/R/laskeLoggis.R @@ -1,7 +1,7 @@ laskeLoggis <- function(counts, sumcounts, adjprior) { npops <- size(counts, 3) - sum1 <- sum(sum(sum(gammaln(counts + repmat(adjprior, c(1, 1, npops)))))) + sum1 <- sum(sum(sum(lgamma(counts + repmat(adjprior, c(1, 1, npops)))))) sum3 <- sum(sum(lgamma(adjprior))) - sum(sum(lgamma(1 + sumcounts))) logml2 <- sum1 - npops * sum3 loggis <- logml2 diff --git a/R/laskeOsaDist.R b/R/laskeOsaDist.R index 1ea9495..018614d 100644 --- a/R/laskeOsaDist.R +++ b/R/laskeOsaDist.R @@ -1,9 +1,15 @@ +#' @title Lower part of the dist +#' @description Constructs from the dist vector a subvector containing the individual inds2, Forms dist sub-vectors the vector, which includes yksiliden inds2 +#' @param inds2 inds2 +#' @param dist dist +#' @param ninds ninds +#' @author Waldir Leoncio laskeOsaDist <- function(inds2, dist, ninds) { # % Muodostaa dist vektorista osavektorin, joka sis�lt�� yksil�iden inds2 # % v�liset et�isyydet. ninds=kaikkien yksil�iden lukum��r�. ninds2 <- length(inds2) - apu <- zeros(nchoosek(ninds2, 2), 2) + apu <- zeros(choose(ninds2, 2), 2) rivi <- 1 for (i in 1:ninds2-1) { for (j in i+1:ninds2) { diff --git a/man/laskeOsaDist.Rd b/man/laskeOsaDist.Rd new file mode 100644 index 0000000..f577c89 --- /dev/null +++ b/man/laskeOsaDist.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/laskeOsaDist.R +\name{laskeOsaDist} +\alias{laskeOsaDist} +\title{Lower part of the dist} +\usage{ +laskeOsaDist(inds2, dist, ninds) +} +\arguments{ +\item{inds2}{inds2} + +\item{dist}{dist} + +\item{ninds}{ninds} +} +\description{ +Constructs from the dist vector a subvector containing the individual inds2, Forms dist sub-vectors the vector, which includes yksiliden inds2 +} +\author{ +Waldir Leoncio +} From de9683dd7de3f019015de758b0c0689520d73f9a Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 19 Nov 2020 08:09:04 +0100 Subject: [PATCH 4/4] Fixed global variable handling --- R/clearGlobalVars.R | 11 +++++----- R/globals.R | 1 + R/poistaLiianPienet.R | 2 +- R/poistaTyhjatPopulaatiot.R | 8 +++---- R/updateGlobalVariables.R | 44 ++++++++++++++++++------------------- 5 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 R/globals.R diff --git a/R/clearGlobalVars.R b/R/clearGlobalVars.R index abc59fb..3a7746c 100644 --- a/R/clearGlobalVars.R +++ b/R/clearGlobalVars.R @@ -1,8 +1,7 @@ clearGlobalVars <- function() { - # COUNTS <- SUMCOUNTS <- PARTITION <- POP_LOGML <- vector() # placeholders - COUNTS <<- vector() - SUMCOUNTS <<- vector() - PARTITION <<- vector() - POP_LOGML <<- vector() - LOGDIFF <<- vector() + COUNTS <- vector() + SUMCOUNTS <- vector() + PARTITION <- vector() + POP_LOGML <- vector() + LOGDIFF <- vector() } \ No newline at end of file diff --git a/R/globals.R b/R/globals.R new file mode 100644 index 0000000..143c664 --- /dev/null +++ b/R/globals.R @@ -0,0 +1 @@ +utils::globalVariables(c("PARTITION", "COUNTS", "SUMCOUNTS", "LOGDIFF", "POP_LOGML")) \ No newline at end of file diff --git a/R/poistaLiianPienet.R b/R/poistaLiianPienet.R index 4ff581d..19eabea 100644 --- a/R/poistaLiianPienet.R +++ b/R/poistaLiianPienet.R @@ -41,7 +41,7 @@ poistaLiianPienet <- function (npops, rowsFromInd, alaraja, PARTITION[yksilot] == n } - # TODO: add COUNTS, SUMCOUNTS and PARTITION to return or use <<- + # TODO: add COUNTS, SUMCOUNTS and PARTITION to return or use <- COUNTS[, , miniPops] <- NA SUMCOUNTS[miniPops, ] <- NA diff --git a/R/poistaTyhjatPopulaatiot.R b/R/poistaTyhjatPopulaatiot.R index b811586..badef3e 100644 --- a/R/poistaTyhjatPopulaatiot.R +++ b/R/poistaTyhjatPopulaatiot.R @@ -2,13 +2,13 @@ poistaTyhjatPopulaatiot <- function(npops) { # % Poistaa tyhjentyneet populaatiot COUNTS:ista ja # % SUMCOUNTS:ista. P�ivitt�� npops:in ja PARTITION:in. notEmpty <- find(any(SUMCOUNTS, 2)) - COUNTS <<- COUNTS[, , notEmpty] - SUMCOUNTS <<- SUMCOUNTS[notEmpty, ] - LOGDIFF <<- LOGDIFF[, notEmpty] + COUNTS <- COUNTS[, , notEmpty] + SUMCOUNTS <- SUMCOUNTS[notEmpty, ] + LOGDIFF <- LOGDIFF[, notEmpty] for (n in 1:length(notEmpty)) { apu <- find(PARTITION == notEmpty(n)) - PARTITION(apu) <<- n + PARTITION[apu] <- n } npops <- length(notEmpty) return(npops) diff --git a/R/updateGlobalVariables.R b/R/updateGlobalVariables.R index 4b17469..1602faf 100644 --- a/R/updateGlobalVariables.R +++ b/R/updateGlobalVariables.R @@ -2,21 +2,21 @@ updateGlobalVariables <- function(ind, i2, diffInCounts, adjprior, priorTerm) { # % Suorittaa globaalien muuttujien muutokset, kun yksil� ind # % on siirret��n koriin i2. - i1 <- PARTITION(ind) - PARTITION(ind) <<- i2 + i1 <- PARTITION[ind] + PARTITION[ind] <- i2 - COUNTS[, , i1] <<- COUNTS[, , i1] - diffInCounts - COUNTS[, , i2] <<- COUNTS[, , i2] + diffInCounts - SUMCOUNTS[i1, ] <<- SUMCOUNTS[i1, ] - sum[diffInCounts] - SUMCOUNTS[i2, ] <<- SUMCOUNTS[i2, ] + sum[diffInCounts] + COUNTS[, , i1] <- COUNTS[, , i1] - diffInCounts + COUNTS[, , i2] <- COUNTS[, , i2] + diffInCounts + SUMCOUNTS[i1, ] <- SUMCOUNTS[i1, ] - sum[diffInCounts] + SUMCOUNTS[i2, ] <- SUMCOUNTS[i2, ] + sum[diffInCounts] - POP_LOGML[c(i1, i2)] <<- computePopulationLogml( + POP_LOGML[c(i1, i2)] <- computePopulationLogml( c(i1, i2), adjprior, priorTerm ) - LOGDIFF[, c(i1, i2)] <<- -Inf + LOGDIFF[, c(i1, i2)] <- -Inf inx <- c(find(PARTITION == i1), find(PARTITION==i2)) - LOGDIFF[inx, ] <<- -Inf + LOGDIFF[inx, ] <- -Inf } updateGlobalVariables2 <- function(i1, i2, diffInCounts, adjprior, priorTerm) { @@ -24,12 +24,12 @@ updateGlobalVariables2 <- function(i1, i2, diffInCounts, adjprior, priorTerm) { # % korissa i1 olevat yksil�t siirret��n koriin i2. inds <- find(PARTITION == i1) - PARTITION(inds) <<- i2 + PARTITION[inds] <- i2 - COUNTS[, , i1] <<- COUNTS[, , i1] - diffInCounts - COUNTS[, , i2] <<- COUNTS[, , i2] + diffInCounts - SUMCOUNTS[i1, ] <<- SUMCOUNTS[i1, ] - sum[diffInCounts] - SUMCOUNTS[i2, ] <<- SUMCOUNTS[i2, ] + sum[diffInCounts] + COUNTS[, , i1] <- COUNTS[, , i1] - diffInCounts + COUNTS[, , i2] <- COUNTS[, , i2] + diffInCounts + SUMCOUNTS[i1, ] <- SUMCOUNTS[i1, ] - sum[diffInCounts] + SUMCOUNTS[i2, ] <- SUMCOUNTS[i2, ] + sum[diffInCounts] POP_LOGML[i1] <- 0 POP_LOGML[i2] <- computePopulationLogml(i2, adjprior, priorTerm) @@ -47,18 +47,18 @@ updateGlobalVariables3 <- function( # % koriin. i1 <- PARTITION[muuttuvat(1)] - PARTITION[muuttuvat] <<- i2 + PARTITION[muuttuvat] <- i2 - COUNTS[, , i1] <<- COUNTS[, , i1] - diffInCounts - COUNTS[, , i2] <<- COUNTS[, , i2] + diffInCounts - SUMCOUNTS[i1, ] <<- SUMCOUNTS[i1, ] - sum[diffInCounts] - SUMCOUNTS[i2, ] <<- SUMCOUNTS[i2, ] + sum[diffInCounts] + COUNTS[, , i1] <- COUNTS[, , i1] - diffInCounts + COUNTS[, , i2] <- COUNTS[, , i2] + diffInCounts + SUMCOUNTS[i1, ] <- SUMCOUNTS[i1, ] - sum[diffInCounts] + SUMCOUNTS[i2, ] <- SUMCOUNTS[i2, ] + sum[diffInCounts] - POP_LOGML[c(i1, i2)] <<- computePopulationLogml( + POP_LOGML[c(i1, i2)] <- computePopulationLogml( c(i1, i2), adjprior, priorTerm ) - LOGDIFF[, c(i1, i2)] <<- -Inf + LOGDIFF[, c(i1, i2)] <- -Inf inx <- c(find(PARTITION == i1), find(PARTITION == i2)) - LOGDIFF[inx, ] <<- -Inf + LOGDIFF[inx, ] <- -Inf }