From c567e5af496857d5f8e5e0bd56b52b9b25faa6a8 Mon Sep 17 00:00:00 2001 From: Hiromasa Takemura Date: Wed, 26 Aug 2020 15:09:51 +0900 Subject: [PATCH] Add functions enabling .tck export compatible with MRTrix3 MRView --- fileFilters/mrtrix/dtiExportFibersMrtrix3.m | 92 +++++++++++++++++++++ mrDiffusion/fiber/fg/fgWrite.m | 5 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100755 fileFilters/mrtrix/dtiExportFibersMrtrix3.m mode change 100644 => 100755 mrDiffusion/fiber/fg/fgWrite.m diff --git a/fileFilters/mrtrix/dtiExportFibersMrtrix3.m b/fileFilters/mrtrix/dtiExportFibersMrtrix3.m new file mode 100755 index 00000000..ff137b24 --- /dev/null +++ b/fileFilters/mrtrix/dtiExportFibersMrtrix3.m @@ -0,0 +1,92 @@ +function tck = dtiExportFibersMrtrix3(fg, tck_filename) +% +% tck = dtiExportFibersMrtrix3(fg, tck_filename) +% +% Saves a FG structure to disk in MRTRIX format (.tck) +% +% INPUTS: +% fg - vistasoft fiber group structure +% tck_filename - file path to output .ick +% +% OUTPUT: +% tck - reconstructed TCK file structure. +% +% Based on MATLAB functions distributed with mrtrix 0.3 +% +% This function is a modification of dtiExportFibersMrtrix.m, by incorportating header information of MRTrix 3 .tck format ("mrtrix tracks"). +% +% Written by Hiromasa Takemura, CiNet BIT 2020 August +% + +% initialize output +tck = dtiGetFgParams(fg); + +% transpose the streamline order +tck.data = fg.fibers'; + +for ii = 1:length(tck.data) + % transpose the node order + tck.data{ii} = tck.data{ii}'; + +end +clear ii + +% save the file w/ mrtrix fxn +write_mrtrix_fibers(tck, tck_filename); + +return +end + +%%%%%%%%%%%%%%%%%%%%%% +% AUXILIARY FUNCTION % +%%%%%%%%%%%%%%%%%%%%%% +function write_mrtrix_fibers (fibers, filename) +% +% function: write_mrtrix_fibers (fibers, filename) +% +% writes the track data stored as a cell array in the 'data' field of the +% fibers variable to the MRtrix format track file 'filename'. All other fields +% of the fibers variable will be written as text entries in the header, and are +% expected to supplied as character arrays. +% +% +% This function was originally distributed with mrtrix 0.2/0.3 + +if ~isfield (fibers, 'data') + disp ('ERROR: input fibers variable does not contain required ''data'' field'); + return; +end + +if ~iscell (fibers.data) + disp ('ERROR: input fibers.data variable should be a cell array'); + return; +end + +f = fopen (filename, 'w', 'ieee-le'); +if (f < 1) + disp (['error opening ' filename ]); + return; +end + +fprintf (f, 'mrtrix tracks\ndatatype: Float32LE\ncount: %d\n', prod(size(fibers.data))); +names = fieldnames(fibers); +for i=1:size(names) + if strcmpi (names{i}, 'data'), continue; end + if strcmpi (names{i}, 'count'), continue; end + if strcmpi (names{i}, 'datatype'), continue; end + fprintf (f, '%s: %s\n', names{i}, getfield(fibers, names{i})); +end +fprintf (f, '%s: %s\n', 'timestamp: ', num2str(now)); % This allows visualizing the streamlines in mrview +data_offset = ftell (f) + 20; +fprintf (f, 'file: . %d\nEND\n', data_offset); + +fwrite (f, zeros(data_offset-ftell(f),1), 'uint8'); +for i = 1:prod(size(fibers.data)) + fwrite (f, fibers.data{i}', 'float32'); + fwrite (f, [ nan nan nan ], 'float32'); +end + +fwrite (f, [ inf inf inf ], 'float32'); +fclose (f); + +end \ No newline at end of file diff --git a/mrDiffusion/fiber/fg/fgWrite.m b/mrDiffusion/fiber/fg/fgWrite.m old mode 100644 new mode 100755 index cb459c85..8bad1dc4 --- a/mrDiffusion/fiber/fg/fgWrite.m +++ b/mrDiffusion/fiber/fg/fgWrite.m @@ -13,7 +13,8 @@ function fgWrite(fg,name,type) % 'pdb' - quench file format [DEFAULT] % 'quench' - same as 'pdb' % 'mat' - matlab file format -% 'tck' - MRTRIX file format (also 'mrtrix'/'.tck') +% 'tck' - MRTRIX3 file format (also 'mrtrix'/'.tck') +% 'tck2' - MRTRIX version 0.2 file format % % * If "name" ends in either .pdb or .mat the correct file type % will be used. @@ -69,6 +70,8 @@ function fgWrite(fg,name,type) case 'mat' dtiWriteFiberGroup(fg, name); case {'tck', 'mrtrix', '.tck'} + dtiExportFibersMrtrix3(fg, name); + case {'tck2'} dtiExportFibersMrtrix(fg, name); end