diff --git a/NAMESPACE b/NAMESPACE index 8d9bb0b..79a2e2f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ export(proportion2str) export(rand) export(randdir) export(repmat) +export(simuloiAlleeli) export(suoritaMuutos) export(times) importFrom(stats,runif) diff --git a/R/admix1.R b/R/admix1.R index f47f69d..d611540 100644 --- a/R/admix1.R +++ b/R/admix1.R @@ -478,12 +478,4 @@ admix1 <- function(tietue) { # end # end # counter = counter+rowsFromInd; -# end - -# function all = simuloiAlleeli(allfreqs,pop,loc) -# % Simuloi populaation pop lokukseen loc alleelin. -# freqs = allfreqs(:,loc,pop); -# cumsumma = cumsum(freqs); -# arvo = rand; -# isommat = find(cumsumma>arvo); -# all = min(isommat); \ No newline at end of file +# end \ No newline at end of file diff --git a/R/simuloiAlleeli.R b/R/simuloiAlleeli.R new file mode 100644 index 0000000..9f26d21 --- /dev/null +++ b/R/simuloiAlleeli.R @@ -0,0 +1,17 @@ +#' @title simuloiAlleeli +#' @description Simuloi populaation pop lokukseen loc alleelin. +#' @note This function is (only?) called by `simulateIndividuals()`. Therefore, exporting it is probably unnecessary. +#' @export + +simuloiAlleeli <- function(allfreqs, pop, loc) { + if (length(dim(allfreqs)) == 3) { # distinguish between arrays and matrices + freqs <- allfreqs[, loc, pop] + } else { + freqs <- allfreqs[, loc] + } + cumsumma <- cumsum(freqs) + arvo <- runif(1) + isommat <- which(cumsumma > arvo) + all <- min(isommat) + return(all) +} \ No newline at end of file diff --git a/man/simuloiAlleeli.Rd b/man/simuloiAlleeli.Rd new file mode 100644 index 0000000..cd19dd0 --- /dev/null +++ b/man/simuloiAlleeli.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/simuloiAlleeli.R +\name{simuloiAlleeli} +\alias{simuloiAlleeli} +\title{simuloiAlleeli} +\usage{ +simuloiAlleeli(allfreqs, pop, loc) +} +\description{ +Simuloi populaation pop lokukseen loc alleelin. +} +\note{ +This function is (only?) called by `simulateIndividuals()`. Therefore, exporting it is probably unnecessary. +} diff --git a/tests/testthat/test-admix1.R b/tests/testthat/test-admix1.R index f4560b0..41cb3f4 100644 --- a/tests/testthat/test-admix1.R +++ b/tests/testthat/test-admix1.R @@ -179,4 +179,14 @@ test_that("computePersonalAllFreqs works like on Matlab", { expect_equal(computePersonalAllFreqs(1, 1:4, mx, 1), c(15, 40)) expect_equal(computePersonalAllFreqs(1, 1:3, mx, 1), c(15, 40)) expect_equal(computePersonalAllFreqs(1, 1:2, mx, 1), c(15, 40)) +}) + +test_that("simuloiAlleeli works like on Matlab", { + # TODO: test on vector + ra1 <- array(1:12, c(2, 2, 3)) + mx1 <- matrix(c(3, 5, 0, 9), 2) + mx2 <- matrix(c(3, 5, 0, 9, 5, 8), 2) + expect_equal(simuloiAlleeli(ra1, 2, 1), 1) + expect_equal(simuloiAlleeli(mx1, 1, 2), 2) + expect_equal(simuloiAlleeli(mx2, 1, 3), 1) }) \ No newline at end of file