diff --git a/NAMESPACE b/NAMESPACE index e8c117d..cb4db89 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,3 +3,4 @@ export(admix1) export(calculatePopLogml) export(learn_simple_partition) +export(ownNum2Str) diff --git a/R/admix1.R b/R/admix1.R index f7f4819..5ffc284 100644 --- a/R/admix1.R +++ b/R/admix1.R @@ -769,40 +769,4 @@ admix1 <- function(tietue) { # rows = reshape(rows', [1,rowsFromInd*ninds]); # %-------------------------------------------------------------------------- -# %----- - -# function str = ownNum2Str(number) - -# absolute = abs(number); - -# if absolute < 1000 -# str = num2str(number); -# elseif absolute < 10000000 -# first_three = rem(number,1000); -# next_four = (number - first_three) /1000; -# first_three = abs(first_three); -# if first_three<10 -# first_three = ['00' num2str(first_three)]; -# elseif first_three<100 -# first_three = ['0' num2str(first_three)]; -# else -# first_three = num2str(first_three); -# end; -# str = [num2str(next_four) first_three]; -# elseif absolute < 100000000 -# first_four = rem(number,10000); -# next_four = (number - first_four) /10000; -# first_four = abs(first_four); -# if first_four<10 -# first_four = ['000' num2str(first_four)]; -# elseif first_four<100 -# first_four = ['00' num2str(first_four)]; -# elseif first_four<1000 -# first_four = ['0' num2str(first_four)]; -# else -# first_four = num2str(first_four); -# end; -# str = [num2str(next_four) first_four]; -# else -# str = num2str(number); -# end; \ No newline at end of file +# %----- \ No newline at end of file diff --git a/R/ownNum2Str.R b/R/ownNum2Str.R new file mode 100644 index 0000000..4ebff24 --- /dev/null +++ b/R/ownNum2Str.R @@ -0,0 +1,39 @@ +#' @title Own number to string +#' @param number number +#' @note On Matlab, if number is NaN the output is 'NaN'. Here, the output will be an error. Also, the function belo expects "number" to have length one, whereas Matlab accepts vectors. +#' @export +ownNum2Str <- function(number) { + absolute <- abs(number) + if (absolute < 1000) { + str <- as.character(number) + } else if (absolute < 10000000) { + first_three <- number %% 1000 + next_four <- (number - first_three) /1000 + first_three <- abs(first_three) + if (first_three < 10) { + first_three <- paste0('00', as.character(first_three)) + } else if (first_three < 100) { + first_three <- paste0('0', as.character(first_three)) + } else { + first_three <- as.character(first_three) + } + str <- paste0(as.character(next_four), first_three) + } else if (absolute < 100000000) { + first_four <- number %% 10000 + next_four <- (number - first_four) / 10000 + first_four <- abs(first_four) + if (first_four < 10) { + first_four <- paste0('000', as.character(first_four)) + } else if (first_four < 100) { + first_four <- paste0('00', as.character(first_four)) + } else if (first_four < 1000) { + first_four <- paste0('0', as.character(first_four)) + } else { + first_four <- as.character(first_four) + } + str <- paste0(as.character(next_four), first_four) + } else { + str <- as.character(number) + } + return(str) +} diff --git a/README.md b/README.md index 3f4c095..eadc557 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ rBAPS is currently under development and a stable version is yet to be released. remotes::install_github("ocbe-uio/rBAPS", "dev") ``` +## Contributing + +rBAPS is Open Software licenced under the [GPL-3](https://tldrlegal.com/license/gnu-general-public-license-v3-(gpl-3)), so all contributions are welcome. You can find a list of todos and pitfalls on the [TODO.md](TODO.md) file. + ## References ### Scientific papers diff --git a/TODO.md b/TODO.md index 6870710..9c64ccd 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,5 @@ +# Minimum requirements for next stable release + For the first stable release of rBAPS, the following features should be implemented: - [ ] Clustering of populations (import `greedyPoPMix.m` from BAPS) @@ -7,3 +9,14 @@ For the first stable release of rBAPS, the following features should be implemen - [ ] Admixture analysis (import `admix1.m` from BAPS) Note to contributors: as tasks get finished, please update this file with an `x` and keep it there. This should help us fill out a changelog for an eventual stable release. + +# Known pitfalls + +The following behavioral differences have been detected between the Matlab functions and their R counterparts. In order to save time, these differences will not be addressed, since they could require extensive reworking of a function. However, such differences may very well cause unexpected problems in some situations, which is why compiling this list is so important. The list below might provide a good starting point for identifying and fixing bugs: + +## `ownNum2Str` + +Argument | Value | Matlab output | R output +---------|-------|---------------|--------- +`number` | `'NaN` | `'NAN'` | error +`number` | `` | `''` | `''` + warning \ No newline at end of file diff --git a/man/ownNum2Str.Rd b/man/ownNum2Str.Rd new file mode 100644 index 0000000..b266187 --- /dev/null +++ b/man/ownNum2Str.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ownNum2Str.R +\name{ownNum2Str} +\alias{ownNum2Str} +\title{Own number to string} +\usage{ +ownNum2Str(number) +} +\arguments{ +\item{number}{number} +} +\description{ +Own number to string +} +\note{ +On Matlab, if number is NaN the output is 'NaN'. Here, the output will be an error. Also, the function belo expects "number" to have length one, whereas Matlab accepts vectors. +} diff --git a/tests/testthat/test-admix1.R b/tests/testthat/test-admix1.R index 7aa8d7d..a9820bf 100644 --- a/tests/testthat/test-admix1.R +++ b/tests/testthat/test-admix1.R @@ -1,7 +1,7 @@ context("Admixture analysis") -test_that("learn*partition behaves like Matlab", { +test_that("learn*partition behaves like on Matlab", { # Test data p1 <- c(0, .5, 1, 1.5) p2 <- c(seq(0, .5, .1), 1, 1, 1, 2) @@ -35,4 +35,11 @@ test_that("learn*partition behaves like Matlab", { object = learn_partition_modified(p4), expected = matrix(c(1, 2, 2, 2)) ) +}) + +test_that("ownNum2Str behaves like on Matlab", { + expect_equal(ownNum2Str(1), "1") + expect_equal(ownNum2Str(-123456789), "-123456789") + expect_equal(ownNum2Str(0), "0") + expect_error(ownNum2Str("a")) }) \ No newline at end of file