/
seqdist.R
144 lines (130 loc) · 5.08 KB
/
seqdist.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#' Compute distance metrics between integer sequences
#'
#' \code{seq_dist} computes pairwise string distances between elements of
#' \code{a} and \code{b}, where the argument with less elements is recycled.
#' \code{seq_distmatrix} computes the distance matrix with rows according to
#' \code{a} and columns according to \code{b}.
#'
#'
#' @section Notes:
#' Input vectors are converted with \code{as.integer}. This causes truncation for numeric
#' vectors (e.g. \code{pi} will be treated as \code{3L}).
#'
#' @param a (\code{list} of) \code{integer} or \code{numeric} vector(s). Will be converted with \code{as.integer} (target)
#' @param b (\code{list} of) \code{integer} or \code{numeric} vector(s). Will be converted with \code{as.integer} (source).
#' Optional for \code{seq_distmatrix}.
#' @param method Distance metric. See \code{\link{stringdist-metrics}}
#' @param weight For \code{method='osa'} or \code{'dl'}, the penalty for
#' deletion, insertion, substitution and transposition, in that order. When
#' \code{method='lv'}, the penalty for transposition is ignored. When
#' \code{method='jw'}, the weights associated with characters of \code{a},
#' characters from \code{b} and the transposition weight, in that order.
#' Weights must be positive and not exceed 1. \code{weight} is ignored
#' completely when \code{method='hamming'}, \code{'qgram'}, \code{'cosine'},
#' \code{'Jaccard'}, or \code{'lcs'}
#' @param q Size of the \eqn{q}-gram; must be nonnegative. Only applies to
#' \code{method='qgram'}, \code{'jaccard'} or \code{'cosine'}.
#' @param p Penalty factor for Jaro-Winkler distance. The valid range for
#' \code{p} is \code{0 <= p <= 0.25}. If \code{p=0} (default), the
#' Jaro-distance is returned. Applies only to \code{method='jw'}.
#' @param nthread Maximum number of threads to use. By default, a sensible
#' number of threads is chosen, see \code{\link{stringdist-parallelization}}.
#'
#' @return
#'
#' \code{seq_dist} returns a numeric vector with pairwise distances between \code{a}
#' and \code{b} of length \code{max(length(a),length(b)}.
#'
#' For \code{seq_distmatrix} there are two options. If \code{b} is missing, the
#' \code{\link[stats]{dist}} object corresponding to the \code{length(a) X
#' length(a)} distance matrix is returned. If \code{b} is specified, the
#' \code{length(a) X length(b)} distance matrix is returned.
#'
#' If any element of \code{a} or \code{b} is \code{NA_integer_}, the distance with
#' any matched integer vector will result in \code{NA}. Missing values in the sequences
#' themselves are treated as a number and not treated specially (Also see the examples).
#'
#' @seealso \code{\link{seq_sim}}, \code{\link{seq_amatch}}, \code{\link{seq_qgrams}}
#'
#' @example ../examples/seq_dist.R
#' @export
seq_dist <- function(a, b
, method=c("osa","lv","dl","hamming","lcs", "qgram","cosine","jaccard","jw")
, weight=c(d=1,i=1,s=1,t=1)
, q=1, p=0
, nthread = getOption("sd_num_thread")
){
a <- ensure_int_list(a)
b <- ensure_int_list(b)
stopifnot(
all(is.finite(weight))
, all(weight > 0)
, all(weight <=1)
, q >= 0
, p <= 0.25
, p >= 0
, ifelse(method %in% c('osa','dl'), length(weight) >= 4, TRUE)
, ifelse(method %in% c('lv','jw') , length(weight) >= 3, TRUE)
, nthread > 0
)
if (length(a) == 0 || length(b) == 0){
return(numeric(0))
}
if ( max(length(a),length(b)) %% min(length(a),length(b)) != 0 ){
warning(RECYCLEWARNING)
}
method <- match.arg(method)
nthread <- as.integer(nthread)
if (method == 'jw') weight <- weight[c(2,1,3)]
do_dist(a=b, b=a
, method=method
, weight=weight
, q=q
, p=p
, nthread=nthread)
}
#' @param useNames label the output matrix with \code{names(a)} and \code{names(b)}?
#' @rdname seq_dist
#' @export
seq_distmatrix <- function(a, b
, method=c("osa","lv","dl","hamming","lcs","qgram","cosine","jaccard","jw")
, weight=c(d=1,i=1,s=1,t=1), q=1, p=0
, useNames=c("names","none")
, nthread = getOption("sd_num_thread")
){
useNames <- match.arg(useNames)
method <- match.arg(method)
nthread <- as.integer(nthread)
if (method == 'jw') weight <- weight[c(2,1,3)]
a <- ensure_int_list(a)
# if b is missing, generate a 'dist' object.
if (missing(b)){
return( lower_tri(a
, method=method
, weight=weight
, q=q
, p=p
, useNames=useNames
, nthread=nthread)
)
}
b <- ensure_int_list(b)
if (length(a) == 0 || length(b) == 0){
return(matrix(numeric(0)))
}
if (useNames == "names"){
rowns <- names(a)
colns <- names(b)
}
#x <- vapply(b, do_dist, USE.NAMES=FALSE, FUN.VALUE=numeric(length(a))
# , b=a, method=method, weight=weight, q=q, p=p, nthread=nthread)
x <- vapply(b
, function(src) do_dist(list(src), b=a, method=method, weight=weight, q=q, p=p, nthread=nthread)
, USE.NAMES=FALSE, FUN.VALUE=numeric(length(a))
)
if (useNames == "names" ){
structure(matrix(x,nrow=length(a),ncol=length(b), dimnames=list(rowns,colns)))
} else {
matrix(x,nrow=length(a),ncol=length(b))
}
}