-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcmtk.R
More file actions
348 lines (327 loc) · 13.9 KB
/
cmtk.R
File metadata and controls
348 lines (327 loc) · 13.9 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# wrappers for some CMTK command line tools
#' Convert CMTK registration to homogeneous affine matrix with dof2mat
#'
#' @details Transpose is true by default since this results in the orientation
#' of cmtk output files matching the orientation in R. Do not change this
#' unless you're sure you know what you're doing!
#' @param reg Path to input registration file or 5x3 matrix of CMTK parameters.
#' @param Transpose output matrix so that form on disk matches R's convention.
#' @param version Whether to return CMTK version string
#' @return 4x4 transformation matrix
#' @family cmtk-commandline
#' @family cmtk-geometry
#' @export
cmtk.dof2mat<-function(reg, Transpose=TRUE, version=FALSE){
if(version) return(cmtk.system2(cmtk.call("dof2mat", version=TRUE, RETURN.TYPE = 'list'), stdout=TRUE))
if(is.numeric(reg)){
params<-reg
reg<-tempfile(fileext='.list')
on.exit(unlink(reg,recursive=TRUE))
write.cmtkreg(params,foldername=reg)
}
call=cmtk.call("dof2mat", transpose=Transpose, RETURN.TYPE = 'list')
rval=cmtk.system2(call, moreargs = path.expand(reg), stdout=TRUE)
numbers=as.numeric(unlist(strsplit(rval,"\t")))
matrix(numbers,ncol=4,byrow=TRUE)
}
#' Use CMTK mat2dof to convert homogeneous affine matrix into CMTK registration
#'
#' @details If no output file is supplied, 5x3 params matrix will be returned
#' directly. Otherwise a logical will be returned indicating success or
#' failure at writing to disk.
#' @details Transpose is true by default since this results in an R matrix with
#' the transpose in the fourth column being correctly interpreted by cmtk.
#' @param m Homogenous affine matrix (4x4) last row 0 0 0 1 etc
#' @param f Output file (optional)
#' @param centre Centre for rotation (optional 3-vector)
#' @param Transpose the input matrix so that it is read in as it appears on disk
#' @param version When TRUE, function returns CMTK version number of mat2dof
#' tool
#' @return 5x3 matrix of CMTK registration parameters or logical
#' @family cmtk-commandline
#' @family cmtk-geometry
#' @export
cmtk.mat2dof<-function(m, f=NULL, centre=NULL, Transpose=TRUE, version=FALSE){
if(version) {
ver=cmtk.system2(cmtk.call('mat2dof', version=TRUE, RETURN.TYPE = 'list'), stdout=TRUE)
return(ver)
}
if(!is.matrix(m) || nrow(m)!=4 || ncol(m)!=4) stop("Please give me a homogeneous affine matrix (4x4)")
inf=tempfile()
on.exit(unlink(inf), add=TRUE)
write.table(m, file=inf, sep='\t', row.names=F, col.names=F)
# always transpose because mat2dof appears to read the matrix with last column being 0 0 0 1
if(!is.null(centre)) {
if(length(centre)!=3) stop("Must supply 3-vector for centre")
}
cmtkcall=cmtk.call('mat2dof', transpose=Transpose, center=centre, RETURN.TYPE = 'list')
if(is.null(f)){
res=cmtk.system2(cmtkcall, stdin=inf, stdout=TRUE)
params=read.table(text=res, sep='\t',comment.char="")[,2]
if(length(params)!=15) stop("Trouble reading mat2dof response")
numbers <- matrix(params, ncol=3, byrow=TRUE)
rownames(numbers) <- c("xlate", "rotate", "scale", "shear", "center")
return(numbers)
} else {
cmtk.system2(cmtkcall, moreargs=c('--list', path.expand(f)), stdin=inf)==0
}
}
#' Return path to directory containing CMTK binaries
#'
#' @description The \href{https://www.nitrc.org/projects/cmtk}{Computational Morphometry
#' Toolkit} (CMTK) is the default image registration toolkit supported by nat.
#' An external CMTK installation is required in order to apply CMTK
#' registrations. This function attempts to locate the full path to the CMTK
#' executable files and can query and set an option.
#' @details Queries options('nat.cmtk.bindir') if \code{firstdir} is not
#' specified. If that does not contain the appropriate binaries, it will look
#' in the system PATH for the \code{cmtk} wrapper script installed by most
#' recent cmtk installations.
#'
#' Failing that, it will look for the cmtk tool specified by \code{cmtktool},
#' first in the path and then a succession of plausible places until it finds
#' something. Setting \code{options(nat.cmtk.bindir=NA)} or passing
#' \code{firstdir=NA} will stop the function from trying to locate CMTK,
#' always returning NULL unless \code{check=TRUE}, in which case it will error
#' out.
#' @param firstdir Character vector specifying path containing CMTK binaries or
#' NA (see details). This defaults to options('nat.cmtk.bindir').
#' @param extradirs Where to look if CMTK is not in \code{firstdir} or the PATH
#' @param set Whether to set options('nat.cmtk.bindir') with the found
#' directory. Also check/sets cygwin path on Windows (see Installation
#' section).
#' @param check Whether to (re)check that a path that has been set appropriately
#' in options(nat.cmtk.bindir='/some/path') or now found in the PATH or
#' alternative directories. Will throw an error on failure.
#' @param cmtktool Name of a specific cmtk tool which will be used to identify
#' the location of all cmtk binaries.
#' @return Character vector giving path to CMTK binary directory or NULL when
#' this cannot be found.
#' @export
#' @aliases cmtk
#' @section Installation: It is recommended to install released CMTK versions
#' available from the \href{https://www.nitrc.org/projects/cmtk}{NITRC website}. A
#' bug in composition of affine transformations from CMTK parameters in the
#' CMTK versions <2.4 series means that CMTK>=3.0 is strongly recommended.
#' CMTK v3 registrations are not backwards compatible with CMTK v2, but CMTK
#' v3 can correctly interpret and convert registrations from earlier versions.
#'
#' On Windows, when \code{set=TRUE}, cmtk.bindir will also check that the
#' cygwin bin directory is in the PATH. If it is not, then it is added for the
#' current R session. This should solve issues with missing cygwin DLLs.
#' @examples
#' message(ifelse(is.null(d<-cmtk.bindir()), "CMTK not found!",
#' paste("CMTK is at:",d)))
#' \dontrun{
#' # set options('nat.cmtk.bindir') according to where cmtk was found
#' op=options(nat.cmtk.bindir=NULL)
#' cmtk.bindir(set=TRUE)
#' options(op)}
#' @seealso \code{\link{options}}
cmtk.bindir<-function(firstdir=getOption('nat.cmtk.bindir'),
extradirs=c('~/bin','/usr/local/lib/cmtk/bin',
'/usr/local/bin','/opt/local/bin',
'/opt/local/lib/cmtk/bin/',
'/Applications/IGSRegistrationTools/bin',
'C:\\cygwin64\\usr\\local\\lib\\cmtk\\bin',
'C:\\Program Files\\CMTK-3.3\\CMTK\\lib\\cmtk\\bin'),
set=FALSE, check=FALSE, cmtktool='gregxform'){
# TODO check pure Windows vs cygwin
if(isTRUE(.Platform$OS.type=="windows" && tools::file_ext(cmtktool)!="exe"))
cmtktool=paste0(cmtktool,".exe")
bindir=NULL
if(!is.null(firstdir)) {
bindir=firstdir
if(check && !file.exists(file.path(bindir,cmtktool)))
stop("cmtk is _not_ installed at:", bindir,
"\nPlease check value of options('nat.cmtk.bindir')")
}
# note the use of while loop + break to avoid heavy if nesting
while(is.null(bindir)){
if(nzchar(cmtkwrapperpath<-Sys.which("cmtk"))) {
# try looking for cmtk wrapper script
# e.g. /usr/bin/cmtk => /usr/lib/cmtk/bin
bindir=file.path(dirname(dirname(cmtkwrapperpath)), "lib", "cmtk","bin")
if(file.exists(file.path(bindir, cmtktool)))
break
# we couldn't find the actual cmtk tools in the appropriate location
bindir <- NULL
}
if(nzchar(cmtktoolpath<-Sys.which(cmtktool))){
bindir=dirname(cmtktoolpath)
break
}
# otherwise check some plausible locations
for(d in extradirs){
if(file.exists(file.path(d,cmtktool))) {
bindir=d
break
}
}
# we're out of luck but we still need to break out of the while loop
break
}
if(!is.null(bindir)){
if(is.na(bindir)) bindir=NULL
else bindir=path.expand(bindir)
}
if(check && is.null(bindir))
stop("Cannot find CMTK. Please install from ",
"https://www.nitrc.org/projects/cmtk and make sure that it is your path!")
if(set) {
options(nat.cmtk.bindir=bindir)
.set_path_for_cygwin(bindir)
}
bindir
}
# set windows path for cygwin to avoid missing dll errors
.set_path_for_cygwin <- function(bindir) {
if(isTRUE(.Platform$OS.type=="windows") && grepl("cygwin", bindir, ignore.case = T)){
cygdir=sub('(.*ygwin64).*', "\\1", bindir)
cygbindir=file.path(cygdir, 'bin')
sp=Sys.getenv('PATH')
if(!grepl(cygbindir, sp, fixed = T)) {
sp=paste(sp, sep=";", cygbindir)
Sys.setenv(PATH=sp)
}
}
}
#' Return cmtk version or test for presence of at least a specific version
#'
#' @param minimum If specified checks that the cmtk version
#' @details NB this function has the side effect of setting an option
#' nat.cmtk.version the first time that it is run in the current R session.
#' @return returns \code{numeric_version} representation of CMTK version or if
#' minimum is not NULL, returns a logical indicating whether the installed
#' version exceeds the current version. If CMTK is not installed returns NA.
#' @seealso \code{\link{cmtk.bindir}, \link{cmtk.dof2mat}}
#' @examples
#' \dontrun{
#' cmtk.version()
#' cmtk.version('3.2.2')
#' }
#' @export
cmtk.version<-function(minimum=NULL){
if(is.null(cmtk_version<-getOption('nat.cmtk.version'))){
cmtk_version=try(cmtk.dof2mat(version=TRUE), silent = TRUE)
if(inherits(cmtk_version,'try-error')) return(NA)
options(nat.cmtk.version=cmtk_version)
}
cmtk_numeric_version=numeric_version(sub("([0-9.]+).*",'\\1',cmtk_version))
if(!is.null(minimum)) cmtk_numeric_version>=numeric_version(minimum)
else cmtk_numeric_version
}
#' Utility function to create and run calls to CMTK command line tools
#'
#' @description \code{cmtk.call} processes arguments into a form compatible with
#' CMTK command line tools.
#'
#' @details \code{cmtk.call} processes arguments in ... as follows:
#'
#' \describe{
#'
#' \item{argument names}{ will be converted from \code{arg.name} to
#' \code{--arg-name}}
#'
#' \item{logical vectors}{ (which must be of length 1) will be passed on as
#' \code{--arg-name}}
#'
#' \item{character vectors}{ (which must be of length 1) will be passed on as
#' \code{--arg-name arg} i.e. quoting is left up to callee.}
#'
#' \item{numeric vectors}{ will be collapsed with commas if of length greater
#' than 1 and then passed on unquoted e.g. \code{target.offset=c(1,2,3)} will
#' result in \code{--target-offset 1,2,3}}
#'
#' }
#' @param tool Name of the CMTK tool
#' @param PROCESSED.ARGS Character vector of arguments that have already been
#' processed by the callee. Placed immediately after cmtk tool.
#' @param ... Additional named arguments to be processed by (\code{cmtk.call},
#' see details) or passed to \code{system2} (\code{cmtk.system2}).
#' @param FINAL.ARGS Character vector of arguments that have already been
#' processed by the callee. Placed at the end of the call after optional
#' arguments.
#' @param RETURN.TYPE Sets return type to a character string or list (the latter
#' is suitable for use with \code{\link{system2}})
#' @return \emph{Either} a string of the form \code{"<tool> <PROCESSED.ARGS>
#' <...> <FINAL.ARGS>"} \emph{or} a list containing elements \itemize{
#'
#' \item command A character vector of length 1 indicating the full path to
#' the CMTK tool, shell quoted for protection.
#'
#' \item args A character vector of arguments of length 0 or greater.
#'
#' }
#' @seealso \code{\link{cmtk.bindir}}
#' @export
#' @examples
#' \dontrun{
#' cmtk.call("reformatx",'--outfile=out.nrrd', floating='floating.nrrd',
#' mask=TRUE, target.offset=c(1,2,3), FINAL.ARGS=c('target.nrrd','reg.list'))
#' # get help for a cmtk tool
#' system(cmtk.call('reformatx', help=TRUE))
#' }
cmtk.call<-function(tool, PROCESSED.ARGS=NULL, ..., FINAL.ARGS=NULL, RETURN.TYPE=c("string", "list")){
RETURN.TYPE=match.arg(RETURN.TYPE)
cmd=file.path(cmtk.bindir(check=TRUE),tool)
cmtkargs=character()
if(!is.null(PROCESSED.ARGS)){
cmtkargs=c(cmtkargs, PROCESSED.ARGS)
}
if(!missing(...)){
xargs=pairlist(...)
for(n in names(xargs)){
arg=xargs[[n]]
cmtkarg=cmtk.arg.name(n)
if(is.character(arg)){
if(length(arg)!=1) stop("character arguments must have length 1")
cmtkargs=c(cmtkargs, cmtkarg, arg)
} else if(is.logical(arg)){
if(isTRUE(arg)) cmtkargs=c(cmtkargs, cmtkarg)
} else if(is.numeric(arg)){
arg=paste(arg, collapse=',')
cmtkargs=c(cmtkargs, cmtkarg, arg)
} else if(is.null(arg)){
# just ignore null arguemnts
} else {
stop("unrecognised argument type")
}
}
}
if(!is.null(FINAL.ARGS)){
cmtkargs=c(cmtkargs, FINAL.ARGS)
}
if(RETURN.TYPE=="string") {
paste(shQuote(cmd), paste(cmtkargs, collapse = " "))
} else {
list(command=cmd, args=cmtkargs)
}
}
#' @description \code{cmtk.system2} actually calls a cmtk tool using a call list
#' produced by \code{cmtk.call}
#'
#' @param cmtkcall A list containing processed arguments prepared by
#' \code{cmtk.call(RETURN.TYPE="list")}
#' @param moreargs Additional arguments to add to the processed call
#'
#' @return See the help of \code{\link{system2}} for details.
#' @export
#'
#' @examples
#' \dontrun{
#' cmtk.system2(cmtk.call('mat2dof', help=TRUE, RETURN.TYPE="list"))
#' # capture response into an R variable
#' helptext=cmtk.system2(cmtk.call('mat2dof', help=TRUE, RETURN.TYPE="list"),
#' stdout=TRUE)
#' }
#' @rdname cmtk.call
cmtk.system2 <- function(cmtkcall, moreargs=NULL, ...){
if(!is.null(moreargs))
cmtkcall$args=c(cmtkcall$args, moreargs)
fullargs=c(cmtkcall, ...)
do.call(system2, fullargs)
}
# utility function to make a cmtk argument name from a valid R argument
# by converting periods to dashes
cmtk.arg.name<-function(x) paste("--",gsub("\\.",'-',x),sep='')