|
a |
|
b/R/write_dummy_data.R |
|
|
1 |
#' Write random sequences to fasta file |
|
|
2 |
#' |
|
|
3 |
#' Create random sequences from predefined vocabulary and write to fasta file. |
|
|
4 |
#' |
|
|
5 |
#' @param file_path Output directory; can also be a file name but only possible if \code{write_to_file_path = TRUE} and |
|
|
6 |
#' \code{num_files = 1}). |
|
|
7 |
#' @param num_files Number of files to create. |
|
|
8 |
#' @param header Fasta header name. |
|
|
9 |
#' @param seq_length Length of one sequence. If vector longer than 1, will randomly sample from that vector. |
|
|
10 |
#' @param num_seq Number of sequences per file. |
|
|
11 |
#' @param fasta_name_start Beginning string of file name. Output files are named fasta_name_start + _i.fasta where i is an integer index. |
|
|
12 |
#' @param write_to_file_path Whether to write output directly to \code{file_path}, i.e. file_path is not a directory. |
|
|
13 |
#' @param prob Probability of each character in the \code{vocabulary} to be sampled. If `NULL` each character has same probability. |
|
|
14 |
#' @param vocabulary Set of characters to sample sequences from. |
|
|
15 |
#' @examples |
|
|
16 |
#' path_output <- tempfile() |
|
|
17 |
#' dir.create(path_output) |
|
|
18 |
#' create_dummy_data(file_path = path_output, |
|
|
19 |
#' num_files = 3, |
|
|
20 |
#' seq_length = 11, |
|
|
21 |
#' num_seq = 5, |
|
|
22 |
#' vocabulary = c("a", "c", "g", "t")) |
|
|
23 |
#' list.files(path_output) |
|
|
24 |
#' |
|
|
25 |
#' @returns None. Writes data to files. |
|
|
26 |
#' @export |
|
|
27 |
create_dummy_data <- function(file_path, |
|
|
28 |
num_files, |
|
|
29 |
header = "header", |
|
|
30 |
seq_length, |
|
|
31 |
num_seq, |
|
|
32 |
fasta_name_start = "file", |
|
|
33 |
write_to_file_path = FALSE, |
|
|
34 |
prob = NULL, |
|
|
35 |
vocabulary = c("a", "c", "g", "t")) { |
|
|
36 |
|
|
|
37 |
if (!is.null(prob)) { |
|
|
38 |
stopifnot(length(prob) == length(vocabulary)) |
|
|
39 |
stopifnot(abs(1 - sum(prob)) < 1e-10) |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
if (write_to_file_path) { |
|
|
43 |
stopifnot(num_files == 1) |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
if (!dir.exists(file_path) & !write_to_file_path) { |
|
|
47 |
dir.create(file_path) |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
for (i in 1:num_files){ |
|
|
51 |
df <- data.frame(Header = NULL, Sequence = NULL) |
|
|
52 |
for (j in 1:num_seq) { |
|
|
53 |
nuc_seq <- random_seq(vocabulary = vocabulary, seq_length = seq_length, prob = prob) |
|
|
54 |
new_row <- data.frame(Header = header, Sequence = nuc_seq) |
|
|
55 |
df <- rbind(df, new_row) |
|
|
56 |
} |
|
|
57 |
df$Header <- as.character(df$Header) |
|
|
58 |
df$Sequence <- as.character(df$Sequence) |
|
|
59 |
if (write_to_file_path) { |
|
|
60 |
out.file <- file_path |
|
|
61 |
} else { |
|
|
62 |
file_name <- paste0(fasta_name_start, "_", i, ".fasta") |
|
|
63 |
out.file <- file.path(file_path, file_name) |
|
|
64 |
} |
|
|
65 |
microseq::writeFasta(fdta = dplyr::as_tibble(df), |
|
|
66 |
out.file = out.file) |
|
|
67 |
} |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
random_seq <- function(vocabulary, seq_length, prob = NULL) { |
|
|
71 |
if (length(seq_length) > 1) { |
|
|
72 |
seq_length <- sample(seq_length, 1) |
|
|
73 |
} |
|
|
74 |
sample(vocabulary, seq_length, replace = TRUE, prob = prob) %>% paste(collapse = "") |
|
|
75 |
} |