Radix Tree (trie) class implementation
root_pointerRoot of the RadixTree
char_counter_pointerCharacter count data for the purpose of validating input
new()Create a new RadixTree object
RadixTree$new(sequences = NULL)sequencesA character vector of sequences to insert into the tree
show()Print the tree to screen
RadixTree$show()
to_string()Print the tree to a string
RadixTree$to_string()A string representation of the tree
graph()Plot of the tree using igraph (needs to be installed separately)
RadixTree$graph(depth = -1, root_label = "root", plot = TRUE)depthThe tree depth to plot. If -1 (default), plot the entire tree.
root_labelThe label of the root node in the plot.
plotWhether to create a plot or return the data used to generate the plot.
A data frame of parent-child relationships used to generate the igraph plot OR a ggplot2 object
to_vector()Output all sequences held by the tree as a character vector
RadixTree$to_vector()A character vector of all sequences contained in the tree. Return order is not guaranteed.
size()Output the size of the tree (i.e. how many sequences are contained)
RadixTree$size()The size of the tree
insert()Insert new sequences into the tree
RadixTree$insert(sequences)sequencesA character vector of sequences to insert into the tree
A logical vector indicating whether the sequence was inserted (TRUE) or already existing in the tree (FALSE)
erase()Erase sequences from the tree
RadixTree$erase(sequences)sequencesA character vector of sequences to erase from the tree
A logical vector indicating whether the sequence was erased (TRUE) or not found in the tree (FALSE)
queryA character vector of sequences to find in the tree
A logical vector indicating whether the sequence was found (TRUE) or not found in the tree (FALSE)
prefix_search()Search for sequences in the tree that start with a specified prefix. E.g.: a query of "CAR" will find "CART", "CARBON", "CARROT", etc. but not "CATS".
RadixTree$prefix_search(query)queryA character vector of sequences to search for in the tree
A data frame of all matches with columns "query" and "target".
search()Search for sequences in the tree that are with a specified distance metric to a specified query.
RadixTree$search(
query,
max_distance = NULL,
max_fraction = NULL,
mode = "levenshtein",
cost_matrix = NULL,
gap_cost = NA_integer_,
gap_open_cost = NA_integer_,
nthreads = 1,
show_progress = FALSE
)queryA character vector of query sequences.
max_distancehow far to search in units of absolute distance. Can be a single value or a vector. Mutually exclusive with max_fraction.
max_fractionhow far to search in units of relative distance to each query sequence length. Can be a single value or a vector. Mutually exclusive with max_distance.
modeThe distance metric to use. One of hamming (hm), global (gb) or anchored (an).
cost_matrixA custom cost matrix for use with the "global" or "anchored" distance metrics. See details.
gap_costThe cost of a gap for use with the "global" or "anchored" distance metrics. See details.
gap_open_costThe cost of a gap opening. See details.
nthreadsThe number of threads to use for parallel computation.
show_progressWhether to show a progress bar.
The output is a data.frame of all matches with columns "query" and "target". For anchored searches, the output also includes attributes "query_size" and "target_size" which are vectors containing the portion of the query and target sequences that are aligned.
single_gap_search()A specialized algorithm for searching for sequences allowing at most a single gap within the alignment itself. The mode is always "anchored" and does not penalize end gaps.
RadixTree$single_gap_search(
query,
max_distance,
gap_cost = 1L,
nthreads = 1,
show_progress = FALSE
)queryA character vector of query sequences.
max_distancehow far to search in units of absolute distance. Can be a single value or a vector. Mutually exclusive with max_fraction.
gap_costThe cost of a gap for use with the "global" or "anchored" distance metrics. See details.
nthreadsThe number of threads to use for parallel computation.
show_progressWhether to show a progress bar.
The output is a data.frame of matches with columns "query", "target" and "distance".
validate()Validate the tree
RadixTree$validate()A logical indicating whether the tree is valid (TRUE) or not (FALSE). This is mostly an internal function for debugging purposes and should always return TRUE.
The RadixTree class is a trie implementation. The primary usage is to be able to search of similar sequences based on a dynamic programming framework. This can be done using the search method which searches for similar sequences based on the Global, Anchored or Hamming distance metrics.
Three types of distance metrics are supported, based on the form of alignment performed. These are: Hamming, Global (Levenshtein) and Anchored.
An anchored alignment is a form of semi-global alignment, where the query sequence is "anchored" (global) to the beginning of both the query and target sequences, but is semi-global in that the end of the either the query sequence or target sequence (but not both) can be unaligned. This type of alignment is sometimes called an "extension" alignment in literature.
In contrast a global alignment must align the entire query and target sequences. When mismatch and indel costs are equal to 1, this is also known as the Levenshtein distance.
By default, if mode == "global" or "anchored", all mismatches and indels are given a cost of 1. However, you can define your own distance metric by setting the substitution cost_matrix and separate gap parameters. The cost_matrix is a strictly positive square integer matrix of substitution costs and should include all characters in query and target as column- and rownames. Any rows/columns named "gap" or "gap_open" are ignored. To set the cost of a gap (insertion or deletion), use the gap_cost parameter (a single positive integer). To enable affine gaps, provide the gap_open_cost parameter (a single positive integer) in addition to gap_cost. If affine alignment is used, the total cost of a gap of length L is defined as: TOTAL_GAP_COST = gap_open_cost + (gap_cost * gap_length).
If mode == "hamming" all alignment parameters are ignored; mismatch is given a distance of 1 and gaps are not allowed.
https://en.wikipedia.org/wiki/Radix_tree
tree <- RadixTree$new()
tree$insert(c("ACGT", "AAAA"))
tree$erase("AAAA")
tree$search("ACG", max_distance = 1, mode = "levenshtein")
# query target distance
# 1 ACG ACGT 1
tree$search("ACG", max_distance = 1, mode = "hamming")
# query target distance
# <0 rows> (or 0-length row.names)
Run the code above in your browser using DataLab