% launchTal2MNIConv.m is a script to easily convert a file containing
% coordinates in both the Talairach and MNI spaces, into a file where
% all coordinates are in MNI space. It calls the functions 'TAL2MNI' and
% 'mni2tal_matrix' by Matthew Brett.
% The input file must contain the original coordinates in subsequent rows;
% the first column of each row is to be set to 1 if the correspoding
% coordinate is in Talairach space (and therefore needs to be converted), 0
% if it is in MNI space (the coordinate will be left untouched). The output
% file will contain the converted (and untouched) coordinates - note that
% the column with the 0/1 code is not included in the output.
% The input/output filenames can be edited directly in this file.
%
% This file is part of Cluster4MetaAnalysis.
%     Cluster4MetaAnalysis is free software: you can redistribute it 
%     and/or modify it under the terms of the GNU General Public License 
%     as published by the Free Software Foundation, either version 3 of 
%     the License, or (at your option) any later version.
% 
%     Cluster4MetaAnalysis is distributed in the hope that it will be 
%     useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
%     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with Cluster4MetaAnalysis.  If not, see
%     <http://www.gnu.org/licenses/>.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input/Output filenames                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Input and output files are in this folder
IOfolder = '../myData';

% Input File
infile = 'mixedCoord.txt';
% Output Files
outfile = 'MNICoord.txt';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% reading data from input file
data = load([IOfolder filesep infile]);

% finding rows to be converted from Talairach to MNI
idx = find(data(:, 1) == 1);
% getting rid of first column
data(:, 1) = [];

% store data to be converted
inpoints = data(idx, :);

% transpose
inpoints = inpoints';

% transform!
outpoints = TAL2MNI(inpoints);

% rounding and converting to int
outpoints = round(outpoints);

% storing converted coordinates into original matrix
data(idx, :) = outpoints';

% writing to file
fid = fopen([IOfolder filesep outfile], 'wt');
fprintf(fid, '%d\t%d\t%d\n', data');
fclose(fid);




