From 2ea0ca44eb38e439ae8318eb0addadc231d8ad54 Mon Sep 17 00:00:00 2001 From: HajkD Date: Thu, 5 Sep 2019 22:01:50 +0100 Subject: [PATCH] When 'replace = FALSE' is set in extract_random_seqs_from_genome(), the command will still hold 'replace = TRUE' for the chromosome and strand to be sampled, but the sequence that was sampled will be removed #2 --- R/extract_random_seqs_from_genome.R | 10 +++++++--- R/sample_chromosome_intervals.R | 18 +++++++++++++++--- man/sample_chromosome_intervals.Rd | 7 ++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/R/extract_random_seqs_from_genome.R b/R/extract_random_seqs_from_genome.R index d5f9faa..914a1e6 100644 --- a/R/extract_random_seqs_from_genome.R +++ b/R/extract_random_seqs_from_genome.R @@ -79,7 +79,7 @@ extract_random_seqs_from_genome <- res <- vector("list", length(size)) for (i in seq_len(size)) { - sample_chromsome <- sample.int(length(chr_names), 1, replace = replace, prob = prob) + sample_chromsome <- sample.int(length(chr_names), 1, replace = TRUE, prob = NULL) sample_strand <- sample.int(2, 1, replace = replace, prob = c(0.5, 0.5)) if (sample_strand == 1){ @@ -87,7 +87,9 @@ extract_random_seqs_from_genome <- chr_size = imported_genome_i[chr_names[sample_chromsome]]@ranges@width, interval_width = interval_width, strand = "plus", - size = 1 + size = 1, + replace = replace, + prob = prob ) sample_i <- dplyr::mutate(sample_i, chr = chr_names[sample_chromsome]) @@ -99,7 +101,9 @@ extract_random_seqs_from_genome <- chr_size = imported_genome_i[chr_names[sample_chromsome]]@ranges@width, interval_width = interval_width, strand = "minus", - size = 1 + size = 1, + replace = replace, + prob = prob ) sample_i <- diff --git a/R/sample_chromosome_intervals.R b/R/sample_chromosome_intervals.R index 42fa648..1f42623 100644 --- a/R/sample_chromosome_intervals.R +++ b/R/sample_chromosome_intervals.R @@ -10,9 +10,17 @@ #' \item \code{strand = "minus"} #' } #' @param size a non-negative integer giving the number of sequences that shall be sampled from random genomic loci. +#' @param replace logical value indicating whether sampling should be with replacement. Default: \code{replace = TRUE}. +#' @param prob a vector of probability weights for obtaining the elements of the vector being sampled. Default is \code{prob = NULL}. #' @author Hajk-Georg Drost -sample_chromosome_intervals <- function(chr_size, interval_width, strand, size) { - +sample_chromosome_intervals <- + function(chr_size, + interval_width, + strand, + size, + replace = TRUE, + prob = NULL) { + if (!is.element(strand, c("plus", "minus"))) stop("The 'strand' argument can only be specified as strand = 'plus' or strand = 'minus'.", call. = FALSE) @@ -27,7 +35,11 @@ sample_chromosome_intervals <- function(chr_size, interval_width, strand, size) } # random start position in chromosome - random_start <- sample.int(ifelse((chr_size - interval_width) == 0,1, chr_size - interval_width), size) + random_start <- + sample.int(ifelse((chr_size - interval_width) == 0, 1, chr_size - interval_width), + size, + replace = replace, + prob = prob) # compute random end position in chromosome given interval width random_end <- random_start + interval_width - 1 diff --git a/man/sample_chromosome_intervals.Rd b/man/sample_chromosome_intervals.Rd index e7c8ee0..3cb1ca0 100644 --- a/man/sample_chromosome_intervals.Rd +++ b/man/sample_chromosome_intervals.Rd @@ -4,7 +4,8 @@ \alias{sample_chromosome_intervals} \title{Helper function to sample random intervals of length \code{interval_width} from chromosomes} \usage{ -sample_chromosome_intervals(chr_size, interval_width, strand, size) +sample_chromosome_intervals(chr_size, interval_width, strand, size, + replace = TRUE, prob = NULL) } \arguments{ \item{chr_size}{an integer value defining the length of the chromosome.} @@ -19,6 +20,10 @@ are: }} \item{size}{a non-negative integer giving the number of sequences that shall be sampled from random genomic loci.} + +\item{replace}{logical value indicating whether sampling should be with replacement. Default: \code{replace = TRUE}.} + +\item{prob}{a vector of probability weights for obtaining the elements of the vector being sampled. Default is \code{prob = NULL}.} } \description{ This function samples a random locus given the chromosome lenght (\code{chr_size})