Skip to content

Commit

Permalink
Merge pull request #29 from BushmanLab/random_type_fix
Browse files Browse the repository at this point in the history
randoms of correct type
  • Loading branch information
anatolydryga committed Aug 24, 2015
2 parents abaa544 + 9302c61 commit 4414204
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
23 changes: 10 additions & 13 deletions R/random_site.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ get_reference_genome <- function(reference_genome) {
#' @param gender 'm' or 'f'
#' @param number_of_positions total number of random positions to generate
#' @param male_chr list of male-specific chromosomes prefixes(only 1 prefix is allowed at present)
#' @return dataframe with columns: siteID, chr, strand, position
#' @return dataframe with columns:
#' siteID(numeric), chr(character), strand(character), position(numeric)
#' @export
get_random_positions <- function(siteIDs, reference_genome, gender,
number_of_positions=3, male_chr=c("chrY")){
Expand All @@ -52,26 +53,22 @@ get_random_positions <- function(siteIDs, reference_genome, gender,

seed <- .Random.seed #don't want to screw up global randomness

mrcs <- sapply(siteIDs, function(x){
mrcs <- lapply(siteIDs, function(x){
set.seed(x)
rands <- round(runif(number_of_positions, 1, genomeLength*2)-genomeLength)
cuts <- cut(abs(rands), breaks=cs, labels=names(chr_len))

#outputs in format of "siteID", "chr", "strand", "position"
list(rep(x,number_of_positions),
as.character(cuts),
as.character(cut(sign(rands), breaks=c(-1,0,1), labels=c("-", "+"), include.lowest=T)),
abs(rands) - cs[match(cuts, names(chr_len))])
data_frame(
"siteID" = rep(x,number_of_positions),
"chr" = as.character(cuts),
"strand"=as.character(cut(sign(rands), breaks=c(-1,0,1), labels=c("-", "+"), include.lowest=T)),
"position"=abs(rands) - cs[match(cuts, names(chr_len))])
})

.Random.seed <- seed #resetting the seed

#this is ugly and requires as.character above but ~5X faster than outputting a list of
#data.frames and then rbinding them all together
mrcs <- data.frame(matrix(unlist(t(mrcs)), nrow=length(siteIDs)*number_of_positions))
names(mrcs) <- c("siteID", "chr", "strand", "position")

mrcs

do.call(rbind, mrcs)
}

#' generate random controls for sites
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test_random_site.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ test_that("generate_random_positions returns df", {
c("siteID", "chr", "position", "strand"), ignore.order=TRUE)
})

test_that("generate_random_positions returns df with correct types", {
# artificially assign one chromosome to gender
randoms <- get_random_positions(1, reference, 'm', male_chr="chrI")
types <- sapply(randoms, class)
expect_equivalent(types, c("numeric", "character", "character", "numeric"))
})

test_that("generate_random_positions only accepts male or female", {
expect_error(get_random_positions(1, reference, 'xxx'))
expect_error(get_random_positions(10, reference, 'A'))
Expand Down

0 comments on commit 4414204

Please sign in to comment.