| read.dti.trk | R Documentation |
Read fiber tracks from Diffusion Toolkit in trk format.
read.dti.trk(
filepath,
shift_origin = TRUE,
max_tracks = Inf,
skip_tracks = 0L,
bbox = NULL,
coords = NULL
)
filepath |
character string, path to file in trk format. Gzip-compressed
files are supported as well (the compression is detected from the file
content, so a |
shift_origin |
logical, whether to apply the half-voxel origin shift when computing the corrected vox2ras matrix. The TRK format stores a matrix that maps to the voxel corner, not the voxel center (as is the NIfTI convention). Set to |
max_tracks |
numeric, the maximum number of tracks to read. Use |
skip_tracks |
integer, the number of tracks to skip before reading any.
Skipped tracks are never held in memory. Note that the count refers to the
tracks that a reader returns, and that empty tracks (tracks without any
point, which the TRK format allows) are returned as well, e.g., a file with
one regular track, then an empty one, yields the empty track for
|
bbox |
numeric vector of length 6 or NULL. If given, only tracks that
have at least one point inside the box are read, the box is given as
|
coords |
character string or NULL, the coordinate system of the returned
track coordinates. One of 'native' (the coordinates exactly as stored in the
file, which for TRK files is usually 'voxelmm' space, in which the
coordinates refer to voxel corners) or 'ras' (RAS+ mm space in which a
coordinate of (0,0,0) is the center of the first voxel, which is what
'nibabel' and DIPY return by default). If The |
named list, the parsed file data. The naming of the variables follows the spec at http://trackvis.org/docs/?subsect=fileformat. The returned header will contain the field vox2ras (the raw matrix stored in the TRK file, mapping from mm space to RAS) and, if shift_origin is TRUE, the additional field vox2ras_corrected (the computed matrix mapping from voxel indices to voxel center RAS coordinates). It also contains the entry coords_space ('native' or 'ras'), which records the coordinate system the returned tracks are in, so that the result can be passed to write.dti.trk() without losing that information. The tracks are stored in an is.fs.tracts instance: tracks[[i]] returns a list with the entries coords (n x 3 matrix), num_points (integer), scalars (matrix or NULL) and properties (numeric vector or NULL) for the i-th track. Use fs.tracts.lengths(trk$tracks) to get the number of points of each track, and as.list(trk$tracks) to convert to a plain list.
The 4x4 matrix stored in TRK files (labeled vox_to_ras in the spec) is actually a transformation from mm space to RAS, not from voxel space to RAS. The TRK format was designed by TrackVis with the assumption that voxels are 1 mm^3, and that coordinates refer to voxel corners rather than centers. To obtain the actual vox2ras matrix (voxel center in RAS), the raw matrix must be combined with a voxel-size scaling and a half-voxel offset correction: vox2ras_corrected = mm2ras %*% mm_correction %*% vox2mm, where vox2mm scales by the inverse voxel size and mm_correction shifts by -0.5 mm. Note that DSI Studio does not apply this half-voxel shift, so you may need to set shift_origin=FALSE for DSI Studio files.
Use coords = 'ras' to have the transformation applied to the returned coordinates. That transformation additionally accounts for the voxel_order stored in the header, like the reference implementation in 'nibabel' does, see trackvis.affine.to.rasmm. Note that the matrix stored in the header alone is not sufficient to transform the coordinates: it has to be combined with the voxel sizes, the half-voxel offset and the orientation.
## Not run:
trk <- read.dti.trk("~/simple.trk")
trk2 <- read.dti.trk("~/standard.trk")
trk3 <- read.dti.trk("~/complex_big_endian.trk")
# Coordinates in RAS+ mm, ready for plotting against an MNI template:
trk_ras <- read.dti.trk("~/simple.trk", coords = "ras")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.