#' Gets pairwise distances from the sequence data at a particular timestamp
#' @param seq_data A data frame that contains a list of sequences
#' @param timestamp The timestamp at which our sequence data coressponds to
#' @return A data frame with the pairwise distances between the sequences for each time step
#' @importFrom magrittr "%>%"
#' @export
pairwise_distances_timestamp <- function(seq_data, timestamp, dist_method)
{
seq_data <- seq_data[seq_data$time == timestamp, ]
if(length(seq_data) == 0)
{
stop("Sequence data provided has no data for timestamp", timestamp)
}
num_sequences <- length(seq_data$sequences[[1]])
num_seq_pairs <- (num_sequences * (num_sequences-1))/2
size <- num_seq_pairs
pair_data <- data.frame(time = rep(NA_integer_, size),
seq1 = rep(NA_integer_, size),
seq2 = rep(NA_integer_, size),
seq_dist = rep(NA, size))
assign_row <- function(time_index, seq_data, num_seq_pairs)
{
if(dist_method == "ape")
{
new_rows <- dist_to_data_frame(stringdist_ape(seq_data$sequences[[1]]))
}
else if(dist_method == "levenshtein")
{
new_rows <- dist_to_data_frame(stringdist_levenshtein(seq_data$sequences[[1]]))
}
assign_row_inner <- function(seq_index, new_rows, time_index)
{
row_index <- seq_index
new_row <- list(time_index,
new_rows$seq1[seq_index], new_rows$seq2[seq_index],
new_rows$dist_1_2[seq_index])
pair_data[row_index, ] <<- new_row
}
purrr::map(1:num_seq_pairs, assign_row_inner, new_rows, time_index)
}
assign_row(timestamp, seq_data, num_seq_pairs)
return(pair_data)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.